charlotte
charlotte

Reputation: 65

storing a negative integer in signed char

I'm trying to get a function that stores integers in char. I need to use char rather than int because I need to use the question mark (?) to terminate my loop. However, I can't seem to make my code work. Here's my work:

int main() {
    signed char num;
    scanf("%c", &num);
    if (num!='?') {
        printf("%c\n", num);
    }
    return 0;
}

When I input a negative number (say, -9), I get the output:

-

I tried using the integer print symbol (%d) rather than %c when I was printing the values, as I saw on this question: https://www.quora.com/Can-I-assign-a-negative-number-to-a-char-variable-Why-or-why-not but makes everything I input junky. ie when I input 2, it returns 50.

I was told that signed char should do the thing here, but I'm not sure that's the case now.

Thanks.

Upvotes: 0

Views: 559

Answers (3)

Steve Summit
Steve Summit

Reputation: 47933

When you have to do anything at all complicated, scanf is almost never the right tool for the job.

And, although it might not be obvious at first, the task "read a thing that's either an integer, or a question mark that indicates the end of the input" is definitely, in this sense, something that's too complicated for scanf. There's no good format specifier for that kind of "thing".

The easiest thing is to read a line of text (using fgets, not scanf), then start trying to figure out what's in it — question mark, integer, or something else. (What if the user types "x"?)

#include <stdio.h>

int main()
{
    char line[512];
    int num;          /* note int not char */

    if(fgets(line, sizeof(line), stdin) == NULL)
         printf("end of file or error\n");
    else if(line[0] == '?')
         printf("end of input\n");
    else if(sscanf(line, "%d", &num) == 1)
         printf("numeric input: %d\n", num);
    else printf("unexpected input!\n");
}

Note that this program still isn't perfect: If the user, types, say, "123x", this program will say it saw numeric input, and won't notice or complain about the extra x. Also, it only looks at the first character of the line to see if it's a ?, so it will also accept things line ?? or ?Cat in the hat. (There are ways to fix both of those things, but they get rather elaborate, and are probably more trouble than they're worth for a simple exercise.)

See also What can I use for input conversion instead of scanf?

Upvotes: 0

4386427
4386427

Reputation: 44274

If you want to scan an integer and at the same time scan a character like ? you can do:

  1. Try to scan an integer
  2. If scan for integer fails, try to scan a char

Like

int num;
char c;
if (scanf("%d", &num) == 1)
{
    // Okay you got an integer.

    c = num;  // BUT... watch out for over/underflow !!!
}
else if (scanf("%c", &c) == 1)
{
    // Okay you got a char
    if (c == '?')
    {
        // You got your ?
    }
    else
    {
        // Some other char

        // Some kind of error handling... perhaps
        c = 0;
    }
}
else
{
    // Input error that can't be recovered
    exit(1);
}

See online example https://ideone.com/kTPE0M

Upvotes: 2

0___________
0___________

Reputation: 67476

You use the wrong format:

it has to be (but you will have to enter 63 instead of ?):

      signed char num;
      scanf("%hhd", &num);

or

    char str[6];
    char num;
    fgets(str,5,stdin);
    if (str[0]!='?') {
        if(sscanf(str, "%hhd", &num) == 1)
            printf("%c\n", num);
        else {/* handle scanf error*/}

Upvotes: 0

Related Questions