Exforsys.com
 
Home Tutorials C Language
 

C Programming Language - Data Types

 

C Programming Language - Data Types

In this tutorial you will learn about C language data types, Primary data type, Integer Type, Floating Point Types, Void Type, Character Type, Size and Range of Data Types on 16 bit machine, derived data type, Declaration of Variables, User defined type declaration, Declaration of Storage Class, auto, static, extern, register, Defining Symbolic Constants, Declaring Variable as Constant and Volatile Variable



A C language programmer has to tell the system before-hand, the type of numbers or characters he is using in his program. These are data types. There are many data types in C language. A C programmer has to use appropriate data type as per his requirement.

C language data types can be broadly classified as
Primary data type
Derived data type
User-defined data type


Primary data type

All C Compilers accept the following fundamental data types


1.


Integer


int


2.


Character


char


3.


Floating Point


float


4.


Double precision floating point


double


5.


Void


void



The size and range of each data type is given in the table below


DATA TYPE


RANGE OF VALUES


char


-128 to 127


Int


-32768 to +32767


float


3.4 e-38 to 3.4 e+38


double


1.7 e-308 to 1.7 e+308



Integer Type :

Integers are whole numbers with a machine dependent range of values. A good programming language as to support the programmer by giving a control on a range of numbers and storage space. C has 3 classes of integer storage namely short int, int and long int. All of these data types have signed and unsigned forms. A short int requires half the space than normal integer values. Unsigned numbers are always positive and consume all the bits for the magnitude of the number. The long and unsigned integers are used to declare a longer range of values.


Floating Point Types :

Floating point number represents a real number with 6 digits precision. Floating point numbers are denoted by the keyword float. When the accuracy of the floating point number is insufficient, we can use the double to define the number. The double is same as float but with longer precision. To extend the precision further we can use long double which consumes 80 bits of memory space.


Void Type :

Using void data type, we can specify the type of a function. It is a good practice to avoid functions that does not return any values to the calling function.


Character Type :

A single character can be defined as a defined as a character type of data. Characters are usually stored in 8 bits of internal storage. The qualifier signed or unsigned can be explicitly applied to char. While unsigned characters have values between 0 and 255, signed characters have values from –128 to 127.

Size and Range of Data Types on 16 bit machine.


type


SIZE (Bits)


Range


Char or Signed Char


8


-128 to 127


Unsigned Char


8


0 to 255


Int or Signed int


16


-32768 to 32767


Unsigned int


16


0 to 65535


Short int or Signed short int


8


-128 to 127


Unsigned short int


8


0 to 255


Long int or signed long int


32


-2147483648 to 2147483647


Unsigned long int


32


0 to 4294967295


Float


32


3.4 e-38 to 3.4 e+38


Double


64


1.7e-308 to 1.7e+308


Long Double


80


3.4 e-4932 to 3.4 e+4932



Declaration of Variables

Every variable used in the program should be declared to the compiler. The declaration does two things.


1. Tells the compiler the variables name.
2. Specifies what type of data the variable will hold.

The general format of any declaration

datatype v1, v2, v3, ……….. vn;

Where v1, v2, v3 are variable names. Variables are separated by commas. A declaration statement must end with a semicolon.

Example:


Int sum;
Int number, salary;
Double average, mean;


Datatype

Keyword Equivalent


Character


char


Unsigned Character


unsigned char


Signed Character


signed char


Signed Integer


signed int (or) int


Signed Short Integer


signed short int (or) short int (or) short


Signed Long Integer


signed long int (or) long int (or) long


UnSigned Integer


unsigned int (or) unsigned


UnSigned Short Integer


unsigned short int (or) unsigned short


UnSigned Long Integer


unsigned long int (or) unsigned long


Floating Point


float


Double Precision Floating Point


double


Extended Double Precision Floating Point


long double



User defined type declaration

In C language a user can define an identifier that represents an existing data type. The user defined datatype identifier can later be used to declare variables. The general syntax is

typedef type identifier;

here type represents existing data type and ‘identifier’ refers to the ‘row’ name given to the data type.

Example:

typedef int salary;
typedef float average;

Here salary symbolizes int and average symbolizes float. They can be later used to declare variables as follows:

Units dept1, dept2;
Average section1, section2;

Therefore dept1 and dept2 are indirectly declared as integer datatype and section1 and section2 are indirectly float data type.

The second type of user defined datatype is enumerated data type which is defined as follows.

Enum identifier {value1, value2 …. Value n};

The identifier is a user defined enumerated datatype which can be used to declare variables that have one of the values enclosed within the braces. After the definition we can declare variables to be of this ‘new’ type as below.

enum identifier V1, V2, V3, ……… Vn

The enumerated variables V1, V2, ….. Vn can have only one of the values value1, value2 ….. value n

Example:

enum day {Monday, Tuesday, …. Sunday};
enum day week_st, week end;
week_st = Monday;
week_end = Friday;
if(wk_st == Tuesday)
week_en = Saturday;


Declaration of Storage Class

Variables in C have not only the data type but also storage class that provides information about their location and visibility. The storage class divides the portion of the program within which the variables are recognized.

auto : It is a local variable known only to the function in which it is declared. Auto is the default storage class.

static : Local variable which exists and retains its value even after the control is transferred to the calling function.

extern : Global variable known to all functions in the file

register : Social variables which are stored in the register.


Defining Symbolic Constants

A symbolic constant value can be defined as a preprocessor statement and used in the program as any other constant value. The general form of a symbolic constant is

# define symbolic_name value of constant

Valid examples of constant definitions are :

# define marks 100
# define total 50
# define pi 3.14159


These values may appear anywhere in the program, but must come before it is referenced in the program.


It is a standard practice to place them at the beginning of the program.


Declaring Variable as Constant

The values of some variable may be required to remain constant through-out the program. We can do this by using the qualifier const at the time of initialization.

Example:


Const int class_size = 40;


The const data type qualifier tells the compiler that the value of the int variable class_size may not be modified in the program.



Volatile Variable

A volatile variable is the one whose values may be changed at any time by some external sources.


Example:

volatile int num;

The value of data may be altered by some external factor, even if it does not appear on the left hand side of the assignment statement. When we declare a variable as volatile the compiler will examine the value of the variable each time it is encountered to see if an external factor has changed the value.



Read Next: C Programming - Operators



 

 

Comments


Darryl Stone said:

  I have a question. Is there an escape character for centering? The closest I can find is \\t for tabbing.<br />
<br />
Thank you very much!<br />
<br />
Darryl Stone<br />
psukhe@centurytel.net
June 15, 2006, 12:43 am

Noel V Zambezi said:

  the nearest centering character you can use is the undescore (_).I also have a question What is another name for an include file
September 11, 2006, 12:51 pm

prathibha said:

  printf("a%%b=%d",a%b);
why should we write the % operator twice within the quotes.because whatever we write with in the quotes is printed as it
is.but in this case it does not happen like that.becoz the operation is performed based on the argument.why should we use it twice.
October 14, 2006, 10:28 pm

richiesoft said:

  Is while and do also a data type
October 30, 2006, 11:07 am

vintox said:

  can you teach me how to work with format specifiers
November 27, 2006, 11:04 am

InvisibleSniper said:

  No,[b]do[/b] and [b]while[/b] are not also a data type.

[b]do[/b] is a keyword in C/C that is used to initiate the start of a particular loop called "Do While Loop". And [b]while[/b] is used in conjuction with the [b]do[/b] keyword, [b]while[/b] is the part of the "Do While" loop that evaluates the loop to see if it should stop looping.
February 12, 2007, 2:24 pm

joshi13 said:

  can anyone tell me how char -128 would be stored in memory in bits.
as 127 is represented by (01111111) how -128 is represented for character data type
August 26, 2007, 6:32 am

Shiv said:

  Well having a negative number implies you are working with signed arithmetic. This means that the most significant bit (MSB) is a 1 for any negative integer (so for 8 bit numbers all negative numbers are 1XXXXXXX where X can be either 1 or 0).

Computers also work on "two's complement". To invert the sign of a number, invert the bits then add 1.

So to get -127, first take 127 = 01111111, invert the bits to get 10000000 then add 1 = 10000001. So -128 must be -127 minus 1 = 10000000.

From two's complement and knowing the MSB is 1 for negative numbers, you can make any number. This same principal applies to any size signed number (e.g. 32-bit integers, 64-bit and so on).
August 29, 2007, 11:54 pm

x!n said:

  cAn anyone telL me what are the typs of values in C??? what variAblE is? rules in naming a variable?
reserved word? data type? declarations? arithmetic expession? leveL of associativity????pls.........
September 4, 2007, 12:13 am

Karthickeyan.s said:

  What is difference between structure and union?
September 5, 2007, 10:30 pm

manamadurai karthick said:

  what is meant by template?
September 8, 2007, 2:31 am

Rajaa said:

  Why we are use the c language around in the world?
September 8, 2007, 2:34 am

Mohan g said:

  Is the 'C' language difficult from any other high level languages
October 1, 2007, 12:54 am

shabna said:

  Is there is any need of negative numbers in the range of a character variable(range -128 to 127).All ASCII values are ( )ve numbers.Please tell me whether it is possible to assign a (-) ve number to a character variable.If possible show me how
November 15, 2007, 3:15 am

rekha gambhir said:

  Classification of C data types?
primary or secondary
or
simple or derived
i m confused between them?
araays ,union ,pointers comes under which category
plz help me out,plz
December 12, 2007, 7:50 am

p anusha said:

  where we used c language mostly we found efficient lanuages than c .what is the use of them
December 25, 2007, 6:22 am

Jagadeesh Kumar said:

  We know the integer data type storing (eg. for char 127 to -128 in binary 0111 1111 to 1000 0000 ) but how float data type is store in memory please explain in binary format.
February 19, 2008, 10:20 pm

Sudhanshu said:

  can u help finding me range of float and long datatypes in C by programming without using headerfiles meant for this purpose..
June 2, 2008, 2:31 pm

ravindhar said:

  is void is a empty datatype or primary datatype
August 21, 2008, 6:20 am

spandan said:

  what is the default data type in c?
September 21, 2008, 4:54 am

Mohammed Mahmoud Abdul Aziz Karim said:

  There is no "default" data type in C.
October 17, 2008, 11:26 am

manish kumar said:

  i wan 2 know that in 1 byte,how many characters r there?
October 24, 2008, 6:39 am

dhineshkumar said:

  #include<stdio.h>
void main()
{ unsigned int i=5;
if(i>-1)
printf("true");
else
printf("falsle");
getch();
}

i m getting output of "false" .
but 5 is greater than -1 ..
then y i m getting this result.
November 5, 2008, 9:04 pm

copy scape said:

  c programing rocks!
November 6, 2008, 1:04 am

Adam said:

  i dont understand how "double" works.. for instance i have a problem here and i dont get how they answer is 8..

"What is the final value of X after performing the following operations?"
int x = 21;
double y = 6;
double z = 14;
y = x/z;
x = 5*y;

Answer is 8.. but how? am i misunderstanding how "double" works? Im solving for X right?

Thanks!
November 7, 2008, 8:23 am

bhushan janbandhu said:

  convert pola into rectrangular form using structure
November 10, 2008, 11:42 pm

sameer farooq said:

  could any one tell me what does c stands for?
December 9, 2008, 11:32 am

chery mel said:

  please give me a summary about programming data types.... tnx
December 10, 2008, 5:03 am

nsaisathya said:

  #include<stdio.h>
int main()
{
struct emp
{
int a;
char ch;
float s;
}e;
scanf("%dn%cn%f",&e.a,&e.ch,&e.s);
printf("%dn%cn%f",&e.a,&e.ch,&e.s);
return 0;
}
in this program when we give i/p say 22 a 45.5
the output sholud be displayed as the same
but instead it display without character and in that it displays a blank space...........plzzzzzzz answers for this q....
id is nsaisathya@gmail.com
December 14, 2008, 6:45 am

RAJ said:

  plz mail me MCQ question on C.
January 4, 2009, 9:52 am

susil said:

  i want to learn c language.please explain the procedure of learning. i am in initial stage of computer studies
January 5, 2009, 8:20 am

rajesh said:

  I cant understood, why they are using software, what is the merits & demerits, can u explain 2 or 3 lines.

January 8, 2009, 7:20 am

shobh nath rai said:

  why we cannot use a blank space character in the decleratin of a variable?
January 12, 2009, 8:53 pm

sachin said:

  I want to know about conversion specifiers in C.
January 16, 2009, 12:45 am

1 said:

  hi.. can anyone help me..?? can anyone teach me how to make a C programming into application, so that it wont see the codes..

pls help.. tnx a lot
January 22, 2009, 9:09 am

pradeep said:

  what does c stands for
January 23, 2009, 6:23 am

metha said:

  what is the data type for a word
February 1, 2009, 4:27 am

shyamapada mukherjee said:

  can you explain in details why we say range of float(32 bit) 3.4e-38 to 3.4e+38.
February 5, 2009, 5:04 am

bharathiraja rajangam said:

  i couldn't fount the error in the following program , please find and tell me

#include<iostream.h>
#include<conio.h>
#include<alloc.h>
#include<stdio.h>
struct node
{
char name[10];
int bt;
int wt;
int tat;
struct node*next;
};
typedef struct node n;
n *start=NULL;
void main()
{
int i,m;
n *p,*temp,*t;
clrscr();
printf("nEnter the number of Process:");
scanf("%d",m);
for(i=0;i<m;i++)
{
p=(n*)malloc(sizeof (n));

printf("nt Enter the Process Name:")
scanf("%c",name);

printf("ntEnter the Burst Time:");
scanf("%d",bt);
if(start==NULL)
{
start=p;
start->next=NULL;
start->wt=0;
start->tat=start->bt;
}
else
{
temp=start;
while(temp!=NULL)
{
t=temp;
temp=temp->next;
}
t->next=p;
temp=p;
temp->wt=t->tat;
temp->tat=t->tat+temp->bt;
temp->next=NULL;
}
}
temp=start;
printf("nProcessesttBTttWTttTAT");
while(temp!=NULL)
{
printf("nt",temp->name);
printf("tt",temp->bt);
printf("tt",temp->wt);
printf("tt",temp->tat);
temp=temp->next;

}
printf("nn Created By:nt Bharathiraja");
getch();
}

February 9, 2009, 7:18 am

Srilakshmi said:

  why string is not a derived datatype even it is also derived from char as arrays?
March 10, 2009, 12:37 am

Praveen Yadav said:

  Why we use # in directives?
March 23, 2009, 1:16 pm

Lawal A Ahmed said:

  1. What are the demerit of C language?
2. What makes C language superior over other PL
April 26, 2009, 5:08 am

dilip agarwal said:

  what is the actual working of void data type.if void means empty then how pointer of void data type can hold address of any data type
June 11, 2009, 9:25 pm

nina said:

  how do i declare a long interger example 1000000000 and 9999999999.
i have tried but it says that the integer is to long.
July 29, 2009, 10:34 am

shoujoreader said:

  What makes C language superior over other PL
My instructor recommend us to use C. Because using C will make you more intellectual.

why string is not a derived data type even it is also derived from char as arrays?

strings is declared in char, arrays are finite set of homogeneous elements (same data type). I don't really get this question, so sorry, this is only i can say...

Is the 'C' language difficult from any other high level languages

Of course, its the root of all programming languages. C#, C++, Java, etc are based based on C

what is the data type for a word.
you declare it in char...
August 20, 2009, 11:47 pm

Deepak singh said:

  Demerit of C lang
1 no runtime checkng in C
2 no strict type rule for data means we can store float in int and vice versa
3rd once the code become large its very difficult to manage
September 2, 2009, 2:08 am

jaspal singh gil said:

  How can we find the range of data type in C language during program.
September 4, 2009, 4:51 am

muniba said:

  Is struct a data type or a data structure?
September 10, 2009, 10:41 am

Sooth said:

  @dhineshkumar: you are getting false because the number you are comparing to -1 is unsigned, this means it is greater than 0, if you want to have a correct result, do not delcare it as unsigned, because working with different declared types to obtain a bigger type won't sometimes work! The same goes if you declare two int numbers x and y that have values 5 and 2, and you have a float number that gets the value x/y. It will get 2 instead of 2.5 because not all data types we're float.
@Adam: read what I said above.
@shobh nath: because the program wont know if you are just putting a space or you are declaring calling or declaring that variable...
@metha: the data type for a word is char but declared as a vector, example: char myword[100]; // and it can be a word up to 100 characters.
@Praveen Yadav: because this is the C syntax I guess
@Lawal A Ahmed: most programs are either user friendly or computer friendly, wich means they are either easier to use and program or harder to use but able to do more things. C makes all users happy, it's easier to use but very powerful as well. It can do very low coding and it can be used, for example, to make operating systems like windows or linux. You can not do that with Pascal and neither object oriented programming.
@dilip agarwal: as said, the void data type is used to declare functions that do not return anything.
@nina: you must now how to use dynamic alocation programming.
October 5, 2009, 12:25 pm

Sunitha said:

  i want 2 know what is the longest integer data type in c lang and what is the access specifier 2 get the value 2 print
October 21, 2009, 5:47 am

sindhu said:

  @Muniba

struct is a keyword.And it is a user-defined data type.
October 25, 2009, 11:47 pm

vijaykumar said:

  what does extern declaration of a function mean?
December 23, 2009, 1:17 pm

ekta said:

  Can we use & with register variable?
January 12, 2010, 6:08 am

nikhil said:

  Dear sir
Good evening
I have a little bit confusion in constants.
In the program of p,n,r the program is below
#include<stdio.h>
int main()
{


float p,r,si;int n;

p=100;

scanf("%f%i%f",n,si);

si=p*n*r100;

printf("%f",si);
}
If i assign the value of p=100 so the assign value of constant that it is not change if i execute any number of
sets in in this program the value of p=100 remain constant.
January 14, 2010, 10:41 am

minal said:

  how to calculate binary values for negative numbers like -10?
January 28, 2010, 9:01 am

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2010 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape