Log in

View Full Version : Why printf?


Mike
20-03-2005, 14:10
Why do we use printf? I started c at the beginning of 6 weeks (minus about a week of c++), but knew PHP before that. In PHP you use the print statement where you just go

<?php
print "The variables value is: $variable";
?>

whereas in c you have to write

printf("The variables value is: %d", variable);



Can you use print() in c? If so why doesn't FIRST?

Greg Marra
20-03-2005, 14:40
Can you use print() in c? If so why doesn't FIRST?

I am not sure that C has a print() function. If it did, it would be functionally identical to printf, I imagine. PHP is an entirely different language than C, and they have different syntax and commands and functions.

Don't forget, not all computers have the same outputs, so you can 'print' to several different things (a printer, a serial connection, a monitor).

Dave Flowerday
20-03-2005, 15:08
PHP is an interpreted language, but C is compiled. In C, everything in between the quotes is stored verbatim in the binary image that gets executed. By the time the program is compiled, your program no longer knows what the variable names were so it could not parse the variables out of the string. Even if it did know the variable names, it would need special knowledge of where the variable was actually stored.

I can't think of any way that it would even be possible to do this in C without doing some pre-processing before it got to the compiler.

Validius
20-03-2005, 15:10
printf is print formatted

I know that in perl there is both print and printf but printf is far more powerfull

jdiwnab
20-03-2005, 15:44
Why do we use printf? I started c at the beginning of 6 weeks (minus about a week of c++), but knew PHP before that. In PHP you use the print statement where you just go

<?php
print "The variables value is: $variable";
?>

whereas in c you have to write

printf("The variables value is: %d", variable);


Can you use print() in c? If so why doesn't FIRST?

There isn't much of a difference between how the two functions are coded. Print() has the varible listed in the string. Printf() has the varible outside of the string. They work about the same. One difference is the d in %d could also be %f for floats and other things like that. This lets you say the type of varible you think you are going to see. It could truncate a long float if you don't need to see all of it. Or other things like that. Printf() is more powerful, and is coded about the same. Printf() just adds one more step.

Mike
20-03-2005, 15:54
Thanks guys, I just wasn't too sure.