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: Thu, 06 Oct 2016 09:12:14 -0700 Organization: None to speak of Lines: 35 Message-ID: References: <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="390"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/npeLF+Se2dOZSV/z0ffU3" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:YqPxZHU+DdMaiTL78oIGVUwlHXU= sha1:hjyqslSXPy1W9LfDnkbROFmGb1Q= Xref: csiph.com comp.lang.c:94538 BartC writes: > On 05/10/2016 20:18, Keith Thompson wrote: [...] >> 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")`. > > The compiler can't dismiss that on the basis that the user could just > write printf("42\n") or puts(). The 42 could be hidden behind a macro, > or is an expression that is either compile-time, or that the compiler > can infer. Of course that's true, and of course gcc or any other compiler *could* optimize printf("%d\n", 42) to the equivalent of puts("42"). My speculation is that, even considering the cases where the argument is not a constant but some expression that can be evaluated at compile time, it just hasn't been considered worth the effort to implement that particular optimization. There's no fundamental principle at work here, just a tradeoff of effort. I wouldn't be astonished to see a future version of gcc do this optimization, perhaps just because some developer thought it would be an interesting thing to implement. -- 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"