Where are the auto variables stored?
Dec 9th
Main memory and CPU registers are the two memory locations where auto variables are stored. Auto variables are defined under automatic storage class. They are stored in main memory. Memory is allocated to an automatic variable when the block which contains it is called and it is de-allocated at the completion of its block execution.
Auto variables:
Storage : main memory.
Default value : garbage value.
Scope : local to the block in which the variable is defined.
Lifetime : till the control remains within the block in which the variable is defined.
Out of fgets( ) and gets( ) which function is safer to use and why?
Dec 9th
Out of functions fgets( ) and gets( ), fgets( ) is safer to use. gets( ) receives a string from the keyboard and it is terminated only when the enter key is hit. There is no limit for the input string. The string can be too long and may lead to buffer overflow.
Example:
gets(s) /* s is the input string */
Whereas fgets( ) reads string with a specified limit, from a file and displays it on screen.The function fgets( ) takes three arguments.
First argument : address where the string is stored. Second argument : maximum length of the string. Third argument More >
What are the differences between malloc() and calloc()?
Dec 9th
Allocation of memory at the time of execution is called dynamic memory allocation. It is done using the standard library functions malloc() and calloc(). It is defined in “stdlib.h”.
malloc(): used to allocate required number of bytes in memory at runtime. It takes one argument, viz. size in bytes to be allocated.
Syntax:
void * malloc(size_t size);
Example:
a = (int*) malloc(4);
4 is the size (in bytes) of memory to be allocated.
calloc(): used to allocate required number of bytes in memory at runtime. It needs two arguments viz.,
1. total number of data and
2. size of each data.
Syntax:
void * calloc(size_t nmemb, size_t size);
Example:
a = (int*) calloc(8, More >
What do the ‘c’ and ‘ v ‘ in argc and argv stand for? Explain their purpose?
Dec 9th
In C, we can supply arguments to ‘main’ function. The arguments that we pass to main ( ) at command prompt are called command line arguments. These arguments are supplied at the time of invoking the program.
The main ( ) function can take arguments as: main(int argc, char *argv[]) { } The first argument argc is known as ‘argument counter’. It represents the number of arguments in the command line. The second argument argv is known as ’argument vector’. It is an array of char type pointers that points to the command line arguments. Size of this array will be equal to the More >
Which bit wise operator is suitable for turning OFF a particular bit in a number?
Dec 9th
Bitwise AND operator (&), one’s complement operator(~)
Example: To unset the 4th bit of byte_data or to turn off a particular bit in a number.
Explanation:
consider,
char byte_data= 0b00010111;
byte_data= (byte_data)&(~(1<<4));
EXPLANATION:
1 can be represented in binary as 0b00000001 (1<<4)
<< is a left bit shift operator, it shits the bit 1 by 4 places towards left.
(1<<4) becomes 0b00010000
And ~ is the one’s complement operator in C language. So ~(1<<4) = complement of 0b00010000
= 0b11101111
replacing value of byte_data and ~(1<<4) in (byte_data)&(~(1<<4));
we get
(0b00010111) & (0b11101111)
Perform AND operation to below bytes. 00010111
11101111
00000111
Thus the 4th bit is unset.
More >Which bitwise operator is suitable for checking whether a particular bit is ON or OFF?
Dec 9th
Bitwise AND operator.
Example: Suppose in byte that has a value 10101101 . We wish to check whether bit number 3 is ON (1) or OFF (0) . Since we want to check the bit number 3, the second operand for AND operation we choose is binary 00001000, which is equal to 8 in decimal.
Explanation:
ANDing operation :
10101101 original bit pattern
00001000 AND mask
00001000 resulting bit pattern
The resulting value we get in this case is 8, i.e. the value of the second operand. The result turned out to be a 8 since the third bit of operand was ON. Had it been OFF, More >
What are the advantages of using pointers in a program?
Dec 9th
Pointers are special variables which store address of some other variables.
Syntax: datatype *ptr;
Here * indicates that ptr is a pointer variable which represents value stored at a particular address.
Example: int *p;
‘p’ is a pointer variable pointing to address location where an integer type is stored.
Advantages:
3. Pointers allow us to pass values to functions using call by reference. This is useful when large sized arrays are passed as arguments to functions. A function can return more than one value by using call by reference.
4. Dynamic allocation of memory is possible with the help of pointers.
5. We can resize data structures. For More >
What are storage memory, default value, scope and life of Static and External storage class?
Dec 9th
1. Static storage class:
Storage : main memory Default value : zero
Scope : local to the block in which the variable is defined
lifetime : till the value of the variable persists between different function calls.
2. External storage class:
Storage : main memory Default value : zero Scope : global
Lifetime : as long as the program execution doesn’t come to an end.
What are storage memory, default value, scope and life of Automatic and Register storage class?
Dec 9th
1. Automatic storage class:
Storage : main memory.
Default value : garbage value.
Scope : local to the block in which the variable is defined. Lifetime : till control remains within the block.
2. Register storage class:
Storage : cpu registers.
Default value : garbage value.
Scope : local to the block in which the variable is defined. Lifetime : till control remains within the block.
What are register variables? What are advantages of using register variables?
Dec 9th
Register variables are stored in the CPU registers. Its default value is a garbage value. Scope of a register variable is local to the block in which it is defined. Lifetime is till control remains within the block in which the register variable is defined.
Variable stored in a CPU register can always be accessed faster than the one that is stored in memory. Therefore, if a variable is used at many places in a program, it is better to declare its storage class as register
Example:
register int x=5;
Variables for loop counters can be declared as register. Note that register keyword may be More >
