Data type error encountered when using getchar()

Publisher:Blissful444Latest update time:2015-08-07 Source: dzscKeywords:getchar Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
1.
This statement is usually used in the letter reading operation
while ((c = getchar() ) != EOF)
When doing the exercise, I did not pay attention to the importance of brackets and wrote
while (c = getchar() != EOF)
As a result, putchar(c) is garbled.
Analysis:
Although the returned bool value is 1, the value obtained by c is garbled. Given that there are only two judgment symbols in the statement
Guess that the symbol priority causes the garbled code?
It is speculated that the value assigned to c is int 1. But when it is output as char, it is garbled. Shouldn't it be treated as ACSII code?

In addition, I did not understand the basic principle of one-time call, which is to set an entry condition and change it during execution:
if ((c == ' ') || (c == t) )
if (flag == 0)
{
...
flag = 1;
}
2.
char s[] = {123,456}; //Compile error!
char s[] = {1,4}; //After compilation, the output s content is garbled! !
Can't char array be stored like that?
However, after using c = getchar();, even though c stores numbers, it is still of char type.
We can have s[i] = c;, and the output is also the correct value (keyboard input 123, s element is also 123);

when executing, a Segmentation fault (core dumped) error occurs. Since array operations are the main problem, it is initially determined that the error is caused by array out of bounds.
Yes, it is caused by array out of bounds! For array s[10], the actual range is s[0]~s[9]! Never store anything in s[10]. I did #! #! #! #! Fortunately, only the end character '' was stored, which does not affect the surface display effect. The principle of counting from 0 is a legacy of C language history. Although it is not in line with the custom, it has become a convention.
Keywords:getchar Reference address:Data type error encountered when using getchar()

Previous article:Problem of jumping to getchar() when using it
Next article:Summary of getchar() and EOF

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号