English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C Language Basic Tutorial

C Language Flow Control

C Language Functions

C Language Arrays

C Language Pointers

C Language Strings

C Language Structure

C Language File

C Other

C Language Reference Manual

C Language Data Types

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:

TypeData type
Basic data typeint, char, float, double
Derived Data Typesarray, pointer, structure, union
Enumeration data typeenum
Void data typevoid

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.

Basic type

The following table lists the commonly used basic data types in C language programming.

Data typeStorage sizeRange
char1 bytes-128 to 127
signed char1 bytes-128 to 127
unsigned char1 bytesfrom 255
short2 bytes-32,768 to 32,767
signed short2 bytes-32,768 to 32,767
unsigned short2 bytesfrom 65,535
int2 or 4 bytes-32,768 to 32,767
signed int2 or 4 bytes-32,768 to 32,767
unsigned int2 or 4 bytesfrom 65,535
short int2 bytes-32,768 to 32,767
signed short int2 bytes-32,768 to 32,767
unsigned short int2 bytesfrom 65,535
long int4 bytes-2,147,483,648 to 2,147,483,647
signed long int4 bytes-2,147,483,648 to 2,147,483,647
unsigned long int4 bytesfrom 4,294,967,295
float4 bytes
double8 bytes
long double10 bytes

int (integer)

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 (single precision floating-point number) and double (double precision floating-point number)

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.

char

The keyword char is used to declare a character type variable. For example,

char test = 'h';

The size of a character variable is1bytes.

void

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.

short and long

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

signed and unsigned

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

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.