PDF

Showing posts with label variables. Show all posts
Showing posts with label variables. Show all posts

Variables

Leave a Comment

Introduction


A variable is just a named area of storage that can hold a single value (numeric or character). The C language demands that you declare the name of each variable that you are going to use and it's type or class, before you actually try to do anything with it.

The Programming language C has Two main Variable Types as following:

Local Variables


Local variables scope is confined within the block or function where it is defined. Local variables must always be defined at the top of a block.
When a local variable is defined it is not initialised by the system, you must initialise it yourself. When execution of the block starts the variable is available and when the block ends the variable 'dies'.


Global Variables


Global variable is defined at the top of the program file and it can be visible and modified by any function that may reference it. Global variables are initialised automatically by the system when you define them !
Every variable in C has it's own name. A variable without any name is not possible in C. Most important properties of variables name are it's unique names. The rules for writing variable name is same as that of identifiers.

Not two variables in C can have same name with same visibility.

For Example:

#include<stdio.h>
int main()
{
int a = 5;    //Visibility is within main block
int a = 10;   //Visibility is within main block

/*  Two variables of same name  */

printf("%d",a);
return 0;
}

Output:  Compilation error


#include<stdio.h>
int a = 5;    // Visibility is in whole program
int main()
{
int a = 10;    //Visibility is within main block

/*  Two variables of ame name  */

printf("%d",a);
return 0;
}

Output:  10

But it is possible that two variable with same name but different visibility.

In this case variable name can access only that variable which is more local.

In C there is not any way to access global variable if any local variable is present of same name.

For Example:


#include<stdio.h>
int main()
{
int a=10;    //Visibility within main block
{
    a= a+5;       //Accessing outer local variable 'a'
    int a=20;     //Visibility within inner block
    a= a+10;     //Accessing inner local variable 'a'

    printf("%d",a);    //Accessing inner local variable 'a'
}
printf("%d",a);    //Accessing outer local variable 'a'
return 0;
}

Output:  30  15

Address of a Variable in C

Leave a Comment
Location in a memory where a variable stores it's data or value is known as address of variable.

To know address of any variable C has provided a special unary operator & which is known as deference operator or address operator.

It operator is only used with variables not with the constant.

For Example:


#include<stdio.h>
int main()
{
int a = 5;
printf("Address of variable 'a' is :",&a);
return 0;
}


We cannot write:  &&a   because:
&&a = &(&a) = & (address of variable 'a') = & (a constant number)
And we cannot use address operator with constant.


Important points about address of variables in C:

(1)

Address of any variable in c is an unsigned integer.
It cannot be a negative number.
So in printf statement we should use  ' %u ' instead of  ' %d ', to print the address of any variable.

Format specifiers:


" %d " : It is used to print signed decimal number.

" %u " : It is used to print unsigned decimal number.

" %x " : It is used to print a number in hexadecimal format using 0 to 9 and a,b,c,d,e,f.

" %X " : It is used to print a number in hexadecimal format using 0 to 9 and A,B,C,D,E,F.


since, if the address of any variable will beyond the range of signed short int it will print a cyclic value.

(2)

To print the address of any variable in hexadecimal number format by printf function we should use  ' %x ' or  ' %X '.

(3)

A programmer cannot know at which address a variable will store the data. It is decided by compiler or operating system.

(4)

Any two variables in C cannot have same physical address.

(5) 

Address of any variable reserve two bytes of memory spaces.

(6)

Address of any variable in C is not integer type so to assign an address to a integral variable we have to type cast the address.


Address arithmetic in C:


(1) 

We can subtract address of any two variables.

For Example:

#include<stdio.h>
int main()
{
int a;
int b;
printf("%d",&b-&a);
return 0;
}

(2) 

we cannot add, multiply, divide two address.

(3) 

We can add or subtract a integer number with address.

(4) 

Other operators which can be used with addresses are :

Negation operator           :  !
Size operator                    :  sizeof
Type casting operator     :  (Type)
Deference operator         :  *
Logical operator               :  && , ||

Examples of Variables

Leave a Comment
Example 1 : what will be output of the following C program?

#include<stdio.h>
int main()
{
int goto = 5;
printf("%d",goto);
return 0;
}

Output:  Compilation error

Explanation:  Invalid variable name 'goto' is keyword in C. Variable name cannot be any of keywords of C language.



Example 2 : What will be output of the following C program?

#include<stdio.h>
int main()
{
int _ = 5;
int __= 10;
int ___;
___ = _ + __;
printf("%d",___);
return 0;
}

Output :  15

Explanation: Variable name can have only underscore.



Example 3 : What will be output of the following C program?

#include<stdio.h>
int main()
{
int abcdefghijklmnopqrstuvwxyz123456789 = 10;
int abcdefghijklmnopqrstuvwxyz123456 = 40;

printf("%d",abcdefghijklmnopqrstuvwxyz123456);
return 0;
}

Output : Compilation error 

Explanation: Only first 32 characters are significant in variable name. So compiler will show error : Multiple declaration of identifier abcdefghijklmnopqrstuvwxyz123456.



Example 4 : What will be output of the following C Program?

#include<stdio.h>
int main()
{
int xyz = 20;
int xyz;
printf("%d",xyz);
return 0;
}

Output : Compilation error.

Explanation : Two local variables cannot have same name in same scope.



Example 5 : What will be output of the following C Program?

#include<stdio.h>
int main()
{
int main = 50;
printf("%d", main);
return 0;
}

Output :  50

Explanation : Variable name can be ' main ' because it is not a keyword.