|
1. Sample Code file inc.h contains only the following line: struct x {int d; char c;} file main.c contains only the three following lines: #include #include "inc.h" main(int n, char**a) { exit(0); } Refer to the above sample code. The program is supposed to be Standard C. What is the problem with main in this code? - main does not return a value.
- The parameters of main must be named argc and argv.
- main is missing a return type.
- The parameters of main are not used.
- The type of the second parameter of main is invalid.
2. Sample Code #include static double (*funcs[])( double ) = { sin, cos, tan, asin, acos, atan, sinh, cosh, tanh }; double computeTrigFunction( int index, double argument ) { return ????; } Referring to the sample code above that should compute the value of a trigonometric function based on its index, what would be a replacement for the ???? to make the correct call? - *funcs[ index ]( argument )
- (*funcs)[ index ]( argument )
- funcs(argument)[index]
- *(funcs[index](argument))
- funcs[index](argument)
3. How do you redirect a standard stream? Most operating systems, including DOS, provide a means to redirect program input and output to and from different devices. This means that rather than your program output (stdout) going to the screen; it can be redirected to a file or printer port. Similarly, your program’s input (stdin) can come from a file rather than the keyboard. In DOS, this task is accomplished using the redirection characters, <>. For example, if you wanted a program named PRINTIT.EXE to receive its input (stdin) from a file named STRINGS.TXT, you would enter the following command at the DOS prompt: C:> PRINTIT <) tells DOS to take the strings contained in STRINGS.TXT and use them as input for the PRINTIT program. The following example would redirect the program’s output to the prn device, usually the printer attached on LPT1: C :> REDIR > PRN
Alternatively, you might want to redirect the program’s output to a file, as the following example shows:
C :> REDIR > REDIR.OUT In this example, all output that would have normally appeared on-screen will be written to the file REDIR.OUT.
Redirection of standard streams does not always have to occur at the operating system. You can redirect a standard stream from within your program by using the standard C library function named freopen(). For example, if you wanted to redirect the stdout standard stream within your program to a file named OUTPUT.TXT, you would implement the freopen() function as shown here: ... freopen(output.txt, w, stdout); ... Now, every output statement (printf(), puts(), putch(), and so on) in your program will appear in the file OUTPUT.TXT.
4. What is a void pointer? A void pointer is a C convention for a raw address. The compiler has no idea what type of object a void Pointer really points to. If you write int *ip; ip points to an int. If you write void *p; p doesn’t point to a void! In C and C++, any time you need a void pointer, you can use another pointer type. For example, if you have a char*, you can pass it to a function that expects a void*. You don’t even need to cast it. In C (but not in C++), you can use a void* any time you need any kind of pointer, without casting. (In C++, you need to cast it). A void pointer is used for working with raw memory or for passing a pointer to an unspecified type. Some C code operates on raw memory. When C was first invented, character pointers (char *) were used for that. Then people started getting confused about when a character pointer was a string, when it was a character array, and when it was raw memory. 5. Sample Code void printTime( time_t *t ) { ???? } Which one of the following can replace the ???? in the code above to print the time passed in t in human-readable form? - char s[ 100 ];
ctime( t, s ); printf( "%s\n", s ); - printf( "%s\n", ctime( t ) );
- printf( "%s\n", asctime( t ) );
- printf( "%s", t );
- char *s = ctime( t );
printf( "%s\n", s ); free( s );
6. Sample Code int my_func( int *a ); int main() { int b[3]; my_func( ???? ); return 0; } Referring to the sample code above, which one of the following must be inserted in place of the ???? to pass the array b to my_func? - (int *)b[0]
- b
- (int *)*b
- (*b)[0]
- *b
7. Which one of the following code fragments causes k to contain the value 16 at some point? - int j = 14;
int k = 1; k += j+; - int k;
**((int **) &k) = 16; - int j;
int k = 4; j = k <<>> 11; k += j; - int j, k;
j = 2 << k =" j" j =" 256;" k =" 14;" k =" j">
8. Sample Code int i = 4; switch (i) { default: ; case 3: i += 5; if ( i == 8) { i++; if (i == 9) break; i *= 2; } i -= 4; break; case 8: i += 5; break; } printf("i = %d\n", i); What will the output of the sample code above be? - i = 5
- i = 8
- i = 9
- i = 10
- i = 18
9. How can type-insensitive macros be created? A type-insensitive macro is a macro that performs the same basic operation on different data types. This task can be accomplished by using the concatenation operator to create a call to a type-sensitive function based on the parameter passed to the macro. The following program provides an example: #include #define SORT(data_type) sort_ ## data_type void sort_int(int** i); void sort_long(long** l); void sort_float(float** f); void sort_string(char** s); void main(void);
void main(void) { int** ip; long** lp; float** fp; char** cp; ... sort(int)(ip); sort(long)(lp); sort(float)(fp); sort(char)(cp); ... } This program contains four functions to sort four different data types: int, long, float, and string (notice that only the function prototypes are included for brevity). A macro named SORT was created to take the data type passed to the macro and combine it with the sort_ string to form a valid function call that is appropriate for the data type being sorted. Thus, the string sort(int)(ip); translates into sort_int(ip); after being run through the preprocessor. 10. Array is an lvalue or not? An lvalue was defined as an expression to which a value can be assigned. Is an array an expression to which we can assign a value? The answer to this question is no, because an array is composed of several separate array elements that cannot be treated as a whole for assignment purposes. The following statement is therefore illegal: int x[5], y[5]; x = y; Additionally, you might want to copy the whole array all at once. You can do so using a library function such as the memcpy() function, which is shown here: memcpy(x, y, sizeof(y)); It should be noted here that unlike arrays, structures can be treated as lvalues. Thus, you can assign one structure variable to another structure variable of the same type, such as this: typedef struct t_name { char last_name[25]; char first_name[15]; char middle_init[2]; } NAME; ... NAME my_name, your_name; ... your_name = my_name;
|