From: Aneesh Bhasin <contact.aneesh@gmail.com>
To: linux-c-programming@vger.kernel.org
Subject: Fwd: What compiler is doing when we pass unnecessary parameters in scanf
Date: Wed, 29 Jul 2009 22:02:42 +0530 [thread overview]
Message-ID: <f662f0210907290932t1e8f7a66xd3ac82e0c41e00b2@mail.gmail.com> (raw)
In-Reply-To: <f662f0210907290855m57c191e2pb8ba28dc3d87f780@mail.gmail.com>
----forgot to add list as cc------
Hi Kaushik,
any extra non-whitespace, non "%" characters are matched against the
input given from stdin - if they match, it is ignored and if it
doesn't match, no further input is taken.
In case of your second scenario, when you specify "p:%d" as string,
the program expects the input to be of the format "p:<some number>"
but since you give 100 as the input, no value is assigned to the
variable p (you can check the return value of scanf - its zero) and
hence, whatever junk value is in variable p, it is used for further
calculation.
With the second scenario, try giving the input as "p:100" and then p
should get value 100 as expected...
Hope that helps.
Aneesh
On Wed, Jul 29, 2009 at 7:48 PM, RAM_LOCK <Kaushik.Sarkar@netapp.com> wrote:
>
> Hi,
> In the second scenario what value is it printing when i have given extra
> parameter in scanf?
> Does it vary from compiler to compiler?
>
> Scenario : I
> -------------
> root@kaushik_Fedora11 ~/C/LET_US_C/ch-1> cat simple-interest.c
> #include <stdio.h>
>
> void main ()
> {
> int p;
> float i=0;
> printf ("enter the principal amount\n");
> scanf ("%d",&p);
> i = (p*5*5)/100;
> printf ("Interterest is : %f\n",i);
> }
> root@kaushik_Fedora11 ~/C/LET_US_C/ch-1> ./a.out
> enter the principal amount
> 100
> Interterest is : 25.000000
>
>
> Scenario : II
> -------------
> > cat simple-interest.c
> #include <stdio.h>
>
> void main ()
> {
> int p;
> float i=0;
> printf ("enter the principal amount\n");
> scanf ("p:%d",&p);
> i = (p*5*5)/100;
> printf ("Interterest is : %f\n",i);
> }
> root@kaushik_Fedora11 ~/C/LET_US_C/ch-1> ./a.out
> enter the principal amount
> 100
> Interterest is : -9321198.000000
>
> --
> View this message in context: http://www.nabble.com/What-compiler-is-doing-when-we-pass-unnecessary-parameters-in-scanf-tp24719839p24719839.html
> Sent from the linux-c-programming mailing list archive at Nabble.com.
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
next prev parent reply other threads:[~2009-07-29 16:32 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-07-29 14:18 What compiler is doing when we pass unnecessary parameters in scanf RAM_LOCK
[not found] ` <f662f0210907290855m57c191e2pb8ba28dc3d87f780@mail.gmail.com>
2009-07-29 16:32 ` Aneesh Bhasin [this message]
2009-07-29 17:01 ` Glynn Clements
2009-10-09 12:45 ` shiva kumar
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=f662f0210907290932t1e8f7a66xd3ac82e0c41e00b2@mail.gmail.com \
--to=contact.aneesh@gmail.com \
--cc=linux-c-programming@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).