有没有办法将 getchar() 中的值更改为 C 中的整数?
我正在用 C 编写一个程序,其中它将请求响应,然后使用数字的第一次出现(使用 isdigit() ),然后将其分配给变量以供以后使用。经过一番测试后,我意识到它的 ascii 值为 10,即换行。这是代码片段。
while(isdigit(ch) == 0){
ch = getchar();
}
ch = getchar();
star = ch;//value is being detected as 10, which is line feed.
我在将 ch 分配给 star 时尝试将其转换为 int,这并没有改变结果值 10。我的问题是在 ch 的分配中,还是语法很差?
I'm working on a program in C in which it will request a response, then use the first appearance of a digit (using isdigit() ) and then assign it to a variable for later use. After testing a bit I realized it was taking the ascii value for 10, which is line feed. Here is the code snippet.
while(isdigit(ch) == 0){
ch = getchar();
}
ch = getchar();
star = ch;//value is being detected as 10, which is line feed.
I tried casting ch to int when assigning it to star, which did not change the resulting value of 10. Is my issue within the assignment of ch, or is it poor syntax?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的问题是你看到一个数字,然后你读下一个字符
只需使用你已有的 ch 中的值,加上你需要将 '0' 转换为 0,不是同一件事
YOur problem is that you see a digit, then you read the next character
Just use the value in ch that you have already, plus you need to convert '0' to 0, not the same thing