C Data Types

Posted in /  

C Data Types
vinaykhatri

Vinay Khatri
Last updated on December 23, 2024

    C is one of the famous programming languages among the programmer community because it is a building block of many high-level programming languages. With a diminishing future, C is about to become a part of programming history, yet beginners are suggested to start their programming journey with C because its data type and structure help to understand the basics of programming.

    What are Data Types in C?

    Data Types are used to define the nature and type of the variable we have defined in the programming languages. When we define a data type to a variable, it signifies the memory size of that variable and also tells which operator bounds to that variable.

    Basic Data Types in C

    Data Type Size (byte) Format specifier Range Example
    int
    • 2 for 16-bit compiler
    • 4 for 32 and 64-bit compiler
    %d ?32,768 to 32,767 int number;
    char 1 %c -128 to 127 char character;
    float 4 %f 1.2E-38 to 3.4E+38 float number;
    Double 8 %lf 2.3E-308 to 1.7E+308 double number;
    void 4 to 8

    Basic Data Types with Modifiers Types:

    Data Type Size (byte) Format specifier Range Example
    short int 2 %hd ?32,768 to 32,767 short int number;
    unsigned int 4 %u unsigned int number;
    long int 8 %li long int number;
    long long int 8 %lli long long int number;
    unsigned long int 4 %lu -2,147,483,648 to 2,147,483,647 unsigned long int number;
    unsigned long int 8 %llu 0 to 4,294,967,295 unsigned long int number;
    signed char 1 %c -128 to 127 signed char character;
    unsigned char 1 %c 0 to 255 unsigned char character;
    long double 12 to 16 %Lf long double number;
    Here number and character are variables' names.

    C basically has 4 basic types of data types, which are int, float, char, and double. Prefixes like short, long, signed, and unsigned before basic data types are known as data type modifiers, and they are just used to enhance the range and property of basic data types.

    int

    int is one of the basic data types of C, and it defines that the variable can hold integer values from ?32,768 to 32,767. If a variable is an int by nature, it can only hold an integer value with no decimal point. With the help of Modifier types such as long , and long long we can increase the range of int data types.

    The size of an int varies from compiler to compiler. If you are using a compiler like a turbo with 16 bits, then the int will occupy 2-byte of size with 32 bits, and more the int has 4 bytes. We can perform arithmetic operations on int variables

    Example:

    int num = 100;
    printf("%d", num)

    char

    char keyword is used to define the character data types. It can hold any Alphanumeric single value because its size is 1. We can not perform arithmetic operations on char variables.

    Example:

    char num =  '1'
    char character = 'A'
    printf("%c %c", num, character)

    Float

    Float is also used to define numbers or digits similar to an integer. With float-type variables, we can store all integer values. Float variable possesses a size of 4 bytes It is similar to an integer. The only difference is it stores numbers in decimal points. Float has a range of 1.2E-38 to 3.4E+38, which is far more than int. We can perform arithmetic operations on float variables .

    Example:

    float num=100.0;
    printf("%f", num)

    double

    double is also used to store decimal numbers and digits, similar to float. We often use double instead of float when we need a number that is out of float range. double variable possesses a size of 8 bytes

    Example:

    double d = 3.14;
    printf("%lf", d)

    void

    void is an empty data type, we often use void with function and pointers that store and return no value.

    Example:

    void *ptr;    
    void func()
      {
     // function body
      }

    Conclusion

    In this tutorial, we have explored all the basic data types in C, i.e., int, float, double, char, and void, along with their sizes, format specifiers, ranges, and examples. In addition, we have discussed the basic data types with modifier types. If you have any queries or suggestions regarding this article, you can post them in the comments section below.

    People are also reading:

    Leave a Comment on this Post

    0 Comments