logo codesdope

Datatypes in C


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;
}
Output

Now, 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 computer to 'x'.

But how much space?

It all depends on your compiler.

x = 10; - It is assigning the value '10' to the variable 'x'. We can give any value to 'x'.

If we want to give a character value to 'x', then we use char before x (in place of int). Similarly, if we want to give a real value( decimals ), then we use float or double before 'x' respectively.

%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;
}
Output
Enter value of float
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

In print function, when we type the character f with %, the number that will be given will be of type float. Therefore, %d is used for variables of type integer, %f for float and %c for variables of type character.

As simple as that !

%.2f means that the variable to be printed will be of type float and '.2' means that the number rounded to only two decimal places will be printed. We can use any number to which we have to round our decimal value.

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;
}
Output
a

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. Keyword signed is used for those variables which can take all positive, negative and zero values. Keyword unsigned is used for those variables which can take only values which are zero or positive.

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).

It is not necessary to give signed keyword to any variable because all the variables are signed by default.

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
In printf, %d is used for signed integers whereas %u is used with unsigned integers.

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;
}
Output
Enter value of char
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 displays its integer value i.e., ASCII value and %c displays 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 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, if we want to convert a char value to an int value.

Type Conversions are of two types - implicit and explicit.

Implicit Conversion


Suppose we are adding two numbers. The first number is of type int and the second number is of type float. Since we cannot add an int and a float, so both the numbers have to be of the same data type i.e., either both are int or both are float. Since float is a larger data type than int, therefore int variable gets converted into float automatically and then both the float variables add up. This automatic conversion is called implicit conversion.

All the character variables get converted to integers while performing arithmetic operations or in any other such expression.
#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;
}
Output
Sum = 116

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 ) expression ;

Consider an example.

#include <stdio.h>
int main()
{
   float x = 2.45;
   printf("%d\n",(int)x);
   return 0;
}
Output
2

In this example, since x is declared of type float, therefore we are converting float to type int by writing (int)x inside 'printf'.

There is no glory in practice but there is no glory without practice.

Download Our App.
BlogsDope App
Get it on Google Play
Doubt? Ask question
Close

Welcome.please sign up.

Close

Welcome.please login.