Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > how to use % symbol in printf

Reply
Thread Tools

how to use % symbol in printf

 
 
manish sahu
Guest
Posts: n/a
 
      07-09-2009
void main()
{
int a=3;
printf("total %age is %d",a);
}


i want output as

total %age is 3


but it is not printing a's value output is

total %age is %d



plz reply .
thankyou

 
Reply With Quote
 
 
 
 
jameskuyper
Guest
Posts: n/a
 
      07-09-2009
manish sahu wrote:
> void main()
> {
> int a=3;
> printf("total %age is %d",a);
> }
>
>
> i want output as
>
> total %age is 3


Then your format string should be "total %%age is %d".
 
Reply With Quote
 
 
 
 
Kenny McCormack
Guest
Posts: n/a
 
      07-09-2009
In article <d57aff42-658b-4a30-ac5d->,
jameskuyper <> wrote:
>manish sahu wrote:
>> void main()
>> {
>> int a=3;
>> printf("total %age is %d",a);
>> }
>>
>>
>> i want output as
>>
>> total %age is 3

>
>Then your format string should be "total %%age is %d".


Ahem, James, you're improving your ability to avoid being sucked in by
trollbait. For that, you deserve commendation.

(As in, no comment about either void main() or the lack of a trailing
newline...)

 
Reply With Quote
 
Michael Tsang
Guest
Posts: n/a
 
      07-10-2009
manish sahu wrote:

> void main()
> {
> int a=3;
> printf("total %age is %d",a);
> }


Usage of /void main()/ is undefined. You should use /int main(void)/
instead. (However, /return 0;/ is unnecessary as of C99)

 
Reply With Quote
 
Beej Jorgensen
Guest
Posts: n/a
 
      07-10-2009
Richard Heathfield <> wrote:
>Nevertheless, explicitly returning a value to the OS is considered
>by some to be worth doing.


Looking for clarity on this issue, but I'm having trouble finding the
answer.

1. Why is main() allowed this liberty (implicit "return 0"), when other
functions are not? I don't see mention of it in the Rationale.

2. It appears that it's legal to have a function such as int foo(void)
not return a value (as long as it doesn't explicitly "return;"). Is
this the case? I'm having trouble finding the bits of the spec that
describe this situation and what occurs. (gcc will warn with -Wall.)

-Beej

 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      07-10-2009
Beej Jorgensen wrote:
> Richard Heathfield <> wrote:
>> Nevertheless, explicitly returning a value to the OS is considered
>> by some to be worth doing.

>
> Looking for clarity on this issue, but I'm having trouble finding the
> answer.
>
> 1. Why is main() allowed this liberty (implicit "return 0"), when other
> functions are not? I don't see mention of it in the Rationale.


To permit the common practice of leaving out that statement, thereby
standardizing something that was already common practice on many
implementations. I don't think it's a good idea to do this, and I'm not
happy about the fact that it was standardized, but it has been.

> 2. It appears that it's legal to have a function such as int foo(void)
> not return a value (as long as it doesn't explicitly "return;"). Is
> this the case? I'm having trouble finding the bits of the spec that
> describe this situation and what occurs. (gcc will warn with -Wall.)


6.9.1p12: "If the } that terminates a function is reached, and the value
of the function call is used by the caller, the behavior is undefined."

Notice that the behavior is only undefined if the caller attempts to use
the non-existent return value. Otherwise, it's perfectly acceptable to
reach the } that terminates a function, even if it is declared as
returning a value. I think that writing code which depends upon this
feature is a bad idea, but that's a separate matter.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      07-10-2009
James Kuyper <> writes:
> Beej Jorgensen wrote:

[...]
>> 2. It appears that it's legal to have a function such as int foo(void)
>> not return a value (as long as it doesn't explicitly "return;"). Is
>> this the case? I'm having trouble finding the bits of the spec that
>> describe this situation and what occurs. (gcc will warn with -Wall.)

>
> 6.9.1p12: "If the } that terminates a function is reached, and the
> value of the function call is used by the caller, the behavior is
> undefined."
>
> Notice that the behavior is only undefined if the caller attempts to
> use the non-existent return value. Otherwise, it's perfectly
> acceptable to reach the } that terminates a function, even if it is
> declared as returning a value. I think that writing code which depends
> upon this feature is a bad idea, but that's a separate matter.


And I think the reason that rule exists is for compatibility with
pre-ANSI (K&R) C. Prior to the 1989 ANSI standard, there was no
"void", and it wasn't possible to write a function that didn't return
a value. The way to do the equivalent was to depend on the implicit
int rule and neither return a value nor attempt to use the result:

foo() /* implicitly "int foo()" */
{
/* do something */
}

...

foo(); /* ignore non-existent result */

If C89/C90 had made this undefined behavior, it would have broken most
existing C programs. C89 *could* have made the behavior undefined
only if the return type is explicitly declared, but that would have
created a distinction between "foo()" and "int foo()", which are
otherwise identical.

C99 dropped implicit int, and could have dropped this rule, but the
committee chose to lave it in place. For one thing, this means it's
still possible to turn pre-ANSI code into valid C99 code just by
making the implicit int explicit:

int foo()
{
/* do something */
}

...

foo();

Of course the right way to do this is to change it to
"void foo(void)", but there might have been some consideration for
automatic translation of pre-ANSI code.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Beej Jorgensen
Guest
Posts: n/a
 
      07-10-2009
James Kuyper <> wrote:
>6.9.1p12: "If the } that terminates a function is reached, and the value
>of the function call is used by the caller, the behavior is undefined."


Dammit, how did I miss this after all that groping through the spec?
Thanks for pointing me to it.

>I think that writing code which depends upon this feature is a bad
>idea, but that's a separate matter.


I'm in agreement with you on these things.

Cheers,
-Beej

 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      07-10-2009
Beej Jorgensen <> wrote:

> Looking for clarity on this issue, but I'm having trouble finding the
> answer.
>
> 1. Why is main() allowed this liberty (implicit "return 0"), when other
> functions are not? I don't see mention of it in the Rationale.


Politics. And bad politics, if you ask me.

Richard
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      07-11-2009
On Fri, 10 Jul 2009 22:13:27 GMT, (Richard Bos)
wrote:

>Beej Jorgensen <> wrote:
>
>> Looking for clarity on this issue, but I'm having trouble finding the
>> answer.
>>
>> 1. Why is main() allowed this liberty (implicit "return 0"), when other
>> functions are not? I don't see mention of it in the Rationale.

>
>Politics. And bad politics, if you ask me.


Is there any other kind?

--
Remove del for email
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
printf affects following printf/s azza C Programming 0 10-17-2010 09:43 AM
Extracting printf(...) from (void) printf(....) guru Perl Misc 8 02-03-2009 10:37 PM
(void) printf vs printf whatluo C Programming 29 09-08-2005 05:42 PM
bus error with printf line included, error without printf line? ben C Programming 4 06-26-2004 04:42 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57