Q:

Structures - find output programs in C (Set 1)

belongs to collection: Find output of programs in C

0

This section contains find output programs on C language Structures; each question has correct output and explanation about the answer.

Predict the output of following programs.

1) Is the following declaration of structure 'tag' is correct?

int main()
{
	struct tag{
		int a;
		float b;
	};
	//other statement 
	return 0;
}

2) Is the following declaration of structure 'tag' is correct?

int main()
{
	struct tag{
		int a=0;
		float b=0.0f;
	};
	//other statement 
	return 0;
}

3) What will be the output of following program?

#include <stdio.h>
int main()
{
	struct tag{
		int a;
		float b;
	};
	
	struct tag t={10,10.23f};
	printf("%d, %.02f\n",t.a,t.b);
	return 0;
}

4) Is the following structure variable declaration is correct? If yes, what will be the output of following program?

#include <stdio.h>
int main()
{
	struct person{
		char *name;
		int age;
	};
	
	struct person p={"Mani",21};

	printf("%s, %d\n",p.name,p.age);	
	
	return 0;
}

5) What will be the output of this program?

#include <stdio.h>
int main()
{
	struct person{
		char name[30];
		int age;
	};
	
	struct person p={"Mani",21};

	//edit values
	p.name="Vanka";
	p.age=27;

	printf("%s, %d\n",p.name,p.age);	
	
	return 0;
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer 1:

Explanation

Yes, structure tag is declared in the main() block and it is allowed in C language, the scope of tag will be local and we can access tag inside main() only.

Answer 2:

Explanation

No, we cannot initialize any member of the structure with in its declaration.

Answer 3:

Output

10, 10.23

Explanation

Structure members can be initialized while declaring its object (structure variable) like struct tag t={10,10.23f};

Answer 4:

Output

Mani, 21

Explanation

Yes, the structure variable declaration is correct, we can initialize string value like this (consider the statement struct person p={"Mani",21};).

Answer 5:

Output

Error

Explanation

String cannot be assigned directly (p.name="Vanka";), we can use strcpy or memcpy to copy the string.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Find output of C programs Questions with Answers (... >>
<< Find output of C programs (strings) | Set 2...