0

I have a program in C, which exits with code 1:

#include <stdlib.h>

...
datatype pop(stack* st){
    if (empty(st)) exit(1);
    return st->data[st->sp--];
}
...

int main(void){
    ...
    // a is empty at this time
    pop(a); 
    ...
    return 0;
}

And I have a bash script:

run() {
    gcc -Wall -W -Wshadow -g -c "$filename.c"                                                                                                        
    echo "Exit code $?"                                                                                                                  
    gcc -Wall -W -Wshadow -g "$filename.o" -o "$filename" -lm  
    ./"$filename"
}

When I run a program, I get Exit code 0, shouldn't I get Exit code 1?

6
  • 1
    Like many *nix commands, gcc returns "0" on success, else "1" for failure. You can fine-tune this behavior using gcc's -pass-exit-codes flag. So your when your echo "Exit code $?" displays 0, it means everything went OK ;)
    – paulsm4
    Commented Nov 23, 2019 at 23:30
  • The Bash script seems to have invalid syntax for what probably should be a function declaration – it should be either run() {...} or function run {...} (or function run() {...}). Commented Nov 23, 2019 at 23:37
  • @BenjaminW. yes it's run() {...}, will edit.
    – ramazan793
    Commented Nov 23, 2019 at 23:38
  • 3
    $? is the exit for of the previous command. The exit code you're seeing is from gcc, not your program. Move the echo. Commented Nov 24, 2019 at 0:21
  • 3
    @ramazan793: Q: what is your "$?" reporting? A: It's reporting the exit status *of your first gcc invocation* *NOT* your program. If you want to capture your program's status ... put "$?" after your program executes ;) ALSO: As you're probably aware, you can combine both "compile" and "link" in the same gcc command, if you wish to.
    – paulsm4
    Commented Nov 24, 2019 at 0:22

1 Answer 1

1

As note by comments, the main issue with current script is that it shows the GCC status, and not the run status. Also note that putting the 'echo' will change the $?. Assuming you want a 'combo' function that will build, execute, and return the status

run () {
    gcc -Wall -W -Wshadow -g -c "$filename.c" || return $?
    gcc -Wall -W -Wshadow -g "$filename.o" -o "$filename" -lm  || return $?
    ./"$filename"
}
run
Status saved in $X
X=$?
echo "Status=$?

The status of the 'combo' is saved in X, zero if everything is OK, non-zero otherwise (including compile error, build error, etc.)

2
  • echo "Exit code $?" is fine so long as it is the next statement after the program invocation -- that will yield the previous exit code, not the exit code of echo
    – M.M
    Commented Nov 24, 2019 at 9:44
  • 1
    The challenge with the 'echo $?' and variant is how to do add a branch AFTER the echo based on the $?. 'echo $? ; [ $? = 4 ] && echo "Crash" will not print "Crash" even when the program return status 4.
    – dash-o
    Commented Nov 24, 2019 at 12:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.