Interview Questions on C Programming Variables

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

    The definition is the one that actually allocates space, and provides an initialization value, if any.
    There can be many declarations, but there must be exactly one definition. A definition tells the compiler to set aside storage for the variable. A declaration makes the variable known to parts of the program that may wish to use it. A variable might be defined and declared in the same statement.

  2. Do Global variables start out as zero? 

    Un initialized variables declared with the "static" keyword are initialized to zero. Such variables are implicitly initialized to the null pointer if they are pointers, and to 0.0F if they are floating point numbers.
    Local variables start out containing garbage, unless they are explicitly initialized.
    Memory obtained with malloc() and realloc() is likely to contain junk, and must be initialized. Memory obtained with calloc() is all-bits-0, but this is not necessarily useful for pointer or floating-point values (This is in contrast to Global pointers and Global floating point numbers, which start as zeroes of the right type).

  3. Does C have boolean variable type?

    No, C does not have a boolean variable type. One can use ints, chars, #defines or enums to achieve the same in C.

    #define TRUE 1 
    #define FALSE 0 
    enum bool {false, true};

    An enum may be good if the debugger shows the names of enum constants when examining variables.

  4. Where may variables be defined in C?

    Outside a function definition (global scope, from the point of definition downward in the source code). Inside a block before any statements other than variable declarations (local scope with respect to the block).

  5. To what does the term storage class refer? What are auto, static, extern, volatile, const classes?

    This is a part of a variable declaration that tells the compiler how to interpret the variable's symbol. It does not in itself allocate storage, but it usually tells the compiler how the variable should be stored. Storage class specifiers help you to specify the type of storage used for data objects. Only one storage class specifier is permitted in a declaration this makes sense, as there is only one way of storing things and if you omit the storage class specifier in a declaration, a default is chosen. The default depends on whether the declaration is made outside a function (external declarations) or inside a function (internal declarations). For external declarations the default storage class specifier will be extern and for internal declarations it will be auto. The only exception to this rule is the declaration of functions, whose default storage class specifier is always extern.
    Here are C's storage classes and what they signify:

    • auto - local variables.
    • static - variables are defined in a nonvolatile region of memory such that they retain their contents though out the program's execution.
    • register - asks the compiler to devote a processor register to this variable in order to speed the program's execution. The compiler may not comply and the variable looses it contents and identity when the function it which it is defined terminates.
    • extern - tells the compiler that the variable is defined in another module.

    In C, const and volatile are type qualifiers. The const and volatile type qualifiers are completely independent. A common misconception is to imagine that somehow const is the opposite of volatile and vice versa. This is wrong. The keywords const and volatile can be applied to any declaration, including those of structures, unions, enumerated types or typedef names. Applying them to a declaration is called qualifying the declaration?that's why const and volatile are called type qualifiers, rather than type specifiers.
    • const means that something is not modifiable, so a data object that is declared with const as a part of its type specification must not be assigned to in any way during the run of a program. The main intention of introducing const objects was to allow them to be put into read-only store, and to permit compilers to do extra consistency checking in a program. Unless you defeat the intent by doing naughty things with pointers, a compiler is able to check that const objects are not modified explicitly by the user. It is very likely that the definition of the object will contain an initializer (otherwise, since you can't assign to it, how would it ever get a value?), but this is not always the case. For example, if you were accessing a hardware port at a fixed memory address and promised only to read from it, then it would be declared to be const but not initialized.
    • volatile tells the compiler that other programs will be modifying this variable in addition to the program being compiled. For example, an I/O device might need write directly into a program or data space. Meanwhile, the program itself may never directly access the memory area in question. In such a case, we would not want the compiler to optimize-out this data area that never seems to be used by the program, yet must exist for the program to function correctly in a larger context. It tells the compiler that the object is subject to sudden change for reasons which cannot be predicted from a study of the program itself, and forces every reference to such an object to be a genuine reference.
    • const volatile - Both constant and volatile.

    The "volatile" modifier
    The volatile modifier is a directive to the compiler?s optimizer that operations involving this variable should not be optimized in certain ways. There are two special cases in which use of the volatile modifier is desirable. The first case involves memory-mapped hardware (a device such as a graphics adaptor that appears to the computer?s hardware as if it were part of the computer?s memory), and the second involves shared memory (memory used by two or more programs running simultaneously). Most computers have a set of registers that can be accessed faster than the computer?s main memory. A good compiler will perform a kind of optimization called ?redundant load and store removal.? The compiler looks for places in the code where it can either remove an instruction to load data from memory because the value is already in a register, or remove an instruction to store data to memory because the value can stay in a register until it is changed again anyway.
    If a variable is a pointer to something other than normal memory, such as memory-mapped ports on a
    peripheral, redundant load and store optimizations might be detrimental. For instance, here?s a piece of code that might be used to time some operation:

    time_t time_addition(volatile const struct timer *t, int a) 

      int n; 
      int x; 
      time_t then; 
      x = 0; 
      then = t->value; 
      for (n = 0; n < 1000; n++) 
      { 
        x = x + a; 
      } 
      return t->value - then; 


    In this code, the variable t->value is actually a hardware counter that is being incremented as time passes. The function adds the value of a to x 1000 times, and it returns the amount the timer was incremented by while the 1000 additions were being performed. Without the volatile modifier, a clever optimizer might assume that the value of t does not change during the execution of the function, because there is no statement that explicitly changes it. In that case, there?s no need to read it from memory a second time and subtract it, because the answer will always be 0. The compiler might therefore ?optimize? the function by making it always return 0. If a variable points to data in shared memory, you also don?t want the compiler to perform redundant load and store optimizations. Shared memory is normally used to enable two programs to communicate with each other by having one program store data in the shared portion of memory and the other program read the same portion of memory. If the compiler optimizes away a load or store of shared memory, communication between the two programs will be affected.
  6. What does the typedef keyword do?

    This keyword provides a short-hand way to write variable declarations. It is not a true data typing mechanism, rather, it is syntactic "sugar coating".
    For example

    typedef struct node
    {
      int value;
      struct node *next;
    }mynode;

    This can later be used to declare variables like this

    mynode *ptr1;
    and not by the lengthy expression
    struct node *ptr1;

    There are three main reasons for using typedefs:

    • It makes the writing of complicated declarations a lot easier. This helps in eliminating a lot of clutter in the code.
    • It helps in achieving portability in programs. That is, if we use typedefs for data types that are machine dependent, only the typedefs need to change when the program is ported to a new platform.
    • It helps in providing better documentation for a program. For example, a node of a doubly linked list is better understood as ptrToList than just a pointer to a complicated structure.
  7. What is the difference between constants defined through #define and the constant keyword?

    A constant is similar to a variable in the sense that it represents a memory location (or simply, a value). It is different from a normal variable, in that it cannot change it's value in the proram - it must stay for ever stay constant. In general, constants are a useful because they can prevent program bugs and logical errors(errors are explained later). Unintended modifications are prevented from occurring. The compiler will catch attempts to reassign new values to constants.
    Constants may be defined using the preprocessor directive #define. They may also be defined using the const keyword.
    So whats the difference between these two?

    #define ABC 5
    and
    const int abc = 5;

    There are two main advantages of the second one over the first technique. First, the type of the constant is defined. "pi" is float. This allows for some type checking by the compiler. Second, these constants are variables with a definite scope. The scope of a variable relates to parts of your program in which it is defined.
    There is also one good use of the important use of the const keyword. Suppose you want to make use of some structure data in some function. You will pass a pointer to that structure as argument to that function. But to make sure that your structure is readonly inside the function you can declare the structure argument as const in function prototype. This will prevent any accidental modification of the structure values inside the function.

  8. What are Trigraph characters?

    These are used when you keyboard does not support some special characters

        ??=    #
        ??(    [
        ??)    ]
        ??<    {
        ??>    }
        ??!    |
        ??/    \
        ??'    ^
        ??-    ~

  9. How are floating point numbers stored? Whats the IEEE format?

    IEEE Standard 754 floating point is the most common representation today for real numbers on computers, including Intel-based PC's, Macintoshes, and most Unix platforms.
    IEEE floating point numbers have three basic components: the sign, the exponent, and the mantissa. The mantissa is composed of the fraction and an implicit leading digit (explained below). The exponent base(2) is implicit and need not be stored.
    The following figure shows the layout for single (32-bit) and double (64-bit) precision floating-point values. The number of bits for each field are shown (bit ranges are in square brackets):

                     Sign   Exponent   Fraction   Bias 
    --------------------------------------------------
    Single Precision 1 [31] 8 [30-23]  23 [22-00] 127 
    Double Precision 1 [63] 11 [62-52] 52 [51-00] 1023 

    The sign bit is as simple as it gets. 0 denotes a positive number; 1 denotes a negative number. Flipping the value of this bit flips the sign of the number.
    The exponent field needs to represent both positive and negative exponents. To do this, a bias is added to the actual exponent in order to get the stored exponent. For IEEE single-precision floats, this value is 127. Thus, an exponent of zero means that 127 is stored in the exponent field. A stored value of 200 indicates an exponent of (200-127), or 73. For reasons discussed later, exponents of -127 (all 0s) and +128 (all 1s) are reserved for special numbers. For double precision, the exponent field is 11 bits, and has a bias of 1023.
    The mantissa, also known as the significand, represents the precision bits of the number. It is composed of an implicit leading bit and the fraction bits. To find out the value of the implicit leading bit, consider that any number can be expressed in scientific notation in many different ways. For example, the number five can be represented as any of these:

            5.00 × 100
            0.05 × 10 ^ 2
            5000 × 10 ^ -3

    In order to maximize the quantity of representable numbers, floating-point numbers are typically stored in normalized form. This basically puts the radix point after the first non-zero digit. In normalized form, five is represented as 5.0 × 100. A nice little optimization is available to us in base two, since the only possible non-zero digit is 1. Thus, we can just assume a leading digit of 1, and don't need to represent it explicitly. As a result, the mantissa has effectively 24 bits of resolution, by way of 23 fraction bits.
    So, to sum up:

    1. The sign bit is 0 for positive, 1 for negative. 
    2. The exponent's base is two. 
    3. The exponent field contains 127 plus the true exponent for single-precision, 
       or 1023 plus the true exponent for double precision. 
    4. The first bit of the mantissa is typically assumed to be 1.f, where f is the 
       field of fraction bits. 

  10. When should the register modifier be used?

    The register modifier hints to the compiler that the variable will be heavily used and should be kept in the CPU?s registers, if possible, so that it can be accessed faster. There are several restrictions on the use of the register modifier.
    First, the variable must be of a type that can be held in the CPU?s register. This usually means a single value of a size less than or equal to the size of an integer. Some machines have registers that can hold floating-point numbers as well. Second, because the variable might not be stored in memory, its address cannot be taken with the unary & operator. An attempt to do so is flagged as an error by the compiler. Some additional rules affect how useful the register modifier is. Because the number of registers is limited, and because some registers can hold only certain types of data (such as pointers or floating-point numbers), the number and types of register modifiers that will actually have any effect are dependent on what machine the program will run on. Any additional register modifiers are silently ignored by the compiler. Also, in some cases, it might actually be slower to keep a variable in a register because that register then becomes unavailable for other purposes or because the variable isn?t used enough to justify the overhead of loading and storing it. So when should the register modifier be used? The answer is never, with most modern compilers. Early C compilers did not keep any variables in registers unless directed to do so, and the register modifier was a valuable addition to the language. C compiler design has advanced to the point, however, where the compiler will usually make better decisions than the programmer about which variables should be stored in registers. In fact, many compilers actually ignore the register modifier, which is perfectly legal, because it is only a hint and not a directive.

  11. When should a type cast be used?

    There are two situations in which to use a type cast.
    The first use is to change the type of an operand to an arithmetic operation so that the operation will be performed properly.
    The second case is to cast pointer types to and from void * in order to interface with functions that expect or return void pointers. For example, the following line type casts the return value of the call to malloc() to be a pointer to a foo structure.

    struct foo *p = (struct foo *) malloc(sizeof(struct foo)); 

    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.


  12. Can structures be assigned to variables and passed to and from functions?

    Yes, they can!
    But note that when structures are passed, returned or assigned, the copying is done only at one level (The data pointed to by any pointer fields is not copied!.


    These Questions are taken from CrackTheInterview team.

12 comments:

  1. 1. declaration create a type for later refrence used by the developer.
    defination allocates space on memory based on the type of variable.

    ReplyDelete
  2. 2.varibale defined out of any function scope start with zero.

    ReplyDelete
  3. 4. when complier see the usage of the variable,it define or allocate the space in memory for variable.

    ReplyDelete
  4. hey these collection of questions are excellent...way to revise what you have learned and even to add to your knowledge..

    ReplyDelete
  5. Thanks for the post

    You can find some good Techical Interview Question Answers on in the below link

    Technical Interview Question Answers

    Thanks
    Joya

    ReplyDelete
  6. int a=10;
    printf("%d",a++ + ++a * 10 - --a* ++a);

    please give explanation ...Its Chalange ....
    OtherWise .. , .. , ..

    ReplyDelete
  7. Good Post i found a great site www.cinterviews.com which is dedicated for interviews its also great just want to share with you

    ReplyDelete
  8. Some more such Question and answer at

    http://shareprogrammingtips.com/c-language-programming-tips/c-programming-interview-questions-and-answers-for-freshers/

    ReplyDelete
  9. @anonymous >> compiler calculate it right to left as Operator precedence .. :P

    ReplyDelete
  10. very helpful...:)

    ReplyDelete