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 = 5;
int func()
{
printf(“%d\n”, a);
}
};
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 = 5;
int func()
{
printf(“%d\n”, a);
}
};
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 to initialize any member inside the structure, therefore, the program gives error whereas in case of C++ this is allowed therefore the program does not give any error.

Join The Discussion