Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Why printf? (http://www.chiefdelphi.com/forums/showthread.php?t=36371)

Mike 20-03-2005 14:10

Why printf?
 
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
Code:

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

whereas in c you have to write
Code:

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

Re: Why printf?
 
Quote:

Originally Posted by MikeWasHere05
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

Re: Why printf?
 
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

Re: Why printf?
 
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

Re: Why printf?
 
Quote:

Originally Posted by MikeWasHere05
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
Code:

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

whereas in c you have to write
Code:

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

Re: Why printf?
 
Thanks guys, I just wasn't too sure.


All times are GMT -5. The time now is 04:41.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi