Data types  in C programming

Data types in C programming

C is a low-level programming language developed by Dennis Ritchie in the year 1972, it was named C because there was already a language called B.

C is a compiled programming language meaning a compiler is needed before one can run a C program. A compiler is computer software that will turn your code from human-readable language to machine language. The computer understands machine language.

There are four stages of C Complilataion there are

  • preprocessing
  • compiling
  • assembling
  • linking

Data TYPES IN C

C is made up of 4 basic data types there are char, int, float, and double. There are also signed and unsigned for these data types. All variables in C must be declared with a data type.

Char

The char data type stores only a single character. size of the char data type is 1 byte. The format specifier for char is %c.

#include <stdio.h>
/**
 * main - function to print a character
 * 
 * Return: 0  on success
 */
int main()
{
    /*Declaring a char*/
    char a;
    /*Assigning the character a*/
    a = 'a';

    printf("The character value of a is: %c\n", a);

    return (0);
}

The output is

The character value of a is: a

Int

The int stores whole numbers without decimal values. The size of int is usually 2 bytes or 4 bytes depending on the computer. In a 64bits architecture, the int size is 4 bytes. The format specifier for int is %d.

#include <stdio.h>
/**
 * main - function to print an integer
 * 
 * Return: 0  on success
 */
int main()
{
    /*declaring an integer*/
    int a;
    /*assigning an integer*/
    a = 10;

    printf("The interger value of a is: %d\n", a);

    return (0);
}

The output is

The interger value of a is: 10

Float

The float stores decimal and floating point numbers with single precision. The size of the float is 4byte. The format specifier for char is %f. The float data type also stores exponential values.

#include <stdio.h>
/**
 * main - function to print a floating point number
 * in single precision
 * Return: 0  on success
 */
int main()
{
    /*declaring a float*/
    float a;
    /*assigning the  float a*/
    a = 1.23456;

    printf("The value of a is: %f\n", a);

    return (0);
}

The output is

The value of a is: 1.234560

Double

The double is used to store decimals(floating point numbers) with double precision. Double has more precision making it to have a bigger size than the float data type. The size of the double is 8 bytes when the float is 4bytes. The format specifier for the double data type is %lf.

#include <stdio.h>
/**
 * main - function to print a floating point number
 * in double precision
 * Return: 0  on successfully
 */
int main()
{
    /*declaring a double*/
    double a;
    /*assigning a value to the double a*/
    a = 1234548959.006;

    printf("The value of a is: %lf\n", a);

    return (0);
}

The output is

The value of a is: 1234548959.006000

Long and Short

If one needs to hold large numbers long can be used to store variables. when one needs a very small number the short is used.

signed and unsigned

The sign allows storage for both positive and negative numbers while the unsigned is for only positive numbers. The format specifier for the unsigned in is %u.

Sizeof Operator

The sizeof() operator is used to know the size of an expression or the data type specified in a char-sized storage unit. With the sizeof operator, one can know the size of all data types. The sizeof takes one operand which is either the expression, data_type, or an already declared variable.

#include <stdio.h>
/**
 * main - print the size of various  data types
 * 
 * Return: 0 on success
 */

int main()
{
    printf("The size of char is %d bytes.\n", sizeof(char));
    printf("The size of int is %d bytes.\n", sizeof(int));
    printf("The size of float is %d bytes.\n", sizeof(float));
    printf("The size of double is %d bytes.\n", sizeof(double));
    return (0);
}

The output is

The size of char is 1 bytes.
The size of int is 4 bytes.
The size of float is 4 bytes.
The size of double is 8 bytes.

Conclusion

In this article, we have been able to cover the basic data types in C programming. There are other derived data types which are array, pointer, structure, and union. This article is open for correction, questions and contributions. Anticipate more articles on C programming.