Monday, 9 June 2014

C interview questions and answers on basics

Posted by Charan Veera
Hey C language, being one of the earliest computing languages, is commonly considered as the basic requirement for any aspiring software professional. All interviews invariably start with questions in C language and it is imperative for every individual to master C language. A collection of C language Frequently asked  Questions and solutions are presented in this Post.



C interview questions :

What are local and global variables ?

Local
  • These variables only exist inside the specific function that creates them.
  • They are unknown to other functions and to the main program.
  • Local  variables are allocated memory on the stack and they cease to exist once the function that created them has completed execution.
  • They are recreated each time a function is executed or called. Local variables are not initialized automatically.
  •  Their scope and life is  limited to the function in which they are declared.
Global
  •  These variables can be accessed by any function contained in the program.
  •  These variables are visible throughout the program from the point of their declaration.
  •  They do not get recreated if the function is recalled.
  •  To declare a global variable, declare it outside all the function.
  •  If a local variable is declared with the same name as global variable, then the function will use the local variable that was declared within it and ignore the global variables.
  • Global variables are normally initialized to 0 by default.
  • Global variables are allocated memory on the data segment.

What is the difference between static and global variables ?

                           While the life of an object determines whether the object is still in the memory, Scope of the object decides whether the variable can be accessed at that point in the program. Static variables are local in scope to the block or file in which they are defined, but their lifespan is throughout the program. For instance, a static variable inside a function cannot be called from outside of the function but is alive and exists in the memory. It retains its old value between function calls.

                           Global variables persist throughout the program; scope is also through out the program. It means that global variables can be accessed from any function, any file to the program.

What are Volatile variables ?

                             The volatile keyword acts as data type qualifier. It alters the default way in which the compiler handles and does not attempt to optimize the storage referenced by it. The Volatile keyword means the storage is likely to change any time by code outside the control of the user program. This means that if you reference a variable, the program should always read from the physical address and not its cached value.

What is the use of auto keyword ?

                            The auto storage class specifier declares an automatic variable, a variable with a local lifetime. It is default storage class specifier for block-scoped variable declarations. An auto variable is visible only in the block in which it is declared.
                             
                              Since variables with auto storage class are not initialized automatically, you should either explicitly initialize them when you declare them, or assign initial values to them in statements within the block. The values of uninitialized auto variables are undefined.

How do we make a global variable accessible across files? Explain the extern keyword ?

                            If a variable is declared in one file but referenced in another, the extern keyword is used to inform the compiler of the variables existence. Note that the keyword extern is for declarations, not definitions. An extern declaration does not create any storage. variable initalizations should not be done with extern.

What is the use to ' register ' keyword in the context ?

                            The register keyword specifies that the variable is to be stored in a CPU register, If possible. Variables are usually stored in stack and passed to and from the computers processor, as and when required, the speed the data sent at is pretty fast but can be improved on. Almost all computer processors contain CPU registers, these are memory slots on the actual processor, and storing data there gets rid of the overhead of retrieving data from the stack. This memory is quite small compared to normal memory, therefore only few variables can be stored there. Having  a register keyword for variables and parameters also reduces code size, which is important in embedded systems. The number of available CPU registers is strictly dependent on the architectural design of CPU itself. The number o CPU registers is 32 in most cases.

What is the scope of the static variable ?

                              The scope of the static global variable is only within the file in which it is declared. A user cannot use extern in a different file and access the static global variable.
                              The scope of the static local variables is identical to that of local variables, i.e., it is local to the block in which it is defined.

0 comments:

Post a Comment