小编Nit*_*esh的帖子

如何使用scanf函数读取特殊字符(标点符号,超标,冒号)?

我写了一小段代码,可以从键盘输入地址.但是,我无法弄清楚如何能够读取特殊字符,例如hypen,冒号等.您能否建议对我的代码进行一些编辑:

#include<stdio.h>

main()
{
       char address[80];


       printf("Enter address: ");
       scanf("%[a-z | A-Z | 0-9]", address); //How may I include characters like hypen.
       printf("\n\n%s\n\n", address);
}
Run Code Online (Sandbox Code Playgroud)

我得到的输出:

Enter Address: Plot No - 16, Palm Grooves, Nagpur - 440022, India

Plot No
Run Code Online (Sandbox Code Playgroud)

没有逗号,没有连字符,没有显示数字.

感谢您的帮助和评论.

c scanf character special-characters

3
推荐指数
1
解决办法
9543
查看次数

为什么输出错了?

#include<stdio.h>

main()
{
      unsigned  x=1;
      signed char y=-1;
      clrscr();
      if(x>y)
       printf("x>y");
      else
     printf("x<=y");
}
Run Code Online (Sandbox Code Playgroud)

有符号字符的值从-128增加到127.所以预期的输出应该是'x> y',但事实并非如此.编译器给出输出 - "x <= y".你能解释一下原因吗?

c types

1
推荐指数
1
解决办法
118
查看次数

标签 统计

c ×2

character ×1

scanf ×1

special-characters ×1

types ×1