c Questions about language structure

Asked 2 weeks ago, Updated 2 weeks ago, 4 views

#include <stdio.h>

struct test
    int a;

int main()
    struct test num;

    num.a = 3;

    printf("%d\n", num.a);
    printf("%d\n", num);

    return 0;

The output from printf("%d\n", num.a); in the above code is 3. And printf("%d\n", num); below it is also printed as 3. Here's my question. How on earth does num have 3? For me, num.I put the value in a. I understand that structure test serves as a data type, but num plays a role other than connecting the values of the structure?

struct c

2022-09-19 23:34

1 Answers

printf("%d\n", num.a); is an example of the normal usage of a structural member variable.

On the other hand, printf("%d\n", num); is an undefined behavior.

Undefined behavior may have different results depending on the compiler version or compiler manufacturer.


2022-09-19 23:34

If you have any answers or tips

© 2022 pinfo. All rights reserved.