Data Types in C

Just like the name suggests, here, data types refer to the type of data that we are using in a C program. Whenever we utilise a data type in a C program, we define the variables or functions used in it. We do so because we must specify the type of data that is in use, so that the compiler knows exactly what type of data it must expect from the given program.

In this article, we will take a closer look at the Data Types in C according to the GATE Syllabus for CSE (Computer Science Engineering). Read ahead to know more.

Table of Contents

Purpose of Data Types in C

Data types used in C language refer to an extensive system that we use to declare various types of functions or variables in a program. Here, on the basis of the type of variable present in a program, we determine the space that it occupies in storage, along with the way in which the stored bit pattern will be interpreted.

A data type specifies the type of data that a variable can store such as integer, floating, character, etc.

Example of Data Types in C

Let us take a look at an example to understand data types better.

For instance, we may want to utilise some numbers such as 5, 8, 600, or maybe a decimal point number such as 43.59, 127.368, 271.49, or maybe a text such as “cappuccino”. Then, the compiler used in C language would handle all of these very differently. Thus, here, we use different data types for defining what data types we want in the program.

Types of Data Types in C

Here are the five major categories into which data types are divided in C language:

Data Type Example of Data Type
Basic Data Type Floating-point, integer, double, character.
Derived Data Type Union, structure, array, etc.
Enumerated Data Type Enums
Void Data Type Empty Value
Bool Type True or False

The basic data types are also known as the primary data types in C programming.

Primary Data Types in C

Here are the five primitive or primary data types that one can find in C programming language:

1. Integer – We use these for storing various whole numbers, such as 5, 8, 67, 2390, etc.

2. Character – It refers to all ASCII character sets as well as the single alphabets, such as ‘x’, ‘Y’, etc.

3. Double – These include all large types of numeric values that do not come under either floating-point data type or integer data type. Visit Double Data Type in C to know more.

4. Floating-point – These refer to all the real number values or decimal points, such as 40.1, 820.673, 5.9, etc.

5. Void – This term refers to no values at all. We mostly use this data type when defining the functions in a program.

Various keywords are used in a program for specifying the data types mentioned above. Here are the keywords that we use:

Keyword Used Data Type
int Integer
float Floating-point
void Void
char Character
double Double

The size of every data type gets defined in bytes/ bits. Also, these data types are capable of holding a very wide range of values.

Different Data Type Values

The size of any given data type in a program depends a lot on the type of processor, as well as the compiler. In simpler words, the size of the data type depends entirely on the computer on which we run C language along with the version of the C program compiler that we installed in the computer.

The int data type can be 4 bytes/ 2 bytes.

Remembering the size of the int data type is very easy. The given size is generally equal to the length of the word of the program’s execution environment. In other words, int will be 2 bytes or 16 bits in the case of an environment that is 16-bit. However, int will be 4 bytes or 32 bits in case of an environment that is 32-bit.

The char data type is 1 byte.

The size of the char data type is basically 8 bits or 1 byte. No variation would occur with different compilers and interpreters. It means that the type of compiler or processor used will have no effect on its size whatsoever.

The double data type is 8 bytes.

The size of the double data type is basically 64 bits or 8 bytes. It is capable of storing values that are comparatively double the size of the bytes that the float data type can store. This is the reason why it is known as the double.

When looking at the 64 bits in total, the program has 1 bit for the sake of sign representation, the exponent uses 11 bits, and it uses the remaining 52 bits for the mantissa.

This data type is capable of holding about 15-17 digits, both after and before the decimal of the data type.

The float data type is 4 bytes.

The size of the float data type is basically 32 bits or 4 bytes. The float data type is single-precision in nature, and we use it for holding the decimal values. It helps in the storage of various large values, but the float is comparatively faster than double. It is because double works with comparatively much larger data values. Thus, it is slower comparatively.

The void data type is 0 bytes.

Since the void data type has no meaning, it has no size at all.

There is a range of values that come under all of these data types. But before we look into that, let us take a look at the modifiers that are used in the data types.

Data Type Modifiers in C Programming Language

There are basically four types of modifiers for all data types used in C language. We use these along with all the basic data types for categorising them further.

For instance, if we say that there is a chocolate bar on the table, the person we are speaking to will know that a chocolate bar is present on the table. But if we get more specific and say that there is a dark chocolate bar on the table or a milk chocolate bar on the table, it will become much more clear and specific, to the person who is listening.

In a very similar manner, the modifiers in C language help in making the primary or primitive data types much more specific.

Here are a few modifiers:

  • short
  • long
  • unsigned
  • signed

Just like the names suggest here, we use the unsigned and signed modifiers for presenting the unsigned (only +) and signed (- and +) values in any given data type. Also, the short and long modifiers cause an effect on the value range of any given data type.

For instance, the long int, short int, unsigned int, signed int, etc., are all very valid data types in the C programming language.

Now, if we combine all the modifiers mentioned above with the five primitive or primary data types, then it will result in the formation of the following data types:

Range of Values of C Data Type

The range of all the C language data types are present in the table given below:

Data Type Format Specifier Minimal Range Typical Bit Size
unsigned char %c 0 to 255 8
char %c -127 to 127 8
signed char %c -127 to 127 8
int %d, %i -32,767 to 32,767 16 or 32
unsigned int %u 0 to 65,535 16 or 32
signed int %d, %i Same as int Same as int

16 or 32

short int %hd -32,767 to 32,767 16
unsigned short int %hu 0 to 65,535 16
signed short int %hd Same as short int 16
long int %ld, %li -2,147,483,647 to 2,147,483,647 32
long long int %lld, %lli -(263 – 1) to 263 – 1 (It will be added by the C99 standard) 64
signed long int %ld, %li Same as long int 32
unsigned long int %lu 0 to 4,294,967,295 32
unsigned long

long int

%llu 264 – 1 (It will be added by the C99 standard) 64
float %f 1E-37 to 1E+37 along with six digits of the precisions here 32
double %lf 1E-37 to 1E+37 along with six digits of the precisions here 64
long double %Lf 1E-37 to 1E+37 along with six digits of the precisions here 80

As you can witness in the table given above, the varied ranges and combinations of the modifiers and data types result in a range of the changing values.

One must utilise the format specifier when they want to print any value of the variable in a program. They must do it in the printf() statement.

When the value of the Data Type happens to be out of Range

Whenever we try to assign any value to a given data type in a program that is more than the allowed value range, then the compiler in C language will generate an error.

Let us look at an example to understand this better.

#include <stdio.h>

int main() {

// the maximum value allowed in the signed short int is 32767

signed short int x = 34767;

return 0;

}

The generated output for this program would be:

warning: very large integer value implicitly truncated to signed type [-Woverflow]

signed short int x = 34767;

^

Whenever we use a type modifier even without using any data type, the program sets the int data type as a form of default data type. Here, signed would refer to a signed int, unsigned would refer to unsigned int, short would refer to short int, and also, long would refer to long int.

Meaning of Unsigned as well as Signed

It is basically a bit tricky to understand. But in easier words, the signed modifier would refer to both the negative and positive values, while the unsigned modifier refers to all of the positive values.

Whenever a compiler happens to get any numeric value, then it converts that very value of the program into a binary number. This means it will be a combination of various 1s as well as 0s. For instance, the binary of 1 is 0001 or 01, the binary value of the number 2 will be 0010, for the number 32767 will be 01111111 11111111, and many more.

When we talk about the signed integers, we use the very first digit starting from the left (in binary values) or the highest bit of order as the sign flag. The number will be negative whenever the sign flag is 1. It will, conversely, be positive whenever the sign flag is 0.

And because we use one bit to show if the given number is negative or positive, one less bit will be representing the number in the program itself. Thus, the range here is very less.

For instance, in the case of a signed int, the binary digit 11111111 11111111 would mean the number -32,767. Here, the first bit would serve as a sign flag that marks it as a negative number. The rest of the bits would represent the actual number that is 32767. The very same binary digit 11111111 11111111 would mean 65,535 in the case of an unsigned int.

Derived Data Types in C

While there are five data types that are primary, various derived data types are also present in C language that help in storing complex types of data.

Thus, the derived data types are basically primary data types, but these are a bit more grouped together or twisted, such as a structure, array, pointers, union, etc. Visit Derived Data Types in C to know more.

Use of C Data Types

The use of C data type is to define the type of data that we use in a program. Let us take a look at each of these in detail.

The char Data Type

This data type basically refers to all the character values that get enclosed in single quotes. Also, this data type ranges from -127 to 127.

As we can see, we can use any of the smaller integer values in a char data type.

For instance,

char status = ‘X’

The float Data Type

We use the float data type for storing all the real numbers. These may have both, the exponential part as well as the decimal part (or the fraction part). It is basically a single-precision type of number.

For instance,

float y = 127.675;

// by using the suffix f or F

float x = 1000.5454F;

Just like the data type int, we can also use the float data type along with various modifiers.

The double Data Type

We store such numbers in a double data type that the float data type can’t store in a program (bigger than the maximum capacity of the float data type). The double data type is basically a double-precision type of number. It is capable of holding about 15 to 17 values, both after and before a decimal point.

For instance,

double y = 424455236424564.24663224663322;

A person will utilise the double data type in a program only when it is needed to store any such large numbers. Otherwise, we don’t use it as it will make the program very slow.

The int Data Type

We use the int data type for storing the whole numbers that might be values that have no exponential part or decimal part in the number.

We can store the octal (or base 8), hexadecimal (or base 16), and decimal (or base 10) in the data types.

For instance,

// a simple int data value

int z = 310;

// a negative data value

z = -4260;

// an unsigned int data value that has a suffix of u or U

int z = 90U;

// a long int data value that has a suffix of l or L

long int long_val = 87500L;

When we use the int data type in a program, then we must use the suffix “u” or “U” so that the compiler can interpret the available value of the unsigned data type. On the other hand, we use the suffix “l” or “L” when using a long int value in a program.

Practice Problems on Data Types in C

1. Which of these is a data type used in C language?

1. Integer

2. Character

3. Floating-Point

4. Void

A. 1, 3 and 4

B. 2, 3 and 4

C. 1, 2 and 3

D. All of the Above

Answer – D. All of the Above

2. Which of these are basic or primitive data types?

1. Integer

2. Character

3. Double

4. Floating-point

5. Void

A. 1, 3, and 5

B. 2, 4, and 5

C. 1, 2, and 4

D. All of the above

Answer – D. All of the above

3. The character data type ranges from:

A. -63,586 to 63,586

B. -127 to 127

C. -50 to 50

D. It has a huge range. There is no limit.

Answer – A. -63,586 to 63,586

4. The size of the int data type is:

1. 3 bytes

2. 4 bytes

3. 2 bytes

4. 8 bytes

A. 1 and 2

B. 2 and 3

C. 3 and 4

D. 1 and 4

Answer – B. 2 and 3


FAQs

Why do we need modifiers in the C programming language?

There are basically four types of modifiers for all the data types used in the C language. We use these along with all the basic data types for categorising them further.
For instance, if we say that there is a chocolate bar on the table, the person we are speaking to knows that a chocolate bar is present on the table. But if we get more specific and say that there is a dark chocolate bar on the table, or a milk chocolate bar on the table, it becomes much more clear and specific for the person who is listening.
In a very similar manner, the modifiers in the C language help in making the primary or primitive data types much more specific.
Here are a few modifiers:

  • short
  • long
  • unsigned
  • signed

Just like the names suggest here, we use the unsigned and signed modifiers for presenting the unsigned (only +) and signed (- and +) values in any given data type. Also, the short and long modifiers cause an effect on the value range of any given data type.
For instance, the long int, short int, unsigned int, signed int, etc., are all very valid data types in the C programming language.

What happens when the value of the Data Type happens to be Out of Range?

Whenever we try to assign any value to a given data type in a program that is more than the allowed value range, the compiler in the C language will generate an error.
Let us look at an example to understand this better.
#include <stdio.h>
int main() {
// the maximum value allowed in the signed short int is 32767
signed short int x = 34767;

return 0;
}
The generated output for this program would be:
warning: very large integer value implicitly truncated to signed type [-Woverflow] signed short int x = 34767;
^
Whenever we use a type modifier, even without using any data type, the program sets the int data type as a form of default data type. Here, signed would refer to a signed int, unsigned would refer to unsigned int, short would refer to short int, and also, long would refer to long int.

Keep learning and stay tuned to get the latest updates on GATE Exam along with GATE Eligibility CriteriaGATE 2023GATE Admit CardGATE Syllabus for CSE (Computer Science Engineering)GATE CSE NotesGATE CSE Question Paper, and more.

Also Explore,

Leave a Comment

Your Mobile number and Email id will not be published.

*

*