views:

599

answers:

6

For clarification purposes I need the program to print the numbers that are input for a and b, not the actual letters a and b.
Okay here's the revised program per yall's suggestions:

int main (int argc, char *argv[])  
{
   int a; /*first number input*/  
   int b; /*second number input*/  

   a = atoi(argv[1]); /*assign to a*/  
   b = atoi(argv[2]); /*assign to b*/  

   if (a < b)  
      printf("%s\n", a < b); /* a is less than b*/  
      else {  
         printf("%s\n", a >= b); /* a is greater than or equal to b*/  
      }  

   if (a == b)  
      printf("%s\n", a == b);  /* a is equal to b*/  
      else {  
         printf("%s\n", a != b); /* a is not equal to b*/  
      }  

   return 0;  
} /* end function main*/

lol, now when I run the program I get told

8 [main] a 2336 _cygtls::handle_exceptions: Error while dumping state   
Segmentation fault 

What the heck does that mean? (If you haven't noticed by now I am pretty hopeless at this stuff lol).

+7  A: 

You're asking printf() to print the values of the boolean expressions (which always resolve to 1 or 0 for true and false respectively).

You probably want your code to look more like:

if (a < b)
     printf("%s\n", "a < b"); /* a is less than b*/
else {
     printf("%s\n", "a >= b"); /* a is greater than or equal to b*/
}

To display the results as strings.

Michael Burr
Right on the mark regarding the boolean expressions. Note however that the printf() statements could trivially be replaced with puts( "a < b" ) and puts( "a >= b" ), respectively.
DevSolar
+6  A: 

This line:

if (a = b)

shouldn't it be

if (a == b)

Same here:

printf("%d\n", a = b);  /* a is equal to b*/

should be

printf("%d\n", a == b);  /* a is equal to b*/
Otávio Décio
A: 

Your problem is, you are trying to substitute logical expressions instead of integers. All of the above (a > b) ... evaluate to true or false (except a = b which assigns the value of b to a). What you should be doing, if you are trying to return the larger value, is the following:


    printf("%d\n", a > b ? a : b)

This says if a is greater than b, print a, otherwise b.

Edit: I think what you are actually looking for is to print out the words "a > b" etc. In which case, put them in the printf. When you place %d in the printf, it subs a specified integer value into that spot in the string.

I believe you want the following:


    if(a > b)
        printf("a > b\n");
    else
        printf("b >= a\n");

Is that correct?

kgrad
You forgot the newlines in the printfs in your second example.
strager
ah sorry, this has been remedied
kgrad
+1  A: 
printf("%s\n", a == b);

"%s" prints a string. a == b isn't a string, it's a boolean expression, resulting in 1 (true) or 0 (false).

So, your printf() attempts to print characters until it finds a null byte, starting at the position of the boolean expression... desaster.

DevSolar
+3  A: 

Based on your edit, I think you're looking for this:

#include <stdio.h>

int main (int argc, char *argv[]) {
    int a; /*first number input*/
    int b; /*second number input*/

    a = atoi(argv[1]); /*assign to a*/
    b = atoi(argv[2]); /*assign to b*/

    if (a < b)
        printf("%d < %d\n", a, b); /* a is less than b*/
    else
        printf("%d >= %d\n", a, b); /* a is greater than or equal to b*/

    if (a == b)
        printf("%d == %d\n", a, b);  /* a is equal to b*/
    else
        printf("%d != %d\n", a, b); /* a is not equal to b*/

    return 0;
}

This code:

wfarr@turing:~$ ./foo 1 2
1 < 2
1 != 2
wfarr
A: 

I'm assuming you want something like this...

Input:

a = 5, b = 7

Output:

5 < 7

5 != 7

If so, you need to print the integers a and b, as well as a string in between to show the relationship.

if( a < b ) {
    printf( "%d < %d\n", a, b );
}
else {
    printf( "%d >= %d\n", a, b );
}

// follow similar pattern for the next if/else block..
MahlerFive