scanf doubt

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abhi17
    New Member
    • Feb 2010
    • 6

    #1

    scanf doubt

    float f;
    scanf("%d",&f);
    scanf("%f",&f);


    first use of scanf is wrong but why didnt the second one work?? howdoes one wrong scanf affects all other later uses of scanf
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    In what way did the second one not work?

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Originally posted by abhi17
      Code:
      float f;
      scanf("%d",&f);
      scanf("%f",&f);
      first use of scanf is wrong but why didnt the second one work?? how does one wrong scanf affects all other later uses of scanf
      In your first call to scanf you directed the function to overwrite a float variable with an int value. If sizeof(int) > sizeof(float) on your compiler then this action corrupted your program and you can't expect anything to work properly after that. On the other hand, I'm not aware of any compilers where sizeof(float) is less than sizeof(int).

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Originally posted by abhi17
        Code:
        float f;
        scanf("%d",&f);
        scanf("%f",&f);
        first use of scanf is wrong but why didnt the second one work?? how does one wrong scanf affects all other later uses of scanf
        Your first call to scanf you directed the function to overwrite a float variable with an int value. If sizeof(int) > sizeof(float) on your compiler then this action corrupted your program and you can't expect anything to work properly after that. On the other hand, I'm not aware of any compilers where sizeof(float) is less than sizeof(int); but that doesn't mean it can't happen.

        Comment

        Working...