C Programming Questions and Answers
C Programming Questions and Answers
This section on C interview questions and answers focuses on “Variable Names”. One shall practice
these interview questions to improve their C programming skills needed for various interviews
(campus interviews, walkin interviews, company interviews), placements, entrance exams and other
competitive exams. These questions can be attempted by anyone focusing on learning C
Programming language. They can be a beginner, fresher, engineering graduate or an experienced
IT professional. Our C Interview questions come with detailed explanation of the answers which
helps in better understanding of C concepts.
Here is a listing of C interview questions on “Variable Names” along with answers,
explanations and/or solutions:
B. James A. Gosling
C. Dennis Ritchie
Answer –c
Anser A
C programs are converted into machine language with the help of
A. An Editor
B. A compiler
C. An operating system
D. None of these.
Answer B
7. Variable name resolving (number of significant characters for uniqueness of variable) depends on
a) Compiler and linker implementations
b) Assemblers and loaders implementations
c) C language
d) None
View Answer-a
8. Which of the following is not a valid C variable name?
a) int number;
b) float rate;
c) int variable_count;
d) int $main;
View Answer-d
9. Which of the following is true for variable names in C?
a) They can contain alphanumeric characters as well as special characters
b) It is not an error to declare a variable to be one of the keywords(like goto, static)
c) Variable names cannot start with a digit
d) Variable can be of any length
View Answer
Answer:c
a) Hello World! x;
b) Hello World! followed by a junk value
c) Compile time error
d) Hello World!
View Answer
Answer:c
Explanation:It results in an error since x is used without declaring the variable x.
Output:
$ cc pgm1.c
pgm1.c: In function ‘main’:
pgm1.c:4: error: ‘x’ undeclared (first use in this function)
pgm1.c:4: error: (Each undeclared identifier is reported only once
pgm1.c:4: error: for each function it appears in.)
3. What is the output of this C code?
1. #include <stdio.h>
2. int main()
3. {
4. int y = 10000;
5. int y = 34;
6. printf("Hello World! %d\n", y);
7. return 0;
8. }
1. #include <stdio.h>
2. int main()
3. {
4. int main = 3;
5. printf("%d", main);
6. return 0;
7. }
1. #include <stdio.h>
2. int main()
3. {
4. int ThisIsVariableName = 12;
5. int ThisIsVariablename = 14;
6. printf("%d", ThisIsVariablename);
7. return 0;
8. }
1. #include <stdio.h>
2. int main()
3. {
4. printf("Hello World! %d \n", x);
5. return 0;
6. }
a) Hello World! x;
b) Hello World! followed by a junk value
c) Compile time error
d) Hello World!
View Answer
Answer:c
Explanation:It results in an error since x is used without declaring the variable x.
Output:
$ cc pgm1.c
pgm1.c: In function ‘main’:
pgm1.c:4: error: ‘x’ undeclared (first use in this function)
pgm1.c:4: error: (Each undeclared identifier is reported only once
pgm1.c:4: error: for each function it appears in.)
3. What is the output of this C code?
1. #include <stdio.h>
2. int main()
3. {
4. int y = 10000;
5. int y = 34;
6. printf("Hello World! %d\n", y);
7. return 0;
8. }
1. #include <stdio.h>
2. int main()
3. {
4. int main = 3;
5. printf("%d", main);
6. return 0;
7. }
1. #include <stdio.h>
2. int main()
3. {
4. int ThisIsVariableName = 12;
5. int ThisIsVariablename = 14;
6. printf("%d", ThisIsVariablename);
7. return 0;
8. }
1. #include <stdio.h>
2. int main()
3. {
4. float f1 = 0.1;
5. if (f1 == 0.1)
6. printf("equal\n");
7. else
8. printf("not equal\n");
9. }
a) equal
b) not equal
c) Output depends on compiler
d) None of the mentioned
View Answer
Answer:b
Explanation:0.1 by default is of type double which has different representation than float resulting in
inequality even after conversion.
Output:
$ cc pgm4.c
$ a.out
not equal
2. Comment on the output of this C code?
1. #include <stdio.h>
2. int main()
3. {
4. float f1 = 0.1;
5. if (f1 == 0.1f)
6. printf("equal\n");
7. else
8. printf("not equal\n");
9. }
a) equal
b) not equal
c) Output depends on compiler
d) None of the mentioned
View Answer
Answer:a
Explanation:0.1f results in 0.1 to be stored in floating point representations.
Output:
$ cc pgm5.c
$ a.out
equal
3. What is the output of this C code (on a 32-bit machine)?
1. #include <stdio.h>
2. int main()
3. {
4. int x = 10000;
5. double y = 56;
6. int *p = &x;
7. double *q = &y;
8. printf("p and q are %d and %d", sizeof(p), sizeof(q));
9. return 0;
10. }
1. #include <stdio.h>
2. union Sti
3. {
4. int nu;
5. char m;
6. };
7. int main()
8. {
9. union Sti s;
10. printf("%d", sizeof(s));
11. return 0;
12. }
a) 8
b) 5
c) 9
d) 4
View Answer
Answer:d
Explanation:Since the size of a union is the size of its maximum datatype, here int is the largest
hence 4.
Output:
$ cc pgm7.c
$ a.out
4
6. What is the output of this C code?
1. #include <stdio.h>
2. int main()
3. {
4. float x = 'a';
5. printf("%f", x);
6. return 0;
7. }
a) a
b) run time error
c) a.0000000
d) 97.000000
View Answer
Answer:d
Explanation:Since the ASCII value of a is 97, the same is assigned to the float variable and printed.
Output:
$ cc pgm8.c
$ a.out
97.000000
7. Which of the datatypes have size that is variable?
a) int
b) struct
c) float
d) double
View Answer
Answer:b
1. #include <stdio.h>
2. int main()
3. {
4. printf("C programming %s", "Class by\n%s Sanfoundry", "WOW");
5. }
a) C programming Class by
WOW Sanfoundry
b) C programming Class by\n%s Sanfoundry
c) C programming Class by
%s Sanfoundry
d) Compilation error
View Answer
Answer:c
Explanation:This program has only one %s within first double quotes, so it does not read the string
“WOW”.
The %s along with the Sanfoundry is not read as a format modifier while new line character prints
the new line.
Output:
$ cc pgm2.c
$ a.out
C programming Class by
%s Sanfoundry
3. For the following code snippet:
char *str = “Sanfoundry.com\0” “training classes”;
The character pointer str holds reference to string:
a) Sanfoundry.com
b) Sanfoundry.com\0training classes
c) Sanfoundry.comtraining classes
d) Invalid declaration
View Answer
Answer:b
Explanation:’\0′ is accepted as a char in the string. Even though strlen will give length of string
“Sanfoundry.com”, in memory str is pointing to entire string including training classes”
4. What is the output of this C code?
1. #include <stdio.h>
2. #define a 10
3. int main()
4. {
5. const int a = 5;
6. printf("a = %d\n", a);
7. }
a) a = 5
b) a = 10
c) Compilation error
d) Runtime error
View Answer
Answer:c
Explanation:The #define substitutes a with 10 leaving no identifier and hence compilation error.
Output:
$ cc pgm3.c
pgm3.c: In function ‘main’:
pgm3.c:5: error: expected identifier or ‘(’ before numeric constant
5. What is the output of this C code?
1. #include <stdio.h>
2. int main()
3. {
4. int var = 010;
5. printf("%d", var);
6. }
a) 2
b) 8
c) 9
d) 10
View Answer
Answer:b
Explanation:010 is octal representation of 8.
Output:
$ cc pgm4.c
$ a.out
8
6. What is the output of this C code?
1. #include <stdio.h>
2. enum birds {SPARROW, PEACOCK, PARROT};
3. enum animals {TIGER = 8, LION, RABBIT, ZEBRA};
4. int main()
5. {
6. enum birds m = TIGER;
7. int k;
8. k = m;
9. printf("%d\n", k);
10. return 0;
11. }
a) 0
b) Compile time error
c) 1
d) 8
View Answer
Answer:d
Explanation:m is an integer constant, hence compatible.
Output:
$ cc pgm5.c
$ a.out
8
7. What is the output of this C code?
1. #include <stdio.h>
2. #define MAX 2
3. enum bird {SPARROW = MAX + 1, PARROT = SPARROW + MAX};
4. int main()
5. {
6. enum bird b = PARROT;
7. printf("%d\n", b);
8. return 0;
9. }
a) Compilation error
b) 5
c) Undefined value
d) 2
View Answer
Answer:b
Explanation:MAX value is 2 and hence PARROT will have value 3 + 2.
Output:
$ cc pgm6.c
$ a.out
5
8. What is the output of this C code?
1. #include <stdio.h>
2. #include <string.h>
3. int main()
4. {
5. char *str = "x";
6. char c = 'x';
7. char ary[1];
8. ary[0] = c;
9. printf("%d %d", strlen(str), strlen(ary));
10. return 0;
11. }
a) 1 1
b) 2 1
c) 2 2
d) 1 (undefined value)
View Answer
Answer:d
Explanation:str is null terminated but ary is not.
Output:
$ cc pgm7.c
$ a.out
15