Thread: Display return value from a function

  1. #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 ?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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).
    Last edited by MK27; 06-30-2009 at 05:54 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    pwning noobs Zlatko's Avatar
    Join Date
    Jun 2009
    Location
    The Great White North
    Posts
    132
    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.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    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.

  5. #5
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    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!

Popular pages Recent additions subscribe to a feed

Similar Threads

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