Short Q/A Programming in C - Students Free Notes

What is dynamic memory allocation in C? Explain malloc(), calloc(), and free() functions with examples.

Dynamic memory allocation allows programs to allocate memory at runtime, enabling flexibility in managing memory. This is achieved through functions like malloc(), calloc(), and free(). malloc(): The malloc() function allocates a block of memory of a specified size and returns a pointer to it. The memory is not initialized. int *ptr = (int *)malloc(5 * … Read more

What are the different types of storage classes in C?

In C, storage classes define the scope, lifetime, and visibility of variables. The main types are: auto: Default for local variables, their scope is limited to the block in which they are declared. register: Used for variables that should be stored in a register rather than RAM for faster access. static: Preserves the value of … Read more

What is the purpose of header files in C language?

Header files in C contain declarations for functions, macros, constants, and variables that are used across multiple source files. They help in separating the interface of a program from its implementation. By including header files using the #include directive, programmers can ensure that the necessary declarations are available in each file that uses the functions … Read more

What are reserved words? Why should they not be used as variable names?

Reserved words (also called keywords) are predefined words in programming languages that have a special meaning and are used for specific programming constructs, such as control flow, data types, or operations. Examples include if, for, int, and return in C. These words are reserved by the language and cannot be used as variable names because … Read more

Which of the following are valid C variables? Give the reason if not a valid variable.

area: Valid, as it is an identifier and doesn’t contain any illegal characters or reserved words. 5x: Invalid, as variables cannot start with a number. Sum: Valid, as it follows all rules for variable names. net pay: Invalid, as spaces are not allowed in variable names. float: Invalid, as float is a reserved keyword in … Read more