Sunday, July 29, 2018

C Questions And Answers – 2018B5

There are many commonly asked questions regarding C programming language. Below are some collected such question-answer examples. The questions are usually related with 32-bit system, Turbo C IDE in windows or GCC under Linux environment [not always].

For more such examples, click C_Q&A label.

-------------------------------------------------------------------------------------------------

 

What is modular programming?

 

If a program is large, it is subdivided into a number of smaller programs that are called modules or subprograms. If a complex problem is solved using more modules, this approach is known as modular programming.

 

-------------------------------------------------------------------------------------------------

 

How many levels deep can include files be nested?

 

Even though there is no limit to the number of levels of nested include files you can have, your compiler might run out of stack space while trying to include an inordinately high number of files. This number varies according to your hardware configuration and possibly your compiler.

 

-------------------------------------------------------------------------------------------------

 

What is the difference between declaring a variable and defining a variable?

 

Declaring a variable means describing its type to the compiler but not allocating any space for it. Defining a variable means declaring it and also allocating space to hold the variable. You can also initialize a variable at the time it is defined.

 

-------------------------------------------------------------------------------------------------

 

What is an lvalue?

 

An lvalue is an expression to which a value can be assigned. The lvalue expression is located on the left side of an assignment statement, whereas an rvalue is located on the right side of an assignment statement. Each assignment statement must have an lvalue and an rvalue. The lvalue expression must reference a storable variable in memory. It cannot be a constant.

 

-------------------------------------------------------------------------------------------------

 

Differentiate between an internal static and external static variable?

 

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.

 

-------------------------------------------------------------------------------------------------

 

What is the difference between a string and an array?

 

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.

 

-------------------------------------------------------------------------------------------------

 

What is an argument? Differentiate between formal arguments and actual arguments?

 

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.

 

-------------------------------------------------------------------------------------------------

 

What are advantages and disadvantages of external storage class?

 

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

 

-------------------------------------------------------------------------------------------------

 

What is a void pointer?

 

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.

 

-------------------------------------------------------------------------------------------------

 

When should a type cast not be used?

 

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.

 

-------------------------------------------------------------------------------------------------

 

When is a switch statement better than multiple if statements?

 

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

 

-------------------------------------------------------------------------------------------------

 

…till next post, bye-bye & take care.

No comments:

Post a Comment