Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jpoleto

macrumors newbie
Original poster
Jan 8, 2009
3
0
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
 

kpua

macrumors 6502
Jul 25, 2006
294
0
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.
 

jpoleto

macrumors newbie
Original poster
Jan 8, 2009
3
0
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
 

Guiyon

macrumors 6502a
Mar 19, 2008
771
4
Cambridge, MA
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 :)
 

mdeh

macrumors 6502
Jan 3, 2009
345
2
Hi all,
I am using xcode to compile the program
-John


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. :)
 

Sander

macrumors 6502a
Apr 24, 2008
520
67
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!
 

jpoleto

macrumors newbie
Original poster
Jan 8, 2009
3
0
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
 

Guiyon

macrumors 6502a
Mar 19, 2008
771
4
Cambridge, MA
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

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.