Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Help with formatting strings like %d
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
QuantumPhysics




PostPosted: Tue Sep 04, 2012 4:38 pm   Post subject: Help with formatting strings like %d

I really want to understand what these are called and what they are used for? Possibly a link to a wiki page?

In c++ text statements programmers/coders usually place u% %d %s << or something along those lines, was I not clear? Sorry. I just dont know how they are used or what they are used for. Never used em'

Please explain Smile
Sponsor
Sponsor
Sponsor
sponsor
Zren




PostPosted: Tue Sep 04, 2012 5:06 pm   Post subject: RE:HELP! Never understood this or actually used it.

They are used to format the arguments passed along with the (format) string. It can be used for padding, precision, conversion, etc. It separates the locale (language) string from the variables/code. It also tends to be way easier to type variables into a string than doing " + var + " every time.

Here's a cheat sheet that popped up in google with 'format string C++'. Check out it's examples.

http://www.cplusplus.com/reference/clibrary/cstdio/printf/

I suggest trying to try making a format string to format a floating point number for money (note: never use floating points to represent money in a real application). Bonus points: make it a function that returns the string. Assumably there is a way to use format strings beside printf.

In case your wondering how printf gets away with a variable number of arguments, the feature is called varargs in Java. Not sure what it is called elsewhere. Anyone know?
[Gandalf]




PostPosted: Tue Sep 04, 2012 9:39 pm   Post subject: RE:HELP! Never understood this or actually used it.

You can call each of those a format specifier, however the string you pass into printf style functions is usually just called a format string.

Zren, standard C does in fact have variadic functions: functions which take a variable amount of arguments and are declared using the ... notation.

Edit: fixed the title for you.
QuantumPhysics




PostPosted: Wed Sep 05, 2012 6:23 pm   Post subject: Re: RE:HELP! Never understood this or actually used it.

Zren @ Tue Sep 04, 2012 5:06 pm wrote:
They are used to format the arguments passed along with the (format) string. It can be used for padding, precision, conversion, etc. It separates the locale (language) string from the variables/code. It also tends to be way easier to type variables into a string than doing " + var + " every time.

Here's a cheat sheet that popped up in google with 'format string C++'. Check out it's examples.

http://www.cplusplus.com/reference/clibrary/cstdio/printf/

I suggest trying to try making a format string to format a floating point number for money (note: never use floating points to represent money in a real application). Bonus points: make it a function that returns the string. Assumably there is a way to use format strings beside printf.

In case your wondering how printf gets away with a variable number of arguments, the feature is called varargs in Java. Not sure what it is called elsewhere. Anyone know?


Thank you for this post. Helped so much, I adore you for giving me the 'cheat sheet'. That's exactly what I was looking for thanks alot for the brilliant explanation, and thank you for the little homework assignment and the side notes Smile
P.S. I like your display picture theme. Its very eerie and spooky. I love the theme.

+1 karma
QuantumPhysics




PostPosted: Wed Sep 05, 2012 6:27 pm   Post subject: Re: RE:HELP! Never understood this or actually used it.

[quote="[Gandalf] @ Tue Sep 04, 2012 9:39 pm"]You can call each of those a format specifier, however the string you pass into printf style functions is usually just called a format string.

Zren, standard C does in fact have variadic functions: functions which take a variable amount of arguments and are declared using the ... notation.

Edit: fixed the title for you.[/quote]

Thanks alot for the fix Smile

So those string formats are also called format specifiers?

Also. How come when I use the printf("") syntax it gives me a template error that links me back to the c++ documentation with the std::traits? Is there a special library to include or is it just for C, because in C I can use it perfectly flawlessly, with no errors just with stdio.h
[Gandalf]




PostPosted: Wed Sep 05, 2012 8:20 pm   Post subject: RE:Help with formatting strings like %d

Yes. In C++, if you #include <cstdio> you should be able to use printf(). Don't use stdio.h in C++ as it is deprecated.
QuantumPhysics




PostPosted: Wed Sep 05, 2012 9:23 pm   Post subject: RE:Help with formatting strings like %d

Yea, I do not, but cstdio worked fine. thanks again.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: