Explore
ProGrad Programs
Placement Prep
TCS Codevita
Webinars
About Us

Edit
Reply




Edit

Data Types in C

Published on 07 Mar 2020

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 typesMemory requirements
char1 byte (8 bits)
int4 bytes (32 bits)
float4 bytes (32 bits)
double8 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'

If you have any feedback about this article and want to improve this, please write to enquiry@faceprep.in