-3

So I have been coding in C for a while now, but one thing that still confuses me is the "return" at the end of functions.

Now I know that return is the value, which a function returns when called by main.

But what does "return 0" mean in main() function of C? I also did the following-

#include <stdio.h>

int main()
    {
       printf("Hello world");
       return 0;
    }

And then, I did this-

#include <stdio.h>

int main()
    {
       printf("Hello world";
       return -1;
    }

And then I finally did this-

#include <stdio.h>

int main()
    {
       printf("Hello world";
       return 45;
    }

They all compile fine and run, but what do these "return" do in each case of the above code. Please explain in detail about this return and what happens in these 3 cases.

  • 2
    How are you running your C programs? If, by chance, you're invoking them on the command line under Unix or Linux, try doing `echo $?` afterwards. – Steve Summit May 27 '22 at 14:49
  • main returns integer code back to OS, 0 indicate success, all other numbers are application dependent error codes – Iłya Bursov May 27 '22 at 14:50
  • @SteveSummit huh, yes I use linux and what does it exactly mean what you are saying ? – Parth Rajawat May 27 '22 at 15:30
  • @ParthRajawat After you run your program by typing `./myprogram` (or whatever) and hitting Return, try typing `echo $?` and hitting Return. `$?` is a *shell variable* that holds the *exit status* of your program. (But if you're not running your program by typing ./myprogram` and hitting Return, you can ignore all of this, as it probably doesn't apply.) – Steve Summit May 27 '22 at 15:32
  • @templatetypedef by hovering over downvote button I see `This question does not show any research effort; it is unclear or not useful`, having this in mind, [google says](https://www.google.com/search?q=c+return+value+main+site:stackoverflow.com&rlz=1C1VDKB_enUS936US936&sa=X&ved=2ahUKEwivqJuOh4D4AhXEnGoFHUyVD50QrQIoBHoECCkQBQ&biw=1920&bih=2048&dpr=1) that there 32 millions pages about this topic on SO only, so for me it is obvious that OP didn't even tried to do any kind of research, but just posted new question – Iłya Bursov May 27 '22 at 16:07
  • @IłyaBursov i have been trying to find answer regarding this for a week now. I did not find answers that could satisfy me and hence I asked here. – Parth Rajawat May 27 '22 at 16:11
  • @ParthRajawat when you create question on SO, on right side you can find `Describe what you’ve tried Show what you’ve tried and tell us what you found (on this site or elsewhere) and why it didn’t meet your needs. You can get better answers when you provide research.` so, what have you found which didn't satisfy you? – Iłya Bursov May 27 '22 at 16:17
  • @IłyaBursov I mean, I tried googling it and all the answers were like "Return 0 means program executed successfully and any other return value is treated as some kind of error" ... but there was no detailed explanation regarding these return values and what do these return values mean. I thought I will get my answers faster here (which I did). – Parth Rajawat May 27 '22 at 16:22

2 Answers2

3

The return value from the main function is the exit value of the program. The process that calls your program can retrieve this exit value.

First a shell example:

./myprogram
exitvalue=$?
echo exitvalue = $exitvalue

And this will print 0 for the first program and 45 for the third. The second program will probably print 255 because the exit status is on most operating systems expected to be between 0 and 255.

dbush
  • 205,898
  • 23
  • 218
  • 273
  • How do I type this in terminal ? – Parth Rajawat May 27 '22 at 15:44
  • @ParthRajawat You mean besides exactly how it's posted? – dbush May 27 '22 at 15:45
  • I mean, I am copying this code in my terminal, but it is not getting executed. only the part "./myprogram exitvalue=$" runs, and then this terminates and then "echo exitvalue = $exitvalue" is copied to the next instruction..... I mean, all of these lines are not executing together. – Parth Rajawat May 27 '22 at 15:51
  • @ParthRajawat You're not making sense. Are you typing these commands, one line at a time, at a shell prompt? – dbush May 27 '22 at 15:53
  • I am trying to type these all together, but they are not working. Each line goe into the new shell prompt. – Parth Rajawat May 27 '22 at 15:54
  • @ParthRajawat Define "not working". What output do you get after each of these three commands? And yes after running each line you should get another shell prompt. – dbush May 27 '22 at 15:56
  • @dbush OK, so this is how it is working- parth@parth-ROG-Strix-G512LI-G512LI:~/C-Programming/Programs/[SOLVED] Arithmetic operators$ ./arithops exitvalue=$? a+b = 50 a-b = 10 a*b = 600 a/b = 1 a%b = 10 c+d = 199 Incrementation = 31 Incrementing again = 33 parth@parth-ROG-Strix-G512LI-G512LI:~/C-Programming/Programs/[SOLVED] Arithmetic operators$ echo exitvalue = $exitvalue exitvalue = parth@parth-ROG-Strix-G512LI-G512LI:~/C-Programming/Programs/[SOLVED] Arithmetic operators$ – Parth Rajawat May 27 '22 at 16:00
  • @dbush hope you understand this, cause return is hurting my brain – Parth Rajawat May 27 '22 at 16:00
  • @ParthRajawat You needed to hit Return between `./arithops` and `exitvalue=$?`. – Steve Summit May 27 '22 at 16:02
  • @ParthRajawat You're running the first two commands on a single line, so the second command is actually parameters to the first. Run your program first, *then* run `exitvalue=$?` on the next line. – dbush May 27 '22 at 16:03
  • Cutting to the chase, the bottom line is that after running your program that did `return 45;` from `main`, the shell variable `$?` will contain 45, as the exit status of your program. – Steve Summit May 27 '22 at 16:03
  • @dbush I got it, I GOT it!!!! thank you very much, this worked. Sorry I took a lot of your time here. – Parth Rajawat May 27 '22 at 16:07
  • @SteveSummit Yeah, so what does this 45 exit status mean ? Does it have any significance and how does it differ from 0 ? – Parth Rajawat May 27 '22 at 16:08
  • 1
    @ParthRajawat It means whatever you want it to mean. But now it's time for you to read the [linked duplicate](https://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c), or [Pepe N O's answer](https://stackoverflow.com/questions/72407187/return-in-c-and-c/72407322#72407322). – Steve Summit May 27 '22 at 16:09
  • Oh, So my program will be used by a prcoess in Operating system and whatever it will return the value, (45 in this case), this particular program will be used either a success or error ? – Parth Rajawat May 27 '22 at 16:14
2

For bash scripts or programs running from the command line (Linux & Unix), these are common exit codes (the return value from the main function in this case, or from calling exit()).

0 - OK
1 - Catchall for general errors
2 - Misuse of shell builtins (according to Bash documentation)
126 - Command invoked cannot execute
127 - “command not found”
128 - Invalid argument to exit
128+n - Fatal error signal “n”
130 - Script terminated by Control-C
255\* - Exit status out of range
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Pepe N O
  • 1,678
  • 1
  • 7
  • 11