Data Types in C Language
Data types provide a valuable role to declare variables or functions of different types in all programming languages.
- If you provide the data type then based on your data type, it makes it easy to return the value of a function.
- It helps a lot when you declare a variable. It becomes easy to differentiate within the data types.
Keeping you updated with latest technology trends, Join TechVidvan on Telegram
What are Data Types in C?
Data types provide a helping hand to the compiler by checking which type of data is being entered by the programmer. C programming language makes it easy for a programmer to use various types of data in their programs.
These are the following data types in C:-
- Basic Data Type:– It includes integer types and floating-point types.
- Enumeration Data Type:- They are used for defining a variable that can only assign separate integer values.
- Void Data Type:– If you see the type is void then it means that no value is available.
- Derived Data Type:– Further categorized into pointer types, array types, structure types, union types etc.
Types Data Types
Basic Data Type int, char, float, double
Derived Data Type array, pointer, structure, union
Enumeration Data Type enum
Void Data Type void
C Data Types
In the C programming language, various types of data are available.
- Primitive data types
- User-defined data types
- Derived data types
Below are the examples of some common data types used in C:
- int (for integer data)
- char (for character data)
- float (for floating point numbers)
- double (double precision floating point numbers)
These data types have different ranges up to which they can store numbers. In addition to these data types, there are few more data types available in the C++ programming language:-
- bool:- It is a boolean value. It can be either true or false.
- wchar_t:- It is more like the character data type. But it is a wide character. The purpose of this data type is to represent characters which require more memory.
- string:- String is generally a sequence of characters. And it is also a data type.
- Integer Data Types in C
int means Integer. Integers can have positive or negative values but no decimal values. For declaring integer variables, use int.
Syntax for declaring multiple variables:-
Type Range Format Specifier Size
%d at least 2, usually 4
short int -32,768 to 32,767 %hd 2
unsigned int 0 to 4,294,967,295 %u at least 2, usually 4
long int -2,147,483,648 to 2,147,483,647 %ld at least 4, usually 8
long long int -(2^63) to (2^63)-1 %lld at least 8
unsigned long int 0 to 4,294,967,295 %lu at least 4
unsigned long long int 0 to 18,446,744,073,709,551,615 %llu at least 8
signed int −32,768 to 32,767 %d 2 bytes
signed short int −32,768 to 32,767 %d 2 bytes
unsigned short int 0 to 65,535 %hu 2 bytes
signed long int -2,147,483,648 to 2,147,483,647 %ld 4 bytes
short -32,768 to 32,767 2 bytes
long -9223372036854775808 to 9223372036854775807 %ld 8 bytes
unsigned long 0 to 18446744073709551615 %u 8 bytes
- Floating data types in C
The purpose of float data type in C is to hold real numbers.
Syntax for declaring a float variable:-
To know more about the Integer types, take a look at the following table.
Type Size Format Specifiers Range of Value
float 4 byte %f 1.2E-38 to 3.4E+38
double 8 byte %lf 2.3E-308 to 1.7E+308
Long double 10 byte %Lf 3.4E-4932 to 1.1E+4932
The difference between float and double is that the size of float is 4 bytes and the size of double is 8 bytes.
- Character Data Types in C
The purpose of these data types is to store single characters. The size of char is 1 bytes.
Syntax for declaring:-
char TechVidvan = ‘D’;
Following is the table to know more about the character data types.
Type Size Format Specifiers Range of Value
char 1 byte %c -128 to 127 or 0 to 255
signed char 1 byte %c -128 to 127
unsigned char 1 byte %c 0 to 255
a. Short and long Data Types in C
Suppose, a programmer wants to use large numbers on their code then they can use the type specifier named long.
Syntax of long:
long j; // for storing integer values
long long o; // for storing integer values
long double p; // for storing floating point values
But if you want to use small integer numbers then you can use a short type specifier.
Syntax of short:
b. signed and unsigned Data types in C
With signed and unsigned type modifiers, a programmer can modify the data storage of a data type.
unsigned int b;
We already know that the size of int is 4 bytes. And the variable a can hold values from -2,147,483,648 to 2,147,483,647. But variable b can hold values from 0 to 4,294,967,295.
User-Defined Data Types in C
- Structure:- To declare a structure variable, use the struct keyword. It stores different data types. A way to combine data of different types.
- Enum:- To define enums, use the enum keyword. It consists of integer values.
- Union:- Union is like Structure. But union uses less memory. It stores a package of different types of data.
Derived Data Types in C
- Arrays:- Arrays store multiple items of the same data type. It is a collection of items stored at contiguous memory locations.
- Pointers:- A powerful feature of c. It helps in accessing the memory and their addresses on the memory.
- References:- Mainly used to return the pointer address of the variable.
- Functions:- C programming language gives many built-in functions like printf(), scanf() etc. But you can also create your own function. It is a group of statements to perform specific tasks.
Void Data Type in C
It means no value available. The use of this data type is to assign null or no return value while declaring a function.
Variable Declaration in C
int a = 1337; // Declaring Integer type Variable
float b = 65.24; // Declaring float type Variable
char ex = ‘T’; // Declaring char type Variable
long d = 41657; // Declaring long +ve integer type Variable
long e = -21556; // Declaring long -ve integer type Variable
Size of Operator in C
If you want to check the size of data types available in C then you can do it by using sizeof() operator. Below is the code to show the storage sizes of int, char and float data types.
printf(“Sizes of int, char and float…\n”);
printf(“int is: %d bytes\n”, sizeof(int));
printf(“char is: %d bytes\n”, sizeof(char));
printf(“float is: %d bytes\n”, sizeof(float));
Sizes of int, char and float…
int is: 4 bytes
char is: 1 bytes
float is: 4 bytes
Below is an example of a code of primary data types in C.
int num = 20;
long int num1 = 455;
char char1 = ‘H’;
signed char char2 = ‘J’;
unsigned char char3 = ‘I’;
float d1 = 25.1;
double d2 = 9.4777;
printf(“Welcome to TechVidvan Tutorial on Data Types of C…\n\n”);
printf(“Size of int data type %d is: %d bytes.\n”, num,sizeof(num));
printf(“Size of long int data type %d is: %d bytes.\n”, num1,sizeof(num1));
printf(“Size of char data type %c is: %d bytes.\n”, char1,sizeof(char1));
printf(“Size of signed char data type %c is: %d bytes.\n”, char2,sizeof(char2));
printf(“Size of unsigned char data type %c is: %d bytes.\n”, char3,sizeof(char3));
printf(“Size of float data type %f is: %d bytes.\n”, d1,sizeof(d1));
printf(“Size of double data type %f is: %d bytes.\n”, d2,sizeof(d2));
Welcome to TechVidvan Tutorial on Data Types of C…Size of int data type 20 is: 4 bytes.
Size of long int data type 455 is: 8 bytes.
Size of char data type H is: 1 bytes.
Size of signed char data type J is: 1 bytes.
Size of unsigned char data type I is: 1 bytes.
Size of float data type 25.100000 is: 4 bytes.
Size of double data type 9.477700 is: 8 bytes.
Format Specifier in C
The purpose of format specifiers is for I/O operations. A programmer can read data from the user using scanf() function and also he can print the data using printf() function.
Below is the list of format specifiers:-
Format Specifier Type
%d Signed integer
%e or %E Scientific notation of floats
%f Float values
%g or %G Similar as %e or %E
%hi Signed integer (short)
%hu Unsigned Integer (short)
%i Unsigned integer
%l or %ld or %li Long
%Lf Long double
%lu Unsigned int or unsigned long
%lli or %lld Long long
%llu Unsigned long long
%o Octal representation
%u Unsigned int
%x or %X Hexadecimal representation
%n Prints nothing
%% Prints % character
- Based on the data type, one can analyze what type of data returned by the function.
- Different ranges and sizes for each data type.
- A constant is a value that stays the same at the time of execution.
Categories: C Tutorials
Tags: Basic data types in C, Data Types in C, Derived data types in C, Format Specifier in C, Void Data Type in C