Data types

Data Types in C

Each variable in C has an associated data type. Each data type requires different amounts of memory and has some specific operations which can be performed over it. Let us briefly describe them one by one:

Following are the examples of 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.

Different data types also have different ranges upto which they can store numbers. These ranges may vary from compiler to compiler. Below is list of ranges along with the memory requirement and format specifiers on 32 bit gcc compiler

DATA TYPEMEMORY (BYTES)RANGEFORMAT SPECIFIER
short int2-32,768 to 32,767%hd
unsigned short int20 to 65,535%hu
unsigned int40 to 4,294,967,295%u
int4-2,147,483,648 to 2,147,483,647%d
long int8-2,147,483,648 to 2,147,483,647%ld
unsigned long int80 to 4,294,967,295%lu
long long int8-(2^63) to (2^63)-1%lld
unsigned long long int80 to 18,446,744,073,709,551,615%llu
signed char1-128 to 127%c
unsigned char10 to 255%c
float4%f
double8%lf


 
                                       
16%Lf


    PRINTF() ;

The function printf() is used for formatted output to standard output based on a format specification. The format specification string, along with the data to be output, are the parameters to the printf() function.

 Syntax: printf (format, data1, data2,……..); 

Example:
#include<stdio.h> 
  #include<conio.h> 
  Main()   
  Char alphabh="A"; 
  int number1= 55;
   float number2=22.34; 
  printf(―char= %c\n‖,alphabh); 
  printf(―int= %d\n‖,number1);
   printf(―float= %f\n‖,number2); 
  getch(); 
  clrscr(); 
  retrun 0; 
  }   
Output Here…  
 char =A
   int= 55 
  flaot=22.340000  

SCANF();

The function scanf() is used for formatted input from standard input and provides many of the conversion facilities of the function printf(). 

Syntax ;
scanf (format, num1, num2,……);
 The function scnaf() reads and converts characters from the standards input depending on the format specification string and stores the input in memory locations represented by the other 
arguments (num1, num2,….). 

Example:
void main()
{
char ch;
printf("\nEnter a character: ");
scanf("%c",&ch);
printf("\nThe character is:%c",ch);
}
Output:
Enter a character:M
The character is :M



Comments