C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 06-30-2009, 05:04 AM   #1
Registered User
 
Join Date: Nov 2008
Posts: 6
Display return value from a function

I want to print a return value of 1 or 0 or some other number in a c program to the screen.

Code:
  if ( readFile() != 0 )
        {
   
                fprintf(stderr, "Unable to read input file!\n");
                program_exit(E_FILES_READ);
        }
      printf("equals zero \n");

int readFile()
{
        char revAccName[51];
        char custSortCode[7];
        char custAccNum[9];
        char custAccName[19];
        char amount[12];
        char date[9];
        char id1[21];
        char id2[21];
        char id3[21];
        char reason[2];
        char record[292];
        cur = &head;
        for(;;)
        {
                if ( !fgets(record, 291, inFile) )
                        break;
                if (strlen(record) < 101)
                        return 1;
                strncpy(revAccName, &record[5], 50);
                revAccName[50] = 0;
                strncpy(custSortCode, &record[55], 6);
                custSortCode[6] = 0;
                strncpy(custAccNum, &record[61], 8);
                custAccNum[8] = 0;
                strncpy(custAccName, &record[69], 18);
                custAccName[18] = 0;
                strncpy(amount, &record[87], 11);
                amount[11] = 0;
                strncpy(id1, &record[106], 20);
                id1[20] = 0;
                strncpy(id2, &record[126], 20);
                id2[20] = 0;
                strncpy(id3, &record[146], 20);
                id3[20] = 0;
                strncpy(reason, &record[166], 1);
                reason[1] = 0;

                /* Allocate space for the record and hook it up to list. */
                *cur = (unpaids_t *)calloc(1, sizeof(unpaids_t));
                if (!*cur)
                        return 1;
                strcpy((*cur)->revAccName, &revAccName[strspn(revAccName," ")]);
                strcpy((*cur)->custSortCode, &custSortCode[strspn(custSortCode," ")]);
                strcpy((*cur)->custAccNum, &custAccNum[strspn(custAccNum," ")]);
                strcpy((*cur)->custAccName, &custAccName[strspn(custAccName," ")]);
                strcpy((*cur)->amount, &amount[strspn(amount," ")]);
                strcpy((*cur)->id1, &id1[strspn(id1," ")]);
                strcpy((*cur)->id2, &id2[strspn(id2," ")]);
                strcpy((*cur)->id3, &id3[strspn(id3," ")]);
                strcpy((*cur)->reason, &reason[strspn(reason," ")]);
                (*cur)->next = NULL;
                cur = &((*cur)->next);
        }
}
The program originally worked like this.

Since moving to a new unix box it fails. Do we think the version of the OS needs to explicitly have a return 0;

It's not my code it's someone elses and I am loathe to change anything because it has always worked before.

Sun 5.9 original

SunOS 5.10 now.

Adding in the return 0 fixes it. But the program does work on 5.9

I would also maybe like to print the return code any suggestions ?
mapleleafblue is offline   Reply With Quote
Old 06-30-2009, 05:52 AM   #2
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
Quote:
Originally Posted by mapleleafblue View Post
I want to print a return value of 1 or 0 or some other number in a c program to the screen.

Since moving to a new unix box it fails. Do we think the version of the OS needs to explicitly have a return 0;

It's not my code it's someone elses and I am loathe to change anything because it has always worked before.

Adding in the return 0 fixes it. But the program does work on 5.9
Of course you should add a return value. If the type of the function is int, it must return a value, even if that value is always the same (0).

The fact that it worked somewhere once does not mean it is or ever was correct. It is not proper C code without the return value, so don't feel bad about correcting it no matter who wrote it. The reason it probably worked before is because 0 == untrue in C, so perhaps the other compiler automatically assigned untrue to a function that had no proper return value; in other words, that programmer was relying on *luck* because this is "undefined behavior" (and this programmer obviously did not test it on more than one compiler and possibly ignored warnings on the one that does make it work).
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS

Last edited by MK27; 06-30-2009 at 05:54 AM.
MK27 is offline   Reply With Quote
Old 06-30-2009, 05:53 AM   #3
pwning noobs
 
Zlatko's Avatar
 
Join Date: Jun 2009
Location: The Great White North
Posts: 125
you need a return statement at the bottom of your function. It may seem to work on some systems because you are unlucky, but the return value will be undefined if there is not explicit return statement.
__________________
Sun Certified Java Programmer / Developer
IEEE CSDP
Zlatko is offline   Reply With Quote
Old 06-30-2009, 11:27 AM   #4
Registered User
 
Join Date: Oct 2008
Location: TX
Posts: 1,262
Quote:
Originally Posted by mapleleafblue View Post
Since moving to a new unix box it fails. Do we think the version of the OS needs to explicitly have a return 0;

It's not my code it's someone elses and I am loathe to change anything because it has always worked before.

Sun 5.9 original

SunOS 5.10 now.

Adding in the return 0 fixes it. But the program does work on 5.9

I would also maybe like to print the return code any suggestions ?
It's not the OS but the compiler version that needs to be looked at. If a function doesn't have an explicit return, the compiler will put one in for you. A free return statement is supplied at the end of each function definition, so running off the end causes control, but no value, to be returned to the caller. The OS has nothing to do with it so your best bet is to insert a return statement and not leave anything to chance or the compiler .

Here's something you can try. Compile the source on 5.9 and move the executable to the 5.10 box and run it there to see if that makes a difference. If backwards compatibility is preserved in the compiler versions, this should work and pinpoint the cause.
itCbitC is offline   Reply With Quote
Old 06-30-2009, 12:06 PM   #5
Registered User
 
slingerland3g's Avatar
 
Join Date: Jan 2008
Location: Seattle
Posts: 476
You do in fact have return values for your function:

Code:
...

if (strlen(record) < 101)
                        return 1;
...

if (!*cur)
                        return 1;

The problem here is that either one of those will have to be successful in order to satisfy the return value of the function call. From the use of your for(; you better!
slingerland3g is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Why only 32x32? (OpenGL) [Please help] Queatrix Game Programming 2 01-23-2006 02:39 PM
<Gulp> kryptkat Windows Programming 7 01-14-2006 01:03 PM
C++ FTP class won't work lord mazdak C++ Programming 8 12-18-2005 07:57 AM
Binary Search Trees Part III Prelude A Brief History of Cprogramming.com 16 10-02-2004 03:00 PM
Request for comments Prelude A Brief History of Cprogramming.com 15 01-02-2004 10:33 AM


All times are GMT -6. The time now is 07:28 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22