scanf problem

This is a discussion on scanf problem within the C Programming forums, part of the General Programming Boards category; I am currently trying to learn C and have stumbled accross the following problem: Code: double y = 7; scanf("%f",&y); ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    2

    scanf problem

    I am currently trying to learn C and have stumbled accross the following problem:

    Code:
    double y = 7;
    scanf("%f",&y);
    printf("%f",y);
    The above code is really simple but it does not produce the desired result, the printf command returns 7 no matter what i enter. Have I made a mistake?

    The code works fine when i change the double to a float.

    Thanks for your help!!

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    scanf("%lf", &y);
    %lf for scanfing doubles.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    2
    Cheers!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with RPC and scanf
    By Ricardo_R5 in forum C Programming
    Replies: 11
    Last Post: 01-08-2007, 06:15 PM
  2. Problem with scanf float..
    By aydin1 in forum C Programming
    Replies: 6
    Last Post: 03-06-2005, 01:35 PM
  3. scanf problem
    By gsoft in forum C Programming
    Replies: 3
    Last Post: 01-05-2005, 12:42 AM
  4. problem with looping scanf
    By crag2804 in forum C Programming
    Replies: 6
    Last Post: 09-12-2002, 09:10 PM
  5. scanf problem
    By Flikm in forum C Programming
    Replies: 2
    Last Post: 11-05-2001, 01:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21