Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

What happens if we run the following code in both C and C++?
#include <stdio.h>
struct STRUCT
{
int a;
int func()
{
printf(“HELLO THIS IS STRUCTURE\n”);
}
};
int main()
{
struct STRUCT s;
s.func();
return 0;
}

What happens if we run the following code in both C and C++?
#include <stdio.h>
struct STRUCT
{
int a;
int func()
{
printf(“HELLO THIS IS STRUCTURE\n”);
}
};
int main()
{
struct STRUCT s;
s.func();
return 0;
}
a) The program runs fine and both prints output “HELLO THIS IS STRUCTURE”
b) The program gives an error in case of C but runs perfectly in case of C++
c) The program gives an error in case of C++ but runs perfectly in case of C
d) The program gives an error in case of both C and C++

Answer: b
Explanation: As C does not allows the structure to have member functions, therefore, it gives an error in case of C but as C++ does allow structures to have member functions, therefore, the C++ does not give an error.

Join The Discussion