C FAQS
Q:Which bit wise operator is suitable for checking
whether a particular bit is on or off?
Ans:The bitwise AND operator. Here is an example:enum {
KBit0 = 1,
KBit1,
…
KBit31,
};
if ( some_int & KBit24 )
printf ( “Bit number 24 is ON\n” );
else
printf ( “Bit number 24 is OFF\n” );
Q:Which bit wise operator is suitable for turning off a particular bit in a
number?
Ans:The bitwise AND operator, again. In the following code snippet, the
bit number 24 is reset to zero.
some_int = some_int & ~KBit24;
Q:Which bit wise operator is suitable for putting on a particular bit in a
number?
Ans:The bitwise OR operator. In the following code snippet, the bit number
24 is turned ON:
some_int = some_int | KBit24;
Q:Does there exist any other function which can be used to convert an
integer or a float to a string?
Ans:Some implementations provide a nonstandard function called itoa(),
which converts an integer to string.
#include
char *itoa(int value, char *string, int radix);
DESCRIPTION
The itoa() function constructs a string representation of an integer.
PARAMETERS
value:
Is the integer to be converted to string representation.
string:
Points to the buffer that is to hold resulting string.
The resulting string may be as long as seventeen bytes.
radix:
Is the base of the number; must be in the range 2 - 36.
A portable solution exists. One can use sprintf():
char s[SOME_CONST];
int i = 10;
float f = 10.20;
sprintf ( s, “%d %f\n”, i, f );
Q:Why does malloc(0) return valid memory address ? What's the use ?
Ans:malloc(0) does not return a non-NULL under every
implementation.
An implementation is free to behave in a manner it finds
suitable, if the allocation size requested is zero. The
implmentation may choose any of the following actions:
* A null pointer is returned.
* The behavior is same as if a space of non-zero size
was requested. In this case, the usage of return
value yields to undefined-behavior.
Q:Notice, however, that if the implementation returns a non-NULL value for
a request of a zero-length space, a pointer to object of ZERO length is
returned! Think, how an object of zero size should be represented?
Ans:For implementations that return non-NULL values, a typical usage is as
follows:
void
func ( void )
{
int *p; /* p is a one-dimensional array,
whose size will vary during the
the lifetime of the program */
size_t c;
p = malloc(0); /* initial allocation */
if (!p)
{
perror (”FAILURE” );
return;
}
/* … */
while (1)
{
c = (size_t) … ; /* Calculate allocation size */
p = realloc ( p, c * sizeof *p );
/* use p, or break from the loop */
/* … */
}
return;
}
Notice that this program is not portable, since an implementation
is free to return NULL for a malloc(0) request, as the C Standard
does not support zero-sized objects.
Q:What are the different storage classes in C?
Ans:C has three types of storage: automatic, static and allocated.
Variable having block scope and without static specifier have automatic
storage duration.
Variables with block scope, and with static specifier have static scope.
Global variables (i.e, file scope) with or without the the static specifier
also have static scope.
Memory obtained from calls to malloc(), alloc() or realloc() belongs to
allocated storage class.
Q:What is the difference between strings and character arrays?
Ans:A major difference is: string will have static storage duration,
whereas as a character array will not, unless it is explicity specified by
using the static keyword.
Actually, a string is a character array with following properties:
* the multibyte character sequence, to which we generally call string, is used
to initialize an array of static storage duration. The size of this array is
just sufficient to contain these characters plus the terminating NUL
character.
* it not specified what happens if this array, i.e., string, is modified.
* Two strings of same value[1] may share same memory area. For example, in the
following declarations:
char *s1 = “Calvin and Hobbes”;
char *s2 = “Calvin and Hobbes”;
the strings pointed by s1 and s2 may reside in the same memory location. But,
it is not true for the following:
char ca1[] = “Calvin and Hobbes”;
char ca2[] = “Calvin and Hobbes”;
[1] The value of a string is the sequence of the values of the contained
characters, in order.
Q:Write down the equivalent pointer expression for referring the same
element a[i][j][k][l]?
Ans:a[i] == *(a+i)
a[i][j] == *(*(a+i)+j)
a[i][j][k] == *(*(*(a+i)+j)+k)
a[i][j][k][l] == *(*(*(*(a+i)+j)+k)+l)
Q:Which bit wise operator is suitable for checking whether a particular bit
is on or off?
Ans:The bitwise AND operator. Here is an example:enum {
KBit0 = 1,
KBit1,
…
KBit31,
};
if ( some_int & KBit24 )
printf ( “Bit number 24 is ON\n” );
else
printf ( “Bit number 24 is OFF\n” );
Q:Which bit wise operator is suitable for turning off a particular bit in a
number?
Ans:The bitwise AND operator, again. In the following code snippet, the
bit number 24 is reset to zero.
some_int = some_int & ~KBit24;