The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Calling shell functions from another shell script jisha Shell Programming and Scripting 6 04-05-2008 04:29 PM
How to pass two or more parameters to the main in shell script pinky UNIX for Dummies Questions & Answers 0 10-12-2007 11:54 AM
calling a prg from the shell!!! andy2000 Shell Programming and Scripting 4 03-31-2007 04:02 AM
Calling subscript but sleep halts the main script doublejz Shell Programming and Scripting 1 09-12-2005 02:08 PM
c++ calling main() function norsk hedensk High Level Programming 3 01-22-2003 08:28 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-25-2006
flounder flounder is offline
Registered User
  
 

Join Date: Jun 2002
Location: PA
Posts: 17
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 10:09 PM.. Reason: add code tags
  #2 (permalink)  
Old 09-25-2006
Corona688 Corona688 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 1,909
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 (permalink)  
Old 09-25-2006
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,643
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 (permalink)  
Old 09-25-2006
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
Supporter
  
 

Join Date: Dec 2004
Location: Singapore
Posts: 2,350
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 */
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 07:52 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0