use of %

D

dis_is_eagle

hi...in "%d", why the % is used..?..for the statement
printf("%%%%");...............the output is %%....would anyone
explain it...?..thanx

eric
 
C

Christopher Benson-Manica

hi...in "%d", why the % is used..?

It depends on the context in which you see "%d". The *printf() family
of functions interpret it as a format specifier; your friendly C
textbook can tell you all about format specifiers. If you pass "%d"
to *puts(), on the other hand, the '%' means nothing special
whatsoever.
..for the statement
printf("%%%%");...............the output is %%....would anyone
explain it...?..thanx

"%%" is a special format specifier denoting a literal '%'.
 
3

31337one

hi...in "%d", why the % is used..?..for the statement
printf("%%%%");...............the output is %%....would anyone
explain it...?..thanx

eric

From my experience, the % denotes a special action. If you have done
any C++ programming with 'cout' then the % is similar to the \ to
escape and mark something special is coming up.

This is just what I have learned from personnal experience.

On the similar subject, isnt %d for numbers and %s for strings?
 
W

Walter Roberson

hi...in "%d", why the % is used..?

They could have chosen anything not likely to appear in text.
Why they choose % specifically as the format introducer is a
historical matter, not a technical matter.
 
G

gw7rib

hi...in "%d", why the % is used..?..for the statement
printf("%%%%");...............the output is %%....would anyone
explain it...?..thanx

eric

I expect it's % in C because it's % in BCPL and they didn't see any
point in changing it. Though that of course begs the question as to why
it is % in BCPL...

I suppose they wanted a character which would be likely to exist on
whatever computer was being used, but was not frequently needed to be
printed in normal output. There's not many possibilities.
 
S

spibou

any C++ programming with 'cout' then the % is similar to the \ to
escape and mark something special is coming up.

This is just what I have learned from personnal experience.

On the similar subject, isnt %d for numbers and %s for strings?

%d is for (signed) integers. There are obviously many more kinds
of numbers.

Spiros Bousbouras
 

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
474,188
Messages
2,571,002
Members
47,591
Latest member
WoodrowBut

Latest Threads

Top