Loading...
Loading...
00:00:00

What Is Data Types In C?

Data types are declarations for variables and its size in memeory blocks. Data types is defined what kind of information is stored in a variable. Data types is used to create a variable.

In C language, int is used to store integer value in a variable, char is used to store character , float is used to store floating point value(decimal number) etc. There are so many data types are availabe in C language.

NOTE: Data types size depends on CPU Architecture. Here is the data types in C

Data Types Size (bytes) Range Format Specifier
signed char 1 -128 to 127 %c
unsigned char 1 0 to 255 %c
short 2 bytes -32,768 to 32,767 %hi
unsigned short 2 bytes 0 to 65,535 %hu
long 8 bytes -9223372036854775808 to 9223372036854775807 %l or %ld or %li
unsigned long 8 bytes 0 to 18446744073709551615 %lu
short int 2 -32,768 to 32,767 %hd
unsigned short int 2 0 to 65,535 %hu
unsigned int 4 0 to 4,294,967,295 %u
int 4 -2,147,483,648 to 2,147,483,647 %d
long int 4 -2,147,483,648 to 2,147,483,647 %ld
unsigned long int 4 0 to 4,294,967,295 %lu
long long int 8 -(263) to (263)-1 %lld
unsigned long long int 8 0 to 18,446,744,073,709,551,615 %llu
float 4 1.2E-38 to 3.4E+38 %f
double 8 2.3E-308 to 1.7E+308 %lf
long double 16 3.4E-4932 to 1.1E+4932 %Lf

32 Bit Architecture Data Type Size And 64 Bit Architecture Data Type Size

Data Type 32-bit sizes (in bytes) 64-bit sizes (in bytes)
char 1 1
short 2 2
int 4 4
long 4 8
long long 8 8
float 4 4
double 8 8
long double 16 16
pointer 4 8

Most Commonly Use Data Types

char - char data type is used to store character in a Variable. Any symbol written inside single quote called character char. In C programming, character (char) data type consumes 8 bits (according to cpu Architecture may be different) of memory, in 8 bits of memory we can store anything in a character whose ASCII value lies in between -127 to 127, char data type can hold any of the 256 different values.

For Taking user input or printing character value we have to use format Specifier %c . we can print ASCII value of any character using %d Format specifier.

Example Of Char Data Types

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{
    // char data types 
    char char_value1 = 'a';
    char char_value2 = 'A';
    char char_value3 = '7';
    char char_value4 = '[';
    printf("The character value is %c ",char_value1);  // a
    printf("ASCII value of %c is %d",char_value1,char_value1);  // a 97
    return 0;
}

Integer (Int) Data Type

In C language, int data is used to store integer value. In 4 bytes of integer range is -2,147,483,648 to 2,147,483,647 . Int data type format Specifier is %d it is used to Take user input or printing integer value.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{
    // int data types 
    int int_value1 = 343;
    int int_value2 = -343;
    printf("The integer value is %d ",int_value1); // output 343
    return 0;
}

Float Data Type ( For Decimal)

float data type is used to store decimal value. In 4 bytes of float range is 1.2E-38 to 3.4E+38 . Taking user input or printing floating data type on console we have to use %f format specifier.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{
    float var1 = 352.502;
    float var2 = -353.23;
    printf("The float value if %f ",var1); // 352.502014 
    return 0;
}

How To Declare Variables

Variables are useful when you are using the same value multiple times or in multiple files. variables have their scope according to declarations. Variable in a block called local variable that is not accessible outside of block. There is global variable that is declare outside of main function in C and it is accessible from everywhere. To declare a variables we have use data type and variables name. Here is the examples of different types of variables.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{
    int first_number;  // variable name first_number and its data type int
    char first_letter;  // variable name first_letter and its data type char
    float price;      // variable name price and its data type float (decimal)
    return 0;
}

Assigning Value In Variable

To assign value in a Variable we have to use = (assignment operator) .

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{
    int first_number;  
    char first_letter; 
    float price;    

    first_number = 100;  //assigning value in variable
    first_letter = 'a';  //assigning value in variable
    price = 2000.45;    //assigning value in variable
    return 0;
}

Declare And Assign Value At The Same Time

We can assign value while declaring a Variable. we can change it latter.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{
    int first_number = 100;  //assigning value in variable while declaration
    char first_letter = 'a';  //assigning value in variable while declaration
    float price = 2000.45;    //assigning value in variable while declaration   
    return 0;
}

Declare A Constant Variable

To Declare a constant variable we have to use const keyword before the data type while declaring a variable. const variable cannot be modify once value is assigned.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{
    const int first_number = 100;  //we can not change it later.
    const char first_letter = 'a';  //we can not change it later.
    const float price = 2000.45;    //we can not change it later.
    first_letter = 500;   // Error 
    return 0;
}

Global And Local Variables

Global variable can be access from any where in the program because it has global scope. it is must be outside of main function. When you declare a variable top of the program and outside of main function it is known as global variable and same thing with function.

Local variable cannot be access from outside of a block because it's scope has block level. when you declare a variable in a function and you want to access from outside of that function block you can't.

#include <stdio.h>
#include <stdlib.h>
int number = 50;   // Global variable

int main(int argc, char const *argv[])
{
    int number = 34;   // local variable
    printf("The value is %d ", number); 
    return 0;
}

Global And Local Variable Has Same Name

Global variable is actually belongs to external storage class. For access global variable we have to use extern keyword. If global and local variable has same name you can access it like this...

#include <stdio.h>
#include <stdlib.h>
int number = 50;

int main(int argc, char const *argv[])
{
    int number = 34;
    {
        extern int number;   // external storage class
        printf("The value is %d ", number); // global variable
    }
    printf("The value is %d ", number); // local variable
    return 0;
}

Know more about external storage classes