Skip to contentSkip to conversation

What compiler is doing when we pass unnecessary parameters in scanf

DISCUSSION RAM_LOCK 4 participants 4 messages Jul 29, 2009 – Oct 9, 2009 1 message not in the archive

lore
Share & tools
lore

RAM_LOCK

author
#0 lore raw

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
-------------

Quoted 1 line

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

Signature · 2 lines
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.

A message that is not in the archive (sent off-list or not yet received): [email protected]

#2 lore raw

----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

Quoted 45 lines · RAM_LOCK

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
-------------

Quoted 1 line

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

Quoted 8 lines

--
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 [email protected]
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 [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#3 lore raw
In reply to RAM_LOCK
Quoted 4 lines · RAM_LOCK

In the second scenario what value is it printing when i have given extra parameter in scanf?
Does it vary from compiler to compiler?

Quoted 8 lines
        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).

Signature · 1 line
Glynn Clements <[email protected]>
#4 lore raw
In reply to RAM_LOCK

behavior of this is undefined just do man scanf

Quoted 46 lines · RAM_LOCK

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
-------------

Quoted 1 line

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

Signature · 2 lines
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.