/*----------------------------------------------------- Script for AdSense -----------------------------------------------------*/ /* */ /* Footer ----------------------------------------------- */ #footer { clear: both; text-align: center; color: #333333; } #footer .widget { margin:.5em; padding-top: 20px; font-size: 85%; line-height: 1.5em; text-align: left; } /** Page structure tweaks for layout editor wireframe */ body#layout #header { width: 750px; } -->

Monday, April 19, 2010

C, C++ fundamentals, interview questions.

What is difference between interpreter and a compiler?

An interpreter reads the source code of your program one line at a time, performs the specific instructions containted in that line. and then gets the next line.
A compiler reads the entire program and converts it into object code, which is translation of the program source code into a form that can be directly executed by the computer.
object code is also called binary code or machine code. once a program is compiled, a line of source code is no longer meaningful in the execution of the program.
For example, BASIC is usually interpreted and C is usually compiled.

A C Program's Memory Map
A compiled C program creates and uses four logically distinct regions of memory that serve specific functions. The first region is the memory that actually holds the code of your program. The next region is the memory where global variables are stored. The remaining two regions are the stack and the heap. The stack is used for a great many things while your program executes. It holds the return address of function calls, arguments to functions, and local variables. It is also used to save the current state of the CPU. The heap is a region of free memory, which your program can use via C's dynamic allocation functions, for things like linked lists and trees.

fig (a): Conceptual Memory Map of C.

General Terms and defenition to understand C, C++ basics.

Source CodeThe text of a program that you can read; commonly thought of as the program. The source code is input into the compiler.
Object code Translation of the source code of a program into machine code. Object code is the input to the linker.
LinkerA program that links separately compiled functions together into one program. It combines the functions in the standard C library with the code that you wrote. The output of the linker is an executable program.
LibraryThe file containing the standard functions that can be used by your program. These functions include all I/O operations as well as other useful routines.

Variable, Constant, Operator and Expression
Variables and constants are manipulated by operators to form expressions.

What is identifier?
The names that are used for variables, functions, labels, and various other user-defined objects are called identifiers.
for ex. count, test123, high_balance.

what is data type?
there are five basic data type in C:
character, integer, floating point, double floating point, and valueless.

What is type modifier?
Excepting type void, the basic data types may have various modifiers preceding them. A modifier is used to alter the meaning of the base type to fit the needs of various situations more precisely. The list of modifiers is shown here:

All Possible Combinations of the Basic Types and Modifiers for 32-Bit Word Sizes

TypeBit WidthRange
char8-128 to 127
unsigned char80 to 255
signed char8-128 to 127
int32-32768 to 32767
unsigned int320 to 655365
signed int32-32768 to 32767
signed int16-32768 to 32767
unsigned short int160 to 655365
signed short int16-32768 to 32787
long int32-2147483648 to 2147483647
unsigned long int320 to 4294967295
signed long int32-2147483648 to 2147483647
float323.4E - 38 to 3.4E + 48
double641.7E-308 to 1.7E+308
long double803.4E-4932 to 1.1E+4932

The difference between signed and unsigned integers is in the way the high-order bit of the integer is interpreted. If a signed integer is specified, then the compiler will generate code that assumes the high-order bit of an integer is to be used as a sign flag. If the sign bit is 0, then the number is positive; if it is 1, then the number is negative.

-127 : 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1
127 : 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1

what is access modifiers?
C has two type modifiers that are used to control the ways in which variables may be accessed or modified. These modifiers are called const and volatile.
Variables of type const may not be changed during execution by your program. For example,
const int a;
will create an integer variable called a that cannot be modified by your program. It can, however, be used in other types of expressions.

The modifier volatile is used to tell the compiler that a variable's value can be changed in ways not explicitly specified by the program.

The volatile keyword is a type qualifier used to declare that an object can be modified in the program by something such as the operating system, the hardware, or a concurrently executing thread.
a variable or object declared with the volatile keyword may be modified externally from the declaring object. Variables declared to be volatile will not be optimized by the compiler because the compiler must assume that their values can change at any time.
For example,
if 0x30 is assumed to be the address of a port that is changed by external conditions only. then you use const and voletile togather.

const volatile unsigned char *port=0x30;

What is local variable and where it is stored?
Variables that are declared inside a function are called local variables. One of the most important things to understand about local variables is that they exist only while the block of code in which they are declared is executing. That is, a local variable is created upon entry into its block and destroyed upon exit. keyword auto (optional) also you can use to declare local variable.

Here, the local variable s is known only within the if code block. Since s is known only within the if block, it may not be referenced elsewhere--not even in other parts of the function that contains it.
storage for local variables is on the stack. The fact that the stack is a dynamic and changing region of memory explains why local variables cannot, in general, hold their values between function calls.

What is global variable? Can local variable and global have variable same name? If yes, then which variable function code will refer?
where global variable storage?

Unlike local variables, global variables are known throughout the entire program and may be used by any piece of code. Also, they will hold their values during the entire execution of the program.

if a global variable and a local variable have the same name, all references to that name inside the function where the local variable is declared refer to the local variable and have no effect on the global variable. This is a convenient benefit. However, forgetting this can cause your program to act very strangely, even though it "looks" correct.
Note: Storage for global variables is in a fixed region of memory set aside for this purpose by the compiler. Global variables are very helpful when the same data is used in many functions in your program. You should avoid using unnecessary global variables, however, for three reasons:
1. They take up memory the entire time your program is executing, not just when they are needed.
2. Using a global variable where a local variable will do makes a function less general because it relies on something that must be defined outside itself.
3. Using a large number of global variables can lead to program errors because of unknown, and unwanted, side effects.

No comments: