Wednesday, January 6, 2016

Data Types in C Language

In a C program, data types are used to specify the type of data held by a variable or type of data returned/accepted by a function. If you remember the HelloWord Program we wrote earlier, you might have noticed that the main function returns an 'int' type response, which represents numbers.
Datatypes in C can be categorised into following sections
Basic Data TypesThese are the basic built-in data types of C Programming Language. They include 'int', 'char', 'float' and 'double'
Enumerated TypesThese are the types which can hold only a specific set of values for the variables defined using them. Name of the datatype is 'enum'
Void TypeType 'void' indicates lack of data.
User DefinedThis includes the types that can be created by the programmer using the basic types. This includes 'struct', 'union', arrays and pointers

Basic data types

Basic data types are the built-in data types supported by C language. You can find the explanation and usage of them below
  • Char : This can be used to store a single character. It can hold only one byte of data.
    char var = 'a';
    In the above example, variable 'var' holds a single character 'a'.
  • Int: This data type can be used to hold integer type of data. It may use 2/4/8 bytes depending on the architecture of the platform you're running the program. If the platform is 16 bit, int data type will use 2 bytes. If platform is of 32 bit, int data type will use 4 bytes. If platform is of 64 bit, int will use 8 bytes. 'int' variable cannot hold a floating point number.
    int var = 10;
    In the above example, variable 'var' holds an integer value '10'.
  • Float: This data type can be used to hold decimal numbers. It uses 4 bytes and hold data of precision upto 6 decimal points.
    float var = 1.123456;
    In the above example, variable 'var' holds a float value '1.123456'. If you specify more than 6 decimal points precision, value will be rounded to 6 decimal precision.
  • Double: This is same as float data type except that, 'double' allows double the precision of float. It uses 8 bytes to store the data and can hold data of precision upto 15 decimal points.
    double var = 1.123456789;
    In the above example, variable 'var' holds a double value '1.123456789'.

No comments: