2

After reading over some K&R C I saw that printf can "recognize %% for itself" I tested this and it printed out "%", I then tried "\%" which also printed "%".

So, is there any difference?

Edit for code request:

#include <stdio.h>

int main()
{
  printf("%%\n");
  printf("\%\n");
  return 0;
}

Output:

%                                                                               
%  

Compiled with GCC using -o

GCC version: gcc (SUSE Linux) 4.8.1 20130909 [gcc-4_8-branch revision 202388]

CS Student
  • 1,613
  • 6
  • 24
  • 40
  • you are right, but this works only with printf (using double % sign), so use it carefully – gaurav5430 Dec 14 '13 at 13:25
  • 2
    [Answer here](http://stackoverflow.com/questions/1860159/how-to-escape-the-sign-in-cs-printf) – Gabriel L. Dec 14 '13 at 13:26
  • Run the `strings` command on the resulting program. The output will show you that the "\%" has been translated to "%" by the compiler when it constructed the string literal from your program source. – wildplasser Dec 14 '13 at 13:30
  • 1
    Which gcc version, which host? – ouah Dec 14 '13 at 13:32
  • @ouah; On my compiler I got only `%` with some warnings. – haccks Dec 14 '13 at 13:34
  • @ouah Edited question with GCC version – CS Student Dec 14 '13 at 13:38
  • 1
    Could you replace the last `printf` line with `printf("1\%2\n");` and show the output? Could you also show the compiler output when compiled with `-Wall`? – ouah Dec 14 '13 at 13:44
  • % 1%2 ...is the altered output. With -wall I get the following error: printf.c:6:3: warning: unknown conversion type character 0xa in format [-Wformat=] printf("1\%2\n"); – CS Student Dec 14 '13 at 13:48

3 Answers3

4

Both are not the same. The second one will print %, but in case of the first one, you will get compiler warning:

[Warning] unknown escape sequence: '%' [enabled by default]
The warning is self explanatory that there is no escape sequence like \% in C.

6.4.4.4 Character constants;

says

The double-quote " and question-mark ? are representable either by themselves or by the escape sequences \" and \?, respectively, but the single-quote ' and the backslash \ shall be represented, respectively, by the escape sequences \' and \\.

It is clear that % can't be represented as \%. There isn't any \% in C.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
haccks
  • 104,019
  • 25
  • 176
  • 264
  • Using GCC to compile, I don't get the warning you showed, the program actually compiles and runs. ( I didn't downvote btw c: ) – CS Student Dec 14 '13 at 13:34
  • 1
    BTW: "\%" is not illegal; unknown escaped characters are replaced by them selves. (eg "\"" ) – wildplasser Dec 14 '13 at 13:37
  • 1
    @wildplasser I think they are invalid. See c99, 6.4.5p1 Syntax of string literals: a `\%` is not allowed in a string literal. – ouah Dec 14 '13 at 13:38
  • 2
    I stand corrected. IIRC it was valid in K&R. It must have been dropped in c89 or c99, then. (probably together with the introduction of the "longest match" rule for "\xabcdef" hex-escapes) – wildplasser Dec 14 '13 at 14:00
  • @wildplasser; I quoted from c11. – haccks Dec 14 '13 at 14:02
  • I looked it up in c99, so it must have been either c89 or c99 that changed it. My bet is on c99. – wildplasser Dec 14 '13 at 14:05
  • @wildplasser; Might be. Can't say anything. – haccks Dec 14 '13 at 14:11
  • 1
    This warning arises in the preprocessor (integrated in `gcc`). Unknown \-escapes are not an error in K&R (2e, A.2.5.2), "behaviour undefined" (though strings and chars are a little vague). This generates a warning from ANSI C onwards, whether you see the warning depends on `-pedantic`. You can observe the behaviour of `gcc` by using `-traditional` (if it works for you) or `-ansi` (or `-std=c89` or later) to compile. – mr.spuratic Dec 14 '13 at 15:38
  • @mr.spuratic; Yes. By using `-ansi` flag, no warning is there. – haccks Dec 14 '13 at 17:30
4

%% is not a C escape sequence, but a printf formatter acting like an escape for its own special character.

\% is illegal because it has the syntax of a C escape sequence, but no defined meaning. Escape sequences besides the few listed as standard are compiler-specific. In all likelihood the compiler ignored the backslash, and printf did not see any backslash at runtime. If it had, it would have printed the backslash in the output, because backslash is not special to printf.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
0

When "%%" is passed to printf it will print % to standard output, but "\%" in not an valid escape sequence in C. Hence the program will compile, but it will not print anything and will generate a warning:

warning: spurious trailing ‘%’ in format [-Wformat=] printf("%");

The list of escape sequences in C can be found in Escape sequences in C.

This won't print % for the second printf.

    int main()
    {
        printf("%%\n");
        printf("\%");
        printf("\n");
        return 0;
    }

Output: %

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131