Data Types in C

05 min read

Data Type: Data Type specifies the type of data that can be stored in a variable and the number of bytes required to represent a type. Following are a few examples of data types used in C:

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

Basic data types Memory requirements
char 1 byte (8 bits)
int 4 bytes (32 bits)
float 4 bytes (32 bits)
double 8 bytes (64 bits) which means more significant figures and an exponent of large magnitude.

 

Examples:

int a = 40; // positive integer data type

float b = 5.8967; // float data type

char c = 'Z'; // char data type

long d = 41657; // long positive integer data type

 

Type Declaration

  • Associates a variable with a specific data type.  Example: int a; associate the variable 'a' with 'int' which means 'a' can hold only integer type of data.
  • All variables must be declared before they are used.
  • Declarations are usually done at the beginning of the C program.
  • The declaration consists of specifying the data type first, followed by 1 or more variable names.

The above example should thus be written like this

int a,b;   (or)  int a; int b;
a=7, b=4;
printf("%d", a-b);

Now, what happens when the above-mentioned declaration statement is executed?

1) The compiler is informed that 'a' and  'b' are variables which can hold only integer values

2) It also allocates 4 bytes of memory for both 'a' and `b'

POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error