English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, you will learn the basic data types in C programming, such as int, float, char, etc.
There are the following data types in C language:
Type | Data type |
---|---|
Basic data type | int, char, float, double |
Derived Data Types | array, pointer, structure, union |
Enumeration data type | enum |
Void data type | void |
In C programming, data type is the declaration of a variable. This determines the type and size of data associated with the variable. For example:
int myVar;
Here, myVar is a variable of int (integer) type. The size of int is4bytes.
The following table lists the commonly used basic data types in C language programming.
Data type | Storage size | Range |
---|---|---|
char | 1 bytes | -128 to 127 |
signed char | 1 bytes | -128 to 127 |
unsigned char | 1 bytes | from 255 |
short | 2 bytes | -32,768 to 32,767 |
signed short | 2 bytes | -32,768 to 32,767 |
unsigned short | 2 bytes | from 65,535 |
int | 2 or 4 bytes | -32,768 to 32,767 |
signed int | 2 or 4 bytes | -32,768 to 32,767 |
unsigned int | 2 or 4 bytes | from 65,535 |
short int | 2 bytes | -32,768 to 32,767 |
signed short int | 2 bytes | -32,768 to 32,767 |
unsigned short int | 2 bytes | from 65,535 |
long int | 4 bytes | -2,147,483,648 to 2,147,483,647 |
signed long int | 4 bytes | -2,147,483,648 to 2,147,483,647 |
unsigned long int | 4 bytes | from 4,294,967,295 |
float | 4 bytes | |
double | 8 bytes | |
long double | 10 bytes |
Integers can have zero, positive, and negative values. For example, 0,-5,10
We can use int to declare an integer variable.
int id;
Here, id is a variable of integer type.
You can declare multiple variables at once in C language programming. For example,
int id, age;
The size of int is usually4bytes (32bits).
float and double are used to store real numbers, that is, numbers with decimal points.
float salary; double price;
In C language, floating-point numbers can also be expressed in exponential form. For example,
float normalizationFactor = 22.442e2;
What is the difference between float and double?
float (single precision floating-point data type) is4bytes. double (double precision floating-point data type) is8bytes.
The keyword char is used to declare a character type variable. For example,
char test = 'h';
The size of a character variable is1bytes.
void is an empty type, generally used to indicate the return type of a function, indicating that the function returns nothing.
For example, if a function does not return any content, its return type should be void.
Please note that you cannot create a variable of void type.
The void type specifies no available value. It is usually used in the following three cases:
Type and description |
---|
Function returns empty In C, there are various functions that do not return any values, or you can say they return null. The return type of functions that do not return values is empty. For example void exit (int status); |
Function parameters are empty In C, there are various functions that do not accept any parameters. Functions without parameters can accept a void. For example int rand(void); |
Pointer points to void Type of void * Pointer represents the address of the object, not the type. For example, the memory allocation function void *malloc( size_t size ); Returns a pointer to void, which can be converted to any data type. |
If larger numbers are needed, the long type specifier can be used. The method is as follows:
long a; long long b; long double c;
Here, variables a and b can store integer values, and c can store floating-point numbers.
is used, then short can be used.32,767, +32,767If the range [−
short d;
You can use the sizeof() operator to check the size of a variable. For example:
#include <stdio.h> int main() { short a; long b; long long c; long double d; printf("short byte size = %d bytes\n", sizeof(a)); printf("long byte size= %d bytes\n", sizeof(b)); printf("long long byte size= %d bytes\n", sizeof(c)); printf("long double byte size= %d bytes\n", sizeof(d)); return 0; }
Output result:
short byte size= 2 bytes long byte size= 4 bytes long long byte size= 8 bytes long double byte size= 8 bytes
In C, signed and unsigned are type modifiers. You can use them to change the data storage of data types. For example,
unsigned int x; int y;
Here, x, since we use the unsigned modifier, can only store zero and positive values.
Considering the size of int is4bytes, the variable y can store from-231 to 231-1The value, while the variable x can store from0 to 232-1value.
In C language programming, there are other data types such as:
Boolean Type
Enum Types
Complex Data Types
Derived data types are derived from basic data types. For example: arrays, pointers, function types, structures, etc.
We will learn these derived data types in the future tutorials.