C Language

Data Types in C

In C Language by Baqir AliLeave a Comment

In C each variable has a type as well as a value:

  • The type determines the meaning of the operations that can be applied to the value
  • For example, declarations like
  int i, j;  
  double d;
  float x;  

determine the operations and space requirements of the variables. The data type of every variable in a C program must be declared before that variable can appear in a statement.

There are four basic data types in C:

 char     – a single byte, capable of       holding one character
 int      – an integer
 float    – single-precision floating point
 double   – double precision floating point 

There is NO (true/false) type in C, the conditions true and false are represented by the integer values 1 and 0, respectively.

Sizes of data types On a 16-bit System

C Type Size (bits) Size (Byte) Format Pacifiers
char 8 1 %c
unsigned char 8 1 %u
int 16 2 %d
short int 16 2 %d
long int 32 4 %ld
float 32 4 %f
double 64 8 %lf
long double 80 10 %Lf

Example 1: Integer in C

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
	int a;
	int c;
	int b;
	a=10;
	b=20;
	c=a+b;
	printf("The sum is %d",c);
	return 0;
}

Output:

The sum is 30

Example 2: Character in C

#include <stdio.h>
#include <stdlib.h>



int main(int argc, char *argv[]) {
	char ch;
	ch='A';
	printf("Character is %c ",ch);
	printf("\n");
	printf("ASCII code for character %c is %d",ch,ch);
	return 0;
}

Output:

Character is A
ASCII code for character A is 65

Example 3: Checking either the character is Capital or Small in C

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	char ch;
	printf("Enter your favourite character: ");
	ch=getche();
	printf("\n");
	printf("Your Favourite character is %c",ch);
	if(ch>=65 &amp;&amp; ch<=91)
		printf("\nIt is Capital Letter");
	else if(ch>=97 &amp;&amp; ch<=122)
		printf("\nIt is Small Letter");
	return 0;
}

Output:

getche() in C

Example 4: Using float data type in C

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
 	float pi=3.1416;
 	float r,a;
 	printf("Enter radius of a circle: "); 
 	scanf("%f",&amp;r);
 	a=pi*r*r;
 	printf("\n");
 	printf("Area of a circle is %f",a);
}

Output:

float data type in C

Support us by sharing this post

About the Author

Baqir Ali

Facebook Twitter

Hi, I am Baqir Ali ---- Blogger, Programmer, and a Teacher. I am currently working as a Programmer for Softnat Technologies. I have tons of ideas and data to share with you all via blogging.

Leave a Comment