Home Other Languages C Basic C Questions - 13

Login Form




Basic C Questions - 13 Print E-mail

1. What is the easiest sorting method to use?

The answer is the standard library function qsort(). It’s the easiest sort by far for several reasons:

It is already written.
It is already debugged.
It has been optimized as much as possible (usually).

Void qsort(void *buf, size_t num, size_t size, int (*comp)(const void *ele1, const void *ele2));

 

2. Sample Code
char c = '\101';

What will the variable c contain in the code above?

  •  It will not compile.
  •  The four characters '\', '1', '0', '1'
  •  A backslash '\'.
  •  65 (which is the letter A in ASCII)
  •  101 (which is the letter e in ASCII)

 

3. Sample Code
f = fopen( fileName, "r" );

From the sample above, which one of the following statements will set the file position for the file f to cause the next byte read from the file to be the last byte?

  •  f = feof( f ) - 1;
  •  fsetpos( f, EOF - 1 );
  •  fseek( f, -1, SEEK_SET );
  •  fseek( f, -1, EOF );
  •  fseek( f, -1, SEEK_END );

 

4. When is a switch statement better than multiple if statements?

A switch statement is generally best to use when you have more than two conditional expressions based on a single variable of numeric type.

 

5. What is a static function?

A static function is a function whose scope is limited to the current source file. Scope refers to the visibility of a function or variable. If the function or variable is visible outside of the current source file, it is said to have global, or external, scope. If the function or variable is not visible outside of the current source file, it is said to have local, or static, scope.

 

6. What is the purpose of main( ) function?

The function main( ) invokes other functions within it.It is the first function to be called when the program starts execution.

- It is the starting function
- It returns an int value to the environment that called the program
- Recursive call is allowed for main( ) also.
- It is a user-defined function
- Program execution ends when the closing brace of the function main( ) is reached.
- It has two arguments 1)argument count and 2) argument vector (represents strings passed).
- Any user-defined name can also be used as parameters for main( ) instead of argc and argv

 

7. What is a pragma?

The #pragma preprocessor directive allows each compiler to implement compiler-specific features that can be turned on and off with the #pragma statement. For instance, your compiler might support a feature called loop optimization. This feature can be invoked as a command-line option or as a #pragma directive.

To implement this option using the #pragma directive, you would put the following line into your code:
#pragma loop_opt(on)

Conversely, you can turn off loop optimization by inserting the following line into your code:
#pragma loop_opt(off)

 

8. #include %lt;stdlib.h>
int f(TYPE x) {
/*Allocates an array of two integers; returns 0
if the allocation succeeded, 1 otherwise.
Returns the allocated array in x.
*/
int *ret = (int*)malloc(sizeof(int[2]));
return ret ? *x=ret, 0 : 1;
}
Referring to the sample code above, how should TYPE be declared?

  •  typedef int**TYPE;
  •  typedef int*TYPE;
  •  typedef int[]TYPE;
  •  typedef &(int*)TYPE;
  •  typedef int[]*TYPE;

 

9. Which one of the following is a true statement about an lvalue?

  •  An lvalue is the result of an arithmetic operation involving quantities of type long int.
  •  All lvalues can be used on the right side of an assignment statement.
  •  An lvalue is, by definition, the value appearing on the rightmost side of an assignment statement.
  •  By definition, an lvalue is the storage space indirectly referenced by a pointer.
  •  An lvalue is any quantity capable of appearing on the left side of a shift operator.

 

10. What is the heap?

The heap is where malloc(), calloc(), and realloc() get memory.
Getting memory from the heap is much slower than getting it from the stack. On the other hand, the heap is much more flexible than the stack. Memory can be allocated at any time and deallocated in any order. Such memory isn’t deallocated automatically; you have to call free().

Recursive data structures are almost always implemented with memory from the heap. Strings often come from there too, especially strings that could be very long at runtime. If you can keep data in a local variable (and allocate it from the stack), your code will run faster than if you put the data on the heap. Sometimes you can use a better algorithm if you use the heap faster, or more robust, or more flexible. It’s a tradeoff.

If memory is allocated from the heap, it’s available until the program ends. That’s great if you remember to deallocate it when you’re done. If you forget, it’s a problem. A memory leak is some allocated memory that’s no longer needed but isn’t deallocated. If you have a memory leak inside a loop, you can use up all the memory on the heap and not be able to get any more. (When that happens, the allocation functions return a null pointer.) In some environments, if a program doesn’t deallocate everything it allocated, memory stays unavailable even after the program ends.