Curiosity, learning and homework help
11/10/2024
Sign upLog in
Language: FR | ENG

> Back to the home page <

Discussion forums

[Visual Studio] [C++] printf does not display instantly
You have to be logged in to reply
Author Message
ExaSec56

Member
Posted on 01/12/2021 at 19:12:57

by ExaSec56
Member
Hello

I have another question. I am trying to make a print in my C++ code.
When I use
printf("My text \n");

the print is done at the end of the execution of the program.
However, when I use
std::cout << "My text" << std::endl;

the print is made instantly.

What is the difference between using printf and cout?
Is it possible to use printf and get an instant print?
TheLibrarian

**Moderator**
Posted on 01/12/2021 at 19:43:09

by TheLibrarian
**Moderator**
Actually, when you use printf which comes from C language, the print is done at the end of execution because stdout is buffered. If you want to print instantly, you can specify it by adding an instruction to release stdout from the buffer as follows
printf("My text \n");
fflush(stdout);

In that case, it will work as well as std::cout.

However, C++ gives us std::cout. Why don't you use it instead of using a C function in a C++ code?
--------------------------
ExaSec56

Member
Posted on 02/12/2021 at 08:35:01

by ExaSec56
Member
Yes, I should use cout. It is just that I am used to write C codes and so I usually use C functions that I know.
I will try to take more advantage of the abilities of C++ instead of still coding in C language while writing a C++ code. 😄

Thank you.
You have to be logged in to reply
Share this page on social media:

Use of cookies on this website:
- If you are not a member of this website, no cookie is intentionally stored on your computer.
- If you are a member of this website, cookies are only used to keep your connection after each visit. This option can be deactivated at will in your profile and is deactivated by default.
- No other information is stored or retrieved without your knowledge, neither your personal information nor any other whatsoever. If in doubt, do not hesitate to contact the administrator of this website .
- Even this information banner does not use cookies and will therefore be displayed constantly on each visit on all pages of the website.