Interview :: C
- In case of dynamic memory allocation, memory is allocated at runtime and memory can be increased while executing the program. It is used in the linked list.
- The malloc() or calloc() function is required to allocate the memory at the runtime.
- An allocation or deallocation of memory is done at the execution time of a program.
- No dynamic pointers are required to access the memory.
- The dynamic memory is implemented using data segments.
- Less memory space is required to store the variable.
The above example allocates the memory at runtime.
- malloc()
- The malloc() function is used to allocate the memory during the execution of the program.
- It does not initialize the memory but carries the garbage value.
- It returns a null pointer if it could not be able to allocate the requested space.
- calloc()
- The calloc() is same as malloc() function, but the difference only is that it initializes the memory with zero value.
- realloc()
- The realloc() function is used to reallocate the memory to the new size.
- If sufficient space is not available in the memory, then the new block is allocated to accommodate the existing data.
- free():The free() function releases the memory allocated by either calloc() or malloc() function.
Syntax
Syntax
Syntax
In the above syntax, ptr is allocated to a new size.
Syntax
The above syntax releases the memory from a pointer variable ptr.
calloc() | malloc() | |
---|---|---|
Description | The malloc() function allocates a single block of requested memory. | The calloc() function allocates multiple blocks of requested memory. |
Initialization | It initializes the content of the memory to zero. | It does not initialize the content of memory, so it carries the garbage value. |
Number of arguments | It consists of two arguments. | It consists of only one argument. |
Return value | It returns a pointer pointing to the allocated memory. | It returns a pointer pointing to the allocated memory. |
- The structure is a user-defined data type that allows storing multiple types of data in a single unit. It occupies the sum of the memory of all members.
- The structure members can be accessed only through structure variables.
- Structure variables accessing the same structure but the memory allocated for each variable will be different.
Syntax of structure
Let's see a simple example.
Output:
Enter the name shikha Enter the age 26 Name and age of a student: shikha,26
- The union is a user-defined data type that allows storing multiple types of data in a single unit. However, it doesn't occupy the sum of the memory of all members. It holds the memory of the largest member only.
- In union, we can access only one variable at a time as it allocates one common space for all the members of a union.
Syntax of union
Let's see a simple example
Output:
value of a is 1085485921 value of b is 5.600022 value of ch is a
In the above example, the value of a and b gets corrupted, and only variable ch shows the actual output. This is because all the members of a union share the common memory space. Hence, the variable ch whose value is currently updated.
In C, every local variable of a function is known as an automatic (auto) variable. Variables which are declared inside the function block are known as a local variable. The local variables are also known as an auto variable. It is optional to use an auto keyword before the data type of a variable. If no value is stored in the local variable, then it consists of a garbage value.
The sprintf() stands for "string print." The sprintf() function does not print the output on the console screen. It transfers the data to the buffer. It returns the total number of characters present in the string.
Syntax
Let's see a simple example
Output:
value of n is 9
The Token is an identifier. It can be constant, keyword, string literal, etc. A token is the smallest individual unit in a program. C has the following tokens:
- Identifiers: Identifiers refer to the name of the variables.
- Keywords: Keywords are the predefined words that are explained by the compiler.
- Constants: Constants are the fixed values that cannot be changed during the execution of a program.
- Operators: An operator is a symbol that performs the particular operation.
- Special characters: All the characters except alphabets and digits are treated as special characters.