Thursday, 15 August 2013

Tricky C questions

Tricky C questions
By admin | May 10, 2006

    How do you write a program which produces its own source code as its output?
    How can I find the day of the week given the date?
    Why doesn’t C have nested functions?
    What is the most efficient way to count the number of bits which are set in a value?
    How can I convert integers to binary or hexadecimal?
    How can I call a function, given its name as a string?
    How do I access command-line arguments?
    How can I return multiple values from a function?
    How can I invoke another program from within a C program?
    How can I access memory located at a certain address?
    How can I allocate arrays or structures bigger than 64K?
    How can I find out how much memory is available?
    How can I read a directory in a C program?
    How can I increase the allowable number of simultaneously open files?
    What’s wrong with the call fopen(”c:\newdir\file.dat”, “r”)?

what are storage-class specifier , auto/extern/static
How can you swap two integer variables without using a temporary?
1.12: What is sizeof('A') ?



A: The same as sizeof(int).  Character constants have type

 int in C.  (This is one area in which C++ differs.)

what is the return type of malloc.... it is void*
we need to typecast it to appropriate object.

char *p = malloc(n * sizeof(char));


How would you implement the va_arg() macro in

 <stdarg.h>?


1.24: State the declaration for a pointer to a function

 returning a pointer to char.



A: char *(*f)();

1.23: What do these declarations mean?



     int **a();

     int (*b)();

     int (*c[3])();

     int (*d)[10];



A: declare a as function returning pointer to pointer to int

 declare b as pointer to function returning int

 declare c as array of 3 pointers to functions returning int

 declare d as pointer to array of 10 ints



 The way to read these is "inside out," remembering that

 [] and () bind more tightly than *, unless overridden by

 explicit parentheses.


1.28: Consider these definitions:



     #define Push(val) (*stackp++ = (val))

     #define Pop() (*--stackp)



     int stack[100];

     int *stackp = stack;



 Now consider the expression



     Push(Pop() + Pop())



 1. What is the expression trying to do?  In what sort of

 program might such an expression be found?



 2. What are some deficiencies of this implementation?

 Under what circumstances might it fail?



A: The expression is apparently intended to pop two values

 from a stack, add them, and push the result.  This code

 might be found in a calculator program, or in the

 evaluation loop of the engine for a stack-based language.



 The implementation has at least four problems, however.

 The Push macro does not check for stack overflow; if more

 than 100 values are pushed, the results will be

 unpredictable.  Similarly, the the Pop macro does not

 check for stack underflow; an attempt to pop a value when

 the stack is empty will likewise result in undefined

 behavior.



 On a stylistic note, the stackp variable is global as far

 as the Push and Pop macros are concerned.  If it is

 certain that, in a particular program, only one stack

 will be used, this assumption may be a reasonable one,

 as it allows considerably more succinct invocations.

 If multiple stacks are a possibility, however, it might

 be preferable to pass the stack pointer as an argument

 to the Push and Pop macros.



 Finally, the most serious problem is that the "add"

 operation as shown above is *not* guaranteed to work!

 After macro expansion, it becomes



     (*stackp++ = ((*--stackp) + (*--stackp)))



 This expression modifies a single object more than once

 between sequence points; specifically, it modifies stackp

 three times.  It is not guaranteed to work; moreover,

 there are popular compilers (one is gcc) under which it

 *will* *not* work as expected.  (The extra parentheses

 do nothing to affect the evaluation order; in particular,

 they do not make it any more defined.)


how does printf works. what is the output of printf{"%d"'}


char *x = "abc";
char arr[] = {a,b,c};

sizeof(x) and sizeof(arr)

1.52: How can a header file be protected against being

 included multiple times (perhaps due to nested

 #include directives)?



A: The standard trick is to place lines like



     #ifndef headerfilename_H

     #define headerfilename_H



 at the beginning of the file, and an extra #endif at the

 end.


what is difference in header file and library
what is the return type of sizeof ... unsigned int or unsigned long int
what is memory leak...
malloc realloc... offset pointers


#define square(x) x*x
main()
{
int i;
i = 64/square(4);
printf("%d",i);
}

Answer:
64

Explanation:
the macro call square(4) will substituted by 4*4 so the expression becomes i = 64/4*4 . Since / and * has equal priority the expression will be evaluated as (64/4)*4 i.e. 16*4 = 64


Predict the output or error(s) for the following:

main()
{
int i;
printf("%d",scanf("%d",&i)); // value 10 is given as input here
}

Answer:
1

Explanation:
Scanf returns number of items successfully read and not 1/0. Here 10 is given as input which should have been scanned successfully. So number of items read is 1.

No comments:

Post a Comment