C Interview Questions and Answers for Freshers

    1 Votes

64) When would you use a pointer to a function?

Pointers to functions are interesting, when you pass them to other functions. A function that takes function pointers says, in effect, "Part of what I do can be customized. Give me a pointer to a function, and I'll call it when that part of the job needs to be done. That function can do its part for me. This is known as a callback". It's used a lot in graphical user interface libraries, in which the style of a display is built into the library but the contents of the display are part of the application.

65) How to return two variables of different data type from a function?
struct STRUCTURE
{
  int integerVar;
  char characterVar;
};
STRUCTURE Function()
{
  STRUCTURE x;
  return (x);
}

 

66) What is the difference between far pointer and near pointer ?

Compilers for PC compatibles, use two types of pointers.

  • Near pointers are 16 bits long and can address a 64KB range. Far pointers are 32 bits long and can address a 1MB range. Near pointers operate within a 64KB segment. There's one segment for function addresses and one segment for data.
  • Far pointers have a 16-bit base (the segment address) and a 16-bit offset. The base is multiplied by 16, so a far pointer is effectively 20 bits long. For example, if a far pointer had a segment of 0x7000 and an offset of 0x1224, the pointer would refer to address 0x71224. A far pointer with a segment of 0x7122 and an offset of 0x0004 would refer to the same address.

67) When should a far pointer be used?

Sometimes, you can get away with using a small memory model in most of a given program. Somethings may not 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 separately manage a heap from the rest of the data.

68) Expand the following fragment of C code to make it more understandable ?

{char* x = *y ? *++*y : *++*z;} 

Code can be rewritten as

char *x;
if (*y != NULL)
{
  (*y)++;
  x = *y;
}
else
{
  (*z)++;
  x = *z;
}

69) Write a C program to print a semicolon without using any semicolon in the whole program?

#include<stdio.h>
#define SEMICOLON 59 
//59 is ASCII value of semicolon 
void main() 
{ 
  if(printf("%c",SEMICOLON)) 
  { 
  } 
}

70) How can you determine the size of an allocated portion of memory?

You can't. free() can, but there's no way for a program to know the use of method free()

71) Add your code in the program so that the given code block print 'unknown' ?

<...Add your code...>
if(a>10) printf("greater"); 
else if(a<10) printf("lesser"); 
else if(a==10) printf("equal"); 
else printf("unknown");

#define a b++ 
main()
{
  int b = 10;
  if(a>10) printf("greater"); 
  else if(a<10) printf("lesser"); 
  else if(a==10) printf("equal"); 
  else printf("unknown");
}

Explanation: After macro expansion the code will be like this :

if(b++>5) printf("greater"); //5>5 : fail 
else if(b++<5) printf("lesser"); //6

72) Can math operations be performed on a void pointer?

No. Pointer addition and subtraction are based on advancing the pointer by a number of elements. By definition, if you have a void pointer, you don't know what it's pointing to, so you don't know the size of what it's pointing to. If you want pointer arithmetic to work on raw addresses, use character pointers.

73) How many parameters should a function have?

There is no fixed guideline or limit to the number of parameters your functions can have. However, it is considered bad programming style for your functions to contain an abnormally high (eight or more) number of parameters. The number of parameters a function has also directly affects the speed at which it is called - the more parameters, the slower the function call.

74) 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.

75) Is it valid to address one element beyond the end of an array?

It's valid to address it, but not to see what's there. (The really short answer is, "Yes, so don't worry about it.") With most compilers, if you say

int i, a[MAX], j;

Then either i or j is at the part of memory just after the last element of the array. The way to see whether i or j follows the array is to compare their addresses with that of the element following the array. The way to say this in C is that either

& i == & a[ MAX ]

is true or

& a[ MAX ] == & j

is true. This isn't guaranteed.

Page 8 of 8

Popular Videos

communication

How to improve your Interview, Salary Negotiation, Communication & Presentation Skills.

Got a tip or Question?
Let us know

Related Articles

C++ Interview Questions and Answers for Freshers, Experienced
Advanced C Interview Questions and Answers for Experienced
Embedded C Interview Questions and Answers