Thread: odd function is odd

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    23

    Cool odd function is odd

    I am trying to learn how to call and write functions using an old book, "C By Discovery" It is a good book.

    My problem is the following code returns the 'odd' message only on the numbers 3, and 11. At times the prog will return others but they aren`t consistent and I haven`t gotten 9, 17, and 19 to show up as odd at all.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int  inputint;
    
        printf ("Enter a positive even ");
        printf ("number that is less than 20.\n");
    
        scanf  ("%d", &inputint);
    
        if (inputint < 20) {
            if ( odd(inputint) )
                printf ("Sorry that number wasn`t even.\n");
            else if (inputint <= 0)
                printf ("That number wasn`t positive.\n");
        }
        else
                printf ("That number was too big.\n");
    
        system("PAUSE");
        return 0;
    
    }
    
    /* ******************odd***************************** */
    /*  odd() returns a 1 if intvar is odd
    */
    
        odd (int intvar)
        {
                return(intvar & 2);
        }
    I`m using CodeBlocks with Mingw compiler. I also tried this on DevC++ and it does the same thing. The compiler griped right at first that I was using an 'implicit' function but then carried on to run the program with the above mysteries.

    Is this because this 'odd' function is an implicit function? If so how do I make it an explict function?

    Thanks, Plain

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Either move odd from the bottom (below main) to the top (above main) in your file (in this way, the function definition would also act as the function declaration), or else place a function prototype of odd before main.

    Code:
    int odd (int intvar);
    ...
    int main(void)
    Also note that since you are returning something (in your case an int) you need to declare odd as returning that type.
    Last edited by kermit; 10-04-2009 at 08:59 PM.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Your main problem is not caused because odd() is implicitly declared, although you should always explicitly declare your functions, and their return types. If your book encourages functions as you posted above, you really should get a newer one. For explicitly declaring, do something like:
    Code:
    int odd(int);
    int main(void)
    {
    ...
    }
    int odd(int intvar)
    {
    ...
    }
    You can also just put the function itself above main(); but make sure you explicitly tell the compiler that it returns int. Leaving the int off is acceptable in the older C standard, but I can think of no logical reason to do so. Be explicit so your code is more clear.

    Anyway, the problem is in your odd() function. Do you know what that code does? It appears that you're mixing two different ways to determine whether a number is odd. The first way is to divide by two and check the remainder (the % operator does this). The second is to check the value of the least significant bit (bitwise anding with 1 does this; & is the bitwise and operator). So either change your & to a %, or your 2 to a 1. But make sure you understand what's going on when you do that.

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    23

    Talking

    Kermit and cas!
    Thank You!

    I can`t believe I had the wrong operator in the function! The program works now perfectly with % instead of &. What a dope! I appreciate you guys help! And yes the book is old and doesn`t use int before integer variables. I didn`t know I could add it to 'odd' but now I do.



    Plain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM