Tuesday 9 April 2013

What is the use of typedef?

What is the use of typedef?
Ans: The typedef help in easier modification when the programs are ported to another machine.
A descriptive new name given to the existing data type may be easier to understand the code.



Blog Author: Vijay Kumar

What are register variables? What are the advantages of using register variables?

What are register variables? What are the advantages of using register variables?
Ans: If a variable is declared with a register storage class,it is known as register variable.The
register variable is stored in the cpu register instead of main memory.Frequently used variables
are declared as register variable as it’s access time is faster.



Blog Author: Vijay Kumar

Describe about storage allocation and scope of global, extern, static, local and register variables?


Describe about storage allocation and scope of global, extern, static, local and register variables?
Ans:
Globals have application-scope. They’re available in any compilation unit that includes an
appropriate declaration (usually brought from a header file). They’re stored wherever the linker puts them, usually a place called the “BSS segment.”
Extern? This is essentially “global.”
Static: Stored the same place as globals, typically, but only available to the compilation unit that contains them. If they are block-scope global, only available within that block and its subblocks.
Local: Stored on the stack, typically. Only available in that block and its subblocks.
(Although pointers to locals can be passed to functions invoked from within a scope where that local is valid.)
Register: See tirade above on “local” vs. “register.” The only difference is that
the C compiler will not let you take the address of something you’ve declared as “register.”

What are enumerations?

What are enumerations?
Ans: They are a list of named integer-valued constants. Example:enum color { black , orange=4,
yellow, green, blue, violet };This declaration defines the symbols “black”, “orange”, “yellow”, etc. to have the values “1,” “4,” “5,” … etc. The difference between an enumeration and a macro is that the enum actually declares a type, and therefore can be type checked.



Blog Author: Vijay Kumar

Where does global, static, and local, register variables get stored?

Where does global, static, and local, register variables get stored?
Ans: Global: Wherever the linker puts them. Typically the “BSS segment” on many platforms.
Static: Again, wherever the linker puts them. Often, they’re intermixed with the global. The only difference between global and statics is whether the linker will resolve the symbols across compilation units.Local: Typically on the stack, unless the variable gets register allocated and never spills.Register: Nowadays, these are equivalent to “Local” variables. They live on the stack unless they get register-allocated.



Blog Author: Vijay Kumar

What is the difference between "calloc(...)" and "malloc(...)"?


What is  the difference between "calloc(...)" and
 "malloc(...)"?

1. calloc(...) allocates a block of memory for an array of elements of a
certain size. By default the block is initialized to 0. The total number of
memory allocated will be (number_of_elements * size).
malloc(...) takes in only a single argument which is the memory
required in bytes. malloc(...) allocated bytes of memory and not blocks
of memory like calloc(...).
2. malloc(...) allocates memory blocks and returns a void pointer to the
allocated space, or NULL if there is insufficient memory available.
calloc(...) allocates an array in memory with elements initialized to 0
and returns a pointer to the allocated space. calloc(...) calls malloc(...)
in order to use the C++ _set_new_mode function to set the new
handler mode.

Where is the auto variables stored?

Where is the auto variables stored?
Ans: Auto variables can be stored anywhere, so long as recursion works. Practically, they’re stored on
the stack. It is not necessary that always a stack exist. You could theoretically allocate function invocation records from the heap.



Blog Author: Vijay Kumar

What is static identifier?

What is static identifier?
Ans: A file-scope variable that is declared static is visible only to functions within that file. A
function-scope or block-scope variable that is declared as static is visible only within that scope. Furthermore, static variables only have a single instance. In the case of function- or block-scope variables, this means that the variable is not “automatic” and thus retains its value across function invocations.


Blog Author: Vijay Kumar

Difference between pass by reference and pass by value?

Difference between pass by reference and pass by value?
Ans: Pass by reference passes a pointer to the value. This allows the callee to modify the variable directly.Pass by value gives a copy of the value to the callee. This allows the callee to modify the value without modifying the variable. (In other words, the callee simply cannot modify the variable, since it lacks a reference to it.)



Blog Author: Vijay Kumar

What are macros? What are its advantages and disadvantages?

What are macros? What are its advantages and disadvantages?
Ans: Macros are abbreviations for lengthy and frequently used statements. When a macro is called the entire code is substituted by a single line though the macro definition is of several lines.
The advantage of macro is that it reduces the time taken for control transfer as in case of function.
The disadvantage of it is here the entire code is substituted so the program becomes
lengthy if a macro is called several times.



Blog Author: Vijay Kumar

What are the differences between malloc () and calloc ()?

What are the differences between malloc () and calloc ()?
Ans: Malloc Calloc 1-Malloc takes one argument Malloc(a);where a number of bytes 2-memory allocated contains garbage values
1-Calloc takes two arguments Calloc(b,c) where b no of object and c size of object
2-It initializes the contains of block of memory to zerosMalloc takes one argument, memory allocated contains garbage values.
It allocates contiguous memory locations. Calloc takes two arguments, memory allocated contains all zeros, and the memory allocated is not contiguous.



Blog Author: Vijay Kumar

In header files whether functions are declared or defined?

In header files whether functions are declared or defined?
Ans: Functions are declared within header file. That is function prototypes exist in a header file, not function bodies. They are defined in library (lib).



Blog Author: Vijay Kumar

What are the differences between structures and arrays?

What are the differences between structures and arrays?
Ans: Structure is a collection of heterogeneous data type but array is a collection of homogeneous data types.
Array 
1-It is a collection of data items of same data type.
2-It has declaration only
3-.There is no keyword.
4- array name represent the address of the starting element.
Structure
1-It is a collection of data items of different data type.
2- It has declaration and definition
3- keyword struct is used
4-Structure name is known as tag it is the short hand notation of the declaration.



Blog Author: Vijay Kumar

What are the differences between structures and union?


What are the differences between structures and union?
Ans: A structure variable contains each of the named members, and its size is large enough to hold all the members. Structure elements are of same size.
A union contains one of the named members at a given time and is large enough to hold the largest member. Union element can be of different sizes.



Difference Between Stucture and Union :


StructureUnion
i. Access Members     
We can access all the members of structure at anytime.Only one member of union can be accessed at anytime.
ii. Memory Allocation     
Memory is allocated for all variables.Allocates memory for variable which variable require more memory.
iii. Initialization     
All members of structure can be initializedOnly the first member of a union can be initialized.
iv. Keyword     
'struct' keyword is used to declare structure.'union' keyword is used to declare union.
v. Syntax     
struct struct_name
{
    structure element 1;
    structure element 2;
 ----------
 ----------
    structure element n;
}struct_var_nm;
union union_name
{
    union element 1;
    union element 2;
 ----------
 ----------
    union element n;
}union_var_nm;
vi. Example     
struct item_mst
{
    int rno;
    char nm[50];
}it;
union item_mst
{
    int rno;
    char nm[50];
}it;

What is a union?


What is a union?
Ans: Union is a collection of heterogeneous data type but it uses efficient memory utilization technique by allocating enough memory to hold the largest member. Here a single area of memory contains values of different types at different time. A union can never be initialized.