C language return value question

Asked 1 years ago, Updated 1 years ago, 114 views

void main() {

printf(" aaa ");

return 'a';

}

This is how we wrote the code. Since the return form of the main function was specified as void, the main does not deliver the return value to the OS.

Based on this theory, I opened the c language debug file with a command prompt window

With the echo %errorlevel% command

We checked whether the main function did not deliver the return value to os.

The above picture is the result of checking the return value through the command prompt window.

According to the theory I know, the return value should not be delivered.

As shown in the picture, you can see that the ASCII code value of a, 97, has been returned.

I googled hard and looked at foreign materials, but there are many difficulties, so I'm posting a question~

c programming

2022-09-22 15:28

1 Answers

If you're asking why...

You need to try debugging that binary.

When returning the main function, the return value must be stored in the register eax.

C language is an advanced language and it is just a tax that cpu doesn't know from the cpu's perspective. It's meaningful when it's compiled and converted into binary code

In other words, the questioner's question is why the compilation error is not informed and the results are wrong. But that's because that's what the compiler is.

The compiler and version you used are also important.

gcc version 8.2.1 20181215 (Red Hat 8.2.1-6) (GCC) warns you as follows:

[allinux@lghah ~]$ gcc -o test test.c
test.c: In function ‘main’:
test.c:5:12: warning: ‘return’ with a value, in function returning void
     return 'a';
            ^~~
test.c:3:6: note: declared here
 void main(){
      ^~~~


2022-09-22 15:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.