Ok lets learn the two very important concepts in C programming , variables and data types.
So as the name suggest variable is something that is not constant that varies , we use variable in programming to store data , that could be numbers or text and data type defines the type of the data stored in the variable , so let me give an example to make it easy
Lets sat we
want to store someone phone number in a program and display the number , so
where we are going to store the number , we store it in a variable named
“phone” (variable names are user defined , we give names to the variable , but
there are some rules to follow , which I will explain very shortly)
So this would look like
Phone = value ;
Here value
can be any integer
So this
becomes
Phone =
123456;
This implies that phone is a variable to which we have assigned a value 123456
Now we also
have to tell the compiler that “phone” can store what kind of value , like an
integer or a character or a decimal value and so on and we tell this by using
data type which tells the compiler what
is the type of data the variable can store , there are different key word for
different data type and the major types are
int for integer value
char for storing individual characters
float for decimal value
double same as float but can
store larger numbers
please note
that every data type has predefined range within which we can store values
which we would discuss shortly
So to use a variable we write
int phone = 123456;
technically speaking variables are named piece of memory that we can assign a value , if you find that difficult , consider variables as a kind of box that we use to store data , the label of the box is its name and the type of content inside the box is its data type and the content is the value
Some rules to
follow while naming a variable are as follows
1 > no two variables should have the same name in other words variables should be unique
2> variables name cannot begin
with a number for example 123baby
3> variables can only have a
to z , A to Z , 0 to 9 and underscore in it
4> variable have to be
declared before they are used that means to specify the data type ,
Syntax for variable declaration is
Syntax for variable declaration is
Data type variable_name = value_of_varibale ;
let’s find more about the integer data type
Data Type
|
Byte
|
Range
|
int
|
4
|
–2,147,483,648 to 2,147,483,647
|
In the above table the byte field
represents the maximum size of a number an integer data type can hold and range
displays that the number could be between negative 2,147,483,648 to positive 2,147,483,647 , so
how we are getting this value ,
well for information every data type has a certain range but to understand the relation between byte size and range let us do some binary calculation
well for information every data type has a certain range but to understand the relation between byte size and range let us do some binary calculation
So computer stores everything in bits that is zero or one , now 4 byte is equal to how many bits ,
since 8 bit is 1 byte so 4 byte will be 32 bit
8 bit = 1Byte
4Byte = 8 * 4 = 32 bit
So in binary if we have 32 numbers of 1s then the value in decimal form will be 4,294,967,295 but since int can store both negative and positive value this number splits in two equal parts including 0 , so the range splits in half to –2,147,483,648 to 2,147,483,647 , but if we add these two values we would get 4,294,967,295 , this is how we get the range from size , if possible try to find the other range by yourself ,
This link might be helpful to you Microsoft library
Enough with variables and data
types now let us do a program with variable
main()
{
int phone = 123456;
printf("Hello World
\n");
printf("Your phone number is %d \n",phone);
getch();
}
This program display hello world
followed by 123456
Now let us examine the new elements
in the program
int phone = 123456; well this is not new , here we have declared a variable
with integer type and assigned a value to it
printf("Your phone number is %d \n",phone); printf is
explained before ,
in order to display the variable in the statement we use the %d notation inside the quotes then a comma and then the variable name,
please remember that the position of %d in the statement will decide the position of the variable in the statement , so it %d is before the word number then the variable will appear in that position
in order to display the variable in the statement we use the %d notation inside the quotes then a comma and then the variable name,
please remember that the position of %d in the statement will decide the position of the variable in the statement , so it %d is before the word number then the variable will appear in that position
Now let us do a program with more
numbers of variables
main()
{
int phone = 123456;
int mobile = 9876543;
printf("Hello World \n");
printf("Your phone number is %d and mobile is %d \n",phone,mobile);
getch();
}
This is how we express multiple
variable in a statement , here the first %d is for phone and the second for
mobile variable
Some examples of how to declare
and display other data type are
main()
{
float number = 55.5;
printf("value of number is %f”,number);
getch();
}
main()
{
double number = 44.54;
printf("value of number is %lf”,number);
getch();
}
main()
{
Char car = ‘m’;
printf("value of character is %c”,car);
getch();
}
As we can see we have %f is for
floats , %lf is for double and %c is for character
Hello just wanted to give you a quick heads up. The
ReplyDeletetext in your article seem to be running off the screen in Firefox.
I'm not sure if this is a formatting issue or something to do with browser compatibility but I thought I'd post to let you know.
The layout look great though! Hope you get the problem solved soon.
Cheers
Remove Pending Friend Request Facebook : Increase chance of not getting block