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.
Keywords:getchar
Reference address:Data type error encountered when using getchar()
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?
if ((c == ' ') || (c == t) )
if (flag == 0)
{
}
2.
char
char s[] = {1,4};
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.
Previous article:Problem of jumping to getchar() when using it
Next article:Summary of getchar() and EOF
- Popular Resources
- Popular amplifiers
Latest Microcontroller Articles
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
MoreDaily News
- How Lucid is overtaking Tesla with smaller motors
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Three steps to govern hybrid multicloud environments
- Three steps to govern hybrid multicloud environments
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Melexis launches ultra-low power automotive contactless micro-power switch chip
Guess you like
- Initialization operations required for C51 microcontroller to output printf
- Please analyze the function and principle of this circuit.
- [Exploration of RTOS and Linux controller cycloneDDS communication and encryption protection] A preliminary study of AC6 development environment
- How to achieve low power consumption when porting a program from MSP430FR4133 to MSP430FR6972?
- The latest proteus 8.9sp2
- Bidirectional 400V-12V DC/DC Converter Reference Design
- 6G is on the way, what is the terahertz technology behind it?
- 100W24v output board, change to 12V output
- How to copy the files used in the compiler and IDE installation directory to the project folder when creating a new CCS project
- Idle series--DIY an Openmv4R