I want to type in a number that is 10 digits long, then put the digits into an array. But for some reason, I get this random 2-digit numbers that seem to have nothing to do with my input (??).

char number[10];            //number containing 10 digits
scanf("%s",number);         //store digits of number
printf("%d\n",number[0]);   //print the 1st digit in the number
printf("%d\n",number[1]);   //print the 2nd digit in the number

Here is what I got:

Input:
1234567890

Output:
49
50

Actually, 49 should be 1, and 50 should be 2.

有帮助吗?

解决方案 2

Warning! Your code may invoke undefined behaviour!

But we'll talk about it later. Let us address your actual question first.

Here is a step by step explanation of what is going on. The first thing you need to know is that every character literal in C is actually an integer for the compiler.

Try this code.

#include <stdio.h>

int main()
{
    printf("%d\n", sizeof '1');
    return 0;
}

The output is:

4

This shows that the character literal '1' is represented as 4 byte integer by the compiler. Now, let us see what this 4 byte integer for '1' is using the next code here.

#include <stdio.h>

int main()
{
    int a = '1';
    printf("a when intepreted as int : %d\n", a);
    printf("a when intepreted as char: %c\n", a);
    return 0;
}

Compile it and run it. You'll see this output.

a when intepreted as int : 49
a when intepreted as char: 1

What do we learn?

The character '1' is represented as the integer 49 on my system. This is so for your system too. That's because in my system as well as yours, the compiler is using ASCII codes for the integers where '1' is 49, '2' is 50, 'A' is 65, 'B' is 66, and so on. Note that the mapping of the characters to these codes could be different for another system. You should never rely on these integer codes to identify the characters.

So when I try to print this value as integer (using %d as the format specifier), well what gets printed is the integer value of '1' which is 49. However, if we print this value as a character (using %c as the format specifier), what gets printed is the character whose integer code is 49. In other words, 1 gets printed.

Now try this code.

#include <stdio.h>

int main()
{
    char s[] = "ABC123";
    int i;
    printf("char  %%d  %%c\n");
    printf("----  --  --\n");
    for (i = 0; i < 6; i++) {
        printf("s[%d]  %d  %c\n", i, s[i], s[i]);
    }
    return 0;
}

Now you should see this output.

char  %d  %c
----  --  --
s[0]  65  A
s[1]  66  B
s[2]  67  C
s[3]  49  1
s[4]  50  2
s[5]  51  3

Does it make sense now? You need to use the %c format specifier when you want to print the character. You should use %d only when you want to see the integer code that represents that character.

Finally, let us come back to your code. This is how you fix it.

#include <stdio.h>

int main()
{
    char number[10];
    scanf("%9[^\n]", number);
    printf("%c\n", number[0]);
    printf("%c\n", number[1]);
    return 0;
}

There are two things to note.

  • I have used %c as the format specifier to print the character representation of the digits read.
  • I have altered the format specifier for scanf to accept at most 9 characters only where the characters are not newline characters. This is to make sure that a user cannot crash your program by inputting a string that is far longer than 9 characters. Why 9 instead of 10?. Because we need to leave one cell of the array empty for the null-terminator. A longer input would overwrite memory locations beyond the allocated 10 bytes for the number array. Such buffer overruns lead to code that invoke undefined behaviour which could either cause a crash or kill your cat.

其他提示

You are getting ASCII value of characters 1 and 2. Use %c specifier to print the digits.

printf("%c\n",number[0]);
printf("%c\n",number[0]);   //print the 1st digit in the number
printf("%c\n",number[1]);   

should do the job for you, what you see are ascii values.

your number array is an array of char, and so every element of it is a char. when you type:
printf("%d\n",number[0]);
you printing the chars as integers, and so you get the ASCII code for each char.

change your statement to printf("%c\n",number[0]); to print chars as chars not as ints

Warning! Your code invokes undefined behaviour!

char number[10];     // Can only store 9 digits and nul character
scanf("%s",number);  // Inputting 1234567890 (11 chars) will overflow the array!

Use fgets instead:

#define MAX_LEN 10
char number[MAX_LEN];
if(fgets(number, MAX_LEN, stdin)) {
     // all went ok
}

Once you have fixed this, you can fix the printing problem. You are printing the character code (number), and not the actual character. Use different type specifier:

printf("%c\n",number[0]);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top