man scanf
...
c Matches a sequence of width count characters (default 1); the
next pointer must be a pointer to char, and there must be enough
room for all the characters (no terminating NUL is added). The
usual skip of leading white space is suppressed. To skip white
space first, use an explicit space in the format.
----------
#include
typedef unsigned char BYTE;
void print_memory(BYTE *start, int size)
{
int i;
for(i = size - 1; i >= 0; --i)
{
printf("%p:%02x: %c\n",
start + i,
*(start + i),
*(start + i));
}
}
int main(void)
{
char a[] = "XXXXXXX";
print_memory(a, sizeof(a));
scanf("%3c", a);
print_memory(a, sizeof(a));
return 0;
}
0xbfffe4a7:00:
0xbfffe4a6:58: X
0xbfffe4a5:58: X
0xbfffe4a4:58: X
0xbfffe4a3:58: X
0xbfffe4a2:58: X
0xbfffe4a1:58: X
0xbfffe4a0:58: X
abcde
0xbfffe4a7:00:
0xbfffe4a6:58: X
0xbfffe4a5:58: X
0xbfffe4a4:58: X
0xbfffe4a3:58: X
0xbfffe4a2:63: c
0xbfffe4a1:62: b
0xbfffe4a0:61: a
----------
#include
typedef unsigned char BYTE;
void print_memory(BYTE *start, int size)
{
int i;
for(i = size - 1; i >= 0; --i)
{
printf("%p:%02x: %c\n",
start + i,
*(start + i),
*(start + i));
}
}
int main(void)
{
char a[] = "XXXXXXX";
print_memory(a, sizeof(a));
scanf("%3c%2c", a, a + 1);
print_memory(a, sizeof(a));
return 0;
}
0xbffff7a7:00:
0xbffff7a6:58: X
0xbffff7a5:58: X
0xbffff7a4:58: X
0xbffff7a3:58: X
0xbffff7a2:58: X
0xbffff7a1:58: X
0xbffff7a0:58: X
abcde
0xbffff7a7:00:
0xbffff7a6:58: X
0xbffff7a5:58: X
0xbffff7a4:58: X
0xbffff7a3:58: X
0xbffff7a2:65: e
0xbffff7a1:64: d
0xbffff7a0:61: a
----------
#include
typedef unsigned char BYTE;
void print_memory(BYTE *start, int size)
{
int i;
for(i = size - 1; i >= 0; --i)
{
printf("%p:%02x: %c\n",
start + i,
*(start + i),
*(start + i));
}
}
int main(void)
{
char ch1 = 'X', ch2 = 'Y';
print_memory(&ch2, 4);
scanf("%3c%2c", &ch1, &ch2);
print_memory(&ch2, 4);
return 0;
}
0xbfffe629:e6: æ
0xbfffe628:48: H
0xbfffe627:58: X
0xbfffe626:59: Y
abcde
0xbfffe629:63: c
0xbfffe628:62: b
0xbfffe627:65: e
0xbfffe626:64: d