C language printf to print the output at the same position, printf to print the output
The console prints the output at the same position, for example, progress 1%-> 100% is displayed at the same position. When I first learned the C language, I always wanted to do it. Unfortunately, I cannot query many materials. After more than six years, I thought of this problem again when I was idle. I decided to give it a try, although the C language was almost forgotten. We finally got it done. This time we were lucky, haha! Pai_^
# Include <stdio. h> # include <pthread. h> // # include <sys/time. h> // linux for sleep (seconds) and usleep (Microsecond) // # include <unistd. h> // windows for Sleep (millisecond) // # include <windows. h> // create a thread function return type pthread_t thread [1];/***** thread function **/void * printThread () {printf ("% s \ n ", "Threads start processing tasks"); printf ("processed:"); for (int I = 1; I <= 100; I ++) {if (I = 1) {// The number occupies 3 cells, and % occupies one cell printf ("% 3d %", I );} else {// return 4 lattice printf ("\ B % 3d % ", I) ;}// real-time standard output (without \ n, not refresh) fflush (stdout ); // 1 second sleep (1);} int main () {printf ("I am the main function, I am creating a thread, huh \ n "); /* Create thread */if (pthread_create (& thread [0], NULL, printThread, NULL )! = 0) {printf ("thread creation failed \ n");} printf ("thread creation successful \ n"); printf ("I am the main function, I am waiting for the thread to complete the task. A, haha \ n ");/* Wait for the thread to end */pthread_join (thread [0], NULL ); printf ("\ n thread has ended \ n"); return 1 ;}
The code is successfully tested in mac OS. The window system needs to introduce the pthread library in the compiler. For details, refer to: https://yq.aliyun.com/articles/35576.
Lite version
# Include <stdio. h> int main () {printf ("processed:"); for (int I = 1; I <= 100; I ++) {if (I = 1) {// The number occupies 3 cells, and % occupies one cell printf ("% 3d %", I );} else {// return 4 lattice printf ("\ B % 3d %", I);} // instant standard output (without \ n, fflush (stdout); // latency 10000 subtle = 10 Hao seconds = 0.01 seconds // usleep (10000); // latency simulation int times = 10000000; while (times --> 0) {}} return 1 ;}