Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Complete guide on Data Types in C in Just 5 Minutes

Introduction to Data Types in C

In the C language, data types mean the semantics and characteristics of the storage of data elements. Data types are represented in the language syntax in form of declarations for memory locations or variables. These also determine the types of operations or methods of processing for the data elements.

Integer Data Types

There are many different types of data. Variables are classified according to their data type, which determines the kind of information that may be stored in them. Integer variables can only hold whole numbers.

Integers are whole numbers like -2, 19, and 24. Floating-point numbers have a decimal point like -2. 35, 19.0, and 0.024. Additionally, the integer and floating-point data types are broken into even more classifications.

Your primary considerations for selecting the best data type for a numeric variable are the following:

  • Whether the variable needs to hold integers or floating-point values.
  • The largest and smallest numbers that the variable needs to be able to store.
  • whether the variable needs to hold signed (both positive and negative) or only unsigned (just zero and positive) numbers.
  • The number of decimal places of precision needed for values stored in the variable.

    Declaration of Integers in c syntax is as below:

    The declaration of integer in C is as follows:

    int a;
    int b;
    int c;

    Floating-Point Data Types

    Floating-point data types are used to define variables that can hold real numbers.

    C uses the decimal point to distinguish between floatingpoint numbers and integers, so a number such as 5.0 is a floatingpoint number while 5 is an integer. Floatingpoint numbers must contain a decimal pointNumbers such as 3.14159, 0.5, 1.0, and 8.88 are floatingpoint numbers.

    Basic Syntax to Declare floating-point values:

    float distance = 1 .496E8;

    This table below shows the processing and memory consumption of floating-point values in c as below:

    The char data type in C

    The char data type is used to store individual characters. To this point, the fundamental data types we’ve looked at have been used to hold numbers (integers and floating-point) or true/false values (Booleans). But what if we want to store letters? The char data type was designed for such a purpose.

    The char data type is an integral type, meaning the underlying value is stored as an integer, and it’s guaranteed to be 1-byte in size.

    However, similar to how a Boolean value is interpreted as true or false, a char value is interpreted as an ASCII character.

    ASCII stands for American Standard Code for Information Interchange, and it defines a particular way to represent English characters (plus a few other symbols) as numbers between 0 and 127 (called an ASCII code or code point). For example, ASCII code 97 is interpreted as the character ‘a’.

    Example of a char data type program:

    #include
    #include
    int main()
    {
        char ch1=  { 'A' }; // (preferred)
        printf(“%d”, ch1); // printf prints a character
        char ch2 = { 98 }; // code point for 'b' (not preferred)
        printf(“%d”, ch2); // printf prints a character
        getch();
    }

    Output on Screen:
    A 98

    Strings in c

    A string variable contains a collection of characters surrounded by double-quotes.Let’s take an example program:

    string greeting = "Hello";
    This is the basic syntax for declaring string in c
    We also have to include string library for this purpose
    // Include the string library
    #include 
    // Create a string variable
    string greeting = "Hello"
    

    The Difference Between Character Literals and String Literals in C

    printf(“%d”, ‘ H’); This displays a character literal.
    printf(“%d”, ‘ Hello’); This displays a string literal.

    This means to say that character literal consists of a single alphabet and string literal consists of a group of alphabets and in the form of the word.

    Boolean data type in C

    The Boolean data type is used to declare a variable whose value will be set as true (1) or false (0). To declare such a value, you use the bool keyword.

    The variable can then be initialized with the starting value. A Boolean constant is used to check the state of a variable, an expression, or a function, as true or false.

    Syntax of Boolean in C

    bool GotThePassingGrade = true;

    Example program of boolean data type:

    void main()
    {
    bool isaFun = true;
    bool isNotaFun = false;
    printf(“%d”,isaFun);   
    printf(“%d”,isNotaFun);  
    }

    Output on Screen:
    True
    False

    How to determine the size of a data type in C?

    The size of the operator may be used to determine the size of a data type on any system. In general computers, we do not have a lot of memory for management so we need to declare variables according to their sizes and preference so it is known fact and important to know how to calculate the size of data types this program below shows us the same thing.

    #include
    #include
    Void main()
    {
    printf("The size of short is: %d\n", sizeof(short x));
    printf("The size of char is %d\n", sizeof(long y));
    }

    Output on Screen:
    The size of the short is: 1
    Size of char is: 4

The post Complete guide on Data Types in C in Just 5 Minutes appeared first on Tech Travel Hub.



This post first appeared on Tech Travel Hub, please read the originial post: here

Share the post

Complete guide on Data Types in C in Just 5 Minutes

×

Subscribe to Tech Travel Hub

Get updates delivered right to your inbox!

Thank you for your subscription

×