printf - Can't print "print\my\message"

E

ern

Got it... sorry for posting

Answer = printf("C:\\myfile.txt");

Just needed to include two \\
 
M

Mike Wahler

ern said:
printf("C:\myfile.txt");

prints as

C:myfile.txt

I doubt that. I suspect the compiler complained.
(because there's no '\m' character).

How do I specify string literal ?

As above, but note that certain 'unprintable'
characters have a special syntax for expressing
them, e.g. '\n' for newline, '\t' for tab, etc.
Since the '\' is used for this 'special purpose',
a special sequence is used to denote a literal
'\' character: '\\'.

printf("C:\\myfile.txt");

Look up 'escape character' in a C text.

-Mike
 
M

Mark B

Mike Wahler said:
I doubt that. I suspect the compiler complained.
(because there's no '\m' character).

I doubt *any* of my compilers would complain about that...
which compiler do you use which *does* check for and
report issues with invalid escape sequences in format
strings?
 
M

Mark B

Mark B said:
I doubt *any* of my compilers would complain about that...
which compiler do you use which *does* check for and
report issues with invalid escape sequences in format
strings?

Apologies to Mike... I checked after the fact for the hell of it
and was suprised to find that:
gcc version 3.4.2 does in fact complain ;-)
as does msvc cl 12.00.8168

They both however still create an executable which
provided output as specified by OP.

Mark
 
K

Keith Thompson

Mark B said:
I doubt *any* of my compilers would complain about that...
which compiler do you use which *does* check for and
report issues with invalid escape sequences in format
strings?

Did you try it?

It's not an invalid format string, it's an invalid string literal.
puts("C:\myfile.txt") would have the same problem.

Strictly speaking, "C:\myfile.txt" isn't a string literal at all. The
compiler is required to issue a diagnostic, as it would for any syntax
error. At least one compiler then proceeds to generate code as if the
literal "C:yfile.txt" had appeared.
 
C

Clark S. Cox III

I doubt *any* of my compilers would complain about that...
which compiler do you use which *does* check for and
report issues with invalid escape sequences in format
strings?


GCC does; and not just with format strings, but with any strings:

[littleclark2:~] clarkcox% cat test.c

int main()
{
"\a\s\d\f\g\h\j\k\l";
return 0;
}

[littleclark2:~] clarkcox% gcc test.c
test.c:4:3: warning: unknown escape sequence '\s'
test.c:4:3: warning: unknown escape sequence '\d'
test.c:4:3: warning: unknown escape sequence '\g'
test.c:4:3: warning: unknown escape sequence '\h'
test.c:4:3: warning: unknown escape sequence '\j'
test.c:4:3: warning: unknown escape sequence '\k'
test.c:4:3: warning: unknown escape sequence '\l'
[littleclark2:~] clarkcox%
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top