|
INTERVIEW QUESTIONS
PROGRAMMING LANGUAGES
C
DETAILS
Question: union u1 { int i; char c; }u2; u2.i=32767; u2.c='a'; now the i value gets replaced. If we want to know the data that is saved in the union..internally, without knowing what values that we are using in the prog.
that is if just want to know whether a union currently holds an int or a char? If it is a combination of both int and char..we must know even that. and the memory locations at which this data is stored?
Answer: enum with_tag {int_tag, char_tag};
struct with_tag{
enum with_tag tag;
union {
int i;
char c;
} twoUnion;
} sampleStruct;
So, when assigning new int values:
sampleStruct.tag = int_tag;
sampleStruct.twoUnion.i = 3;
when assigning new char vaules:
sampleStruct.tag = char_tag;
sampleStruct.twoUnion.c = 'C';
So, later, you can know what kind of data is assigned to union by checking tag's vaule.
|
|
|
Category |
C Interview Questions & Answers -
Exam Mode /
Learning Mode
|
Rating |
(0.3) By 6680 users |
Added on |
7/18/2011 |
Views |
70274 |
Rate it! |
|
|
Question:
union u1 { int i; char c; }u2; u2.i=32767; u2.c='a'; now the i value gets replaced. If we want to know the data that is saved in the union..internally, without knowing what values that we are using in the prog.
that is if just want to know whether a union currently holds an int or a char? If it is a combination of both int and char..we must know even that. and the memory locations at which this data is stored?
Answer:
enum with_tag {int_tag, char_tag};
struct with_tag{
enum with_tag tag;
union {
int i;
char c;
} twoUnion;
} sampleStruct;
So, when assigning new int values:
sampleStruct.tag = int_tag;
sampleStruct.twoUnion.i = 3;
when assigning new char vaules:
sampleStruct.tag = char_tag;
sampleStruct.twoUnion.c = 'C';
So, later, you can know what kind of data is assigned to union by checking tag's vaule. Source: CoolInterview.com
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|
|
Related Questions |
View Answer |
|
main(int x).............
explaination on arguments passed thr' main
|
View Answer
|
|
How do you write a C program which can calculate lines of code but not counting comments?
|
View Answer
|
|
Output of this Programme please??
main() {
int a[]={2,4,6,8,10}; int i; change(a,5); for(int i=0;i<=4;i++) printf("
%d", a[i]); }
change(int *b,int n) {
int i; for(i=0;i *(b+i) = *(b+i)+5; }
sytaxis correct?? was asked i a test
|
View Answer
|
|
How can we open a image file through C program
|
View Answer
|
|
What does it mean-
a[i]=i+i
|
View Answer
|
|
I want C program code for : Reverse the links of a linked list by traversing only once
Input: The input consists of the information in each node of the linked list.
Output: The program displays the information in the linked list after the links are reversed.
Sample Input: Enter the information in the linked list (Enter -1 to exit): 10 20 30 40 50 -1
Sample Output: After the links are reversed Information in the linked list: 50 40 30 20 10
|
View Answer
|
|
Write a program which accepts a filename as a command line argument and reverses the contents of the file( ie first character
becomes the last character of the file and so on)
Input: The program takes the file name whose content should be reversed.
Output: The program reverses the contents of the file.
|
View Answer
|
|
What is page thrashing?
|
View Answer
|
|
When should the register modifier be used? Does it really help?
|
View Answer
|
|
When should a type cast be used?
|
View Answer
|
Please Note: We keep on updating better answers to this site. In case you are looking for Jobs, Pls Click Here Vyoms.com - Best Freshers & Experienced Jobs Website.
View All C Interview Questions & Answers - Exam Mode /
Learning Mode
|