Variable naming rule questions in c


Following question are based on variable naming rules in c. If you have any doubt in the variable naming rules you can ask here.

Rules for identifiers in c language


1.

What will be output of the following c program?

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

(A)5
(B)**
(C)**
(D)Compilation error
(E)None of these


Explanation:

Invalid variable name. goto is keyword in c. variable name cannot be any keyword of c language.

2.

What will be output of the following c program?

#include<stdio.h>
int main(){
long int 1a=5l;
printf("%ld",1a);
    return 0;
}

(A)5
(B)51
(C)6
(D)Compilation error
(E)None of these


Explanation:

Invalid variable name. Variable name must star from either alphabet or underscore.

3.

What will be output of the following c program?

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

(A)5
(B)10
(C)15
(D)Compilation error
(E)None of these

Explanation:

Variable name can have only underscore.

4.

What will be output of the following c program?

#include<stdio.h>
int main(){
    int max-val=100;
    int min-val=10;
    int avg-val;
    avg-val = max-val + min-val / 2;
    printf("%d",avg-val);
    return 0;
}

(A)55
(B)105
(C)
60
(D)Compilation error
(E)None of these


Explanation:

We cannot use special character – in the variable name.

5.

What will be output of the following c program?

#include<stdio.h>
int main(){
    int class=150;
    int public=25;
    int private=30;
    class = class >> private - public;
    printf("%d",class);
    return 0;
}

(A)1
(B)2
(C)4
(D)Compilation error
(E)None of these


Explanation:

Variable name can be keyword of c++.

6.

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;
}


(A)10
(B)40
(C)50
(D)Compilation error
(E)None of these


Explanation:

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


7.

What will be output of the following c program?

#include<stdio.h>
int main(){
    register xyz_123=91;
    auto pqr_123=991;
    const _1a1_=pqr_123+~xyz_123;
    printf("%d",_1a1_);
    return 0;
}

(A)900
(B)999
(C)899
(D)Compilation error
(E)None of these


Explanation:

Variable name can have digits.

8.

What will be output of the following c program?

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

(A)11
(B)5
(C)0
(D)Compilation error
(E)None of these


Explanation:

Variable name cannot be global identifier.

9.

What will be output of the following c program?

#include<stdio.h>
int main(){
    int __BIG__ = 32;
    int y;
    y= __BIG__ && 8;
    printf("%d",y);
    return 0;
}

(A)
32
(B)8
(C)1
(D)Compilation error
(E)None of these


Explanation:

Variable name can be in the format: __NAME__. But it is bad practice since this format is used for global identifiers.


10.

What will be output of the following c program?

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

(A)5
(B)10
(C)0
(D)Compilation error
(E)None of these


Explanation:

Two or more global variables can have same name but we can initialize only one of them.


11.

What will be output of the following c program?

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

(A)
0
(B)5
(C)25
(D)Compilation error
(E)None of these


Explanation:

Two or more global variables can have same name but we can initialize only one of them.


12.

What will be output of the following c program?

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

(A)0
(B)1
(C)25
(D)Compilation error
(E)None of these


Explanation:

Two or more global variables can have same name but we can initialize only one of them.

13.

What will be output of the following c program?

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

(A)10
(B)20
(C)30
(D)Compilation error
(E)None of these


Explanation:

Two variables can have same name in different scope.

14.

What will be output of the following c program?

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

(A)20
(B)
0
(C)Garbage
(D)Compilation error
(E)None of these


Explanation:

Two local variables cannot have same name in same scope.

15.

What will be output of the following c program?

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

(A)20
(B)
40
(C)
0
(D)Compilation error
(E)None of these


Explanation:

Two variables can have same name in different scope.

16.

What will be output of the following c program?

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

(A)80
(B)
0
(C)
Garbage value
(D)Compilation error
(E)None of these


Explanation:

Variable name can be main.

17.

What will be output of the following c program?

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

(A)0
(B)
10
(C)Garbage value
(D)Compilation error
(E)None of these


Explanation:

Two variables can have same name in different scope.

18.

What will be output of the following c program?

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

(A)10
(B)0
(C)5
(D)Compilation error
(E)None of these


Explanation:

Variable name is case sensitive.

19.

What will be output of the following c program?

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

(A)12
(B)
0
(C)5
(D)Compilation error
(E)None of these


Explanation:

Variable name cannot be pre defined function of included header file.

20.

What will be output of the following c program?

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

(A)12
(B)
0
(C)5
(D)Compilation error
(E)None of these


Explanation:

Variable name cannot be pre defined function of included header file.


C Tutorial

Variables in c
C questions

43 comments:

a said...

good tricky question bro!!1

Ashish Dhiman said...

good question !!!!!!!!!!!

Unknown said...

awesome question ......good work

Anonymous said...

Very good work... but question 8 has wrong answer.

Priyanka kumari said...

Answer of question 8 is correct. You may using different compiler. Check it in Turbo c 3.5 compiler.

Aman Agarwal said...

Question 10 & 11 are giving error redefinition of ‘num’
with gcc compiler :-/

Anonymous said...

Nice questions........

Unknown said...

gud que's like it

Anonymous said...

Can you pleaze explain question number 5.How does it give output 4?what is operator << doing?

Anonymous said...

nice questions .Good work

Anonymous said...

nice...

Anonymous said...

hiiiiii...this is SILKY BANKA ..i m nt getting ques no.7 and 8.

Anonymous said...

In question 5......

remember the value becomes half during every right shift(>>) and 150>>30-25
150>>5 times right shift

now ,150 becomes 75(1st time)
75 becomes 37(2nd time)
37 becomes 18(3rd time)
18 become 9 (4th time)
9 become 4 (5th time)
BY ABHINAV SAXENA

Anonymous said...

Tricky Questions dude...nice explanation

Anonymous said...

can't understand qs no. 7 and 17.... plz explain....

Anonymous said...

didnt get question 7. how 899 ? what is effect of +~ ?

Alampally Dilipkumar said...

question 7....

~ (xyz_123)= -92

991 + (-92)=899

saurabh sinha said...

12. #include
static num;
int main(){
printf("%d",num);
return 0;
}
int num =25;

i don't think the answer t this ques. is 25 coz variables are usable only from it's point of declaration.......here for answer to be 25 we have to use extern

12. #include
extern int num;
int main(){
printf("%d",num);
return 0;
}
int num =25;

Unknown said...

Thank U very much for such interesting questions and answers.However, what is the difference b/n q.8 and q.9?

Anonymous said...

in question 5 class is a keyword how it is used as a variable name ?????????????

Anonymous said...

the answer given by the writer is correct, remember for the precedence of the operators, to help u,the first that yo must be operate is unary operators search what unary mean, then * / % then + - then shifting. now according to the given problem as substrahend has high precedence than shifting first evaluate the substraction and then shfting propert you will get the answer
bye GEBREHIWET ABRHAM
MEKELE INSTITUE OF TECHNOLGY
MEKELE, ETHIOPIA

Anonymous said...

it seems that incorrect number 6 and 8; i have complied using gcc compier but they have value and are corect but the given answer does not much> how this is ???
bye gebrehiwet abrham
mekele institue of technolgy
ethiopia

Unknown said...

nice and good questions

nishant said...

10th ques's answer is wrong

Anonymous said...

can somebody please explain 8th question,how is the lessthan(<) operator working here and what will be the value of y?

Anonymous said...

#include
int main(){
register xyz_123=91;
auto pqr_123=991;
const _1a1_=pqr_123+~xyz_123;
printf("%d",_1a1_);
return 0;


In this question 7, the variable _1a1_ we have two underscores.. so it is an invalid declaration

Anonymous said...

CAN ANY ONE EXPLAIN ME Q.5 PLZZZZZZZZZZZZZZZZZZZZ XPLAIN ME IN DETAIL

devi said...

very interesting..

kalyan kumar said...

to understand this problem, first you should know about the BITWISE SHIFT OPERATORS.
there are two types of bitwise shift operators that is <<(left shift operator), >>(right shift operator).
the shift operators are used to move bit patterns either to left or right.
so here we have class=150 and private-public=5.
that is 150 >> 5. now we have to convert 150 to bits. i.e. 0000 0000 1001 0110 >> 5. now shift(remove) rightmost 5 bits of operand(value) and add 5 zeros at left side. now it will 0000 0000 0000 0100. that is 4.
similarly, for <<(left shift operator) shift leftmost 'n' bits and add 'n' zeros at right side.
example : x=4
x << 2 => 0000 0000 0000 0100 << 2 now it will be 0000 0000 0001 0000 .
x >> 2 => 0000 0000 0000 0100 >> 2 now it will be 0000 0000 0000 0001 .

kalyan kumar said...

"class" is a keyword in c++ not in c language,, so we can use it as a variable name.

kalyan kumar said...

to understand this problem, first you should know about the BITWISE SHIFT OPERATORS.
there are two types of bitwise shift operators that is <<(left shift operator), >>(right shift operator).
the shift operators are used to move bit patterns either to left or right.
so here we have class=150 and private-public=5.
that is 150 >> 5. now we have to convert 150 to bits. i.e. 0000 0000 1001 0110 >> 5. now shift(remove) rightmost 5 bits of operand(value) and add 5 zeros at left side. now it will 0000 0000 0000 0100. that is 4.
similarly, for <<(left shift operator) shift leftmost 'n' bits and add 'n' zeros at right side.
example : x=4
x << 2 => 0000 0000 0000 0100 << 2 now it will be 0000 0000 0001 0000 .
x >> 2 => 0000 0000 0000 0100 >> 2 now it will be 0000 0000 0000 0001 .

kalyan kumar said...

less than(<) operator is a relational operator, it will return either 0 or 1.
here y= _SMALL_ < 5 i.e. y = 11 < 5 ; , here 11<5 is falls so it will return 0, that is y=0.
compiled on gcc compiler.

kalyan kumar said...

sorry,, it is (11<5) false not falls , spelling mistake.

kalyan kumar said...

Q 7.
Remember that digits are stored in two's complement.
here 991+~91
in bits 91=>0101 1011,, ~91(one's compliment of 91)=>1010 0100. first bit is 1,so it will be negative,
now look at -92 i.e. twos complement of 92 =>010 0100 ,,so we can say here ~91 is equals to -92.

91 =>0101 1011 ~91=>1010 0100 (first bit is 1,so it will be negative)
ones complement of 0100100 =>1011011 + 1 =>1011100 i.e. 92. with sign bit it will be -92.
now answer is 991-92=899.
Ex : x=4 then ~x is -(x+1). that is -(4+1)=-5.

Q 17. to understand this problem first you should study clear about STRUCTURES concept..we can declare different data type as single using structure. and 'struct' is a key word to declare structures.
ex : struct book
{
char author;
int pages;
float price;
};
in problem no 17, b is a variable type of 'struct a' , and we can access structure members(int a) using .(dot) operator
so b.a will be 10

mrigendra said...

output is incorrect in those questions because multiple definitions is not possible
here static num=5 ; is a definition
int num;// is also a definition
extern int num; is a declaration

Anonymous said...

good

Unknown said...

class = class >> private - public; explation

Unknown said...

TELL ME WHAT PROBLEM IN THIS CODE...?(MILIND MEHKARKAR)
#include
int main()
{
int p,c,m;
printf("Enter the marks of three subjects:");
scanf("%d %d %d",&p,&c,&m);
if((p>=50 && c>=55 && m>=60)&&(p+c+m>=220||p+m>=130))
{
printf("Admission is given");
}
else
{
printf("Admission is not given");
}
return 0;
}



Unknown said...

THIS PRG RIGHT OR NOT GIVE ME UR VALUABLE SUGGESTION...?'

#include
#include
void main()
{
int p,c,m,n,i;
printf("Enter the marks of p,c,m");
scanf("%d %d %d",&p,&c,&m);
if(p>=50)
{
if(c>=55)
{
if(m>=60)
{
if(p+c+m>=220)
{
n=p+c+m;
printf("Admission is given %d",n);
}
else if(m+p>=130)
{
i=m+p;
printf("Admission is given %d",i);
}
else
{
printf("Admission is not given");
}
}
else
{
printf("Admission is not given");
}
}
else
{
printf("Admission is not given");
}
}
else
{
printf("Admission is not given");
}
getch();
}



Unknown said...

thanks

Unknown said...

que 8 ans will be 0

Unknown said...

please clear that that question answer
int a=10;
printf("%d\n",~a);
printf("%d\n",!a);
what is answer this question explain its.

Unknown said...

can a macro be used as a variable name
#include
#define ONE TWO
#define TWO ONE
main()
{
int TWO=1;
int ONE=2;
printf("%d %d", ONE, TWO);
}