Categories > Coding > C++ >

(Probably annoying by now) std::cout or std::printf?

New Reply

TERIHAX

Tires#3415(no space sig)

Posts: 1869

Threads: 80

Joined: Jul, 2020

Reputation: 22

Posted

title

 

i asked chatgpt and i still cant decide

 

srry if this is getting annoying

  • 0

  • Comment

https://i.gyazo.com/ecbdd0ec43a2e902248d519dbc6c11d2.png

chr1srbx, iCyphy, yranxzyMoonLaxionKanvaboykeStadiaHirokuatariDelta, RiceUsesArchBtwJJSploit_On_TopSyraxes_Pekkarev, <3

Posts: 2

Threads: 0

Joined: Apr, 2023

Reputation: 0

Replied

printf stands for print format

or the og spell it printfffffffffffffeeee

It's on the stdio.h header which is the std input output lib for C and it's the standard way of printing to the console using C, but since C++ still maintains C backwards compatibility so it's still there and to make it easier to access so they include it in the std namespace i guess.

 

There's not much a difference to be considered should I use this or that. Pretty sure printf is just printing string to conout using placeholder formatting like

 

printf("Hello, %s", "Bob"); %s for string or %d for digit or int stuff so that the %s is replaced by "Bob" or whatever I can't explain really well icl

 

Doesn't matter afaik, I mean it doesn't affect your program stuff that much so yeah stop 😶

Comments

TERIHAX 22 Reputation

Commented

wdym by "stop" at the end, should i stop making these annoying c++ threads

  • 0

rilyn 0 Reputation

Commented

@TERIHAX No. don't afraid to ask questions, just stop asking question like this.

  • 0

TERIHAX 22 Reputation

Commented

@rilyn alright

  • 0

  • 0

  • Comment

Posts: 1658

Threads: 80

Joined: Feb, 2020

Reputation: 87

Replied

Not really a wrong answer here. You can use both but you might want to use std::cout just because it's intended to be used in C++ and printf generally isn't.

Comments

TERIHAX 22 Reputation

Commented

alright then

  • 0

  • 0

  • Comment

0x90

dingleberry#2286

vip

Posts: 256

Threads: 25

Joined: Dec, 2020

Reputation: 28

Replied

std::cout is safe whilst printf may not be.

printf uses C runtime formatting which is notorious for causing some of the biggest bugs in history and causing billions of dollars in damage, outputting a string in printf is dangerous, whilst formatting them through a stream is not.

Comments

TERIHAX 22 Reputation

Commented

damn i didnt know it was that bad, also i rarely see you active anymore, what happened?

  • 0

SeizureSalad 37 Reputation

Commented

he has better things to do

  • 0

TERIHAX 22 Reputation

Commented

@SeizureSalad oh i can understand

  • 0

Added

@0x90 prob stupid question again, is using the system() function safe? like if i want to clear the console, like system("cls");

  • 0

Cyros 17 Reputation

Commented

@TERIHAX system isn't safe i dont think

  • 0

  • 0

  • Comment

Cyros

ligma

Posts: 977

Threads: 42

Joined: Feb, 2021

Reputation: 17

Replied

I use printf("yes") because I don't need weird-looking arrows in C++ that may look out of place

anyways it's probably better to use std::cout instead of printf() but I've never really had any issues with either so you can decide I guess

 

Afaik type safety; std::cout is 100% better, though printf() has better format flexibility
Reasons vary on what you are doing, if you want to format stuff easily it's recommended to use printf() but anything else should be handled with std::cout. Issues can occur with printf() like a data type doesn't match in printf() which causes a runtime error (afaik)

In an Object Oriented Approach or whatever it's better to use std::cout because it allows chaining and customizability of its behaviour (I think, but not sure)

Comments

TERIHAX 22 Reputation

Commented

alr ill just stick to cout for now

  • 0

  • 0

  • Comment

https://cdn.discordapp.com/attachments/1071679953894129755/1077097007673511967/banner.png

https://i.imgur.com/O3KpfKy.png

Posts: 524

Threads: 19

Joined: Nov, 2022

Reputation: 60

Replied

printf is a C function, can be used in C++ and has an entry in the standard library for some reason I'm unaware of. The difference between cout and printf is that cout doesn't require type specification with wildcards, whereas the latter does.

Comments

TERIHAX 22 Reputation

Commented

oh i see, alright then

  • 0

  • 0

  • Comment

Posts: 121

Threads: 8

Joined: May, 2020

Reputation: 30

Replied

In C++23, there is the print header which iirc could be used like this

#include <print>
#include <cstdint> 

auto main() -> std::int32_t {
    std::println("Hello, World!");

    return 0;
}

 

Though right now it's not even adopted by gcc or clang yet

Comments

TERIHAX 22 Reputation

Commented

so am i able to use this or should i just stick with cout

  • 0

smiley_man 30 Reputation

Commented

Not yet, but probably very soon so I guess just stick with cout for now

  • 0

TERIHAX 22 Reputation

Commented

@smiley_man alright

  • 0

  • 0

  • Comment

sniff

VoidableMethod

monday left me broken

vip

Posts: 1980

Threads: 186

Joined: Apr, 2021

Reputation: 17

Replied

std::cout cause yes

Comments

TERIHAX 22 Reputation

Commented

ok

 

/charesadasd

  • 0

  • 0

  • Comment

Posts: 124

Threads: 24

Joined: Feb, 2021

Reputation: 0

Replied

I recommend you to use printf, as it is faster, and simpler to use.

printf(char *a, ...);

The argument a represents a pointer to the first character of a string that contains the format (example: "Text: %s\nNumber: %d\n");

The argument(s) ... represents either pointers to first character in string that represents `%s`, or just a number, or some other value...

  • 0

  • Comment

https://ibb.co/j6KjGX5

Login to unlock the reply editor

Add your reply

Users viewing this thread:

( Members: 0, Guests: 1, Total: 1 )