Sunday, March 29, 2009

Are the parentheses in a return statement mandatory?

Answer: No. The formal syntax of a return statement is

return expression ;

But it's legal to put parentheses around any expression, of course, whether they're needed or not.

Thursday, March 19, 2009

‘C’ allows arrays of greater than two dimensions, who will determined this

a. Programmer
b. Compiler
c. Parameter
d. None of these

Converting Hex Value To Decimal & Vice Versa - Simple Logic

Theory works like this:

* Consider a number (say 15482) in decimal system
* This is equivalent to (1 * 10000) + (5 * 1000) + (4 * 100) + (8 * 10) + 2
* This is equivalent to (1 * 10^4) + (5 * 10^3) + (4 * 10^2) + (8 * 10^1) + (2 * 10^0)
where ^ is the power-of operator.

* Now consider a number (say 12F3B) in hexadecimal system
* Using similar logic, this is equivalent to (1 * 16^4) + (2 * 16^3) + (F * 16^2) + (3 * 16^1) + (B * 16^0)

Now all you have to do is compute 16^4, 16^3 etc. and also take F=15 and B=11 and there you have it... the answer in decimal

Note that you can use this logic to convert a number from *ANY* base into decimal.

Sunday, March 15, 2009

How can you swap two integer variables without using a temporary variable?

A: The reason that this question is poor is that the answer ceased to be interesting when we came down out of the trees and stopped using assembly language.

The "classic" solution, expressed in C, is

a ^= b;
b ^= a;
a ^= b;

Due to the marvels of the exclusive-OR operator, after these three operations, a's and b's values will be swapped.

However, it is exactly as many lines, and (if we can spare one measly word on the stack) is likely to be more efficient, to write the obvious

int t = a;
a = b;
b = t;

No, this doesn't meet the stipulation of not using a temporary. But the whole reason we're using C and not assembly language (well, one reason, anyway) is that we're not interested in keeping track of how many registers we have.

If the processor happens to have an EXCH instruction, the compiler is more likely to recognize the possibility of using it if we use the three-assignment idiom, rather than the three-XOR.

By the way, the even more seductively concise rendition of the "classic" trick in C, namely

a ^= b ^= a ^= b

is, strictly speaking, undefined, because it modifies a twice between sequence points. Also, if an attempt is made to use the idiom (in any form) in a function which is supposed to swap the locations pointed to by two pointers, as in

swap(int *p1, *p2)
{
*p1 ^= *p2;
*p2 ^= *p1;
*p1 ^= *p2;
}

then the function will fail if it is ever asked to swap a value with itself, as in

swap(&a, &a);

or

swap(&a[i], &a[j]);

when i == j. (The latter case is not uncommon in sorting algorithms. The effect when p1 == p2 is that the pointed- to value is set to 0.)

Monday, March 9, 2009

Declaring structure pointer

Suppose that you declare

struct x *xp;

without any definition of struct x. Is this legal? Under what circumstances would it be useful?

Answer: It is perfectly legal to refer to a structure which has not been "fleshed out," as long as the compiler is never asked to compute the size of the structure or generate offsets to any members. Passing around pointers to otherwise undefined structures is quite acceptable, and is a good way of implementing "opaque" data types in C.

What's the difference between these three declarations?

char *a = "abc";
char b[] = "abc";
char c[3] = "abc";

Answer:The first declares a pointer-to-char, initialized to point to a four-character array somewhere in (possibly read-only) memory containing the four characters a b c \0. The second declares an array (a writable array) of 4 characters, initially containing the characters a b c \0. The third declares an array of 3 characters, initially containing a b c. (The third array is therefore not an immediately valid string.)

Thursday, March 5, 2009

The following code should add a new element to the beginning of a linked list

The pointer head should point to the start of the list.
It is called as: Push(head, 192);

1: void Push(struct node* head, int data) {
2: struct node* newNode = malloc(sizeof(struct node));
3: newNode->data = data;
4: newNode->next = head;
5: head = newNode;
6: }

Given the scenario described above, what changes need to be made to the code in order for it to work as intended?
Choice a: Line 2 should be: struct node* newNode = malloc(sizeof(struct *node)); and change head in lines 4 and 5 to *head.
Choice b: Line 1 should be: void Push(struct node** head, int data) and change head in lines 4 and 5 to *head.
Choice c: The node struct needs to be created within the Push function prior to being used.
Choice d: Line 1 should be: void Push(struct node** head, int data).
Choice e: When calling the function, the value needs to be 287 or less due to the type specified in the function definition.