Structure Questions with Answers

15 practice questions on Structure (C) with answers and step-by-step solutions. Try 10 samples below, then practise all 15 free in the AptiNest app.

Sample questions

Question 1Medium

Which of the following is not possible under any scenario?

  1. s1 = &s2;
  2. s1 = s2;
  3. (*s1).number = 10;
  4. None of the mentioned

Answer: D. None of the mentioned

Explanation

All the expression mention above is possible with the instance of a structure.
The instance may be pointer variable or not.

Step-by-step solution

  1. s1 = &s2 is valid when s1 is a pointer to the same type as s2.
  2. s1 = s2 is valid when s1 and s2 are both structs of the same type.
  3. (*s1).number = 10 is valid when s1 is a pointer to a struct that has a number member.
  4. Each statement is possible under some declaration of s1 and s2, so none is impossible.
  5. So the answer is None of the mentioned.

Why the other options are wrong

  • A. It is possible if s1 is a pointer and s2 is a struct of the same type.
  • B. It is possible if s1 and s2 are both structs of the same type.
  • C. It is possible if s1 is a pointer to a struct with a number member.

Tip: Check each statement against some valid declaration before calling it impossible.

Question 2Medium

What is the output of this C code?

#include <stdio.h>
void main()
{
 struct student
  {
    int no;
    char name[20];
   };
  struct student s;
  no = 8;
  printf("%d", no);
}
  1. Nothing
  2. Compile time error
  3. Junk
  4. 8

Answer: B. Compile time error

Explanation

To access structure variable we use (.) operator. Here variable no is accessed without using structure and (.) operator.

Step-by-step solution

  1. The struct student declaration has members no and name, but no variable called no exists outside it.
  2. The statement no = 8 uses the name no, which is not declared anywhere in main.
  3. C requires every name to be declared before it is used, so the compiler stops with an error.
  4. So the answer is Compile time error.

Why the other options are wrong

  • A. The program does not run, so nothing is printed.
  • C. A junk value needs the program to run, but it does not compile.
  • D. 8 would need a valid variable no, which is not declared.

Tip: A struct member is not a variable outside its struct; declare the variable before you use it.

Question 3Medium

Point out the error in the program?

#include <stdio.h>
int main()
{
 struct emp
 {
   char name[20];
   float sal;
 };
struct emp e[10];
int i;
for(i=0; i<=9; i++)
scanf("%s %f", e[i].name, &e[i].sal); return 0;
}

(Assume Turbo C, a 16-bit compiler.)

  1. Error: invalid structure member
  2. Error: Floating point formats not linked
  3. No error
  4. None of above

Answer: B. Error: Floating point formats not linked

Explanation

At run time it will show an error then program will be terminated.
Sample output: Turbo C (Windows)
c:\>myprogram

Sample
12.123
scanf: floating point formats not linked
Abnormal program termination

Step-by-step solution

  1. The loop reads a name and a float with scanf, using the format %s %f and the address &e[i].sal.
  2. On a Turbo C style compiler, the floating point conversion code is linked only when the program uses floating point in a way the linker can see, and here the float is read only by scanf.
  3. The link step then fails with the message Floating point formats not linked.
  4. So the answer is Error: Floating point formats not linked.

Why the other options are wrong

  • A. The member names name and sal are valid, so there is no invalid structure member.
  • C. Modern compilers accept this code, but on the Turbo C style compiler the float formats are not linked, so there is an error.
  • D. Option B is the error, so none of the above is wrong.

Tip: A float read with scanf can fail at link time on old DOS compilers; that is a link error, not a syntax error.

Question 4Medium

Which of the following return-type cannot be used for a function in C?

  1. char *
  2. struct
  3. void
  4. None of the mentioned

Answer: D. None of the mentioned

Explanation

In C, functions can return various types including pointers (like char *), structures (struct), and void (no return value). 'None of the mentioned' is incorrect because all the options are valid return types in C.

Step-by-step solution

  1. A C function can return a pointer, such as char *.
  2. A C function can return a struct by value, and it can return void.
  3. All three listed return types are valid in C, so none of them is the one that cannot be used.
  4. So the answer is None of the mentioned.

Why the other options are wrong

  • A. Char * is a pointer type, and pointers can be returned.
  • B. A struct type can be returned by value in C.
  • C. Void is valid as the return type of a function that returns nothing.

Tip: Check each listed type against the rule before picking the odd one out.

Question 5Medium

What is the output of this C code?

#include <stdio.h>
struct
{
  int k;
  char c;
} p;
 int p = 10;
 int main()
 {
   p.k = 10;
   printf("%d %d\n", p.k, p);
 }
  1. Compile time error
  2. 10 10
  3. Depends on the standard
  4. Depends on the compiler

Answer: A. Compile time error

Explanation

Explanation:
We cannot create same name variable.P is structure instance so its invalid to create any other variable same name of P at global scope.

Step-by-step solution

  1. P is declared twice at file scope: once as a struct variable and once as int p = 10.
  2. Two declarations of the same name with different types in the same scope are not allowed.
  3. The compiler reports a redeclaration error, so the program does not compile.
  4. So nothing runs and no output is printed.
  5. The answer is Compile time error.

Why the other options are wrong

  • B. 10 10 would need the program to compile, and the duplicate p stops it.
  • C. This is an error under the standard, so the result does not depend on the standard.
  • D. The error is the same on all compilers, so it does not depend on the compiler.

Tip: Two file-scope variables with the same name and different types are a compile error.

Question 6Medium

What is the output of this C code?

#include <stdio.h>
struct p
{
  int k;
  char c;
  float f;
};
int p = 10;
int main()
{
  struct p x = {1, 97};
  printf("%f %d\n", x.f, p);
}
  1. Compile time error
  2. 0.000000 10
  3. Some garbage value 10
  4. 0 10

Answer: B. 0.000000 10

Explanation

In above code structure variable declare using initialization list, struct p x = {1, 97}; here only two members of structure are initialized using initialization list, variable float f implicitly initialize to 0.00000.

Step-by-step solution

  1. X is a struct p whose k is 1 and whose c is 97.
  2. The third member f is not given a value, so it is set to 0.0.
  3. The file-scope int p is 10, and the struct tag p does not clash with it.
  4. Printf prints x.f as 0.000000 and p as 10.
  5. So the output is 0.000000 10.

Why the other options are wrong

  • A. The code compiles, since the tag and variable names do not clash.
  • C. Members left out of an initializer are set to zero, so no garbage value is printed.
  • D. %f prints six decimal places, so the float prints as 0.000000, not 0.

Tip: Members left out of a struct initializer are set to zero.

Question 7Medium

What is the output of this C code?

 #include <stdio.h>
 struct
 {
   int k;
   har c;
 };
 int main()
 {
   struct p;
   p.k = 10;
   printf("%d\n", p.k);
 }
  1. Compile time error
  2. 10
  3. Undefined behaviour
  4. Segmentation fault

Answer: A. Compile time error

Explanation

Anonymous structure variable is createdat time of declaration of structure. It is invalid to create anonymous structure instance after declaration of structure.

Step-by-step solution

  1. The struct has no tag, and its member type is har, which is not a C type.
  2. The name har is not declared, so the compiler reports an error on the member line.
  3. Struct p; only declares a tag, and p is then used as a variable, which is not declared.
  4. The compiler stops with an error before the program runs.
  5. The answer is Compile time error.

Why the other options are wrong

  • B. The program does not compile, so 10 is never printed.
  • C. Undefined behaviour needs a program that compiles and runs; this one is rejected.
  • D. A segmentation fault needs a program that runs; this one never runs.

Tip: A struct tag and a variable name are different things; check each name is declared.

Question 8Medium

Which of the following operation is illegal in structures?

  1. Typecasting of structure
  2. Pointer to a variable of same structure
  3. Dynamic allocation of memory for structure
  4. All of the mentioned

Answer: A. Typecasting of structure

Explanation

A struct cannot be typecast to another type, so typecasting a structure is illegal in C. A pointer to a struct of the same type and malloc() for a struct are both allowed.

Step-by-step solution

  1. Typecasting a struct to another type is not allowed in C.
  2. A pointer to a struct of the same type is allowed, as used in linked lists.
  3. Dynamic allocation with malloc is allowed for a struct.
  4. So the illegal operation is Typecasting of structure.

Why the other options are wrong

  • B. A pointer to the same struct type is legal, as in linked lists.
  • C. Malloc can allocate memory for a struct, so this is legal.
  • D. Options B and C are legal, so not all of them are illegal.

Tip: Structs can be pointed to and allocated, but not cast to another type.

Question 9Medium

What is the output of this C code?

#include <stdio.h>
struct p
{
  int k;
  char c;
};
 int p = 10;
int main()
{
  struct p x;
  x.k = 10;
  printf("%d %d\n", x.k, p);
}
  1. Compile time error
  2. 10 10
  3. Depends on the standard
  4. Depends on the compiler

Answer: B. 10 10

Explanation

Scope of int p and structure instance is different its valid to declare same name variable at different scope.

Step-by-step solution

  1. Struct p is a tag, and tags are in a separate namespace from ordinary variable names.
  2. So int p = 10 is a separate variable named p, and it does not clash with the tag.
  3. x.k is set to 10, and p is still 10.
  4. Printf prints x.k and then p, giving 10 10.
  5. The answer is 10 10.

Why the other options are wrong

  • A. The tag and the variable have different namespaces, so there is no compile error.
  • C. The output is fixed by the code here, so it does not depend on the standard.
  • D. The output does not depend on the compiler here.

Tip: A struct tag and a variable can share a name in C.

Question 10Medium

What is the output of this C code?

#include <stdio.h>
struct student
{
  char *name;
};
void main()
{
  struct student s, m;
  s.name = "st";
  m = s;
  printf("%s%s", s.name, m.name);
}
  1. Compile time error
  2. Nothing
  3. Junk values
  4. st st

Answer: D. st st

Explanation

Unlike array, Structure allow to assignment operation on structure instances.

Step-by-step solution

  1. A struct can be assigned to another struct of the same type.
  2. m = s copies the pointer name, so both point to "st".
  3. printf("%s%s", ...) prints both strings.
  4. Both print "st", so option D.

Why the other options are wrong

  • A. Struct assignment is allowed in C.
  • B. The printf prints two strings.
  • C. Both pointers hold a valid string, not junk.

Tip: Structs can be copied with =, unlike arrays.

Practise all 15 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all C topics · All topics