What is C programming?
C is a high-level, general-purpose programming language that is particularly good for system programming and embedded systems due to its versatility, efficiency, and control over system resources.
Can you explain the data types in C?
C supports several data types, including:
- Basic data types (int, char, float, double)
- Derived data types (array, pointer, structure, union)
- Enumeration data type (enum)
- Void data type (void)
What is a pointer in C?
A pointer in C is a variable that stores the memory address of another variable. Pointers are used for dynamic memory allocation, arrays, and other data structures.
What is a structure in C?
A structure in C is a user-defined data type that allows grouping variables of different data types under a single name.
What is the difference between printf()
and sprintf()
?
printf()
is used to send formatted output to the standard output (the screen), while sprintf()
sends the formatted output to a string pointed to by a char pointer.
How do you dynamically allocate memory in C?
The malloc() function is used to dynamically allocate a single large block of memory with the specified size. The calloc() function is used to allocate an array of elements of a given size.
What is the use of a static variable in C?
A static variable retains its value between multiple function calls and is only initialized once. It has a local scope but a static lifetime.
What is a segmentation fault?
A segmentation fault is a specific kind of error caused by accessing memory that the CPU cannot physically address. It occurs when you try to read or write an illegal memory location.
What are macros?
Macros are a fragment of code that is given a name. Whenever the name is used, it is replaced by the contents of the macro.
What is a volatile keyword?
Macros are a fragment of code that is given a name. Whenever the name is used, it is replaced by the contents of the macro.
What is recursion in C?
Recursion in C is the technique of making a function call itself. This is used to solve problems that can be broken down into smaller, repetitive problems.
How does the free() function work?
The free() function deallocates the memory previously allocated by a call to malloc(), calloc(), or realloc(), making it available for future allocations.
What is a const keyword?
The const keyword is used to declare constants and prevent modification of the data that they hold.
Explain the difference between == and =.
== is the comparison operator that checks whether two values are equal. = is the assignment operator that assigns the value on the right to the variable on the left.
What is a linker error?
A linker error occurs when the linker cannot find a definition for a symbol or function in any of the linked object files or libraries, or when multiple definitions are found.
How do you declare a comment in C?
Comments in C are declared with /* comment here */ for multi-line comments and // comment here for single-line comments.
What is the purpose of a header file in C?
Header files in C contain declarations for functions and macros that are shared between several source files. It is used to import the interface of the encapsulated functionalities.
What is the difference between malloc() and calloc()?
Both malloc() and calloc() are used for dynamic memory allocation, but malloc() does not initialize the memory allocated, while calloc() initializes the allocated memory to zero.
What is typecasting in C?
Typecasting is a way to convert a variable from one data type to another. In C, it can be done explicitly using casting operators or implicitly by the compiler.
Can you explain what a file pointer is?
A file pointer is a pointer used to hold the reference of the file stream. It points to the data structure that represents a file to be read from or written to.
What is an array in C?
An array in C is a collection of variables of the same type that are accessed by a common name using an index number.
What is a union in C?
A union in C is a user-defined data type that allows different data types to be stored in the same memory location. It’s used to manage different data types using the same memory space.
Explain the difference between arrays and pointers.
Arrays and pointers are closely related in C. An array is a collection of elements of the same type, whereas a pointer is a variable that holds a memory address. Arrays are fixed in size, while pointers can be pointed to different memory locations dynamically.
What is a function prototype?
A function prototype is a declaration of a function that informs the compiler about the function name, return type, and parameters without giving the full function definition.
What is the scope of a global variable?
Global variables are accessible from any part of the program after their declaration. They are declared outside of all functions, typically at the top of the program.
What is the auto keyword in C?
The auto keyword declares automatic variables, which means they are automatically allocated and deallocated when the program enters and exits the variable’s scope. In modern C, the auto keyword is rarely used because all variables are automatic by default when declared within a block.
What is the extern keyword used for?
The extern keyword is used to declare a global variable or function in another file. It extends the visibility of the C variables and C functions.
What is a storage class in C?
In C, a storage class defines the scope (visibility) and lifetime of variables and/or functions within a C program. There are four storage classes in C: auto, register, static, and extern.
Explain the use of the register keyword.
The register keyword suggests to the compiler that the variable should be stored in a CPU register instead of in memory to optimize access speed. However, it’s only a suggestion, and the compiler can ignore it.
What are command-line arguments?
Command-line arguments are arguments specified after the program name in the command line when launching a C program. They are used to control programs from outside instead of hard coding those values inside the code.