Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

45. Explain Extern C Extern C is keyword. To avoid the name mangling or decoration.

n. It means different names are given to the different versions of an overloaded function. Name mangling is complier dependent. Different complier may provide different name mangling functions. Compiler mangles the name as per type, order and no. of args.

44. Difference b/w MACRO and INLINE functions? MACRO: Its a pre-define string composed of one or more statements. Its preprocessor command, it can be executed at pre-Compilation time Syntax: #define <macro name> <macro expression> For example, #define COUNT 100 Note: The pre processor replaces the macro COUNT in the program with 100. Advantages: Macros works similar to a function, but its execution time is faster than function. Disadvantages: Macros are not suitable for arithmetic operations. Macros simply substitutes value without performing calculation. Macros type checking is not there. #define SQUARE (x) x*x If we call SQUARE (5+5) Result will be 35, why because macro directly substitutes the value in case of 5+5 * 5+5=35

INLINE: Inline function can be replaced at compile time. Inline function body is inserted in place of the function call statement during the compilation process. If we declare one function as inline, compiler can treat that function whether it is inline or not. That is depends on that inline function code (size). If there is need to take the function Implemented outside the class as inline function, then we should declare that function with a keyword inline. Like inline void somefunction(); Advantages: By def all functions which are implemented inside a class are inline. Can perform all arithmetic operations. Inline functions provides type checking Disadvantages: If inline function having any loop then complier wont treat as inline. 43. Explain about tree data structure inorder, preorder and postorder? 42.Difference between Malloc() and Calloc()? Malloc() and calloc() both are dynamic allocation of memory ie at run time. The difference is that calloc initializes the allocated memory to 0 or Null while malloc contains garbage values. Both are requires type-casting. malloc() takes a single argument (memory required in bytes), while calloc() needs two arguments.
ptr_var= (cast_type *)malloc(Size_in_bytes);

ptr_var= (cast_type *)calloc(no_of_blocks , size_of_each_block); malloc() does not initialize the memory allocated, while calloc() initializes the allocated memory to ZERO. malloc() function reserves a contiguous memory block, while calloc() function reserves a block of memory.

Difference b/w malloc() and new? Malloc() and new both are dynamic allocation of memory ie at run time. Malloc () is a function, while new is an operator. We cant overload in malloc function, we can overload new operator. If we allocate memory dynamically by using malloc, we have to deallocate by using free() function. While in case of new, we have to deallocate by using delete operator. Malloc will not call constructor if we create class object using malloc. While in case of new class constructor will be called. In case of malloc typecasting is going on ,while in case of new no typecasting is there. While using malloc, if there is not sufficient memory is available,then NULL can assigned to that pointer. While in case of new exception raises. Using malloc ,there is need to include stdlib.h header file, no need to include any header file incase of new.

38.How to create the file scope variable? static int nValue; // file scoped variable float fValue; // global variable int main() { double dValue; // local variable }

File scoped variables act exactly like global variables.

37.Explain about storage classes? Storage classes are automatic, static, extern and register. Automatic Life time with in the function only Keyword- auto Visibility- local

Storage- stack Initial value- garbage value Static Life time throughout the application Keyword- static Visibility- global Storage- stack Initial value- zero Extern Life time throughout the application Keyword- Extern Visibility- global Storage- stack Initial value- zero Register Life time with in the function only Keyword- register Visibility- local Storage- cpu register Initial value- garbage

Memory layout of c program? Memory representation of C program consists of following sections. Text segment Initialized data segment Uninitialized data segment Stack Heap

Text Segment:
A text segment, also known as a code segment or simply as text. This contains executable instructions. As a memory region, a text segment may be placed below the heap or stack in order to prevent heaps and stack overflows from overwriting it.

Initialized data segment:


Initialized data segment, usually called the Data Segment. A data segment is a portion of virtual address s, which contains the global variables and static variables that are initialized by the programmer. Ex: static int i = 10 will be stored in data segment and global int i = 10 will also be stored in data segment.

Uninitialized data segment:


Uninitialized data starts at the end of the data segment and contains all global variables and static variables that are initialized to zero.

Stack:
Stack, where automatic variables are stored, along with information that is saved each time a function is called. Heap: Heap is the segment where dynamic memory allocation usually takes place.

You might also like