-3

I am trying to make a program to determine if the entered number is positive or negative with the conditional operator but when I enter the number the program doesn't print anything after

#include <stdio.h>

void main(void)
{    
  char number;   
  char sum;

  printf("enter your number ");
  scanf("%d",&number);

  sum = (number > 0)?  "positive" : "negative";

  printf("your number is %s\n", sum);
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 6
    Didn't you get any compiler warning such as `assignment to 'char' from 'char *' makes integer from pointer without a cast`? Consider this as ans error. – Jabberwocky Sep 29 '20 at 09:58

3 Answers3

1

It seems you mean the following

int number;

char *sum;

printf("enter your number ");

scanf("%d",&number);

sum = (number > 0)?  "positive" : "negative";

printf("your number is %s\n", sum);

That is the variable number should have the type int instead of char. Otherwise this call

scanf("%d",&number);

can invoke undefined behavior.

The variable sum should have the type char * instead of char because it is initialized by a string literal.

Pay attention to that if number is not greater than 0 it does not mean that it is a negative number. It can be equal to 0.:)

So this statement

sum = (number > 0)?  "positive" : "negative";

has a logical error. You could write at least like

sum = (number > 0)?  "positive" : "non-positive";

or use a more compound expression something like

sum = (number > 0) ?  "positive" : ( number == 0 ? "zero" : "negative" );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

sum holds a string, it should be defined as a char*, not a char.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

sum is character type and you are trying to save string in it. Either save a character in it or declare it as char*