© 2014 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
enggedu

C and C++ Interview Questions

14.  How do you link a C++ program to C functions?

Ans:

     By using the extern "C" linkage specification around the C function declarations. Programmers should know about mangled function names and type-safe linkages. Then they should explain how the extern "C" linkage specification statement turns that feature off during compilation so that the linker properly links function calls to C functions.

Ex - 1: What is the output?
main()
{
            char string[]="Hello World";
      display(string);
}
void display(char *string)
{
      printf("%s",string);
}
Ans:

Compiler Error : Type mismatch in redeclaration of function display
Explanation :
In third line, when the function display is encountered, the compiler doesn't know anything about the function display. It assumes the arguments and return types to be integers, (which is the default type). When it sees the actual function display, the arguments and type contradicts with what it has assumed previously. Hence a compile time error occurs. 

Ex - 2: Waht is the output?
main()
{
    int i=1;
    while (i<=5)
    {
       printf("%d",i);
       if (i>2)
        goto here;
       i++;
    }
}
fun()
{
   here:
     printf("PP");
}
Ans: Compiler error: Undefined label 'here' in function main
Explanation:

Labels have functions scope, in other words The scope of the labels is limited to functions . The label 'here' is available in function fun() Hence it is not visible in function main.


SLogix Student Projects
bottom