Visit The New, Modern Unix Linux Community


Return value (int) from main to calling shell


 
Thread Tools Search this Thread
Top Forums Programming Return value (int) from main to calling shell
# 1  
Return value (int) from main to calling shell

What is the sytax to return an int from C program main back to calling shell?
Code:
#!/usr/bin/ksh 

typeset -i NO_RECS

$NO_RECS=process_file

# Process file is a C program that is set up to return an int from main. The
#program complies with no issues, but an error is generated when the shell
#calls the program. Is the syntax correct to return the int value to a shell
#variable?

Here is the C program:
Code:
int main(argc,argv)
{

int no_recs_tot ;

/* This is just a function within the c program that returns the int value */

no_recs_tot = bld_detail(v_out_path,v_in_path,inrec_cnt,v_src_data_dt);
 
return (no_recs_tot);

} /* End main */


Last edited by blowtorch; 09-25-2006 at 11:09 PM.. Reason: add code tags
# 2  
Code tags please. Like {code} int main(); {/code} but with [ ] instead of { }.

The syntax for returning a code to the shell is exactly as you show it. What might be wrong is the VALUE you return. Any non-zero value means some sort of error. Zero means success.
# 3  
Plus, on a POSIX compliant system :
Code:
int main()
{
	return 42001;
}

Code:
$ cc -o testc test.c
$ testc
$echo $?
17

because 42001 % 512 = 17. There is a max value allowed for return codes. So, if the OP has several thousand records the value will be meaningless.

Return codes are for program status.
# 4  
First of all,
Code:
#!/usr/bin/ksh 

typeset -i NO_RECS

$NO_RECS=process_file

Will not print the return code. The syntax is wrong to begin with, it should be
Code:
NO_RECS=$(process_file)

The second thing is that the return code of any command executed in the shell is not printed, but stored in a variable $?. To directly get the value that your C program has, you should do it like this:
Code:
#!/usr/bin/ksh
typeset -i NO_RECS
NO_RECS=$(process_file)

And your C program should be:
Code:
int main(argc,argv)
int argc; char *argv[];
{

int no_recs_tot ;

/* This is just a function within the c program that returns the int value */
no_recs_tot = bld_detail(v_out_path,v_in_path,inrec_cnt,v_src_data_dt);
 
fprintf(stdout,"%d",no_rec_tot);
} /* End main */


Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #794
Difficulty: Medium
1/10 has a finite binary representation.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to return programming to calling shell script?

Hi, I need to edit a shell script which is calling another shell script. At the moment the program returns to the command prompt after executing each called script. I need to change it to make it return to the calling script so that the user is able to make another choice to execute the next... (11 Replies)
Discussion started by: PTL
11 Replies

2. Programming

Function main returning int?

H friends, As we know, a function returns a value and that value is saved somwhere. like int Sum( int x, int y ) { return x + y; } Total = Sum( 10, 20 ); The value 30 is saved in variable Total. Now the question is, what int value does the function main return, and where is it... (5 Replies)
Discussion started by: gabam
5 Replies

3. Programming

How to pass int and return string in C?

hi I want to write a function which takes int as input and returns a string like this. char GetString(int iNo) { switch(iNo) { case 0: return "Zero"; break; case 1: return "One"; break; } } void main() { int i; printf("Enter... (1 Reply)
Discussion started by: atharalikhan
1 Replies

4. Programming

Handle int listen(int sockfd, int backlog) in TCP

Hi, from the manual listen(2): listen for connections on socket - Linux man page It has a parameter called backlog and it limits the maximum length of queue of pending list. If I set backlog to 128, is it means no more than 128 packets can be handled by server? If I have three... (3 Replies)
Discussion started by: sehang
3 Replies

5. Shell Programming and Scripting

main program is not calling small other programs

I am trying to understand a program in a book and this program suppose to call other programs which are in the same folder, the other programs are called 'lu' and 'add' but for some reason when it gets to the last line of each case to call these programs there is an error message saying ./rolo:... (2 Replies)
Discussion started by: bartsimpsong
2 Replies

6. Shell Programming and Scripting

$? and main return value

It seems $? can only contain one byte. see below: int main() { return 0x12345678; } After I run the code and check the $? value it output 120, obviously it is the decimal of the last byte of 0x12345678. And if I return a value less than 255, the $? value seems right. Why? and... (3 Replies)
Discussion started by: vistastar
3 Replies

7. UNIX for Dummies Questions & Answers

calling process and going back to the main loop

hi everyone , i want to read an option and depending on the option call the program .For ex #! /bin/ksh export JAVA_HOME=/home/oracle/jdk1.6.0_20 echo " Please enter mod-modeler, dev - sqldeveloper" read choice if ; then echo ' SQL DEVELOPER IS STARTING NOW ... ' cd... (0 Replies)
Discussion started by: kdev
0 Replies

8. Shell Programming and Scripting

calling pl/sql procedure from shell and return values

How could I call an Oracle PL/SQL procedure from any shell (bash) and catch returning value from that procedure (out param) or get a returning value if it's a function. also, I got into trouble when I tried to send a number as a param #!/bin/bash -e username=$1 pwd=$2 baza=$3... (0 Replies)
Discussion started by: bongo
0 Replies

9. Programming

Help - Cast converts default int return type

What does the warning message 724 "Cast converts default int return type to." tell me. I am new to C. (used it some in college). We are migrating our c-code to 10.2.0.3.0. The programs compiled clean. However there were several warning messages that kick out. The most prominent warning is: ... (5 Replies)
Discussion started by: rtgreen
5 Replies

10. Programming

c++ calling main() function

i just finished a project for a c++ class that i wrote at home on my computer, compiled with gcc. when i brought the code into school it would not compile, it would complain that cannot call main() function. at school we use ancient borland c++ from 1995. anyway my program has 20 different... (3 Replies)
Discussion started by: norsk hedensk
3 Replies

Featured Tech Videos