Question:
What does "return 200" do in a program in C?
Cesar Soto
2013-05-28 09:01:45 UTC
I am really new at programming and we are using C.

For example:
#include "bergmann.h"
31 #include "string.h"
32
33 int main(int argc, char **argv)
34 {
35 FILE *infile;
36 FILE *outfile;
37 char in[100];
38 int c;
39 char *efl;
40 bool nl = false;
41
42 efl = getenv("EFLECH");
43 if(efl == NULL) {
44 printf("*** undefined environment EFLECH\n");
45 return 200;
46 } else {
47 assign(efl,eflech);
48 }
49
50 if(argc != 2)
51 return 1;
52
53 assign(argv[1], in);
54 makeextension(in, "lic", true);
55 if((infile = eflopen(in, "r")) == NULL) {
56 printf("*** Error reading %s\n", in);
57 return 1;
58 }
59
60 if((outfile = eflopen("licence.c", "w")) == NULL) {
61 printf("*** Error writing licence.c\n");
62 return 1;
63 }
What does return 1 do? What does return 200 do?Why would you use it? and what does int main(int argc, char **argv) do?
Four answers:
lansingstudent09101
2013-05-28 09:52:41 UTC
At the lowest level,it puts the number "200" into the register EAX, and jump back to the statement after the one that called this function, (or if this is the main function, the program is over) this means that if you have a variable for another function:



Take as a simple example, this function.



int addFive(a){

return a + 5;

}



because it returns, a value, you can have a higher statement:



int b = addFive(2);



This statement will take the value in EAX and store it to the variable a. You can just as easily ignore that returned value.



In the case of the main() function, the value is still just put into EAX. The operating system can check this on exit. In Linux it's very common to execute a program from the command line and if it returns a non-zero value, exit the script and report the error. In windows, this is less common.



Note: There are some caveats to the program being over on the return, for instance, a "finally" block may be executed AFTER the "return" is called, but these are exceptions, not the rule (pun intended).
Eric
2013-06-05 04:39:46 UTC
You return 1, or anything other than 0, to indicate that your program ran into an error.

You usually return 0, to indicate that the program successfully ran and ended normally.

int main is where the main function where everything starts.

int argc is the argument count, the number of arguments passed to the program including the invoking program.

char **argv is the same as char *argv[] is a char array of the arguments passed to the program.

argv[0] would be the executable's name.
mantilla
2016-08-07 05:09:33 UTC
A * in entrance of a pointer method you're getting the worth (de-referencing the pointer) saved in the deal with where the pointer features to. Should you put nothing in entrance of it you will get the vicinity in memory the place the pointer points to so its two special matters on this case you wish to have to examine the tackle no longer the exact value so the code is proper. The return would simply discontinue execution of the operate right there on this case in view that you find that p is NULL its invalid so there is no factor in continuing execution if you're going to work with p on account that you recognize its invalid and can most of the time cause an exception.
Steven
2013-05-28 09:06:22 UTC
Returns the number 200


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...