Home Other Languages C Basic C Questions - 8

Interview Dates

« < September 2010 > »
S M T W T F S
29 30 31 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 1 2
Subscribe to cyberqbank.com

Powered by
in.groups.yahoo.com

Login Form




Basic C Questions - 8 Print E-mail

1. When should the register modifier be used? Does it really help?

The register modifier hints to the compiler that the variable will be heavily used and should be kept in the CPU’s registers, if possible, so that it can be accessed faster.

There are several restrictions on the use of the register modifier.


First, the variable must be of a type that can be held in the CPU’s register. This usually means a single value of a size less than or equal to the size of an integer. Some machines have registers that can hold floating-point numbers as well.


Second, because the variable might not be stored in memory, its address cannot be taken with the unary & operator. An attempt to do so is flagged as an error by the compiler. Some additional rules affect how useful the register modifier is. Because the number of registers is limited, and because some registers can hold only certain types of data (such as pointers or floating-point numbers), the number and types of register modifiers that will actually have any effect are dependent on what machine the program will run on. Any additional register modifiers are silently ignored by the compiler.
Also, in some cases, it might actually be slower to keep a variable in a register because that register then becomes unavailable for other purposes or because the variable isn’t used enough to justify the overhead of loading and storing it.


So when should the register modifier be used? The answer is never, with most modern compilers. Early C compilers did not keep any variables in registers unless directed to do so, and the register modifier was a valuable addition to the language. C compiler design has advanced to the point, however, where the compiler will usually make better decisions than the programmer about which variables should be stored in registers.
In fact, many compilers actually ignore the register modifier, which is perfectly legal, because it is only a hint and not a directive.

 

2. Which one of the following functions is the Standard C functional equivalent of the AT&T Unix function rindex(), which returns a pointer to the last occurrence of a character in a string or NULL if no such character exists?

  •  strrchr()
  •  stridx()
  •  strcspn()
  •  strchr()
  •  strridx()

 

3. Differentiate between a linker and linkage?

A linker converts an object code into an executable code by linking together the necessary build in functions. The form and place of declaration where the variable is declared in a program determine the linkage of variable.

 

4. How do you declare a constant pointer to a constant string?

  •  const* char const p;
  •  const const char* p;
  •  char const const* p;
  •  const char const* p;
  •  const char* const p;

 

5. Can the sizeof operator be used to tell the size of an array passed to a function?

No. There’s no way to tell, at runtime, how many elements are in an array parameter just by looking at the array parameter itself. Remember, passing an array to a function is exactly the same as passing a pointer to the first element.

Is using exit() the same as using return?

No. The exit() function is used to exit your program and return control to the operating system. The return statement is used to return from a function and return control to the calling function. If you issue a return from the main() function, you are essentially returning control to the calling function, which is the operating system. In this case, the return statement and exit() function are similar.

 

6. How do you include a system header file called sysheader.h in a C source file?

  •  #includefile
  •  #include sysheader.h
  •  #incl "sysheader.h"
  •  #include
  •  #incl

 

7. Which one of the following is a valid variable name?

  •  Account_Number
  •  Account Number
  •  "Account Number"
  •  Account^Number
  •  Account-Number

 

8. What is the difference between NULL and NUL?

NULL is a macro defined in for the null pointer.

NUL is the name of the first character in the ASCII character set. It corresponds to a zero value. There’s no standard macro NUL in C, but some people like to define it.

The digit 0 corresponds to a value of 80, decimal. Don’t confuse the digit 0 with the value of ‘’ (NUL)! NULL can be defined as ((void*)0), NUL as ‘’.

 

9. int x[] = {1, 2, 3, 4, 5};
int *ptr, **ptr2;

ptr = x;
ptr2 = &ptr;
Referring to the sample code above, how do you update x[2] to 10 using ptr2?

  •  (**ptr2 + 2) = 10;
  •  **(ptr2 + 2) = 10;
  •  *(*ptr2 + 2) = 10;
  •  *(&ptr2 + 2) = 10;
  •  **(*ptr2 + 2) = 10;

 

10. For which one of the following reasons did the writers of MS-DOS C compilers augment C by adding the reserved words __near, __far, and __huge?

  •  They are reserved for usage by compiler writers and have no relevance to other programmers, hence the leading pair of underscores.
  •  They indicate that the compiler should place objects in the data segment, on the stack, and on the heap, respectively.
  •  They are specific to x86 architecture, and allow the programmer to override the default memory model for specific objects.
  •  They allow the compiler to perform additional code optimizations on pointer values.
  •  They indicate that an object should be placed near the lowest address, near the highest address, and that an object requires multiple pages,  respectively.




 


 

  No Comments.
Quick Post


Discuss this item on the forums. (0 posts)