0

How does output came as "102 f" and it also throwing some error why?

Doubt in output https://code.sololearn.com/cm39nZiexF9b/?ref=app

8th Feb 2023, 5:22 AM
Yogeshwaran P
Yogeshwaran P - avatar
7 Respostas
+ 3
Yogeshwaran read ArsenicolupinIII's explanation of the output. If you pay attention to what the error message says, it is clear that the problem is in how the union is being initialized. A union can hold only one of those two values. It is different from a struct, which has separate memory storage for each field name. Instead, a union it is assigning multiple names to one memory storage space. Choose one value or the other, but not both at once. union course c = {102}; or union course c = {"Java"}; This will correct the error.
8th Feb 2023, 6:28 AM
Brian
Brian - avatar
+ 2
ArsenicolupinIII yes, also I should add this to clear up a misunderstanding: By assigning the integer value 102 it stores four bytes: 102, 0, 0, 0. This is because the size of an int type is four bytes long. Therefore, it does have a terminating null for the second printf. Note that this is running on a little-endian computer. On a big-endian computer, the output might be different. It might encounter the null first, but still it would have a null.
8th Feb 2023, 2:22 PM
Brian
Brian - avatar
+ 2
Thanks Brian for giving me more insight into the behavior of the code on a small endianness computer. Yes, it is important to note that the endianness of the computer can affect how the data is displayed. It is imperative to understand how the memory model works to avoid unexpected output.
8th Feb 2023, 2:30 PM
ArsenicolupinIII
+ 1
The output "102 f" and the error are happening because of the way the union "course" is used in the code. In C, a union is a user-defined data type that can store different data types in the same memory location. In this code, the union "course" has two members: "courseno" and "coursename". The first member, "courseno", is an integer and the second member, "coursename", is an array of characters. When the union "c" is initialized with the values {102, "Java"}, it stores the values in the memory location allocated for the union. When the first printf statement is executed, it retrieves the value stored in the memory location as an integer and prints it, which results in the output "102", when the second printf statement is executed, it retrieves the value stored in the memory location as an array of characters and prints it, which results in an error. This is because the array of characters, "coursename", was not properly terminated with a null character.
8th Feb 2023, 5:49 AM
ArsenicolupinIII
+ 1
As a result, the printf statement tries to print a string that goes beyond the memory allocated for the union and causes a runtime error.
8th Feb 2023, 5:49 AM
ArsenicolupinIII
+ 1
Brian it seems correct, I think to note that the union used can only contain one of two values ​​(courseno or coursename). It is not possible to use both at the same time, as they use the same memory space.
8th Feb 2023, 7:05 AM
ArsenicolupinIII
+ 1
Thank you ArsenicolupinIII and Brian for clearing my doubt
8th Feb 2023, 3:39 PM
Yogeshwaran P
Yogeshwaran P - avatar