Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Top 10 Interview Questions and Answers for C Language Programming


May 31, 2021 Article blog



No matter what programming language to learn can not only stay at the viewing level, and finally have to actually write code to grow, the following is the programming lion organized c language programming ten interview questions and their answers.

1. What is the difference between the declaration and definition of a variable/function?

Answer: The declaration of a variable/function declares only that the variable/function exists somewhere in the program, but does not allocate memory for it. B ut the declaration of variables/functions plays an important role. T his is the type of variable/function. T herefore, when a variable is declared, the program knows the data type of the variable. I n the case of declaring a function, the program knows what the arguments of the function are, their data type, the order of the arguments, and the type of return of the function. T hat's the statement. A bout definitions, when we define a variable/function, it allocates memory for that variable/function in addition to the purpose of the declaration. T herefore, we can think of definitions as supersets of declarations. ( or declaration as a subset of the definition).

What are the different storage class descriptors in the 2.C?

Answers: auto, register, static, extern

3. What is the range of variables? W hat is the scope of variables in C?

Answer: The range of variables is part of the program and can be accessed directly. I n C, all identifiers are within the lexical (or static) range.

4. How will you print Hello World without a semicolon?

#include <stdio.h>

int main(void)

{

if (printf("Hello World")) {

}

}

5. When should I use pointers in Program C?

answer:

1. Get the address of the variable

2. In order to implement reference delivery in C: Pointers allow different functions to share and modify their local variables.

3. Pass large structures to avoid full replication of structures.

4. Implement "link" data structures, such as link lists and binary trees.

6. What is a NULL pointer?

Answer: NULL is used to indicate that the pointer is not pointing to a valid position. I deally, if you do not know the value of the pointer at the time of declaration, you should initialize the pointer to NULL. I n addition, when the memory it points to is released in the middle of the program, we should make the pointer NULL.

7. What is a dangling pointer?

Answer: The dangling pointer is not a pointer to a valid memory location. W hen an object is deleted or released, if you do not modify the value of the pointer, a dangling pointer appears, so the pointer still points to where the freed memory is stored. H ere's an example.

Example 1

int* ptr = (int*)malloc(sizeof(int));

.......................... f ree(ptr);

ptr is a dangling pointer and the following operation is not valid

*ptr = 10;

Example 2

int* ptr = NULL { int x = 10; p tr = &x; }

x is out of range, and memory allocated to x is now available

So ptr is now a dangling pointer

8. What is a memory leak? W hy you should avoid using it

Answer: A memory leak occurs when a programmer creates memory in the heap and forgets to delete it. M emory leaks are a particularly serious problem for programs such as daemons and servers, which by definition never terminate.

9. What is a local static variable? W hat's the use of them?

Answer: A local static variable is a variable whose lifetime does not end with a function call that declares it. I t extends the life of the entire program. A ll calls to the function share the same copy of the local static variable. S tatic variables can be used to calculate the number of times a function is called. I n addition, the default value for static variables is 0. F or example, the following program outputs "0 1"

#include <stdio.h>

void fun()

{

The default value for static variables is 0

static int x;

printf("%d ", x);

x = x + 1;

}

int main()

{

fun();

fun();

return 0;

}

Output: 0 1

10. What is a static function? W hat's the use of them?

Answer: In C, functions are global by default. T he "static" keyword before the function name makes it static. U nlike global functions in C, access to static functions is limited to the files that declare them. S o when we want to restrict access to functions, we set them to static. Another reason to make a function static may be to reuse the same function name in other files.