Home Other Languages C Basic C Questions - 6

Login Form




Basic C Questions - 6 Print E-mail

1. What is an argument ? differentiate between formal arguments and actual arguments?

An argument is an entity used to pass the data from calling funtion to the called funtion. Formal arguments are the arguments available in the funtion definition.They are preceded by their own data types.Actual arguments are available in the function call.

 

2. Sample Code
long factorial (long x)
{
return x * factorial(x - 1);
}

What will the function above return if called with x equal to 5?

  •  0
  •  15
  •  120
  •  The program will not compile
  •  The function will never return

 

3. In C, what is the difference between a static variable and global variable?

A static variable declared outside of any function is accessible only to all the functions defined in the same file (as the static variable). However, a global variable can be accessed by any function (including the ones from different files).

 

4. 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)?

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

 

5. What does it mean when a pointer is used in an if statement?

Any time a pointer is used as a condition, it means “Is this a non-null pointer?” A pointer can be used in an if, while, for, or do/while statement, or in a conditional expression.

 

6. Sample Code
extern void *ptr1;
extern void *ptr2;

int compare( int n )
{
return ????;
}

What should replace the ???? in the code above to compare the first n bytes of the memory pointed to by ptr1 and ptr2 and return a non-zero value if they are equal?

  •  memcmp( ptr1, ptr2, n )
  •  !memcmp( ptr1, ptr2, n )
  •  !strncmp( ptr1, ptr2, n )
  •  memncmp( ptr1, ptr2, n )
  •  strncmp( ptr1, ptr2, n )

 

7. Can a variable be both const and volatile?

Yes. The const modifier means that this code cannot change the value of the variable, but that does not mean that the value cannot be changed by means outside this code. For instance, in the example in FAQ 8, the timer structure was accessed through a volatile const pointer. The function itself did not change the value of the timer, so it was declared const. However, the value was changed by hardware on the computer, so it was declared volatile. If a variable is both const and volatile, the two modifiers can appear in either order.

 

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

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

Function Name Purpose

itoa() Converts an integer value to a string.
ltoa() Converts a long integer value to a string.
ultoa() Converts an unsigned long integer value to a string.

The following functions can be used to convert floating-point values to strings:
Function Name Purpose

ecvt() Converts a double-precision floating-point value to a string without an embedded decimal point.
fcvt() Same as ecvt(), but forces the precision to a specified number of digits.
gcvt() Converts a double-precision floating-point value to a string with an embedded decimal point.

 

9. Why should we assign NULL to the elements (pointer) after freeing them?

This is paranoia based on long experience. After a pointer has been freed, you can no longer use the pointed-to data. The pointer is said to dangle; it doesn’t point at anything useful. If you NULL out or zero out a pointer immediately after freeing it, your program can no longer get in trouble by using that pointer. True, you might go indirect on the null pointer instead, but that’s something your debugger might be able to help you with immediately. Also, there still might be copies of the pointer that refer to the memory that has been deallocated; that’s the nature of C. Zeroing out pointers after freeing them won’t solve all problems;

 

10. Which one of the following is NOT a valid statement?

  •  ++d+++e++;
  •  f*=g+=h=5;
  •  l-->>m<<--n; 4 int a(int a),b=0,c=a((c=b,++b)); 5 i->j<-k;