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 realize the function of cursor rotation and countdown in C language

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the knowledge of "how to realize the cursor rotation and countdown function in C language". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Rotate the cursor

First of all, let me ask you a question: do you think that carriage return and line change are the same concept?

Many people tend to regard both of them as the same concept, why do they say so? Think about it. Do you think this is a carriage return symbol? Or a newline symbol? It seems that we do not specifically distinguish between these two concepts in our daily life. We say that'\ n' is not only a carriage return symbol, but also a newline symbol. This is the appearance of our confusion, but carriage return and line feeds are really two different concepts. Next, let's take you to understand carriage return and line feeds.

Enter: indicates that the cursor returns to the beginning of the current line.

Line wrapping: indicates that the cursor moves to the next line.

According to the above concept, the real carriage return symbol should be'\ rcarriage', that is, returning to the starting position of the current line, while the'\ n'we usually know should be the carriage return newline character, because its function includes both carriage return and line feed, that is, the cursor jumps to the beginning of the next line.

And the cursor rotation function we are talking about today is the use of the carriage return character'\ rpm. Next, don't talk too much nonsense, just go to the code! However, it will be explained to you according to the code.

# include#includeint main () {int index = 0; const char* lable = "| / -\\"; / /\ is a special character. If you want to take the character itself, add'\ 'while (1) {index% = 4; printf ("[% c]\ r", lable [index]); index++; Sleep (30);} system ("pause"); return 0;}

This program first defines an array lable of char type, in which the elements are stored in turn as'| / -\'. As to why two\\ are used at last, the corresponding explanation has been given in the program, and then an endless loop is constructed to print the array elements one by one. In order to ensure that the first element is printed after printing the final element, the array subscript is always 0x3 by taking the remainder of index to 4. This ensures circular printing of array elements.

Return to the starting position of the output line by'\ r 'ensures that each output is output at the starting position of a line, and then controls the interval of each output through the dormant function Sleep () to form a dynamic effect, thus realizing the function of cursor rotation.

Note: to use the sleep function, you need to refer to the windows.h header file, and the number unit in () in the sleep function is millisecond.

2. Countdown

Countdown function is relatively not difficult to achieve, directly on the code, and then give you a brief explanation.

# include#includeint main () {int I = 9; for (; I > = 0politic iMurb -) {printf ("[% d]\ r", I); Sleep (500);} system ("pause"); return 0;}

In fact, this code simply uses the'\ r 'and Sleep () hibernation function, and then implements the countdown function.

What the two programs have in common is that they both use the'\ r 'and Sleep () functions. See, this small point that we ignored in the C language can play such a huge role.

This is the content of "how to realize the cursor rotation and countdown function in C language". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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