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

Username:   Password: 
 RegisterRegister   
 Question about Printf
Index -> Programming, C -> C Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Zampano




PostPosted: Thu Dec 13, 2007 12:36 pm   Post subject: Question about Printf

Sam (aka Tony Zhang) told me about he following code:
code:
#include <stdio.h>
int main()
{
int sum;
sum=4
printf("The value of sum is d% \n", sum);
return 0
}


My question pertains to printf. I know that %d and \n aren't actually written elements, but why does C include them in the quotation marks. I'm used to having it be like Turing, where text enclosed in quotations means that the actual text is written even if it is the name of a library function or something. And if C is determined to not accept text that is identical to syntax, why even keep the quotation marks to illuminate text? I know that its to distinguish from a variable, but then again, '%d' is on the text's side of the comma separator, and what's more, it's [i]inside[\i] the quotation marks!
Can someone please explain the purpose of this alien syntax?
Sponsor
Sponsor
Sponsor
sponsor
Nick




PostPosted: Thu Dec 13, 2007 1:30 pm   Post subject: RE:Question about Printf

try /n in turing within quatations you awill find that this is not language specific
Tony




PostPosted: Thu Dec 13, 2007 3:09 pm   Post subject: Re: Question about Printf

Zampano @ Thu Dec 13, 2007 12:36 pm wrote:
Can someone please explain the purpose of this alien syntax?

printf prints formated text. Simply inserting integers into the string (like in the example), might indeed seem alien, but there's more to a placeholder than "insert here" -- that's just the very basic functionality. It's more like: %[parameter][flags][width][.precision][length]type
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
md




PostPosted: Thu Dec 13, 2007 3:14 pm   Post subject: RE:Question about Printf

printf takes a variable number of arguments. If decides how many by parsing the string you pass as the first argument. %d means that the second argument is an integer; /n is an escaped newline character.
Clayton




PostPosted: Thu Dec 13, 2007 3:16 pm   Post subject: RE:Question about Printf

How does it know which integer it's printing?
Tony




PostPosted: Thu Dec 13, 2007 3:24 pm   Post subject: RE:Question about Printf

first placeholder will take first argument after the string. second placeholder will take the second argument... etc. They are all in order.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Clayton




PostPosted: Thu Dec 13, 2007 3:25 pm   Post subject: RE:Question about Printf

I see now, I didn't see the second argument to printf on that line. Smile
md




PostPosted: Thu Dec 13, 2007 4:53 pm   Post subject: RE:Question about Printf

You can even write your own functions that take an unknown number of arguments; a very useful feature!
Sponsor
Sponsor
Sponsor
sponsor
Nick




PostPosted: Thu Dec 13, 2007 5:05 pm   Post subject: RE:Question about Printf

well I'm a little curious how to do that md and also if it's diffrent in C++ could you explain that as well?

as far as I know it's just
code:
int foo (boo:int){
return 0
}
Clayton




PostPosted: Thu Dec 13, 2007 5:07 pm   Post subject: RE:Question about Printf

You can do it in Ruby too Wink
Tony




PostPosted: Thu Dec 13, 2007 5:12 pm   Post subject: RE:Question about Printf

heck, you can pass a block of code as a parameter in Ruby Laughing
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Geminias




PostPosted: Thu Dec 13, 2007 6:44 pm   Post subject: RE:Question about Printf

Quote:
well I'm a little curious how to do that md


http://publications.gbdirect.co.uk/c_book/chapter9/stdarg.html


You gotta think... If it is possible in C it shouldn't surprise you that i can be done in a higher level language. What should surprise you, is something you cant do in C that you can do in a higher level language. (nothing)

p.s.

code:

int foo (boo:int){
return 0
}


This is not how you declare variadic functions in C++. C++ declares them similar to C.

code:

variadicFunction(const char* formatString, ...);


or you can cram an STL container of variable arguments into a function template...
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  [ 12 Posts ]
Jump to:   


Style:  
Search: