sprintf and sscanf

This is a discussion on sprintf and sscanf within the C Programming forums, part of the General Programming Boards category; Does anyone know what the purpose of the sprintf and sscanf statements for this program are for? I do not ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    78

    sprintf and sscanf

    Does anyone know what the purpose of the sprintf and sscanf statements for this program are for? I do not understand these 2 functions at all. Thanks......

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main()
    {
    	char array[80];
    	int num1=1;
    	int num2=2;
    
    	sprintf(array,"%d" "%d", num1, num2);
    	puts(array);
    
    	char data[]="$23.45 10";
    	char dol;
    	float price;
    	int units;
    
    	scanf(data,"%c%f %d", &dol,&price,&units);
    	printf("\n%c%5.2f %d\n", dol, price, units);
    
    	getch();
    	return 0;
    }
    *****Output with the SSCANF and SPRINTF Function********
    /*
    12

    $23.45 10
    */
    *****Output with the SCANF and SPRINTF Function*********
    /*
    12
    */

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Here is an example. Basicly, sprintf prints from a string. sscanf scans into a string (buffer). Just like their non-"s" counter parts.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    507
    i think you have that backwards.....sprintf prints to a string, and sscanf reads from a string?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yeah. What he said. I know what I meant.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    507
    i know you did..just wanted to clarify for him

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    78
    Thanks for the info guys. Are these functions used a lot? What's a good example of using them? Thanks again for your help. Tommy

  7. #7
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    here
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void){
            char buffer[BUFSIZ];
            int x;
    
            printf("Enter a number:  ");
            fflush(stdout);
            if(!(fgets(buffer,sizeof buffer,stdin))){
                   fprintf(stderr,"Error getting input\n");
                   exit(0);
            }
            sscanf(buffer,"%d",&x);
            printf("Now x is a digit!!\n");
            printf("X == %i\n",x);  /*%i is interchangeable with %d here*/
            return 0;
    }

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Are these functions used a lot?
    sscanf is used a great deal as the preferred method for parsing a string read by fgets. This combination is a replacement for scanf because scanf bites, and also gives you a better chance for error handling when reading from files (instead of fscanf).

    >What's a good example of using them?
    Converting numeric data to and from strings in a standard and portable way.
    My best code is written with the delete key.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by tommy69
    What's a good example of using them?
    I guess you didn't actually read the link to the FAQ then...

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Quote Originally Posted by linuxdude
    here
    Code:
     #include <stdio.h>
     #include <stdlib.h>
     
     int main(void){
             char buffer[BUFSIZ];
             int x;
     
             printf("Enter a number:  ");
             fflush(stdout);
             if(!(fgets(buffer,sizeof buffer,stdin))){
                    fprintf(stderr,"Error getting input\n");
                    exit(0);
             }
    sscanf(buffer,"%d",&x);
             printf("Now x is a digit!!\n");
             printf("X == %i\n",x);  /*%i is interchangeable with %d here*/
             return 0;
     }
    Where you say "x is now a digit", you're sort of wrong, and right, all at the same time x was always a number, as its an int type variable, it just started the program with an unpredictable value. Unfortunately, even after sscanf() has done it's magic, x still has an unpredictable value, as parsing might have failed. Remember to always check the return code from sscanf() to ensure it did what it was supposed to.

    >>fprintf(stderr,"Error getting input\n");
    This would be better replaced with perror("Error getting input");
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    I hate you people

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sprintf and sscanf functions slow
    By Roaring_Tiger in forum C Programming
    Replies: 9
    Last Post: 03-22-2009, 01:26 PM
  2. Dumb Question: What is sscanf?
    By KingZoolerius66 in forum C Programming
    Replies: 3
    Last Post: 10-04-2003, 09:19 PM

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