Data Types in C

Data types in c refer to an extensive system used for declaring variables or functions of different types. The type of a variable determines how much space it occupies in storage and how the bit pattern stored is interpreted.

The types in C can be classified as follows −

screenshot_1

Following are some very common data types used in C:

char: The most basic data type in C. It stores a single character and requires a single byte of memory in almost all compilers.
int: As the name suggests, an int variable is used to store an integer.
float: It is used to store decimal numbers (numbers with floating point value) with single precision.
double: It is used to store decimal numbers (numbers with floating point value) with double precision.

Integer and floating-point types

data-types-in-c-rivisited-2

Data Types Example

c-data-types-arrays-and-structs-7-638

Output

This is my float:3.1415

This is my integer:20

This is my character:a

Size Of Data Types

Output

Note

  • We can’t store decimal values using int data type.
  • If we use int data type to store decimal values, decimal values will be truncated and we will get only whole number.
  • In this case, float data type can be used to store decimal values in a variable.

Enumeration Data type

  • Enumeration data type consists of named integer constants as a list.
  • It start with 0 (zero) by default and value is incremented by 1 for the sequential identifiers in the list.
  • Enum syntax in C:
    enum identifier [optional{ enumerator-list }];
  • Enum example in C: 

enum month { Jan, Feb, Mar }; or
/* Jan, Feb and Mar variables will be assigned to 0, 1 and 2 respectively by default */
enum month { Jan = 1, Feb, Mar };
/* Feb and Mar variables will be assigned to 2 and 3 respectively by default */
enum month { Jan = 20, Feb, Mar };
/* Jan is assigned to 20. Feb and Mar variables will be assigned to 21 and 22 respectively by default */

  • The above enum functionality can also be implemented by “#define” preprocessor directive as given below. Above enum example is same as given below.

#define Jan 20;
#define Feb 21;
#define Mar 22;

Example For Enumerated Data Type

Output

Derived Data Type

  • Array, pointer, structure and union are called derived data type in C language.

Void Data Type

  • Void is an empty data type that has no value.
  • This can be used in functions and pointers.