You are on page 1of 3

When should we use pointers in a C program?

1. To get address of a variable


2. For achieving pass by reference in C: Pointers allow different functions to share and modify their local
variables.
3. To pass large structures so that complete copy of the structure can be avoided.
C
4. To implement linked data structures like linked lists and binary trees.


What is NULL pointer?
Ans: NULL is used to indicate that the pointer doesnt point to a valid location. Ideally, we should
initialize pointers as NULL if we dont know their value at the time of declaration.
What is Dangling pointer?
Ans: Dangling Pointer is a pointer that doesnt point to a valid memory location. Dangling pointers arise
when an object is deleted or deallocated, without modifying the value of the pointer
What are local static variables? What is their use?
Ans:A local static variable is a variable whose lifetime doesnt end with a function call where it is
declared. It extends for the lifetime of complete program. All calls to the function share the same copy
of local static variables. Static variables can be used to count the number of times a

What are static functions? What is their use?
Ans:In C, functions are global by default. The static keyword before a function name makes it
static. Unlike global functions in C, access to static functions is restricted to the file where they
are declared. Therefore, when we want to restrict access to functions, we make them static.
Another reason for making functions static can be reuse of the same function name in other files.
See this for examples and more details.
We will soon be covering more C programming questions. Please write comments if you find
anything incorrect, or you want to share more information about the topic discussed above.




What are Storage Classes? What are the Types of Storage
Classes?
Storage classes are used to determine the scope and the life time of a function or a variable in the
system memory. There are four types of storage classes in C: register, static, auto and extern, out
of which auto is considered the default storage class for variables.
What is a Pointer in C? What is a NULL pointer? Can you
show with an example.
Pointers are one of the most important concepts in C. A pointer is used to store the memory
address of a variable within the system memory. Its a handle to the variables memory address.
A pointer is itself considered a variable as well. Pointers give a programmer a great deal of
power and flexibility within a program. They are typically used to indirectly access structures,
arrays and functions which are all core functionalities of the C language. A null pointer, is a
pointer that does not yet point to a memory address.
/* declare a pointer to type char */
char *bufptr;
bufptr=NULL;
/* allocate memory on the heap */
bufptr = malloc(1000);
/* check if the allocation was successful */
if (bufptr == NULL)
{
printf("Memory allocation failed !\n");
}
/* Do some processing here */

/* Free the memory */
free(bufptr);
bufptr = NULL;
What are Structures in C?
Structures are a collection of locally declared variables in C. Structures are used to group
together an assortment of related variables together, which allows for better handling by the
programmer. For example, you want to store information about a new bike in the computer.
Instead of storing information about its mileage, different parts, top speed under different names,
you can use a single name (structure) to group these variables together.
struct bike_details {
char* pmake;
char* pRegistration;
int mileage;
int top_speed;
};
What is Nesting in C?
Including one constructed inside another, is nesting. Nesting can be a structure inside another
structure, an if-else statement within another, multiple while loops, one inside the other.
Continuing our earlier example, if you have a collection of bikes, instead of grouping them
separately, you can nest them within a main structure, which you can name Collection of
Bikes.
How is calloc () different from malloc ()
The main difference between the two is that calloc (), when it is used to assign a block of
memory, the allocated contents are initialized to 0. Malloc (), on the other hand, does not
initialise the memory block it assigns. The memory just has random values left over from
previous usage.
What are the differences between printf () and scanf ()
This is a very basic question, but one you may be asked by the interviewer to just check your
basics. Printf() is used to print (or display) the value of a variable on the screen, while scanf() is
used to accept the value of a variable. Refer our earlier program on the Fibionacci series for an
example.