Difficulty with scanf function (C)

Discussion in 'Mac Programming' started by jpoleto, Jan 8, 2009.

  1. jpoleto macrumors newbie

    Joined:
    Jan 8, 2009
    #1
    Hi all,
    This is my first post, so please excuse me if I come off a little (extremely) inexperienced.

    I'm trying to teach myself C because I am an engineering student and have seen that it can be useful for simple analysis problems. Using a very out of date book (Published 1987), I have been going through some lessons. One of the lessons asks you to write a program that determines the number of letters between two input letters. I am using xcode to compile the program, and it has caused me no other issues so far. The issue that I am having is that the output number makes no sense.
    The code that I have so far is as follows:

    Code:
    main()
    {
    int a, b, diff;
    printf("Please type two characters below:\n");
    scanf("%c %c", &a, &b);
    if(a>b)
    diff=a-b;
    else
    diff=b-a;
    printf("The number of letters between each character is %d.", diff);
    }
    
    When I run the program, I am given a result that is magnitudes larger than the number of letters in the alphabet, so something must be wrong here. Any thoughts?
    -John
     
  2. kpua macrumors 6502

    Joined:
    Jul 25, 2006
    #2
    I believe %c expects a pointer to the 8-bit char type. int is 32-bits, so scanf() is only writing to the first 8 bits of a and b, leaving uninitialized garbage in the last 24 bits.

    The solution is to either make variables a and b chars, or initialize a and b to 0 before calling scanf. I recommend the former, since it's more type correct.
     
  3. jpoleto thread starter macrumors newbie

    Joined:
    Jan 8, 2009
    #3
    Aha! That makes looooads of sense. This might also be why all my attempts to generate an ASCII table result in just letters and quesiton marks :p .
    Thanks,
    -John
     
  4. Guiyon macrumors 6502a

    Joined:
    Mar 19, 2008
    Location:
    North Shore, MA
    #4
    A couple of things:
    • Your program has no idea what scanf and printf are! You need to include the stdio.h library (at the minimum) which has the prototypes for scanf and printf. This can be done by inserting the next statement at the beginning of your code
      Code:
      #include <stdio.h>
    • main is defined incorrectly. In the C standard main MUST return an int so, at the minimum, main should be defined as
      Code:
      int main( void )
      and return a value. Once you get to more advanced topics, like parsing arguments, you could use the definition
      Code:
      int main( int argc, char **argv )
      *
    • Similar to the previous item, main MUST return a value. Simply adding a
      Code:
      return 0;
      to the end of your function would suffice.
    • Although not incorrect per-se,
      Code:
        if( a > b )
          diff = a - b;
        else
          diff = b - a;
      is considered bad form by some as it can make code a bit difficult to read. Simply adding in the braces helps quite a bit (IMHO):
      Code:
        if( a > b ) {
          diff = a - b;
        } else {
          diff = b - a;
        }
    • Now to your primary problem, the %c flag is looking for a char to store it's data in; now take a look at how a and b are defined. diff is defined and used correctly as %d expects an int

    One last thing: passing the -Wall flag to gcc will be your best friend. It enabled ALL (well, MOST) of GCCs warnings :)
     
  5. mdeh macrumors 6502

    Joined:
    Jan 3, 2009
    #5

    John, if you choose the "Standard Tool" under New Project, a lot of the things you missed will be set up for you ( eg the correct usage of main, etc). (Standard tool is meant to be used for command line C programs).
    Scanf is one of the trickier functions to use in C. A quick Google, actually even on this forum, should enlighten you and entertain you. :)
     
  6. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #6
    I agree with most things Guiyon wrote, except for the curly braces around single statements - but that's a matter of taste.

    Note that stdlib.h contains abs(x) for integer x, so you can remove the entire if-statement :)

    Have fun programming!
     
  7. jpoleto thread starter macrumors newbie

    Joined:
    Jan 8, 2009
    #7
    Wow! Thanks for all that help!

    I had stdio.h included, I just neglected to put that in the code I posted. Changing a and b from integers to characters seemed to fix the problem. As for the rest of the things mentioned like changing main() to main( void ) etc, the program ran fine without these; I think I still must be missing something important :p

    I can't believe I didn't think to just use the absolute value function! Maybe I shouldn't have dropped math as a major after all...

    Thanks again everyone,
    John
     
  8. Guiyon macrumors 6502a

    Joined:
    Mar 19, 2008
    Location:
    North Shore, MA
    #8
    The main problem with not defining main per the spec is that it becomes an implementation detail of the compiler; it may work fine on GCC but another compiler may fail or you might have unexpected behavior.
     

Share This Page