Home Other Languages C Basic C Questions - 14

Login Form




Basic C Questions - 14 Print E-mail

1. What is storage class and what are storage variable ?

A storage class is an attribute that changes the behavior of a variable. It controls the lifetime, scope and linkage.
There are five types of storage classes

  •  auto
  •  static
  •  extern
  •  register
  •  typedef

 

2. What is indirection?

If you declare a variable, its name is a direct reference to its value. If you have a pointer to a variable, or any other object in memory, you have an indirect reference to its value.

 

3. How can I convert a string to a number?

The standard C library provides several functions for converting strings to numbers of all formats (integers, longs, floats, and so on) and vice versa.
The following functions can be used to convert strings to numbers:

Function Name Purpose

atof() Converts a string to a double-precision floating-point value.
atoi() Converts a string to an integer.
atol() Converts a string to a long integer.

strtod() Converts a string to a double-precision floating-point value and reports any leftover numbers that could not be converted.
strtol() Converts a string to a long integer and reports any leftover numbers that could not be converted.
strtoul() Converts a string to an unsigned long integer and reports any leftover numbers that could not be converted.

 

4. What is a pointer value and address?

A pointer value is a data object that refers to a memory location. Each memory locaion is numbered in the memory. The number attached to a memory location is called the address of the location.

 

5. short testarray[4][3] = { {1}, {2, 3}, {4, 5, 6} };
    printf( "%d\n", sizeof( testarray ) );

Assuming a short is two bytes long, what will be printed by the above code?

  •  It will not compile because not enough initializers are given.
  •  6
  •  7
  •  12
  •  24

 

6. Sample Code

#define X(t,m) (size_t)((char*)&((t*)0)->m-(char*)(t*)0)

Consider the poorly named macro X(), defined above. Of which one of the following Standard C macros could X() be an implementation?

  •  va_start()
  •  putchar()
  •  va_arg()
  •  offsetof()
  •  feof_unlocked()

 

7. What is #line used for?


The #line preprocessor directive is used to reset the values of the _ _LINE_ _ and _ _FILE_ _ symbols, respectively. This directive is commonly used in fourth-generation languages that generate C language source files.

 

8. When should a far pointer be used?

Sometimes you can get away with using a small memory model in most of a given program. There might be just a few things that don’t fit in your small data and code segments. When that happens, you can use explicit far pointers and function declarations to get at the rest of memory. A far function can be outside the 64KB segment most functions are shoehorned into for a small-code model. (Often, libraries are declared explicitly far, so they’ll work no matter what code model the program uses.) A far pointer can refer to information outside the 64KB data segment. Typically, such pointers are used with farmalloc() and such, to manage a heap separate from where all the rest of the data lives. If you use a small-data, large-code model, you should explicitly make your function pointers far.

 

9. How are portions of a program disabled in demo versions?

If you are distributing a demo version of your program, the preprocessor can be used to enable or disable portions of your program. The following portion of code shows how this task is accomplished, using the preprocessor directives #if and #endif:


int save_document(char* doc_name)
{
#if DEMO_VERSION
printf(Sorry! You can’t save documents using the DEMO version of this program!n);
return(0);
#endif
...
}

 

10. Which one of the following Standard C functions can be used to sort a string array?

  •  qsort
  •  sort
  •  quicksort
  •  asort
  •  There is no Standard C function for such a sort.