PDF

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.

0 comments:

Post a Comment