1. What is C language?
C is a general-purpose programming language that was developed in the early 1970s. It is widely used for developing system software, application software, and embedded systems.
2. What are the key features of C language?
The key features of C language include simplicity, portability, efficiency, modularity, and a rich set of library functions.
3. What is the difference between C and C++?
C is a procedural programming language, while C++ is an extension of C that supports both procedural and object-oriented programming paradigms.
4. What are the basic data types in C?
The basic data types in C include int, char, float, and double.
5. How do you declare a variable in C?
A variable can be declared in C by specifying its data type followed by its name. For example, int age;
6. What is the difference between declaration and definition of a variable?
Declaration refers to specifying the type and name of a variable, while definition includes allocating memory for the variable.
7. How do you initialize a variable in C?
A variable can be initialized at the time of declaration by assigning a value to it. For example, int age = 25;
8. What is a constant in C?
A constant is a value that cannot be changed during the execution of a program. It can be of different types, such as integer constants, character constants, and floating-point constants.
9. What are the different types of operators in C?
The different types of operators in C include arithmetic operators, relational operators, logical operators, assignment operators, and bitwise operators.
10. What is the syntax for a for loop in C?
The syntax for a for loop in C is as follows:
for (initialization; condition; increment/decrement) {
// code to be executed
}
11. What is the difference between while and do-while loop?
In a while loop, the condition is checked before the execution of the loop, while in a do-while loop, the condition is checked after the execution of the loop.
12. What is a function in C?
A function is a block of code that performs a specific task. It can be called from anywhere in the program to execute the code inside it.
13. How do you define a function in C?
A function can be defined in C by specifying its return type, name, and parameters (if any), followed by the code inside curly braces.
14. What is recursion in C?
Recursion is the process of a function calling itself. It is a powerful technique used in solving problems that can be divided into smaller subproblems.
15. What is a pointer in C?
A pointer is a variable that stores the memory address of another variable. It allows direct manipulation of memory and enables efficient memory management.
16. How do you declare a pointer in C?
A pointer can be declared in C by using the asterisk (*) symbol before the variable name. For example, int *ptr;
17. What is the difference between malloc() and calloc()?
malloc() is used to allocate a block of memory of a specified size, while calloc() is used to allocate a block of memory and initialize it to zero.
18. What is the difference between arrays and pointers?
Arrays are a collection of elements of the same data type, while pointers are variables that store memory addresses. However, arrays and pointers are closely related in C.
19. What is the difference between pass by value and pass by reference?
In pass by value, a copy of the value is passed to a function, while in pass by reference, the memory address of the value is passed. Changes made to the value in pass by reference are reflected outside the function.
20. What is the purpose of the “const” keyword in C?
The “const” keyword is used to declare constants in C. It ensures that the value of a variable cannot be modified once it is assigned.
21. What is the difference between local and global variables?
Local variables are declared inside a function and are only accessible within that function, while global variables are declared outside any function and can be accessed by any function in the program.
22. What is the use of the “static” keyword in C?
The “static” keyword is used to modify the behavior of variables and functions. A static variable retains its value between function calls, while a static function can only be called within the file it is defined in.
23. What is the difference between structure and union?
A structure is a user-defined data type that allows combining different types of variables into a single entity, while a union is a special data type that allows storing different types of variables in the same memory location.
24. What is the purpose of the “enum” keyword in C?
The “enum” keyword is used to define an enumeration, which is a user-defined data type consisting of a set of named constants.
25. What is the difference between break and continue statements?
The “break” statement is used to terminate the execution of a loop or switch statement, while the “continue” statement is used to skip the current iteration of a loop and continue with the next iteration.
26. What is file handling in C?
File handling in C involves reading from and writing to files. It allows data to be stored and retrieved for later use.
27. How do you open a file in C?
A file can be opened in C using the fopen() function, which takes the file name and mode as parameters. For example, FILE *fp = fopen(“filename.txt”, “r”);
28. What is the difference between “r” and “w” mode in file handling?
The “r” mode is used for reading from a file, while the “w” mode is used for writing to a file. If the file does not exist, the “w” mode creates a new file.
29. How do you read a line from a file in C?
A line can be read from a file in C using the fgets() function, which takes the file pointer, buffer, and maximum number of characters to read as parameters.
30. What is the purpose of the “feof()” function?
The feof() function is used to check if the end of a file has been reached. It returns a non-zero value if the end of the file has been reached, and zero otherwise.
31. What is the difference between structure and typedef in C?
A structure is a user-defined data type that allows combining different types of variables into a single entity, while typedef is used to create aliases for existing data types.
32. What is the purpose of the “sizeof” operator in C?
The sizeof operator is used to determine the size of a variable or data type in bytes. It returns the size as an unsigned integer.
33. What is the difference between pre-increment and post-increment operators?
The pre-increment operator (++x) increments the value of a variable before its use in an expression, while the post-increment operator (x++) increments the value of a variable after its use in an expression.
34. What is the purpose of the “const” keyword in function parameters?
The “const” keyword in function parameters is used to specify that the value of the parameter cannot be modified within the function.
35. What is the difference between a macro and a function in C?
A macro is a preprocessor directive that is replaced by its definition before the compilation of the program, while a function is a block of code that is executed at runtime.
36. What is the purpose of the “sizeof” operator in function parameters?
The “sizeof” operator in function parameters is used to determine the size of an array passed as a parameter. It allows the function to iterate over the elements of the array.
37. What is the purpose of the “static” keyword in function parameters?
The “static” keyword in function parameters is used to specify that the value of the parameter is retained between function calls. It acts as a local variable that is initialized only once.
38. What is the use of the “volatile” keyword in C?
The “volatile” keyword is used to indicate that a variable can be modified by external factors, such as hardware or other threads. It prevents the compiler from applying certain optimizations.
39. What is the purpose of the “register” keyword in C?
The “register” keyword is used to suggest to the compiler that a variable should be stored in a register for faster access. However, the compiler is not obligated to honor this suggestion.
40. What is the difference between a structure and an array of structures?
A structure is a user-defined data type that allows combining different types of variables into a single entity, while an array of structures is a collection of multiple instances of the same structure type.
41. What is the purpose of the “break” statement in a switch statement?
The “break” statement is used to terminate the execution of a switch statement. It prevents the execution of subsequent case statements and the default case.
42. What is the purpose of the “continue” statement in a loop?
The “continue” statement is used to skip the rest of the code in the current iteration of a loop and continue with the next iteration.
43. What is the difference between a local and global variable with the same name?
A local variable with the same name as a global variable takes precedence within the scope of the function where it is declared. It shadows the global variable.
44. What is the purpose of the “goto” statement in C?
The “goto” statement is used to transfer control to a labeled statement within the same function. It allows for non-linear control flow.
45. What is the purpose of the “do-while” loop in C?
The “do-while” loop is used to execute a block of code repeatedly until a specified condition is met. It ensures that the code is executed at least once, even if the condition is initially false.
46. What is the difference between a structure and a class in C?
In C, there is no built-in support for classes. Structures in C can only contain variables, while classes in languages like C++ can also contain member functions.
47. What is the purpose of the “exit” function in C?
The “exit” function is used to terminate the execution of a program. It can be called with an integer argument to indicate the exit status of the program.
48. What is the difference between a library function and a user-defined function in C?
A library function is a function that is provided by the C standard library or other libraries, while a user-defined function is a function that is created by the programmer.
49. What is the purpose of the “assert” function in C?
The “assert” function is used to check if a given condition is true. If the condition is false, the program terminates and an error message is displayed.
50. What is the purpose of the “NULL” keyword in C?
The “NULL” keyword is used to represent a null pointer, which is a pointer that does not point to any memory address. It is commonly used to initialize pointers or to check if a pointer is valid.