C Programming Questions and Answers – Declarations – 1

«
»
This section on C interview questions and answers focuses on “Declarations”. One shall practice these interview questions to improve their C programming skills needed for various interviews (campus interviews, walk-in 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 the detailed explanation of the answers which helps in better understanding of C concepts.

Here is a listing of C interview questions on “Declarations” along with answers, explanations and/or solutions:

1. What will be the output of the following C code?

advertisement
  1.     #include <stdio.h>
  2.     void foo(const int *);
  3.     int main()
  4.     {
  5.         const int i = 10;
  6.         printf("%d ", i);
  7.         foo(&i);
  8.         printf("%d", i);
  9.  
  10.     }
  11.     void foo(const int *i)
  12.     {
  13.         *i = 20;
  14.     }

a) Compile time error
b) 10 20
c) Undefined value
d) 10
View Answer

Answer: a
Explanation: Cannot change a const type value.
Output:
$ cc pgm1.c
pgm1.c: In function ‘foo’:
pgm1.c:13: error: assignment of read-only location ‘*i’
advertisement
advertisement

2. What will be the output of the following C code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         const int i = 10;
  5.         int *ptr = &i;
  6.         *ptr = 20;
  7.         printf("%d\n", i);
  8.         return 0;
  9.     }

a) Compile time error
b) Compile time warning and printf displays 20
c) Undefined behaviour
d) 10
View Answer

Answer: b
Explanation: Changing const variable through non-constant pointers invokes compiler warning.
Output:
$ cc pgm2.c
pgm2.c: In function ‘main’:
pgm2.c:5: warning: initialization discards qualifiers from pointer target type
$ a.out
20
advertisement

3. What will be the output of the following C code?

advertisement
  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         j = 10;
  5.         printf("%d\n", j++);
  6.         return 0;
  7.     }

a) 10
b) 11
c) Compile time error
d) 0
View Answer

Answer: c
Explanation: Variable j is not defined.
Output:
$ cc pgm3.c
pgm3.c: In function ‘main’:
pgm3.c:4: error: ‘j’ undeclared (first use in this function)
pgm3.c:4: error: (Each undeclared identifier is reported only once
pgm3.c:4: error: for each function it appears in.)
advertisement

4. Will the following C code compile without any error?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         for (int k = 0; k < 10; k++);
  5.             return 0;
  6.     }

a) Yes
b) No
c) Depends on the C standard implemented by compilers
d) Error
View Answer

Answer: c
Explanation: Compilers implementing C90 do not allow this, but compilers implementing C99 allow it.
Output:
$ cc pgm4.c
pgm4.c: In function ‘main’:
pgm4.c:4: error: ‘for’ loop initial declarations are only allowed in C99 mode
pgm4.c:4: note: use option -std=c99 or -std=gnu99 to compile your code

5. Will the following C code compile without any error?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int k;
  5.         {
  6.             int k;
  7.             for (k = 0; k < 10; k++);
  8.         }
  9.     }

a) Yes
b) No
c) Depends on the compiler
d) Depends on the C standard implemented by compilers
View Answer

Answer: a
Explanation: There can be blocks inside the block. But within a block, variables have only block scope.
Output:
$ cc pgm5.c

6. Which of the following declaration is not supported by C?
a) String str;
b) char *str;
c) float str = 3e2;
d) Both String str; & float str = 3e2;
View Answer

Answer: a
Explanation: It is legal in Java, but not in C.

7. Which of the following format identifier can never be used for the variable var?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         char *var = "Advanced Training in C by Sanfoundry.com";
  5.     }

a) %f
b) %d
c) %c
d) %s
View Answer

Answer: a
Explanation: %c can be used to print the indexed position.
%d can still be used to display its ASCII value.
%s is recommended.
%f cannot be used for the variable var.

Sanfoundry Global Education & Learning Series – C Programming Language.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

advertisement
advertisement

Leave a Comment

Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter