C Inetrview Question and Answer Set-1

| Thursday 9 June 2011

Declarations and Initializations

1.1: How do you decide which integer type to use?

Use ``short'' when you need to avoid values over 32,767, ``int'' when you want to store integers, ``long'' for long numbers (more than 6 digits), and ``float'' for numbers over 4 billion.

1.2: What should the 64-bit type on new, 64-bit machines be?

int.

1.3: If I write the code int i, j; can I assume that (&i + 1) == &j?

Only sometimes. It's not portable, because in EBCDIC, i and j are not adjacent.
1.4: What's the best way to declare and define global variables?
In headers; this way, you can get link errors when you include the same header twice. Generally, you will have to define a variable everywhere you want to use it, and then declare it someplace so you know what it is.

1.5: What does extern mean in a function declaration?

It refers to a variable which is not actually in your program. For instance,
main() {
        extern int bar;
        printf("%d\n", bar);
        return 0;
}
Will compile without errors because bar is declared as being external. (It won't run, though, because you never assign bar a value.)

1.6: I finally figured out the syntax for declaring pointers to functions, but now how do I initialize one?

With the assignment operator. You were perhaps expecting a screwdriver?

1.7: I've seen different methods used for calling through pointers to functions. What's the story?

In the old days, when Microsoft first invented C, the syntax for calling functions involved more parentheses; this was after their market research indicated that most C programmers would be coming from a Lisp environment. Later, when Kernighan took over the language design (right after AT&T bought Microsoft's language technology), he decided to eliminate the parentheses, but the old form is still allowed.
You do need the parentheses to call a function with more than one argument, for instance,
int (*foo)(char *, ...) = printf;
(*foo)("hello, %s\n", "world!");
needs the parens, but they would not be needed for
foo, "hello, world!\n";
(The ``*'' just means to execute foo, just like the ``*'' on the end of an executable filename in ``ls -F''.)

1.8: What's the auto keyword good for?

Declaring vehicles.

1.9: I can't seem to define a linked list successfully. I tried

        typed struct {
               char *item;
               NODEPTR next;
        } *NODEPTR;
but the compiler gave me error messages. Can't a structure in C contain a pointer to itself?
Not exactly; it can contain a pointer to another structure of the same type. Try:
typedef struct {
        char *item;
        double *next;
} NODEFAKE;
typedef struct {
        char *item;
        NODEFAKE *next;
} NODEPTR;
Make sure that sizeof(NODEPTR) == sizeof(double).
This technique is called a ``backwards reference''.

1.10: How do I enter values using hexadecimal?

long ints can be entered using hexadecimal notation; for instance,
long int foo = 07;
sets foo to hex 7.

1.11: How do I declare an array of N pointers to functions returning pointers to functions returning pointers to characters?

Well, first you need to know how to declare an array of N items of type T - that's
T foo[N];
Now you need to look at how to declare a pointer to function returning something, say, an object of type S. That's like this:
S (*bar)();
Now assume that S is ``pointer to function returning pointer to char''. We get
(char *) (*)() (*bar)().
So, the whole thing turns out to be (with appropriate parentheses)
(((char)(*))((*)())(((*)((foo)))())([(N)]));
If your compiler complains, break this down into subexpressions.
To call it, just use
foo[i]();
This works because, in C, declaration reflects use, but it's one of those weird distorted mirrors.

0 comments:

Post a Comment

Popular Posts

Company Placement Papers

 

Copyright © 2010 All Question Papers Blogger Template by Dzignine