Strange behaviour of scanf()

This is a discussion on Strange behaviour of scanf() within the C Programming forums, part of the General Programming Boards category; This was most unexpected. I have a big program which was behaving strangely so I decided to test out each ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    43

    Strange behaviour of scanf()

    This was most unexpected. I have a big program which was behaving strangely so I decided to test out each section. I wrote a small program as shown below:

    Code:
    #include <stdio.h>
    
    int main() {
    	
    printf("go");
    float a;
    scanf("Enter LTP", &a);
    printf("TheLTPis%f", a);
    return 0;
    }
    The program prints:
    Code:
    go
    I then types in 1.344444

    The output continues:
    Code:
    The LTPisnanPress any key to continue...
    The output always stays the same.
    What is happening there? I've used scanf hundreds of times yet I cant see what is presumably a very simple mistake. Any ideas?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    Yeah, there's no % conversion in your scanf call - only what I presume is a prompt which should have been printed with printf.

    Plus the position in which you have float a; makes this C99 code at best.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    43
    Ah yes, silly me. Thanks, I guess I've been having too many late nights

  4. #4
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    If you've used scanf hundreds of times, when did you start using it to output a text string?

    edit: ne'r mind. Looks like you've already seen the mistake

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange scanf behavior
    By exvor in forum C Programming
    Replies: 7
    Last Post: 07-25-2005, 11:54 AM
  2. strange behaviour in glut
    By wgan in forum Game Programming
    Replies: 1
    Last Post: 07-29-2003, 11:55 AM
  3. Strange Direct Input Behaviour
    By Diamonds in forum Windows Programming
    Replies: 2
    Last Post: 06-23-2003, 04:34 PM
  4. GetClientRect strange behaviour
    By btq in forum Windows Programming
    Replies: 2
    Last Post: 10-02-2002, 02:13 PM
  5. Strange behaviour
    By PrivatePanic in forum Windows Programming
    Replies: 11
    Last Post: 07-23-2002, 12:54 AM

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