Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!thunder.mcrcim.mcgill.edu!snorkelwacker.mit.edu!usc!wuarchive!uunet!taumet!steve From: steve@taumet.com (Stephen Clamage) Newsgroups: comp.lang.c Subject: Re: not really Re: Style guides and portability Message-ID: <554@taumet.com> Date: 17 Jan 91 17:15:48 GMT References: <1991Jan13.182655.17672@athena.mit.edu> <10608@hydra.Helsinki.FI> <1991Jan15.054540.1466@athena.mit.edu> <1991Jan16.172439.15205@Neon.Stanford.EDU> Organization: Taumetric Corporation, San Diego Lines: 66 dkeisen@Gang-of-Four.Stanford.EDU (Dave Eisen) writes: >>In article <10608@hydra.Helsinki.FI>, Lars Wirzenius asks: >>>Is there any problem in using >>> printf("%ld", (long) bigint) >>>other than that it's clumsy? >It sorta defeats the purpose, doesn't it? >The point of using a type like int32 is to avoid hardcoding the >current compiler's idea of what a long looks like into the code and >here you are using "lf" and "long" thousands of times all over your >code. If you wanted to go this route, maybe something like >#define BIGINT_FORMAT "ld" >#define BIGINT_TYPE long >would be appropriate. The source of the problem is inherent in printf-like functions, where the receiving function does not know the type of the parameter being passed, and has to be told in an auxiliary parameter (the format string). Whenever you have the same piece of information kept in two places you have synchronization problems -- in this case the type of the parameter being passed is known by the compiler at the calling location, and has to be duplicated by the programmer independently. Other languages have solved this problem in different ways -- none of them by using a printf-like function. In Pascal, I/O is built into the language, so you can say write('the value is ', x); which works no matter what the numeric type of x. This solution is not available in C. In Modula-2, there is a separate output function for each data type. This solution can be used in C, and with prototyping the compiler will convert the numeric argument to the right type, or warn you about dangerous conversions: int i; long l; void printi(int); void printl(long); printl(i); /* i will be automatically converted to a long */ printi(l); /* compiler should warn about truncating l */ In C++, you can take advantage of function overloading to declare multiple versions of a function with different parameter types, and the compiler will call the right one: int i; long l; void print(int); void print(long); print(i); // print(int) is called print(l); // print(long) is called You can also use one of the standard stream I/O packages, in which the << operator is overloaded for output and can be chained: #include // or int i; long l; cout << "the value is " << i; // cout is standard output cout << "the value is " << l; The key to minimizing porting difficulties is to avoid the inherent problem in printf by one of the above solutions. I find them cleaner than printf(BIGINT_FORMAT, (BIGINT_TYPE) bigint); -- Steve Clamage, TauMetric Corp, steve@taumet.com