Data Types in C

by | Aug 14, 2021 | C

Home » C » Data Types in C

Introduction

In the previous article, we have seen how to declare a statement as an integer type. The declaration of a variable is a must to use in a program. In the English language, we have a group of alphabets. In the number system, we have a variety of numbers like rational, decimal numbers, integers, etc. Classifications are also made in programming languages so that the compiler knows how the programmer wants to use the data. These classifications are achieved with the help of Data Types in C.

Data types

Basically, data types are used for assigning a particular type to a variable.

#include<stdio.h>

int main ( )

{

   int a; //declaring an integer a

   a=100; //assigning a value to a

   return 0;

}

 

In the above code,

int a is a declaration statement for a, which declares that variable ‘a’ belongs to the integer type of group, and it will store an integer value in itself. Moreover, we discussed earlier that it would allocate some space in the computer’s memory to store that integral value.

The amount of space allocated will completely depend on the compiler.

Note: Sometimes, ‘defining a variable’ is used instead of ‘declaring a variable’ which is acceptable.

a=100;

This statement is assigning a value (100) to the variable declared, i.e. ‘a’. In short, ‘100’ is the integer stored in the space allocated to the variable ‘a’ in the memory.

We can assign different values to ‘a’ like a=z or a=5.55 etc. For such changes, it is mandatory to change the type used in the declaration statement.

For assigning a character value to the variable declared, we use char before that particular variable in the declaration statement. For decimal values, we use float or double.

Words like int, char, float, double are the keywords of C languages, and the compiler is familiar with such keywords.

Format Specifiers

%d, %c, %f are the format specifiers which tell the compiler what type of data is in a variable. They are used while taking input from the user using scanf () or printing any message on screen using printf ().

%d is used for int data type

%c is used for char data type

%f is used for float datatype

%u is used for unsigned int datatype

This will be more clear with the code written below

#include <stdio.h>

int main()

{

    int a;

    float b;

    char c;

   

    printf("Enter an integer value\n");

    scanf("%d", &a);




    printf("Enter a float type value\n");

    scanf("%f", &b);




    printf("Enter a character value\n");

    scanf(" %c", &c);




    printf("Value of int : %d\n", a);

    printf("Value of float : %f\n", b);

    printf("Value of character : %c\n", c);

   

    return 0;

}

 

In the example above, three declaration statements define three variables a, b, and c as integer, float, and character, respectively.

The format specifiers

%d is used to print the integer value that is stored in ‘a’,

%f is used to print the float value that is stored in ‘b’,

And %c is used to print the character value that is stored in ‘c’.

Output:

Data types in C Format Specifiers

In output, you can observe that the format specifiers used in printf( ) statements are replaced by the values assigned to their respective variables.

Now, here is a question for the readers:

Why did the compiler give the output of float value as ’25.500000’ even though the input was ’25.5’?

In the C compiler, every float value is rounded to 6 decimal places. So, if the input given is ‘25.5555555555’, the output for this value will be ’25.555555’. By default, the C compiler rounds every float value to 6 decimal places.

But we users can use any number to which we have to round our decimal/float value. For example, to round our value to 3 decimal places, we will use ‘%.3f’ instead of ‘%f’ in the printf() statement.

#include <stdio.h>

int main()

{

   float b;

    printf("Enter a float type value\n");

    scanf("%f", &b);

    printf("Value of float : %f\n",b);

    printf("Round of value : %.4f\n",b);

    return 0;

}

Output:

Data types in C Output

Try to make some changes in the code to understand better.

Note: char (character) values are given inside ‘’ while assigning a character value to a variable.

#include <stdio.h>

int main()

{

    char x;

    x='r';

    printf("character value is %c",x);

    return 0;

}

‘x’ is a variable with char datatype, and r is the character value assigned to x, written inside ‘’.

Signed and Unsigned

Now, we have just seen the data types for storing integers, characters, etc. However, integers are positive as well as negative. So, signed and unsigned are the terms used to differentiate variables that take positive values, negative values, and both.

A signed keyword is used for those variables which can take positive and negative values.

An unsigned keyword is used for those variables which can take only positive values, including zero.

It is important to note that signed and unsigned keywords are only used with ‘int’ and ‘char’ data types.

For 16 or 32 bit compilers:

int data type has

Size =2 or 4 bytes

Range of value= -32,768 to 32,76 or -2,147,483,648 to 2,147,483,647

char data type has

Size =1 bytes

Range of value= -128 to 127 or 0 to 255

If we declare a variable ‘x’ as

unsigned int x;

then ‘x’ can take values from 0 to 4,294,967,295.

The range of signed char datatypes is -128 to 127, and that of unsigned is 0 to 255.

Note: By default, all declared variables are signed. So, we can avoid the use of signed keywords for the variables in our code.

All code above is written without using signed keywords.

Author

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Author