www.digitalmars.com         C & C++   DMDScript  

D - printf()

reply "Andrew Edwards" <edwardsac spamfreeusa.com> writes:
Should the following be legal?

==============
import stream;

void main()
{
  printf("%d + %d = %d"\n);        // does not require stream import.
  stdout.printf("%d + %d = %d"\n); // requires stream import.
}
Jul 05 2003
parent reply "Walter" <walter digitalmars.com> writes:
Yes.

"Andrew Edwards" <edwardsac spamfreeusa.com> wrote in message
news:be8bb6$9ra$1 digitaldaemon.com...
 Should the following be legal?

 ==============
 import stream;

 void main()
 {
   printf("%d + %d = %d"\n);        // does not require stream import.
   stdout.printf("%d + %d = %d"\n); // requires stream import.
 }

Jul 08 2003
parent reply "Andrew Edwards" <edwardsac spamfreeusa.com> writes:
"Walter" <walter digitalmars.com> wrote in message
news:bedtlp$2bi0$2 digitaldaemon.com...
 Yes.

The concern is that D allows us to compile and execute the program without providing the variables to resolve the conversion specifiers; resulting in output of logical garbage. For example, compiling and executing the code as submitted earlier results in the following output: 1244984 + 4202759 = 1 4284568 + 1244984 = 4202759 Shouldn't the compiler prevent that?
Jul 08 2003
parent reply "Walter" <walter digitalmars.com> writes:
"Andrew Edwards" <edwardsac spamfreeusa.com> wrote in message
news:beduip$2cho$1 digitaldaemon.com...
 The concern is that D allows us to compile and execute the program without
 providing the variables to resolve the conversion specifiers; resulting in
 output of logical garbage.

 For example, compiling and executing the code as submitted earlier results
 in the following output:

 1244984 + 4202759 = 1
 4284568 + 1244984 = 4202759

 Shouldn't the compiler prevent that?

The compiler doesn't parse the printf format string any more than C does. Right now, printf is just the C printf. There have been many proposals for a typesafe D printf, but there isn't an official one yet.
Jul 08 2003
next sibling parent reply "Nic Tiger" <tiger7 progtech.ru> writes:
Intel C/C++ compiler DOES analyze string formats for ...printf family of
functions and issues warning if parameters doesn't coincide.

Maybe for D it is not necessary (printf should be replaced with something
better), but is it very difficult to provide this kind of check in DMC? Of
course, check should be performed only for static format strings.

Nic Tiger.

"Walter" <walter digitalmars.com> wrote in message
news:beeph4$4qh$2 digitaldaemon.com...
 "Andrew Edwards" <edwardsac spamfreeusa.com> wrote in message
 news:beduip$2cho$1 digitaldaemon.com...
 The concern is that D allows us to compile and execute the program


 providing the variables to resolve the conversion specifiers; resulting


 output of logical garbage.

 For example, compiling and executing the code as submitted earlier


 in the following output:

 1244984 + 4202759 = 1
 4284568 + 1244984 = 4202759

 Shouldn't the compiler prevent that?

The compiler doesn't parse the printf format string any more than C does. Right now, printf is just the C printf. There have been many proposals for

 typesafe D printf, but there isn't an official one yet.

Jul 08 2003
parent reply "Walter" <walter digitalmars.com> writes:
"Nic Tiger" <tiger7 progtech.ru> wrote in message
news:bef1rr$dag$1 digitaldaemon.com...
 Intel C/C++ compiler DOES analyze string formats for ...printf family of
 functions and issues warning if parameters doesn't coincide.

 Maybe for D it is not necessary (printf should be replaced with something
 better), but is it very difficult to provide this kind of check in DMC? Of
 course, check should be performed only for static format strings.

No, it's not that hard.
Jul 08 2003
parent reply Burton Radons <loth users.sourceforge.net> writes:
Walter wrote:
 "Nic Tiger" <tiger7 progtech.ru> wrote in message
 news:bef1rr$dag$1 digitaldaemon.com...
 
Intel C/C++ compiler DOES analyze string formats for ...printf family of
functions and issues warning if parameters doesn't coincide.

Maybe for D it is not necessary (printf should be replaced with something
better), but is it very difficult to provide this kind of check in DMC? Of
course, check should be performed only for static format strings.

No, it's not that hard.

Harder than safe varargs, and hackish - either you put in an ugly syntax hinting about parameter usage (as GCC does) or you put in hardcoded identification. It's a dead end!
Jul 08 2003
parent "Walter" <walter digitalmars.com> writes:
"Burton Radons" <loth users.sourceforge.net> wrote in message
news:beftcl$182k$1 digitaldaemon.com...
 Walter wrote:
 "Nic Tiger" <tiger7 progtech.ru> wrote in message
 news:bef1rr$dag$1 digitaldaemon.com...

Intel C/C++ compiler DOES analyze string formats for ...printf family of
functions and issues warning if parameters doesn't coincide.

Maybe for D it is not necessary (printf should be replaced with



better), but is it very difficult to provide this kind of check in DMC?



course, check should be performed only for static format strings.

No, it's not that hard.

Harder than safe varargs, and hackish - either you put in an ugly syntax hinting about parameter usage (as GCC does) or you put in hardcoded identification. It's a dead end!

I agree. That's why I never did it.
Jul 09 2003
prev sibling parent "Matthew Wilson" <matthew stlsoft.org> writes:
Intel C/C++ parses the printf()-famliy format strings, and I've been
(un)pleasantly surprised to see how many times code that was deemed good was
found wanting.

"Walter" <walter digitalmars.com> wrote in message
news:beeph4$4qh$2 digitaldaemon.com...
 "Andrew Edwards" <edwardsac spamfreeusa.com> wrote in message
 news:beduip$2cho$1 digitaldaemon.com...
 The concern is that D allows us to compile and execute the program


 providing the variables to resolve the conversion specifiers; resulting


 output of logical garbage.

 For example, compiling and executing the code as submitted earlier


 in the following output:

 1244984 + 4202759 = 1
 4284568 + 1244984 = 4202759

 Shouldn't the compiler prevent that?

The compiler doesn't parse the printf format string any more than C does. Right now, printf is just the C printf. There have been many proposals for

 typesafe D printf, but there isn't an official one yet.

Jul 08 2003