* What compiler is doing when we pass unnecessary parameters in scanf
@ 2009-07-29 14:18 RAM_LOCK
[not found] ` <f662f0210907290855m57c191e2pb8ba28dc3d87f780@mail.gmail.com>
` (2 more replies)
0 siblings, 3 replies; 7+ messages in thread
From: RAM_LOCK @ 2009-07-29 14:18 UTC (permalink / raw)
To: linux-c-programming
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.
^ permalink raw reply [flat|nested] 7+ messages in thread[parent not found: <f662f0210907290855m57c191e2pb8ba28dc3d87f780@mail.gmail.com>]
* Fwd: What compiler is doing when we pass unnecessary parameters in scanf
[not found] ` <f662f0210907290855m57c191e2pb8ba28dc3d87f780@mail.gmail.com>
@ 2009-07-29 16:32 ` Aneesh Bhasin
0 siblings, 0 replies; 7+ messages in thread
From: Aneesh Bhasin @ 2009-07-29 16:32 UTC (permalink / raw)
To: linux-c-programming
----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
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: What compiler is doing when we pass unnecessary parameters in scanf
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 17:01 ` Glynn Clements
2009-10-09 12:45 ` shiva kumar
2 siblings, 0 replies; 7+ messages in thread
From: Glynn Clements @ 2009-07-29 17:01 UTC (permalink / raw)
To: RAM_LOCK; +Cc: linux-c-programming
RAM_LOCK wrote:
> In the second scenario what value is it printing when i have given extra
> parameter in scanf?
> Does it vary from compiler to compiler?
> 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
The p: isn't matched in the input, so scanf() skips the following %d
conversion, leaving p unchanged. So p will contain garbage (i.e.
whatever value happened to be stored in that memory location already).
--
Glynn Clements <glynn@gclements.plus.com>
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: What compiler is doing when we pass unnecessary parameters in scanf
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 17:01 ` Glynn Clements
@ 2009-10-09 12:45 ` shiva kumar
2 siblings, 0 replies; 7+ messages in thread
From: shiva kumar @ 2009-10-09 12:45 UTC (permalink / raw)
To: linux-c-programming
behavior of this is undefined just do man scanf
RAM_LOCK 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-tp24719839p25820534.html
Sent from the linux-c-programming mailing list archive at Nabble.com.
^ permalink raw reply [flat|nested] 7+ messages in thread
* RE: What compiler is doing when we pass unnecessary parameters in scanf
@ 2009-07-29 16:02 Trevor Woollacott
2009-07-29 18:00 ` Sarkar, Kaushik
0 siblings, 1 reply; 7+ messages in thread
From: Trevor Woollacott @ 2009-07-29 16:02 UTC (permalink / raw)
To: linux-c-programming, Kaushik.Sarkar
> -----Original Message-----
> From: linux-c-programming-owner@vger.kernel.org [mailto:linux-c-
> programming-owner@vger.kernel.org] On Behalf Of RAM_LOCK
> Sent: Wednesday, 29 July 2009 04:19 PM
> To: linux-c-programming@vger.kernel.org
> Subject: What compiler is doing when we pass unnecessary parameters in
> scanf
>
>
> 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
Hi
With scenario II your input of 100 is not matching, so you end up with
whatever value was stored at &p before the scanf.
i.e.
(gdb) r
Starting program: ./a.out
Breakpoint 1, main () at test.c:6
6 float i=0;
(gdb) p p
$4 = -37284792
(gdb) s
7 printf ("enter the principal amount\n");
(gdb) s
enter the principal amount
8 scanf ("p:%d",&p);
(gdb) s
100
9 i = (p*5*5)/100;
(gdb) p p
$6 = -37284792
(gdb) s
10 printf ("Interterest is : %f\n",i); }
(gdb) s
Interterest is : -9321198.000000
Here you can see p = -37284792 before and after your scanf
Then i = (-37284792*5*5)/100 = -9321198.000000
If you check the value returned by scanf, you can determine how many input
values were successfully matched. In the case where you entered 100 in
scenario I, scanf would have returned 1, and in scenario II it would have
returned 0.
Regards,
Trevor
^ permalink raw reply [flat|nested] 7+ messages in thread* RE: What compiler is doing when we pass unnecessary parameters in scanf
2009-07-29 16:02 Trevor Woollacott
@ 2009-07-29 18:00 ` Sarkar, Kaushik
0 siblings, 0 replies; 7+ messages in thread
From: Sarkar, Kaushik @ 2009-07-29 18:00 UTC (permalink / raw)
To: Trevor Woollacott, linux-c-programming
Thanks Trevor for your details explaination...
It is clear to me now... thanks again for clearing this concept :)
-----Original Message-----
From: Trevor Woollacott [mailto:trevorw@pharos-avantgard.com]
Sent: Wednesday, July 29, 2009 9:32 PM
To: linux-c-programming@vger.kernel.org; Sarkar, Kaushik
Subject: RE: What compiler is doing when we pass unnecessary parameters
in scanf
> -----Original Message-----
> From: linux-c-programming-owner@vger.kernel.org [mailto:linux-c-
> programming-owner@vger.kernel.org] On Behalf Of RAM_LOCK
> Sent: Wednesday, 29 July 2009 04:19 PM
> To: linux-c-programming@vger.kernel.org
> Subject: What compiler is doing when we pass unnecessary parameters in
> scanf
>
>
> 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
Hi
With scenario II your input of 100 is not matching, so you end up with
whatever value was stored at &p before the scanf.
i.e.
(gdb) r
Starting program: ./a.out
Breakpoint 1, main () at test.c:6
6 float i=0;
(gdb) p p
$4 = -37284792
(gdb) s
7 printf ("enter the principal amount\n");
(gdb) s
enter the principal amount
8 scanf ("p:%d",&p);
(gdb) s
100
9 i = (p*5*5)/100;
(gdb) p p
$6 = -37284792
(gdb) s
10 printf ("Interterest is : %f\n",i); }
(gdb) s
Interterest is : -9321198.000000
Here you can see p = -37284792 before and after your scanf
Then i = (-37284792*5*5)/100 = -9321198.000000
If you check the value returned by scanf, you can determine how many
input
values were successfully matched. In the case where you entered 100 in
scenario I, scanf would have returned 1, and in scenario II it would
have
returned 0.
Regards,
Trevor
^ permalink raw reply [flat|nested] 7+ messages in thread
* What compiler is doing when we pass unnecessary parameters in scanf
@ 2009-07-29 14:18 RAM_LOCK
0 siblings, 0 replies; 7+ messages in thread
From: RAM_LOCK @ 2009-07-29 14:18 UTC (permalink / raw)
To: linux-c-programming
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
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
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-tp24719837p24719837.html
Sent from the linux-c-programming mailing list archive at Nabble.com.
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2009-10-09 12:45 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` Fwd: " Aneesh Bhasin
2009-07-29 17:01 ` Glynn Clements
2009-10-09 12:45 ` shiva kumar
-- strict thread matches above, loose matches on Subject: below --
2009-07-29 16:02 Trevor Woollacott
2009-07-29 18:00 ` Sarkar, Kaushik
2009-07-29 14:18 RAM_LOCK
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).