Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What should I do to get the whole return value of c-program from command line?

Tags:

c

bash

unix

return

I have a simple C-program "./my_program"

#include <stdio.h>

int main (int argc , char **argv) {


   unsigned int return_result = 0x474;
   printf("return_result = %d = 0x%x \n",return_result,return_result);
   return return_result;

}

As a result, this program prints:

return_result = 1140 = 0x474 

I want to get a return value of c-program within bash script. According to this link Anyway to get return value of c program from command line? I should get this variable from $?

But when I launch such commands consequence:

./my_program
echo $?

I get

116

It is obviously, that 116 = 0x74 (the lowest byte). But I'd like to get the whole unsigned int value. What is wrong? What should I do to get the whole return value of c-program from command line? This is not about only "unsigned int" type. What should I do in case if I created some complicated type, for example, - structure.

I tried this:

return ((unsigned int) return_result_section_size);

It doesn't work.

like image 333
Lucky Man Avatar asked May 07 '12 09:05

Lucky Man


People also ask

What is the value returned by the command?

The return value of a command is stored in the $? variable. The return value is called exit status. This value can be used to determine whether a command completed successfully or unsuccessfully.

What is C command line arguments?

What are Command Line Arguments in C? Command line arguments are nothing but simply arguments that are specified after the name of the program in the system's command line, and these argument values are passed on to your program during program execution.


1 Answers

Exit codes on Unix are restricted to a single byte. If you want to output more, you could write it to stdout or to a file instead.

Then why does the C standard decree that main returns int and not char? I have no idea...

like image 152
Thomas Avatar answered Oct 22 '22 00:10

Thomas