Home Other Languages C Basic C Questions - 17

Login Form




Basic C Questions - 17 Print E-mail

1. What are the advantages of auto variables?

  • The same auto variable name can be used in different blocks
  • There is no side effect by changing the values in the blocks
  • The memory is economically used
  • Auto variables have inherent protection because of local scope

 

2. How many levels deep can include files be nested?

Even though there is no limit to the number of levels of nested include files you can have, your compiler might run out of stack space while trying to include an inordinately high number of files. This number varies according to your hardware configuration and possibly your compiler.

 

3. Sample Code
time_t currentTime = time( NULL );
printf("%s\n", ???? );

Which one of the following can replace ???? in order for the above sample code to print out the current time as a human-readable string?

  •  ctime(¤tTime)
  •  asctime(¤tTime)
  •  asctime(currentTime)
  •  timestr(currentTime)
  •  strtime(currentTime)

 

4. What is the difference between far and near?

Some compilers for PC compatibles use two types of pointers. near pointers are 16 bits long and can address a 64KB range. far pointers are 32 bits long and can address a 1MB range.

Near pointers operate within a 64KB segment. There’s one segment for function addresses and one segment for data. far pointers have a 16-bit base (the segment address) and a 16-bit offset. The base is multiplied by 16, so a far pointer is effectively 20 bits long. Before you compile your code, you must tell the compiler which memory model to use. If you use a smallcode memory model, near pointers are used by default for function addresses.
That means that all the functions need to fit in one 64KB segment. With a large-code model, the default is to use far function addresses. You’ll get near pointers with a small data model, and far pointers with a large data model. These are just the defaults; you can declare variables and functions as explicitly near or far.

Far pointers are a little slower. Whenever one is used, the code or data segment register needs to be swapped out. far pointers also have odd semantics for arithmetic and comparison. For example, the two far pointers in the preceding example point to the same address, but they would compare as different! If your program fits in a small-data, small-code memory model, your life will be easier.

 

5. Sample Code
int x = 0;

for ( ; ; )
{

if (x++ == 4) break;
continue;
}

printf("x=%d\n", x);

What will be printed when the sample code above is executed?

  •  x=0
  •  x=1
  •  x=4
  •  x=5
  •  x=6

 

6. How can you determine the maximum value that a numeric variable can hold? Which expression always return true? Which always return false?

  • expression if (a=0) always return false
  • expression if (a=1) always return true

 

7. Why should I prototype a function?

A function prototype tells the compiler what kind of arguments a function is looking to receive and what kind of return value a function is going to give back. This approach helps the compiler ensure that calls to a function are made correctly and that no erroneous type conversions are taking place.

 

8. How can I sort a linked list?

Both the merge sort and the radix sort are good sorting algorithms to use for linked lists.

 

9. Sample Code
int a[5] = {1, 2, 3, 4, 5};
int *aPtr;
aPtr = a;
printf("element=%d\n", *(aPtr + 2));

What will be printed when the sample code above is executed?

  •  element=1
  •  element=2
  •  element=3
  •  element=4
  •  element=5

 

10. What is the quickest searching method to use?

A binary search, such as bsearch() performs, is much faster than a linear search. A hashing algorithm can provide even faster searching. One particularly interesting and fast method for searching is to keep the data in a digital trie. A digital trie offers the prospect of being able to search for an item in essentially a constant amount of time, independent of how many items are in the data set.


A digital tree combines aspects of binary searching, radix searching, and hashing. The term digital trie refers to the data structure used to hold the items to be searched. It is a multilevel data structure that branches N ways at each level.