Funny C Interview Questions

Funny C Interview Questions
1.Write a C program which prints Hello World! without using a semicolon?

a) #include
int main()
2. Write a small C program to determine whether a machine's type is little-endian or big-endian.?
Big endian order:-Here the “big end” (most significant value in the sequence) is stored first, at the lowest storage address. The most significant byte is stored in the leftmost position
int main()
unsigned int number = 10;
char *ptr;
ptr = (char *)&number;
Printf(“little-endian \n”);
Printf(“big-endian \n”);
return 0;
3. What is the format specifiers for printf to print double and float values?
a)printf(“%lf , %f”);
%lf - double
%f - float
4. What is the difference between memcpy and memmove?
memcpy memmove
Takes 3 arguments a Void *ptr, const void *cptr , and an int n. Takes 3 arguments a Void *ptr, const void *cptr , and an int.
Copies ‘n’ characters from const void ptr cptr to void ptr ptr and returns void ptr. Copies ‘n’ characters from const void ptr cptr to void ptr ptr and returns void ptr.
Not reliable Reliable when an objects cptr and ptr overlap.
4. Write a C program to find the smallest of three integers, without using any of the comparision operators.
a) #include
int main() {
int x = 2;
int y = 1;
int z = 3;
int r;
r = y + ((x - y) & ((x - y) >> (sizeof(int) * CHAR_BIT - 1)));
r = z + ((r - z) & ((r - z) >> (sizeof(int) * CHAR_BIT - 1)));
printf("%d\n", r);
5.What does the format specifier %n of printf function do?
a)Tthe format specifier “ %n” will give the number of characters actually formatted in the printf
Int num = 10;
Char *a= “helloworld”;
Printf(“ %3x,%n ” ,num, &a);
Printf(“%d no of bytes are formatted here into hex”,a);
6. What's the difference between the following two C statements?
const char *p;
char* const p;
a) const char *p; p is a pointer to a constant character;
b) char* const p; p is a constant character pointer;
both are same.
7. How do you print I can print % using the printf function?
a) using %% in the format specifier.
Printf(“%d %% %d” ,a ,b);
Output is a % b;
8.Whats the difference between bitwise and logical operators?
Bitwise Example:
if ((val==1)|(val==2)) printf("%d\n",val);
else printf("False");??
a)bitwise operators are operated on bits. for a bitwise OR operator, both of the left hand side and right hand side values/expressions are evaluated. then the 'evaluated values' are ORed together.
int a = 33, b = 40, c;
c = a | b;
33 => 100001 a
40 => 101000 b
------------ OR
41 => 101001 c
on the other hand, if expressions are used as the operands of bitwise operator OR:
int val = 2, a;
a = (val == 1) | (val == 2);
printf("%d\n", a);
then the output for the above code is always 1 if val is either 1 or 2, and the output is 0 otherwise. because, in the above case, (val == 1) is evaluated. if this is true, then (val==1) is considered 1, otherwise 0. similarly, (val == 2) is evaluated and is considered 1 or 0 depending if it is true or false. then these two 0 or 1 are ORed together.
logical operators work a little bit different way. say, the code is
(val==1) || (val==2)
the above is a logical expression, whose value is either true or false. to evaluate the above expression, first (val==1) is checked. if this is found true, then the entire expression become true and no further checking is required to check whether (val==2) or not, computer skips checking the right hand side, since they are ORed. if (val==1) is found false, then the value of the expression depends on whether (val==2) is true or false.

similarly, for logical AND operator,

(a==1) && (b==2)

first (a==1) is evaluated. if this is found false, then the entire expression become false and computer skips checking (b==2), since they are ANDed together. if (a==1) is found true, then the checking (b==2) is required to evaluate the whole expression.

so use logical operators when "if this AND that" or "if this OR that" kind of checking is required. go for the bitwise operator when u really need a new bit stream. remember, logical operators skip evaluating in certain conditions where bitwise operators always evaluate both sides.

No comments:

Top Blogs