| |

VerySource

 Forgot password?
 Register
Search
View: 4928|Reply: 31

Discuss a C language problem

[Copy link]

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-17 13:40:01
| Show all posts |Read mode
Everyone knows the order of function calls in C, but I don't quite understand this problem.
printf ("% d,% d", i, i ++);
printf ("% d,% d", i, ++ i);
printf ("% d,% d", i ++, i);
printf ("% d,% d", ++ i, i);
It's elusive. The order is inconsistent ~~~

Master tie up ~~~~
Reply

Use magic Report

0

Threads

6

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-1-22 18:09:02
| Show all posts
It won't be clear if you run it on the machine
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-1-22 18:27:01
| Show all posts
It is because the order of running on the computer feels inconsistent. I don't know what is going on.
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-22 21:00:01
| Show all posts
The difference between i ++ and ++ i ~~~~
i ++ increases by 1 after i use ++ i increases by 1 before i use
This way you can see the process more clearly ~ ...
Reply

Use magic Report

1

Threads

11

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-1-22 21:45:02
| Show all posts
You just need to know the difference between i ++ and ++ i
As for whether it is from left to right or right to left, you don't need to care.
If you find that the result on one compiler is different from the result on another compiler, you don't need to worry, this is normal.
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-1-23 00:00:01
| Show all posts
No, no, there will be different orders in a compiler. I will give the results as you know, suppose i = 2
printf ("% d,% d", i, i ++); // This case is displayed as: 3,2
printf ("% d,% d", i, ++ i); // This case is displayed as: 3,3
printf ("% d,% d", i ++, i); // This case is displayed as: 2,3
printf ("% d,% d", ++ i, i); // This case is displayed as: 3,3

Obviously, the order of parameter invocation of the two statements is opposite. How can this happen in a compiler?
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-23 12:27:01
| Show all posts
Do n’t drill into such a problem, know the principle
When running,
I write these programs as library functions and fall out when needed.
Hehe
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-25 13:27:01
| Show all posts
The direction of parameter passing in C is different from Fortan. It will be learned in the compilation principle. The function call test (a, b), the parameter passing order is actually b, a, (Fortan is a, b)
i = 0;
printf ("% d,% d", i, i ++);
Incoming is i ++: 0, i: 1
Printing in sequence or i: i ++, 1: 0
Reply

Use magic Report

0

Threads

78

Posts

29.00

Credits

Newbie

Rank: 1

Credits
29.00

 China

Post time: 2020-1-25 22:09:01
| Show all posts
Such an execution statement,
The order of execution is not specified in the standard,
Then, it will be decided by the compiler itself.

and so,
The execution result of such a statement depends on the compiler.

As for "opposite",
It's just that your understanding is inconsistent with the implementation of the compiler ...
Reply

Use magic Report

0

Threads

78

Posts

29.00

Credits

Newbie

Rank: 1

Credits
29.00

 China

Post time: 2020-1-25 23:09:02
| Show all posts
printf ("% d,% d\n", i, i ++); // This case is displayed as: 3,2
    printf ("% d,% d\n", i, ++ i); // This case is displayed as: 3,3
    printf ("% d,% d\n", i ++, i); // This case is displayed as: 2,2
    printf ("% d,% d\n", ++ i, i); // This case is displayed as: 3,2

This result may be easier to understand ~~

As for the compiler you use,
Why did you get that result,
Need to analyze compiler behavior,
This is not necessary,
Because analysis is useless,
The feeling is to find a reasonable "excuse" to get a certain result,
Doesn't make any sense ...
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list