Before a variable can be used in program, it needs to be declared. Data types are used for assigning a type to a variable.
Let's take an example.
#include <stdio.h>
int main()
{
//defining an integer x
int x;
//now this x is 10
x = 10;
return 0;
}
Let's understand the code.
int x;
→ This is known as declaration of x. As discussed earlier, it will allocate a space in the memory of the computer to 'x' and the amount of space allocated will depend on the complier.
x = 10
→ It is assigning the value '10' to the variable 'x'. We can give any value to 'x'.
We have given an integer value to the variable 'x' because we have already declared that the variable 'x' is an integer - int x
. If we want to give something other than an integer like a character value to 'x', then we use char before x (in place of int). Similarly, if we want to give a decimal value, then we use float or double before 'x'.
%d is for int
%c is for char
%f is for float
Let's see an example of taking and displaying a float
, a double
and a char
value.
#include <stdio.h>
int main()
{
float a;
int b;
char ch;
printf("\nEnter value of float\n");
scanf("%f", &a);
printf("\nEnter value of int\n");
scanf("%d", &b);
printf("\nEnter value of char\n");
scanf(" %c", &ch);
printf("\nValue of float : %f", a);
printf("\nValue of int : %d", b);
printf("\nValue of char : %c", ch);
printf("\nValue of float(rounded) : %.2f\n", a);
return 0;
}
2.4
Enter value of int
23
Enter value of char
d
Value of float : 2.400000
Value of int : 23
Value of char : d
Value of float(rounded) : 2.40
So, you can see here that %d
is used for integers, %f
for floats and %c
for characters.
As simple as that!
One thing you should note that char values are given inside (''
). An example will make it clear:
#include <stdio.h>
int main()
{
//defining a char ch
char ch;
//now this ch is 'a'
ch = 'a';
printf("%c\n",ch);
return 0;
}
As you saw, (a) is written inside (''). i.e., 'a'
Signed and Unsigned
It’s fun to play with data types, isn’t it?
Now let's see what is meant by signed and unsigned variables.
Signed and Unsigned variables tell us whether it can take positive values, negative values or both. signed keyword is used for those variables which can take positive as well as negative values. unsigned keyword is used for those variables which can take only values which are zero or positive i.e., without - (negative sign).
We can use signed and unsigned keywords with only 'int' and 'char' data types.
For example, normally an integer variable of size 4 bytes can take values from -2,147,483,648 to 2,147,483,647, whereas if we declare 'x' as
unsigned int x;
then 'x' can take values from 0 to 4,294,967,295 (since 'x' is now an unsigned variable).
For 16 or 32 bit compiler
Type | Size | Range of Value |
---|---|---|
int | 2 or 4 bytes | -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647 |
unsigned int | 2 or 4 bytes | 0 to 65,535 or 0 to 4,294,967,295 |
char | 1 byte | -128 to 127 or 0 to 255 |
signed char | 1 byte | -128 to 127 |
unsigned char | 1 byte | 0 to 255 |
How is a character value stored?
Whenever a character value is given to a variable of type char, its ASCII value gets stored (and not the character value).
While printing a character, if we use %c, then its character value will be displayed and if we use %d, then its integer value (ASCII value) will be displayed.
#include <stdio.h>
int main()
{
char ch;
printf("\nEnter value of char\n");
scanf("%c", &ch);
printf("\nCharacter value of character = %c", ch);
printf("\nASCII value = %d\n", ch);
return 0;
}
a
Character value of character = a
ASCII value = 97
When we gave the value 'a' to the variable 'ch', the ASCII code (97) of 'a' gets stored in ch. So, %d displayed its integer value i.e., ASCII value and %c displayed its character value.
Type Casting
Suppose, you have a variable whose value is 23.2332 but at some line of your code, you want to use its integer value (floor value) only i.e., 23. The simplest solution is type casting.
Type Casting is the conversion of a variable from one data type to another data type. For example, converting a char value to an int value.
Type Conversions are of two types - implicit and explicit.
Implicit Conversion
Suppose we are adding an integer and a character in C, for example, 2 + 'a'
. This is a valid expression in C because C automatically converts the character value of 'a' to integer (ASCII value) and then adds them up. This automatic conversion is called implicit conversion.
#include <stdio.h>
int main()
{
int a = 12;
char ch = 'h';
//will add ASCII value of ch
int sum = a + ch;
printf("Sum = %d\n", sum);
return 0;
}
In the above code, when 'a' and 'ch' were added, the integer value of 'ch' (ASCII value) i.e. 104 is added to integer value of 'a' to produce a sum of 116.
Explicit Conversion
We can also manually convert values from one data type to another as follows:
( data-type ) value ;
Here, the 'value' will get converted to the 'data-type' mentioned. For example, (int)2.5
will convert 2.5 into an integer of its floor value.
#include <stdio.h>
int main()
{
float x = 2.45;
printf("%d\n",(int)x);
return 0;
}
In this example, 'x' is declared of type float and we are converting float to type int by writing (int)x
inside 'printf'.