user1852954
user1852954

Reputation: 41

How to check the value which my program returns?

i have created a program in C. I would like to know, how to check the value which my program returns ... I mean, at the end of the code my program return zero if no error occurred. If an error occurred my program should return 1. And that's the problem, i want to see if really 1 is returned if something went wrong. For memory leaks, etc I am using valgrind on Linux. Thank you for any help.

Upvotes: 4

Views: 929

Answers (4)

John Beckett
John Beckett

Reputation: 126

normally you don't have to consider the specific return code, you can use the shell logic to detect if something non-zero has been returned.

Just to print a message if failure was returned
./myprog || echo "Something went wrong"

or

only run myprog2 if myprog1 returns success
./myprog1 && ./myprog2

Upvotes: 0

Omkant
Omkant

Reputation: 9204

It's simple

Use echo $? on your terminal just after exexcution of your program.

It gives the return value of previously executed command

e.g.

./my_program.out  // execution of program 
echo $?    // checking the return value , printing on terminal

Upvotes: 2

Vijay
Vijay

Reputation: 67211

i guess you are looking for $?

call your program in a script and check the return value using $?

$?---it actually check the return code of the previously executed statement.

Upvotes: 2

Paul R
Paul R

Reputation: 212949

You can just print the special shell variable $?, e.g.:

$ ./my_program ; echo "status = $?"

Upvotes: 6

Related Questions