Today, we will create a simple 'C' program which would read a number from the keyboard and would display it on the screen.
#include<stdio.h>//Include these files for using standard input and output functions
#include<conio.h>
void main(void)
{
int num; //Define an integer variable which would store a number
printf("Enter a number:"); //This would print on the screen "Eenter a number:"
scanf("%d", &num); //This would allow the user to enter a number
printf("Number : %d", num); //Prints the entered number back to the screen
getch();
getch();
}
There are many things which can be noticed in this sample program and which can confuse us, lets focus on a few things.
Question 1: Why do we have a semi colon ; after every statement inside main function?
Answer: 'C' compiler marks an end of the statement wherver it finds a semicolon. It would be easier if if take it this way, in English whenever we want to mark the end of a sentence, we put a full stop (.), but in case of 'C' language we put a (;) semi colon.
Question 2: Why do we have "int" written before num, what does it denote?
Answer: 'C' language has got certain data types, these data types can store different types of data in them, for instance integers, floating point numbers, characters, strings etc. The syntax of using these data types is:
datatype variablename;
e.g.
int num; // As in our example
Now, for storing values between -32768 to +32767 we use int data type, this data type requires 2 bytes of memory and the keyword in 'C' for this is 'int'.
In the next post I will answer a few more questions on this sample program we made today.
No comments:
Post a Comment