linux%3c%3c特殊符号解释,有关scanf("%3c%2c")的问题

本文通过几个具体的C语言程序示例,展示了如何利用scanf函数的不同格式字符串来读取固定数量的字符,并将它们存储到指定的内存位置。示例包括了对多个连续字符的读取,以及对特定字符位置的指定。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值