C language printf to print the output at the same position, printf to print the output

Source: Internet
Author: User
Tags usleep

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 ;}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.