Home Other Languages C Basic C Questions - 11

Login Form




Basic C Questions - 11 Print E-mail

1. What is the difference between a string copy (strcpy) and a memory copy (memcpy)? When should each be used?

The strcpy() function is designed to work exclusively with strings. It copies each byte of the source string to the destination string and stops when the terminating null character () has been moved. On the other hand, the memcpy() function is designed to work with any type of data. Because not all data ends with a null character, you must provide the memcpy() function with the number of bytes you want to copy from the source to the destination.

 

2. How can you avoid including a header more than once?

One easy technique to avoid multiple inclusions of the same header is to use the #ifndef and #define
preprocessor directives. When you create a header for your program, you can #define a symbolic name that is unique to that header. You can use the conditional preprocessor directive named #ifndef to check whether that symbolic name has already been assigned. If it is assigned, you should not include the header, because it has already been preprocessed. If it is not defined, you should define it to avoid any further inclusions of the header.

The following header illustrates this technique:

#ifndef _FILENAME_H
#define _FILENAME_H
#define VER_NUM 1.00.00
#define REL_DATE 08/01/94
#if _ _WINDOWS_ _
#define OS_VER WINDOWS
#else
#define OS_VER DOS
#endif
#endif


When the preprocessor encounters this header, it first checks to see whether _FILENAME_H has been defined. If it hasn’t been defined, the header has not been included yet, and the _FILENAME_H symbolic name is defined. Then, the rest of the header is parsed until the last #endif is encountered, signaling the end of the conditional #ifndef _FILENAME_H statement. Substitute the actual name of the header file for FILENAME in the preceding example to make it applicable for your programs.

 

3. Which one of the following will read a character from the keyboard and will store it in the variable c?

  •  c = getchar();
  •  c = getchar( stdin );
  •  getchar( &c )
  •  getc( &c );
  •  c = getc();

 

4. Sample Code
void listFile( FILE *f )
{
int c;
while( c = fgetc( f ) != EOF )
{
printf( "%d", c );
}
printf( "\n" );
}

What will be printed when the function above is called with a pointer to an open file that contains the three characters abc?

  •  111
  •  000
  •  656667
  •  abc
  •  The characters ab followed by an infinite number of c's

 

5. What are the advantages of the functions?

- Debugging is easier
- It is easier to understand the logic involved in the program
- Testing is easier
- Recursive call is possible
- Irrelevant details in the user point of view are hidden in functions
- Functions are helpful in generalizing the program

 

6. What is a method?

Method is a way of doing something, especially a systematic way; implies an orderly logical arrangement (usually in steps).

 

7. When should a type cast not be used?

A type cast should not be used to override a const or volatile declaration. Overriding these type modifiers can cause the program to fail to run correctly.
A type cast should not be used to turn a pointer to one type of structure or data type into another. In the rare events in which this action is beneficial, using a union to hold the values makes the programmer’s intentions clearer.

 

8. Which one of the following will declare a pointer to an integer at address 0x200 in memory?

  •  int *x = &0x200;
  •  int *x = *0x200;
  •  int *x( &0x200 );
  •  int *x;
  • *x = 0x200;
  •  int *x = 0x200;

 

9. Sample Code #include
int ab=1;
int a=2;
int main(){
int b=3;
int ab=4;
printf("%i/*%i*/%i",a,b,ab);
}

Refer to the above sample code. What will be printed?

  •  21
  •  0/*4*/1
  •  01
  •  %i/*%i*/%i041
  •  2/*3*/4

 

10. What is Preprocessor?

The preprocessor is used to modify your program according to the preprocessor directives in your source code. Preprocessor directives (such as #define) give the preprocessor specific instructions on how to modify your source code. The preprocessor reads in all of your include files and the source code you are compiling and creates a preprocessed version of your source code. This preprocessed version has all of its macros and constant symbols replaced by their corresponding code and value assignments. If your source code contains any conditional preprocessor directives (such as #if), the preprocessor evaluates the condition and modifies your source code accordingly.


The preprocessor contains many features that are powerful to use, such as creating macros, performing conditional compilation, inserting predefined environment variables into your code, and turning compiler features on and off. For the professional programmer, in-depth knowledge of the features of the preprocessor can be one of the keys to creating fast, efficient programs.