Data Types in C Language

    2 Votes

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 Types These are the basic built-in data types of C Programming Language. They include 'int', 'char', 'float' and 'double'
Enumerated Types These 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 Type Type 'void' indicates lack of data.
User Defined This 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'.

Other Basic Type Specifiers

Apart from the above mentioned type specifiers there are other type specifiers which can be used to augment the basic data types. They include the following

  • Signed : 'signed' specifier helps us to declare signed integer or character type variable, which can hold positive as well as negative values. By default integer and character type variables are signed.
  • Unsigned : On the other hand, 'unsigned' qualifier helps us to declare variables which can hold only positive values starting from 0. If you are working with values that can’t be less than 0 like the number of cars or visitor count, you should use 'unsigned' as it gives one extra benefit. Declaring an int as unsigned can it able to hold up to double the maximum value supported by int.
  • Long and Short :'Long' and 'short' specifier enables us to declare integers and double (only long specifier supported with double, short is not supported) with different lengths. Short integers are 16 bit long and have -32768 to 32767 range. Long integers have greater range -2147483647 to +2147483647.

Enumeration Types

Enumeration types are used to hold a value that belongs to a particular set. Keyword 'enum' is used to define enumeration types. Each element in the enumeration type is assigned with a consecutive integer constant starting with 0 by default.

Syntax for declaring enum is given below

enum type_name{value_1, value_2, value_3,...}

Each of the values in the value set (ie; value_1, value_2 etc) are integer constants. By default, they start with 0 and increments by 1. You can override this values your self. Let's take a look at it with the help of an example.

#include <stdio.h>
enum day_of_week{ sunday, monday, tuesday, wednesday, thursday, friday, saturday};
int main(){
    enum day_of_week today;
	//Assigns enum value sunday to the variable today.
    today = sunday;
    printf("%d", today);
    return 0;
}

Output:
0

In the above program enum type "day_of_week" consists of 7 values; Sunday to Saturday. Each of them is internally assigned with an integer constant starting with 0 by default. 'Sunday' gets 0, 'Monday' gets 1 and so on. Here the 'printf' statement will output the value 0, because we've assigned enum value 'Sunday' to the variable 'today' of type 'day_of_week'.

If you want to use different integer values for the enumeration constants instead of the default values, you can assign it during the declaration as given below.

#include <stdio.h>
enum day_of_week{ sunday=4, monday, tuesday, wednesday=10, thursday, friday, saturday};
int main(){
    enum day_of_week today;
	//Assigns enum value sunday to the variable today.
    today = monday;
    printf("%d", today);
    return 0;
}

Output:
5

In the above program, we've assigned integer constant 4 to enum constant 'Sunday'. Now the value sequence will start with this assigned value. ie; Monday will get value 5, Tuesday will get value 6 and so on. Again we've overriden the default value for Wednesday (which would have been 7) to 10. This causes the value sequence to reset the values starting from Wednesday. So Thursday will get value 11, Friday will get value 12 and so on. Values assigned to the enumeration constants or the name of the constants it self shouldn't repeat in an enumeration.

Also Read - Top C Interview Questions and Answers for Freshers & Advanced C Interview Questions and Answers

Advantage of enums over integer constants

What advantage you see by using enum over integer constants directly?
You can see that enum improves the code readability and maintainability. If you directly use value 0, 1.. etc to represent days of a week, somebody else looking at the code might get confused. enum is a basic tool used in software programming to avoid such magic numbers.

Void Type

Void type is defined using keyword 'void'. It's used to represent absence of data. For example, if a functions is declared with 'void' return type, it means that function doesn't return any values. But you cannot define a variable with 'void' type, program will not compile. You can declare void pointers though. More about them in chapter for pointers.

User Defined Types

Refer following chapters for user defined datatypes which include Structures, Unions, Arrays and Pointers.

Variables

In C programs, sometimes you may need to store some values so that you can later do some calculation or operation on the stored values. To hold these values we need variables. The variable can be considered as a type of identifiers that are used to represent some type of value or information in a designated portion of a program. A variable can be assigned different values in the course of its life span, i.e. the value stored in a variable may change. However, the type of data associated with a variable can never be changed.

Also Read - Top Embedded C Interview Questions and Answers & C++ Interview Questions and Answers

Variable declaration

Declaration means associating a variable with some data type. Declaring variable will enforce that the variable can only represent values of already specified data types. A declaration has a specific format. Let’s consider following declaration as an example.

int number;

By this declaration, we are specifying that the number is a variable of data type integer. Until now, the variable number does not contain the value we want. The values can be assigned to the variable as follows

number = 6;

This is called assigning a value to a variable. Variable declaration and assignment can be merged as shown in the example below

float price = 1234.50;

Character and character strings

In C, for single character we have a data type char, but for storing multiple characters, like a word, there is no specific data type provided. So, unlike other programming languages like C++, Java etc., C doesn’t have string data types. Strings are often stored as a character array in C. At this point, we haven’t discussed the concept of array yet, so strings will be described in later chapters.

Popular Videos

How to speak to people

How to speak so that people want to listen.

Got a tip or Question?
Let us know

Related Articles

C Overview and Basic Syntax
C Qualifiers - Constant and Volatile type Qualifier
C Type Casting with examples - float to int / int to char / int to long
C Constants and Literals
C Operators - Arithmetic, Assignment, Relational and Logical
C Operators - Bitwise Operators and Conditional Operators
C Operator Precedence and Associativity Table with Examples
C Input and Output Statements
C Format Specifiers for printf and scanf
C Decision Making Control Structures - 'if', 'if - else', nested 'if', 'switch'
Loops in C - for, while and do...while Loops
C 'break', 'continue' and 'goto' Statements to Control Program Flow
C Preprocessor Directives and Macros
C Functions Tutorial with Examples
C Arrays - Dynamic, 2d and Multidimensional with Examples
C Strings - Internal Working and Functions with example
C Pointers basics explained with examples
Structures in C Programming with Example
Unions in c programming with example
C Storage Class Specifiers - auto, register, static and extern