C Interview Questions

1 what is the difference between a while statement and a do statement?

Ans: A while statement checks at the beginning of a loop to see whether the next loop iteration should occur. A do statement checks at the end of a loop to see whether the next iteration of a loop should occur. The do statement will always execute the body of a loop at least once.

2 what is the difference between a break statement and a continue statement?

Ans: A break statement results in the termination of the statement to which it applies (switch, for, do, or while). A continue statement is used to end the current loop iteration and return control to the loop statement.

3 what does break and continue statements do?

Ans: Continue statement continues the current loop (if label not specified) in a new iteration whereas break statement exits the current loop.

4 how can you tell what shell you are running on unix system?

Ans: You can do the Echo $RANDOM. It will return a undefined variable if you are from the C-Shell, just a return prompt if you are from the Bourne shell, and a 5 digit random numbers if you are from the Korn shell. You could also do a ps -l and look for the shell with the highest PID.

5 what is the difference between a pointer and a reference?

Ans: You can do the Echo $RANDOM. It will return a undefined variable if you are from the C-Shell, just a return prompt if you are from the Bourne shell, and a 5 digit random numbers if you are from the Korn shell. You could also do a ps -l and look for the shell with the highest PID.

6 How do you print an address?

Ans: The safest way is to use printf() (or fprintf() or sprintf()) with the %P specification. That prints a void pointer (void*). Different compilers might print. a pointer with different formats. Your compiler will pick a format that?s right for your environment. If you have some other kind of pointer (not a void*) and you want to be very safe, cast the pointer to a void*:? printf( ?%Pn?, (void*) buffer ).

7 Can math operations be performed on a void pointer?

Ans: No. Pointer addition and subtraction are based on advancing the pointer by a number of elements. By definition, if you have a void pointer, you don?t know what it?s pointing to, so you don?t know the size of what it?s pointing to. If you want pointer arithmetic to work on raw addresses, use character pointers.

8 How can you determine the size of an allocated portion of memory?

Ans: You can?t, really. free() can , but there?s no way for your program to know the trick free() uses. Even if you disassemble the library and discover the trick, there?s no guarantee the trick won?t change with the next release of the compiler.

9 what is a null pointer?

Ans: There is times when it?s necessary to have a pointer that doesn?t point to anything. The macro NULL, defined in , has a value that?s guaranteed to be different from any valid pointer. NULL is a literal zero, possibly cast? to void* or char*. Some people, notably C++ programmers, prefer to use 0 rather than NULL.

The null pointer is used in three ways:
1) To stop indirection in a recursive data structure
2) As an error value
3) As a sentinel value.

10 Is NULL always defined as 0?

Ans: NULL is defined as either 0 or (void*)0. These values are almost identical; either a literal zero or a void pointer is converted automatically to any kind of pointer, asNecessary, whenever a pointer is needed (although the compiler can?t always tell when a pointer is needed).

11 Difference between arrays and pointers?

Ans: Pointers are used to manipulate data using the address. Pointers use * operator to access the data pointed to by them.
Arrays use subscripted variables to access and manipulate data. Array variables can be
Equivalently written using pointer expression.

12 Are pointers integers?

Ans: pointers are not integers. A pointer is an address. It is merely a positive number and not an integer.

13 How are pointer variables?

Ans: Pointer variable are initialized by one of the following two ways
Static memory allocation
Dynamic memory allocation.

14 What are the advantages of the pointer?

Ans: Debugging is easier
It is easier to understand the logic involved in the program
Testing is easier
Recursive call is possible
Irrelevant details in the user point of view are hidden in functions

Functions are helpful in generalizing the Program

15 What is a pointer value and Address?

Ans: A pointer value is a data object that refers to a memory location. Each memory location is numbered in the memory. The number attached to a memory location is called the address of the location.

16 What is a pointer variable?

Ans: A pointer variable is a variable that may contain the address of another variable or any valid address in the memory.

17 What is static memory allocation and dynamic memory allocation?

Ans: Static memory allocation: The compiler allocates the required memory space for a declared variable. By using the address of operator, the reserved address is obtained and this address may be assigned to a pointer variable. Since most of the declared variable
Have static memory, this way of assigning pointer value to a pointer variable is known as static memory allocation. Memory is assigned during compilation time.

Dynamic memory allocation: It uses functions such as malloc ( ) or calloc ( ) to get memory dynamically. If these functions are used to get memory dynamically and the values returned by these functions are assigned to pointer variables, such assignments are known as dynamic memory allocation. Memory is assigned during run time.

18 What is the purpose of main ( )

Ans: The function main ( ) invokes other functions within it. It is the first function to
Be called when the program starts execution.
It is the starting function
It returns an int value to the environment that called the program
Recursive call is allowed for main ( ) also.
It is a user-defined function
Program execution ends when the closing brace of the function main ( ) is reached.
It has two arguments 1) argument count and 2) argument vector (represents strings passed).
Any user-defined name can also be used as parameters for main ( ) instead of argc and argv

19 What is the purpose of realloc ( )?

Ans: The function realloc (ptr, n) uses two arguments. the first argument ptr is a pointer to a block of memory for which the size is to be altered. The second argument n specifies the new size. The size may be increased or decreased. If n is greater than the old size and
If sufficient space is not available subsequent to the old region, the function realloc ( )
may create a new region and all the old data are moved to the new region.

20 What are the advantages of auto variables?

Ans: 1) The same auto variable name can be used in different blocks
2) There is no side effect by changing the values in the blocks
3) The memory is economically used
4) Auto variables have inherent protection because of local scope.