• +91 9723535972
  • info@interviewmaterial.com

C Interview Questions and Answers

C Interview Questions and Answers

Question - 111 : - Diffenentiate between an internal static and external static variable?

Answer - 111 : - An internal static variable is declared inside a block with static storage class whereas an external static variable is declared outside all the blocks in a file.An internal static variable has persistent storage,block scope and no linkage.An external static variable has permanent storage,file scope and internal linkage.

Question - 112 : - What is the difference between a string and an array?

Answer - 112 : - An array is an array of anything. A string is a specific kind of an array with a well-known convention to determine its length. There are two kinds of programming languages: those in which a string is just an array of characters, and those in which it’s a special type. In C, a string is just an array of characters (type char), with one wrinkle: a C string always ends with a NUL character. The “value” of an array is the same as the address of (or a pointer to) the first element; so, frequently, a C string and a pointer to char are used to mean the same thing. An array can be any length. If it’s passed to a function, there’s no way the function can tell how long the array is supposed to be, unless some convention is used. The convention for strings is NUL termination; the last character is an ASCII NUL (‘’) character.

Question - 113 : - What is an argument ? differentiate between formal arguments and actual arguments?

Answer - 113 : - An argument is an entity used to pass the data from calling function to the called function. Formal arguments are the arguments available in the function definition. They are preceded by their own data types. Actual arguments are available in the function call.

Question - 114 : - What are advantages and disadvantages of external storage class?

Answer - 114 : - Advantages of external storage class 1)Persistent storage of a variable retains the latest value 2)The value is globally available Disadvantages of external storage class 1)The storage for an external variable exists even when the variable is not needed 2)The side effect may produce surprising output 3)Modification of the program is difficult 4)Generality of a program is affected

Question - 115 : - What is a void pointer?

Answer - 115 : - A void pointer is a C convention for a raw address. The compiler has no idea what type of object a void Pointer really points to. If you write int *ip; ip points to an int. If you write void *p; p doesn’t point to a void! In C and C++, any time you need a void pointer, you can use another pointer type. For example, if you have a char*, you can pass it to a function that expects a void*. You don’t even need to cast it. In C (but not in C++), you can use a void* any time you need any kind of pointer, without casting. (In C++, you need to cast it). A void pointer is used for working with raw memory or for passing a pointer to an unspecified type. Some C code operates on raw memory. When C was first invented, character pointers (char *) were used for that. Then people started getting confused about when a character pointer was a string, when it was a character array, and when it was raw memory.

Question - 116 : - How can type-insensitive macros be created?

Answer - 116 : - A type-insensitive macro is a macro that performs the same basic operation on different data types. This task can be accomplished by using the concatenation operator to create a call to a type-sensitive function based on the parameter passed to the macro. The following program provides an example: #include #define SORT(data_type) sort_ ## data_type void sort_int(int** i); void sort_long(long** l); void sort_float(float** f); void sort_string(char** s); void main(void); void main(void) { int** ip; long** lp; float** fp; char** cp; ... sort(int)(ip); sort(long)(lp); sort(float)(fp); sort(char)(cp); ... } This program contains four functions to sort four different data types: int, long, float, and string (notice that only the function prototypes are included for brevity). A macro named SORT was created to take the data type passed to the macro and combine it with the sort_ string to form a valid function call that is appropriate for the data type being sorted. Thus, the string sort(int)(ip); translates into sort_int(ip); after being run through the preprocessor.

Question - 117 : - When should a type cast not be used?

Answer - 117 : - A type cast should not be used to override a const or volatile declaration. Overriding these type modifiers can cause the program to fail to run correctly. A type cast should not be used to turn a pointer to one type of structure or data type into another. In the rare events in which this action is beneficial, using a union to hold the values makes the programmer’s intentions clearer.

Question - 118 : - When is a switch statement better than multiple if statements?

Answer - 118 : - A switch statement is generally best to use when you have more than two conditional expressions based on a single variable of numeric type.

Question - 119 : - What is storage class and what are storage variable ?

Answer - 119 : - A storage class is an attribute that changes the behavior of a variable. It controls the lifetime, scope and linkage. There are five types of storage classes 1) auto 2) static 3) extern 4) register 5) typedef

Question - 120 : - Is it better to use malloc() or calloc()?

Answer - 120 : - Both the malloc() and the calloc() functions are used to allocate dynamic memory. Each operates slightly different from the other. malloc() takes a size and returns a pointer to a chunk of memory at least that big: void *malloc( size_t size ); calloc() takes a number of elements, and the size of each, and returns a pointer to a chunk of memory at least big enough to hold them all: void *calloc( size_t numElements, size_t sizeOfElement ); There’s one major difference and one minor difference between the two functions. The major difference is that malloc() doesn’t initialize the allocated memory. The first time malloc() gives you a particular chunk of memory, the memory might be full of zeros. If memory has been allocated, freed, and reallocated, it probably has whatever junk was left in it. That means, unfortunately, that a program might run in simple cases (when memory is never reallocated) but break when used harder (and when memory is reused). calloc() fills the allocated memory with all zero bits. That means that anything there you’re going to use as a char or an int of any length, signed or unsigned, is guaranteed to be zero. Anything you’re going to use as a pointer is set to all zero bits. That’s usually a null pointer, but it’s not guaranteed.Anything you’re going to use as a float or double is set to all zero bits; that’s a floating-point zero on some types of machines, but not on all. The minor difference between the two is that calloc() returns an array of objects; malloc() returns one object. Some people use calloc() to make clear that they want an array.


NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners