Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to better compile C++ code

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about how to better compile C++ code. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

Just at the beginning of learning C++ code encountered a problem, this problem caused the program can not be fully run, after reading this article will be of great help to you. The following article in addition to learning the basic nature of C++ code, but also to conduct a comprehensive study of C++ code programming. The code is as follows:

# include int main () {chara, b; printf ("Please input the first character:"); scanf ("% c", & a); printf ("Please input the second character:"); scanf ("% c", & b); printf ("The two characters are% c,% c", aMagne b); return 0;}

The running result of the program is as follows: however, the following program can run normally:

The running results are as follows:

# include int main () {chara; int b; printf ("Please input the first character:"); scanf ("% c", & a); printf ("Please input the second integer:"); scanf ("% d", & b); printf ("The two characters are% c,% d", a, b); return 0;}

At that time, I was very confused about this problem, and it faded away over a long time. Yesterday, a classmate asked a question about the overloading of C++ operators. the following code shows what the while loop does. It seems that there is no difference in how the program runs after being commented out.

Iostream& operator > > (iostream& is, b & s). {char temp [b::MAX] =. {0}; is.get (); is.get (temp, b::MAX); if (strlen (temp) > 0). {strncpy (b.str, temp, b::MAX);} while (is & & is.get ()! ='). {continue;} return is;}

Sure enough, when the input string is less than b::MAX, everything is fine, when more than b::MAX, when entering * string enter, without waiting for the second string input, the program will display the contents of b1 and b2, and the content of b2 is the part of the input string that exceeds the length of b::MAX, indicating that the previous analysis is correct.

When I think of this problem today, I suddenly think of the problem caused by the previous use of two scanf in C++ language. is it also the problem that the input buffer is not automatically emptied? When you enter two characters in a row with scanf, enter * * characters and enter enter.

As a result, the newline character is entered as the second character, scanf will not distinguish between a newline character and an ordinary character, but when entering a character and then entering an integer, because of the type mismatch, scanf will ignore the newline character and wait for the correct integer to be entered before returning, so get the correct result, , I see!

It can be seen that the standard input stream in C++ has already dealt with newline characters, so it seems that using standard input to deal with special characters in C++ has to do some work.

The above is the editor for you to share how to better compile C++ code, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report