[go: up one dir, main page]

0% found this document useful (0 votes)
2 views3 pages

Question and Answers

The document provides a comprehensive list of C programming interview questions and answers, covering topics such as program structure, data types, variable declaration rules, operators, control statements, and loops. It explains the roles of key components like #include and main(), the differences between data types, and the use of control structures like if-else and switch. Additionally, it addresses common programming concepts such as variable scope, initialization, and loop behavior.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Question and Answers

The document provides a comprehensive list of C programming interview questions and answers, covering topics such as program structure, data types, variable declaration rules, operators, control statements, and loops. It explains the roles of key components like #include and main(), the differences between data types, and the use of control structures like if-else and switch. Additionally, it addresses common programming concepts such as variable scope, initialization, and loop behavior.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

C Programming Interview Questions

and Answers (SHANMUK ABHIRAM)


1. What are the main sections of a C program?
The main sections of a C program are:
1. Documentation Section
2. Preprocessor Section
3. Definition Section
4. Global Declaration Section
5. main() Function Section
6. Subprogram Section (user-defined functions)

2. What is the role of #include and main() in a C program?


#include is used to include header files in a C program.
main() is the entry point of every C program where the execution starts.

3. What is the difference between int, float, char, and double?


int: Stores integer values (e.g., 5)
float: Stores single precision floating point numbers (e.g., 3.14)
char: Stores single characters (e.g., 'a')
double: Stores double precision floating point numbers (e.g., 3.1415926535)

4. What are the rules for declaring variables in C?


Variables must start with a letter or underscore, not a number. They can include letters,
digits, and underscores, and must not be a reserved keyword.

5. What is the difference between = and ==?


= is the assignment operator, used to assign values.
== is the comparison operator, used to compare two values.

6. What are the types of operators in C?


1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Bitwise Operators
6. Conditional Operators
7. Increment/Decrement Operators
7. Why do we use #include <stdio.h>?
It includes the standard input/output header file, which is necessary for functions like
printf() and scanf().

8. What is a header file?


A header file contains definitions of functions and macros. It allows code reuse and better
organization.

9. What is the role of return 0; in the main() function?


It indicates that the program has executed successfully.

10. What happens if main() is written without a return type?


In modern C (C99 and above), it will result in a compilation error. Earlier, it was assumed to
return int implicitly.

11. What are the different data types in C? Explain with memory size.
int (2 or 4 bytes), float (4 bytes), double (8 bytes), char (1 byte), short (2 bytes), long (4 or 8
bytes depending on system).

12. What is the difference between float and double?


float has 4 bytes of precision, double has 8 bytes and higher precision.

13. Can variable names start with a number? Why or why not?
No, variable names cannot start with a number because it's against the syntax rules of C.

14. What is the difference between a local variable and a global variable?
Local variables are declared inside functions and accessible only within them. Global
variables are declared outside all functions and accessible throughout the program.

15. What is the default value of an uninitialized variable?


It is undefined (contains garbage value) if declared locally. Global/static variables are
initialized to 0.

16. When should we use if-else and when switch?


Use if-else for range or complex conditions. Use switch for specific, known values.

17. Can we use switch for float values? Why or why not?
No, switch only works with integer or character types. Float comparisons are not precise.

18. What happens if we don’t use break in a switch statement?


It causes fall-through; the program continues to execute the next case(s) until a break is
found.
19. What is the significance of default in a switch?
It is executed when no case matches. It's optional but recommended.

20. Can if statements be nested? Give an example.


Yes. Example:
if (a > 0) {
if (a < 10) {
printf("a is between 1 and 9");
}
}

21. What is the difference between for, while, and do-while loops?
for: Loop with initialization, condition, and increment in one line.
while: Checks condition before execution.
do-while: Executes once before checking the condition.

22. Which loop is guaranteed to execute at least once?


do-while loop.

23. Can we use break and continue inside loops? What do they do?
Yes. break exits the loop. continue skips the current iteration and moves to the next.

24. What is an infinite loop? How can you write one using each loop?
A loop that never ends.
for(;;) {}
while(1) {}
do {} while(1);

25. Can loops be nested in C? Give an example.


Yes. Example:
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d %d\n", i, j);
}
}

You might also like