Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Is there any place to talk about language features? Date: Wed, 05 Oct 2016 12:18:29 -0700 Organization: None to speak of Lines: 34 Message-ID: References: <5b93213c-4a2a-442d-96ca-c79bd09ed470@googlegroups.com> <3e7e20c2-c69d-4777-a98a-b7d212f546c9@googlegroups.com> <72aa4608-96be-4c09-bb2a-cf5bd0da6efa@googlegroups.com> <5def853b-2418-4f89-a7ad-3ea77b66b654@googlegroups.com> <87ponezzoe.fsf@bsb.me.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="945944de09706c9b4e29b53c9d2efdc2"; logging-data="6432"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+JSN0h6NCbs2mElBjBIuc+" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:QFas2gJM39iZhwtlN3HE0knmAuM= sha1:TyvHNKE81FuUix757o3EkG+nJW0= Xref: csiph.com comp.lang.c:94492 Thiago Adams writes: > On Wednesday, October 5, 2016 at 2:38:55 PM UTC-3, > ... >> The design of "printf" trades off run-time complexity and code size for >> compiler simplicity. > > Some thoughts.. > > Compilers can check in compile time the printf arguments. (Visual > Studio 2015 for instance does) Only if the format string is, or can be resolved to, a string literal -- which is the most common case, so it's a worthwhile check. > So, I was wondering that some compiler could possible generate inline > and static code for printf. > printf("%d", 1); //compiler says: ok is "1" > printf("%d", i); //compiler says: ok is just itoa Yes, gcc does this. For example, it's able to compile printf("%s\n", "hello"); as a call to puts(). It doesn't do the same for printf("%d\n", 42); probably (this is speculation) because constant arguments to "%d" are not as common. There's certainly no fundamental reason it couldn't compile that to the equivalent of `puts("42")`. -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"