News:

GinGly.com - Used by 85,000 Members - SMS Backed up 7,35,000 - Contacts Stored  28,850 !!

Main Menu

Declarartion Statements in C

Started by thiruvasagamani, Sep 22, 2008, 10:41 AM

Previous topic - Next topic

thiruvasagamani

Declaration statements: Int a=25;creating a variable in a statement is declaration statement.Then we create a variable, memory is allocated to variable. It is system which allocates memory to a variable. eg: a=25; ---> 2bytes of memory is created for variable 'a'.

Variable 'a' is created and 2 bytes of memory is allocated to variable.Address of variable is &a. & is address operator,we can find address of variable by attaching ampersand to variable name.Value 25 is assigned to variable 'a'.

i) int b=32768;
We cannot store more than 32767 in int variable , so 'b' is -32768 (cycle repeats).
int sum; if a variable is not intialise,it contains garbage value.
ii) float x = 10.98;
variable 'x' is created and 4 bytes of memory is allocated to that variable.Address of variable is &x,value of 'x' is 10.98.
iii) char ch = 'g';
variable 'ch' is created and 1 byte of memory is allocated to 'ch'. Address of variable is &ch,value of 'ch' is 'g'.
iv) long prod = 100000;
double sum = 2500000.6890;
unsigned u= -1;
To store a value more than 32767 use long variable, long is extension to int,double is extension to float,we cannot store negative values in unsigned variable 'u' is 65535 (cycle repeats).
unsigned a =25;
unsigned char b= 'g';
Unsigned implies unsigned int,write unsigned char explicitly for unigned char.If char is ommited it becomes unsigned int.

String Declaration :
String a ; //error
char a[9];//ok
There is no string declaration data type in 'c' language.Use char array to declare string,[] represents array.'a' is char array of size 9,we can store maximum of 8 characters in array 'a'.One location must be left for '\0'.

Comparison of char a, char a[20] :
Char a :

1. It is char declaration
2. variable 'a' can hold only one character. a= 'g'
3. address of variable is &a.
4. one byte of memory is allocated to vriable 'a'.

Char a [20] :

1. It is string declaration.
2. Array 'a' can hold 20 characters.
3. do not attach &(ampersand)to array name since array name itself is address.If array
name is 'a' address is also 'a'.
4. 20 bytes of memory is allocated to variable 'a'.

char a[] = "rama rao ";
char b[20];
char[];//error
Array size must be specified,square brackets cannot be empty.Array 'a' size is number of characters in rama rao + 1, 8+1=9.Array 'a' contains rama raowhile array 'b' contains garbage.
Data type formats:
int ------ > %d
float ------ > %f
char ------ > %c
long ------ > %ld
double ------ > %lf
long double -- ---- > %Lf
unsigned ----- > %u
unsigned long------ > %ul
char array ------ > %s
Thiruvasakamani Karnan