IndiaBIX.com
Arithmetic Aptitude Data Interpretation
Logical Reasoning Verbal Reasoning Non Verbal Reasoning
General Knowledge
Sudoku Number puzzles Missing letters puzzles Logical puzzles Playing cards puzzles Clock puzzles
C Programming C++ Programming C# Programming Java Programming
Microbiology Biochemistry Biotechnology Biochemical Engineering
Civil Engineering Mechanical Engineering Chemical Engineering Networking Database Questions Computer Science Basic Electronics Digital Electronics Electronic Devices Circuit Simulation Electrical Enigneering Engineering Mechanics Technical Drawing
Placement Papers Group Disucssion HR Interview Technical Interview Body Language
Aptitude Test Verbal Ability Test Verbal Reasoning Test Logical Reasoning Test C Programming Test Java Programming Test Data Interpretation Test General Knowledge Test
Data Structures Operating Systems Networking DATABASE Database Basics SQL Server Basics SQL Server Advanced SQL Server 2008 JAVA Core Java Java Basics Advanced Java UNIX Unix File Management Unix Memory Management Unix Process Managemnt C Interview Questions The C Language Basics .NET Interview Questions .NET Framework ADO.NET ASP.NET Software Testing

C Programming - Complicated Declarations - Discussion

@ : Home > C Programming > Complicated Declarations > Point Out Errors - Discussion

Read more:

"It takes a very long time to become young."
- Pablo Picasso
1. 

Point out the error in the following program (in Turbo C under DOS).

#include<stdio.h>

union emp
{
    int empno;
    int age;
};

int main()
{
    union emp e = {10, 25};
    printf("%d %d", e.empno, e.age);
    return 0;
}

[A]. Error: Lvalue required
[B]. Error: Rvalue required
[C]. Error: cannot initialize more than one union member.
[D]. No error

Answer: Option C

Explanation:

No answer description available for this question.


Shashank said: (Wed, Sep 7, 2011 05:55:31 PM)    
 
I did this with gcc compiler. No errors but there were two warning.

prog.c: In function 'main':
prog.c:11: warning: excess elements in union initializer
prog.c:11: warning: (near initialization for 'e')

Output: 10 10

Ayesha said: (Sun, Jun 10, 2012 02:06:44 PM)    
 
Please anybody explain me in detail.

Vcillusion said: (Wed, Sep 12, 2012 02:30:20 PM)    
 
union emp e = {10, 25};.

As we can see in above statement both of the members are initialized.

This is not possible as single memory slot is available which is property of union which comes with this one drawback too.

So this is not allowed as maximum of member function only 2 bytes is allocated to emp union so how could it use this 2 byte memory to initialize all member function at once.

I hope it makes it clear.

Raja Ghosh said: (Wed, Jan 23, 2013 07:21:59 PM)    
 
However we can pass as many values as in the union variable.

But union only gets initialized with one value (leftmost in the parenthesis as per comma operator usage) so here it will take 10.

So the programs runs correctly with warning as: excess elements in union initializer.

But as a programmer we can ignore warnings :).

Write your comments here:
Name *:     Email:


© 2008-2013 by IndiaBIX™ Technologies. All Rights Reserved | Copyright | Terms of Use & Privacy Policy

Contact us: info@indiabix.com     Follow us on twitter!