Curiosité, apprentissage et soutien scolaire
16/04/2024
InscriptionConnexion
Langue : FR | ENG

> Retour sur la page d'accueil <

Forums de discussion

[Visual Studio] [C++] printf does not display instantly
Vous devez être connecté pour répondre
Auteur Message
ExaSec56

Membre
Posté le 01/12/2021 à 19:12:57

de ExaSec56
Membre
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

**Modérateur**
Posté le 01/12/2021 à 19:43:09

de TheLibrarian
**Modérateur**
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

Membre
Posté le 02/12/2021 à 08:35:01

de ExaSec56
Membre
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.
Vous devez être connecté pour répondre
Partager cette page sur les réseaux sociaux :

Utilisation des cookies sur ce site web :
- Si vous n'êtes pas inscrit à ce site, aucun cookie n'est stocké intentionnellement sur votre ordinateur.
- Si vous êtes inscrit à ce site, les cookies sont utilisés uniquement pour conserver votre connexion après chaque visite. Cette option est désactivable à volonté dans votre profil et est désactivée par défaut.
- Aucune autre information n'est stockée ou récupérée à votre insu, ni vos informations personnelles ni aucune autre quelle qu'elle soit. En cas de doute, n'hésitez pas à contacter l'administrateur de ce site .
- Même ce bandeau d'information n'utilise pas de cookies et donc sera affiché constamment à chaque visite sur toutes les pages du site.