nagative return value?

This is a discussion on nagative return value? within the C Programming forums, part of the General Programming Boards category; I wrote this because I am learning about linux and return values and such, and I wanted to try some ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    452

    nagative return value?

    I wrote this because I am learning about linux and return values and such, and I wanted to try some stuff with

    Code:
    echo $?
    The program is nothing but a return value

    Code:
    #include <stdio.h>
    
    int main()
    {
      return 1;
    }
    Now, if x (the return value) is positive and less than 255, $? = x
    if x is negative, is returns some [positive value that apparently counts down from 255, ei -1 prints 255, -2 prints 254, etc. And going aove 255 seems to start looping back around to 0. Is this just a linux convention that restricts returns values to something between 0 and 255 to keep tings simple? Don't a lot of programs use negative values to indicate an error? How does this work if the values are forced into a positive?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    Quote Originally Posted by KBriggs
    if x is negative, is returns some [positive value that apparently counts down from 255, ei -1 prints 255, -2 prints 254, etc. And going aove 255 seems to start looping back around to 0. Is this just a linux convention that restricts returns values to something between 0 and 255 to keep tings simple?
    Cast those negative values to unsigned char and print them as ints. What do you get as output?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    452
    You get the return value. Cool. So linux casts the value to an unsigned char before echoing it? And since char has ASCII value between 0 and 255, it gives one of those regardless of where in the number line it is?

    I am not so much up on these things - how does a number outside the range of char get cast into the range/?
    Last edited by KBriggs; 06-30-2009 at 09:27 AM.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Nb. just to be anal: it is not "linux" you are referring to, it is the bash shell, which has a different (and much longer) history, and is simply the shell of choice generally on linux distros. You can use a different shell, altho I imagine they are all the same in this sense...
    Last edited by MK27; 06-30-2009 at 09:38 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

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    You only get the least significant byte back from the child program, either by a return from main(), or as the parameter to exit().

    The integer status result the parent program receives from the OS also includes such information as whether it exited normally, was killed, or crashed.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    First of all it's not a linux convention but the return value of the exec'd program is cast into an unsigned char by the shell (korn, bash, etc.).
    As main() returns an int the excess high order bits are discarded by the shell leaving 8 bits which are stored in the shell's $? built-in variable.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  2. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 01:39 PM
  3. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 01:08 AM
  4. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 09:01 PM
  5. Algorithm to walk through a maze.
    By Nutshell in forum C Programming
    Replies: 30
    Last Post: 01-21-2002, 12:54 AM

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