Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The compiler simply interprets this as calling <code>printf</code> with two strings as arguments (but see Zack's comment).</p> <h3>This happens at compile time (i.e. the compiler does this):</h3> <p>The strings (<code>"%%%s"</code> and <code>"hello"</code>) are copied directly into the executable, the compiler leaves them as-is.</p> <h3>This happens at runtime (i.e. the C standard library does this when the app is running):</h3> <p><code>printf</code> stand for 'print formatted'. When this function is called, it needs at least one argument. The first argument is the format. The next arguments are "arguments" to this format. They are formatted as specified in the first argument.</p> <p><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/2/2c/Printf.svg/500px-Printf.svg.png" alt="img"></p> <hr> <h1>About optimization</h1> <p>I have written an example and ran Clang/LLVM with <code>-S</code>:</p> <pre><code>$ emacs printftest.c $ clang printftest.c -S -o printftest_unopt.s # not optimized $ clang printftest.c -S -O -o printftest_opt.s # optimized: -O flag </code></pre> <h2>C code</h2> <pre><code>#include &lt;stdio.h&gt; int main() { printf("%%%s", "hello"); return 0; } </code></pre> <h2>printftest_unopt.s (not optimized)</h2> <pre><code>; ... _main: pushq %rbp movq %rsp, %rbp subq $16, %rsp movl $0, %eax movl $0, -4(%rbp) movl %eax, -8(%rbp) xorb %al, %al leaq L_.str(%rip), %rdi leaq L_.str1(%rip), %rsi callq _printf ; printf called here &lt;---------------- movl %eax, -12(%rbp) movl -8(%rbp), %eax addq $16, %rsp popq %rbp ret .section __TEXT,__cstring,cstring_literals L_.str: .asciz "%%%s" L_.str1: .asciz "hello" ; ... </code></pre> <h2>printftest_opt.s (optimized)</h2> <pre><code>; ... _main: pushq %rbp movq %rsp, %rbp leaq L_.str(%rip), %rdi leaq L_.str1(%rip), %rsi xorb %al, %al callq _printf ; printf called here &lt;---------------- xorl %eax, %eax popq %rbp ret .section __TEXT,__cstring,cstring_literals L_.str: .asciz "%%%s" L_.str1: .asciz "hello" ; ... </code></pre> <h2>Conclusion</h2> <p>As you can see (in the <code>__TEXT,__cstring,cstring_literals</code> section <strong>and</strong> the <code>callq</code> to <code>printf</code>), LLVM (a very, very good compiler) does <strong>not</strong> optimize <code>printf("%%%s", "hello");</code>. :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload