Home Other Languages C Basic C Questions - 10

Login Form




Basic C Questions - 10 Print E-mail

1. Which one of the following statements regarding macros is INCORRECT?

  •  Macro definitions can contain expressions.
  •  Macro definitions cannot be altered at runtime.
  •  Macros can have parameters.
  •  Macros are evaluated in the preprocessor.
  •  Macros are evaluated at runtime.

 

2. What is a null pointer assignment error? What are bus errors, memory faults, and core dumps?

These are all serious errors, symptoms of a wild pointer or subscript.

Null pointer assignment is a message you might get when an MS-DOS program finishes executing. Some such programs can arrange for a small amount of memory to be available “where the NULL pointer points to (so to speak). If the program tries to write to that area, it will overwrite the data put there by the compiler.

When the program is done, code generated by the compiler examines that area. If that data has been changed, the compiler-generated code complains with null pointer assignment.

This message carries only enough information to get you worried. There’s no way to tell, just from a null pointer assignment message, what part of your program is responsible for the error. Some debuggers, and some compilers, can give you more help in finding the problem.

Bus error : core dumped and Memory fault: core dumped are messages you might see from a program running under UNIX. They’re more programmer friendly. Both mean that a pointer or an array subscript was wildly out of bounds. You can get these messages on a read or on a write. They aren’t restricted to null pointer problems.

The core dumped part of the message is telling you about a file, called core, that has just been written in your current directory. This is a dump of everything on the stack and in the heap at the time the program was running. With the help of a debugger, you can use the core dump to find where the bad pointer was used.

That might not tell you why the pointer was bad, but it’s a step in the right direction. If you don’t have write permission in the current directory, you won’t get a core file, or the core dumped message.

 

3. Which one of the following declarations ensures that ptr CANNOT be used to modify the string to which it points (although ptr itself may be changed to point to another string)?

  •  const char *ptr = "Hello";
  •  char *ptr = const "Hello";
  •  char * static ptr = "Hello";
  •  static char *ptr = "Hello";
  •  char * const ptr = "Hello";

 

4. What is a function and built-in function?

A large program is subdivided into a number of smaller programs or subprograms. Each subprogram specifies one or more actions to be performed for a large program.such subprograms are functions.

The function supports only static and extern storage classes.By default, function assumes extern storage class.functions have global scope. Only register or auto storage class is allowed in the function parameters. Built-in functions that predefined and supplied along with the compiler are known as built-in functions.They are also known as library functions.

 

5. Which of the following choices most accurately describes variable DEFINITION (as opposed to declaration)?

  •  The assignment of properties and storage space to a variable
  •  The identification of a variable that resides elsewhere in the program
  •  The assignment of storage space to a variable
  •  The assignment of properties to a variable
  •  The assignment of storage space to a variable whose properties have been specified external to the current file scope

 

6. How do you print an address?

The safest way is to use printf() (or fprintf() or sprintf()) with the %P specification. That prints a void pointer (void*). Different compilers might print a pointer with different formats. Your compiler will pick a format that’s right for your environment.
If you have some other kind of pointer (not a void*) and you want to be very safe, cast the pointer to a void*:
printf( %Pn, (void*) buffer );

 

7. Difference between arrays and pointers?

- Pointers are used to manipulate data using the address. Pointers use * operator to access the data pointed to by them
- Arrays use subscripted variables to access and manipulate data.Array variables can be equivalently written using pointer expression.

 

8. int x = 3;

if( x == 2 ); x = 0;
if( x == 3 ) x++;
else x += 2;

What value will x contain when the sample code above is executed?

  •  1
  •  2
  •  3
  •  4
  •  5

 

9. When does the compiler not implicitly generate the address of the first element of an array?

Whenever an array name appears in an expression such as
- array as an operand of the sizeof operator
- array as an operand of & operator
- array as a string literal initializer for a character array

Then the compiler does not implicitly generate the address of the address of the first element of an array.

 

10. Which one of the following best describes the Standard C convention regarding variables with names that start with an underscore (_)?

  •  They are reserved for usage by standards committees, system implementers, and compiler engineers.
  •  They are deprecated by Standard C and are permitted only for backward compatibility with older C libraries.
  •  Applications programmers are encouraged to employ them in their own code in order to mark certain symbols for internal usage.
  •  They are case-insensitive.
  •  They are generally treated differently by preprocessors and compilers from other identifiers.