Thread: sprintf returns random values upon unsuccessful conversion

  1. #1
    Registered User
    Join Date
    Dec 2019
    Posts
    10

    sprintf returns random values upon unsuccessful conversion

    After running the following code 6 times and for different values of number, I get :

    number = 908
    str = 908.000000
    check = 10

    number = 98.7
    str = 98.699997
    check = 9

    number = 09870.123
    str = 9870.123047
    check = 11

    number = how
    str = 0.000000
    check = 8

    number = hi
    str = 0.000000
    check = 8

    number = 0
    str = 0.000000
    check = 8

    Why do I get random values for invalid floats instead of a negative number as specified on the man page? And how can I overcome this and properly check error?

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>
    
    int main()
    {
        do
         {
            char str[20];
            float number;
    
          	printf("Enter a number: ");
           	scanf("%f",&number);
    
           	int check = sprintf(str,"%f",number);
    
            //Check for error
            if (check<0)
            {
                printf("Error occured\n");
                exit(EXIT_FAILURE);
            }
    
            printf("You entered: %s\n",str);
           
            //Reveal check
            printf("sprintf returned %d\n",check);
           
            getch(); //halt
    
          }while (1);
    
          return 0;
    
    }
    Last edited by SergeP; 12-31-2019 at 01:28 PM.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    Your quesiton is senseless.
    The values you display are not "random".
    And the code you display is not what generated the output.
    In particular, you have an error that your compiler should've warned you about on line 24.

    And sprintf doesn't return -1 for the kind of "error" you are talking about. For example, if you enter "hello" it will return 0, not -1. It returns the length of the string that it creates, which is 0 in that case. It returns -1 if some kind of "encoding error" occurs, which is not likely to happen in your case.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Dec 2019
    Posts
    10
    Sorry, I forgot that.

    So can you answer me now?

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    Check the return value of scanf(). It gives you the number of successful conversions.

  5. #5
    Registered User
    Join Date
    Dec 2019
    Posts
    10
    scanf? How is it related to sprintf?

  6. #6
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    It's not. You want to check for an input error, correct? Checking the return value of sprintf won't do that, but checking the return value of scanf will.

  7. #7
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by SergeP View Post
    scanf? How is it related to sprintf?
    If you read carefully the definition of printf() functions. All returns the number of printer characters. A negative value is returned only in case of "output error" (like using fprintf() and trying to write to an invalid stream). The only exception is snprintf() functions. They will return the number of characters that would be printed regardless of the size of the bufffer.

    scanf() will return the number of successful convertions.

  8. #8
    Registered User
    Join Date
    Dec 2019
    Posts
    10
    I see! It makes sense now.
    Gracias!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. atof returns weird values
    By Domo230 in forum C Programming
    Replies: 2
    Last Post: 03-27-2011, 06:27 AM
  2. what exactlly these values returns?
    By nkrao123@gmail. in forum C Programming
    Replies: 3
    Last Post: 12-09-2009, 06:42 AM
  3. scanf returns random character
    By giannisapi in forum C Programming
    Replies: 3
    Last Post: 06-20-2009, 12:06 PM
  4. fread returns strange values
    By seaking1 in forum C Programming
    Replies: 6
    Last Post: 04-30-2009, 02:10 AM
  5. sprintf values
    By darfader in forum C Programming
    Replies: 4
    Last Post: 10-23-2003, 06:40 AM

Tags for this Thread