Thread: sscanf always stores 0 in int variable

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    180

    sscanf always stores 0 in int variable

    I have a variable userInput that contains something like C50+50 and an int variable number;
    If i do
    Code:
    sscanf(userInput, "%d", &number)
    when i do a printf number has 0 and now 50, why is that?

  2. #2
    Registered User cstryx's Avatar
    Join Date
    Jan 2013
    Location
    Canada
    Posts
    123
    You can't parse a number from userInput because of the 'C' at the front. Try this:
    Code:
    sscanf(userInput + 1, "%d", &number);

  3. #3
    Registered User
    Join Date
    Apr 2015
    Posts
    180
    Oh i thought it would skip trash and go to the end until it finds an int. So i always need mo make sure i give an address that matches the type i want to read. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 10-27-2011, 11:25 AM
  2. Replies: 2
    Last Post: 04-27-2011, 12:52 PM
  3. Using SSCANF to get a variable
    By aladin in forum C Programming
    Replies: 3
    Last Post: 03-23-2009, 08:17 PM
  4. Replies: 5
    Last Post: 05-30-2003, 12:46 AM
  5. sscanf
    By mickle in forum C Programming
    Replies: 3
    Last Post: 01-10-2003, 03:30 PM