0% found this document useful (0 votes)
2 views

C_Language_Interview_Questions_Answers (1)

The document provides a comprehensive overview of the C programming language, covering its history, basic concepts, data types, operators, control structures, functions, memory management, and file handling. It includes definitions, examples, and explanations of key terms and functionalities in C. The content serves as a reference for both beginners and experienced programmers looking to understand or refresh their knowledge of C.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

C_Language_Interview_Questions_Answers (1)

The document provides a comprehensive overview of the C programming language, covering its history, basic concepts, data types, operators, control structures, functions, memory management, and file handling. It includes definitions, examples, and explanations of key terms and functionalities in C. The content serves as a reference for both beginners and experienced programmers looking to understand or refresh their knowledge of C.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

1. Q: What is C language?

A: C is a general-purpose, procedural programming language developed in the


early 1970s by Dennis Ritchie at Bell Labs. It is known for its efficiency and
control over system resources.

2. Q: Who developed C and when?


A: Dennis Ritchie developed C in 1972 at Bell Labs.

3. Q: What are keywords in C?


A: Keywords are reserved words in C that have special meaning to the compiler,
like int, return, if, while, etc.

4. Q: What is a variable?
A: A variable is a named location in memory used to store data. Its value can be
changed during program execution.

5. Q: What is the difference between a variable and a constant?


A: A variable can change value during execution, whereas a constant remains
fixed once defined.

6. Q: Explain the basic structure of a C program.


A: A C program typically includes headers (`#include`), the main function
(`main()`), declarations, and statements.

7. Q: What is the purpose of `main()`?


A: `main()` is the entry point of any C program where execution starts.

8. Q: What are the data types in C?


A: Primary data types include int, float, char, and double. Derived types
include arrays, pointers, and structures.

9. Q: What are format specifiers? Give examples.


A: Format specifiers define the type of data to be input/output. Example: %d for
int, %f for float, %c for char, %s for string.

10. Q: What is the use of `printf()` and `scanf()`?


A: `printf()` outputs data to the screen; `scanf()` reads input from the user.

11. Q: What are the different types of operators in C?


A: Arithmetic, relational, logical, assignment, bitwise, increment/decrement,
and conditional (ternary).

12. Q: What is the difference between `=` and `==`?


A: `=` is the assignment operator; `==` is the equality comparison operator.

13. Q: Explain arithmetic operators with examples.


A: Arithmetic operators include +, -, *, /, %. Example: 5 + 2 = 7.

14. Q: What is the modulus operator?


A: It gives the remainder of a division operation. Example: 5 % 2 = 1.

15. Q: What is a ternary operator? Give an example.


A: It’s a shorthand for `if-else`. Syntax: `condition ? expr1 : expr2`. Example:
`a > b ? a : b`.

16. Q: What is precedence and associativity?


A: Precedence determines the order of evaluation; associativity determines the
direction (left-right or right-left).
17. Q: What is the output of `5 + 2 * 10`?
A: It’s 25 because `*` has higher precedence. So, 2 * 10 = 20; 5 + 20 = 25.

18. Q: What is the use of increment/decrement operators?


A: They increase or decrease a variable’s value by one. (++ and --)

19. Q: Differentiate between `++i` and `i++`.


A: `++i` increments first then uses the value; `i++` uses the value then
increments.

20. Q: Can we use multiple increment operators in a single expression?


A: Yes, but it may lead to undefined behavior. Best to avoid.

21. Q: What is an `if` statement?


A: It allows conditional execution of code blocks based on a condition.

22. Q: What is an `else if` ladder?


A: It allows checking multiple conditions one after the other.

23. Q: What is a `switch` statement?


A: It allows selection among multiple options using case labels.

24. Q: How is `switch` different from `if`?


A: `switch` is used when comparing a variable against constant values; `if` is
more flexible with conditions.

25. Q: What is a loop? Types of loops?


A: Loops repeat a block of code. Types: for, while, do-while.

26. Q: Difference between `while` and `do while`.


A: `while` checks condition before execution; `do while` checks after.

27. Q: What is a `for` loop?


A: It is a compact loop with initialization, condition, and increment/decrement
in one line.

28. Q: Can loops be nested?


A: Yes, loops can be placed inside other loops.

29. Q: What is `break` statement?


A: It exits the nearest loop or switch.

30. Q: What is `continue` statement?


A: It skips the current iteration and jumps to the next iteration of the loop.

31. Q: What is a function?


A: A block of reusable code that performs a specific task.

32. Q: How to define and call a function in C?


A: Define using return_type function_name(); Call it using function_name();

33. Q: What is `return` in a function?


A: It returns a value from the function to the caller.

34. Q: What is the scope of a variable?


A: Scope is the region of code where the variable is accessible.

35. Q: What is recursion?


A: A function calling itself is called recursion.
36. Q: Write a recursive function for factorial.
A: `int fact(int n){ if(n==0) return 1; else return n*fact(n-1); }`

37. Q: Difference between call by value and call by reference.


A: Call by value sends a copy; call by reference sends the actual address.

38. Q: What is a void function?


A: A function that returns nothing (uses `void` as return type).

39. Q: Can functions return more than one value?


A: Not directly, but we can use pointers or structures.

40. Q: What is function prototype?


A: It declares the function before its actual definition.

41. Q: What is an array?


A: A collection of elements of the same type stored in contiguous memory.

42. Q: How do you declare and initialize an array?


A: `int arr[5] = {1, 2, 3, 4, 5};`

43. Q: How to find the length of an array?


A: `sizeof(arr)/sizeof(arr[0])`

44. Q: What are multidimensional arrays?


A: Arrays with more than one dimension like 2D, 3D arrays.

45. Q: How to declare a 2D array?


A: `int arr[2][3];`

46. Q: What is a string in C?


A: A 1D array of characters ending with a null character (`\0`).

47. Q: How to read and print strings?


A: `scanf("%s", str); printf("%s", str);`

48. Q: Difference between `gets()` and `fgets()`?


A: `gets()` is unsafe and deprecated; `fgets()` is safe and limits input.

49. Q: What are common string functions in `<string.h>`?


A: `strlen`, `strcpy`, `strcat`, `strcmp`, `strrev`, etc.

50. Q: How to reverse a string in C?


A: Use a loop or `strrev()` (in some compilers) or write custom reverse logic.

51. Q: What is a pointer?


A: A variable that stores the address of another variable.

52. Q: How to declare and initialize a pointer?


A: `int *p; p = &x;`

53. Q: What is the use of `*` and `&` operators?


A: `*` dereferences a pointer; `&` gets the address of a variable.

54. Q: What is the difference between `*ptr` and `ptr`?


A: `ptr` is the address, `*ptr` is the value at that address.

55. Q: What is pointer arithmetic?


A: Using `+`, `-`, etc., with pointers to navigate through memory.

56. Q: What is a NULL pointer?


A: A pointer that points to nothing (`NULL`).

57. Q: What is a wild pointer?


A: An uninitialized pointer pointing to an unknown memory location.

58. Q: What is a dangling pointer?


A: A pointer pointing to a memory location that has been freed.

59. Q: What are double pointers?


A: Pointers to pointers. `int **pp;`

60. Q: How to pass pointers to functions?


A: By using function parameters as pointer types: `void fun(int *p);`

61. Q: What is a structure?


A: A structure is a user-defined data type in C that allows grouping variables
of different types under a single name.

62. Q: How to define and use a structure?


A: Use `struct` keyword. Example: `struct Person { char name[20]; int age; };`
Then declare: `struct Person p;`

63. Q: What is a union?


A: A union is similar to a structure but shares memory for all members, so only
one member can hold a value at a time.

64. Q: Difference between structure and union?


A: Structures allocate memory for all members separately, unions share the same
memory.

65. Q: What is a typedef?


A: `typedef` gives a new name to an existing data type. Example: `typedef
unsigned int UI;`

66. Q: What is an enumeration?


A: An enum is a user-defined type consisting of integral constants. Example:
`enum colors {RED, GREEN, BLUE};`

67. Q: What is dynamic memory allocation?


A: It allows allocating memory during runtime using functions like `malloc`,
`calloc`, `realloc`, and `free`.

68. Q: What is the use of malloc()?


A: `malloc` allocates a block of memory dynamically. Example: `int *p =
(int*)malloc(10 * sizeof(int));`

69. Q: What is calloc()?


A: `calloc` allocates memory and initializes all bytes to zero.

70. Q: Difference between malloc() and calloc()?


A: `malloc` does not initialize memory, `calloc` does.

71. Q: What is realloc()?


A: `realloc` is used to resize previously allocated memory.

72. Q: What is free()?


A: `free` releases dynamically allocated memory.

73. Q: What is a file in C?


A: A file is a storage unit used to store data permanently. C provides functions
to read/write files.

74. Q: How to open a file in C?


A: Using `fopen()` with mode: `r`, `w`, `a`, etc.

75. Q: What is the use of fopen(), fclose()?


A: `fopen()` opens a file; `fclose()` closes the file.

76. Q: How to read/write to a file?


A: Use `fprintf`, `fscanf`, `fgets`, `fputs`, `fread`, `fwrite`.

77. Q: What is the difference between text and binary files?


A: Text files store data as characters; binary files store data in binary
format.

78. Q: What is stderr, stdin, stdout?


A: They are standard I/O streams: input (keyboard), output (screen), and error.

79. Q: What is command line argument?


A: Arguments passed to `main()` via command line: `int main(int argc, char
*argv[])`

80. Q: How to handle errors in C?


A: Use `perror()`, `strerror()`, and check return values of functions.

81. Q: What is preprocessor?


A: Preprocessor handles directives before compilation like `#include`,
`#define`, `#ifdef`.

82. Q: What is the use of #define?


A: `#define` creates macro constants or functions.

83. Q: What is the difference between #include <file> and #include "file"?
A: `<>` is for system headers; `""` is for user-defined headers.

84. Q: What are macros?


A: Macros are preprocessor definitions that are replaced before compilation.

85. Q: What is the use of `const` keyword?


A: It makes a variable’s value unchangeable after initialization.

86. Q: What is a static variable?


A: It retains its value between function calls.

87. Q: What is an extern variable?


A: It is declared in one file and used in another.

88. Q: What is a register variable?


A: A request to store variable in CPU register for faster access.

89. Q: What is the difference between local and global variables?


A: Local variables exist inside functions; global variables exist outside and
are accessible throughout the program.

90. Q: What is the scope and lifetime of a variable?


A: Scope is where it's accessible; lifetime is how long it exists in memory.

91. Q: What is the output of `printf("%d", sizeof(char));`?


A: Typically outputs 1.

92. Q: Can we use `goto` in C?


A: Yes, though it is discouraged. It provides unconditional jump.

93. Q: What is a memory leak?


A: Memory that is allocated but not freed.

94. Q: What is segmentation fault?


A: An error when a program tries to access invalid memory.

95. Q: What are inline functions?


A: Not available in C, but macros can behave similarly.

96. Q: What is the difference between exit() and return?


A: `return` exits from a function, `exit()` terminates the program.

97. Q: What are volatile variables?


A: Variables that can be changed by external sources and should not be optimized
by the compiler.

98. Q: What is a null-terminated string?


A: A string that ends with `\0` character.

99. Q: What is the difference between `strcpy()` and `strncpy()`?


A: `strncpy()` limits the number of characters copied.

100. Q: How to swap two numbers using pointers?


A: By passing their addresses to a function and swapping using dereference.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy