C Programming - Structures, Unions, Enums - Discussion

Discussion Forum : Structures, Unions, Enums - Find Output of Program (Q.No. 8)
8.
What will be the output of the program in Turbo C (under DOS)?
#include<stdio.h>

int main()
{
    struct emp
    {
        char *n;
        int age;
    };
    struct emp e1 = {"Dravid", 23};
    struct emp e2 = e1;
    strupr(e2.n);
    printf("%s\n", e1.n);
    return 0;
}
Error: Invalid structure assignment
DRAVID
Dravid
No output
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
60 comments Page 4 of 6.

Naresh said:   1 decade ago
e2.n="hello";
printf("%s,,,%s",e1.n,e2.n);

The output is : Dravid,,,hello

Why it is not changing the e2.n if above o/p is correct ?

@harish said:   1 decade ago
Structure is a value type. So in e2 there is a copy of e1 not a reference. So whatever changes made to e2 won't be reflected in e1. Hence answer is 'Dravid'.

Gopal Bansal said:   1 decade ago
In competitive exams our mind will not go towards e2=e1 so we will think that we are changing e2 string not e1 string so Be Aware from this question.

Varsha Kurpad said:   8 years ago
Answer will not b DRAVID. Because both e1 and e2 have different tables. How come the changes on e2 affects e1? Can anyone tel me please.

N n said:   1 decade ago
strupr and strlwr are not part of the ANSI C standard. This question is pointless if your std C library doesn't have the functions.

Ravi chaitanya said:   1 decade ago
strupr-> converts the string into upper case
strlwr-> converts the string to lower case

C strings manipulation functions

Mohanty said:   1 decade ago
If means e1 is assigned to e2 so as strupr () is used with e2.

So how does the string in e1 change when it is printed?

Ramya said:   9 years ago
@Mohamed Mekawy.

Why there is no effect on 'toupper', why it simply prints 'Dravid' and not printing in uppercase?

Aswarth said:   1 decade ago
The Answer is Dravid. Because e2 is not a pointer. So not point to the e1. So any changes cannot be occur in e1.

Jain said:   1 decade ago
Hello friends,

Why gcc is not consider string.h ? and except it which header file is include to run it in gcc?


Post your comments here:

Your comments will be displayed after verification.