sscanf, sprintf, strcmp 
Author Message
 sscanf, sprintf, strcmp

Hey all, could anyone help me with these three functions, i'm unsure what
they all do..

Thanks



Sat, 10 Jan 2004 16:34:50 GMT  
 sscanf, sprintf, strcmp

Quote:

> Hey all, could anyone help me with these three functions (sscanf, sprintf
> and strcmp), i'm unsure what they all do..

    First, it is generally frowned upon to have your question depend on
the subject line. However, iin answer to your question:

    sscanf()    -   works just like scanf(), except that the data comes
                    from a string instead of from stdin.

    sprintf()   -   works just like printf(), except that the data goes
                    to a string instead of to stdout.

    strcmp()    -   compares two strings. strcmp() returns 0 if the two
                    strings are equal, a negative value if the first
                    string is less than the second, and a positive value
                    if the first string is greater than the second.

--
Clark S. Cox III

http://www.whereismyhead.com/clark/



Sat, 10 Jan 2004 16:48:37 GMT  
 sscanf, sprintf, strcmp


Quote:
> Hey all, could anyone help me with these three functions, i'm
> unsure what they all do..

Dear John,

please always put the full question into the body, it is somehow
harder to read the question and the thread when you need to read
in the subject line and in the body.

Now, sscanf is supposed to read formatted input from a string,
whereas sprintf is used to print formatted output tot a string.
strcmp compares to strings and returns 0 if they are equal,
a negativ value if the first string is smaller (that is either
shorter or the first character where the two differ has a smaller
value in the first string) and a positiv value when it is
bigger.

I hope you already know what strings are!?

HTH,
--

"LISP  is worth learning for  the profound enlightenment  experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days."   -- Eric S. Raymond



Sat, 10 Jan 2004 17:06:11 GMT  
 sscanf, sprintf, strcmp
Ahh, sorry about that this is my first post

Thanks for your help



Sat, 10 Jan 2004 17:12:42 GMT  
 sscanf, sprintf, strcmp

Quote:

> Hey all, could anyone help me with these three functions, i'm unsure what
> they all do..

> Thanks

This should be covered in your handy C reference manual.  If you do
not have a handy C reference manual, you should get one.  If you do
have a handy C reference manual and it does not explain what these
functions do, then you need to burn it and get a *good* one.

Look for Kernighan & Ritchie's "The C Programming Language", 2nd. ed.,
or Harbison & Steele's "C: A Reference Manual", 4th ed.

sscanf() works like fscanf(), except you're reading from a character
array instead of an input stream.  sprintf() works like fprintf(),
except you are writing characters to a character array instead of an
output stream.  strcmp() compares the contents of two character
arrays.

Example:


$ cat strs.c
#include <stdio.h>

int main (void)
{
    char ibuf[16] = "one 2 three 4";
    char c1[4], c2[6];
    int i1, i2;
    char obuf[16];
    int r;

    sscanf (ibuf, "%s %d %s %d", c1, &i1, c2, &i2);
    sprintf (obuf, "%d %s %d %s", i2, c2, i1, c1);

    r = strcmp (ibuf, obuf);
    if (r < 0)
    {
        printf ("%s is lexicographically less than %s\n", ibuf, obuf);
    }
    else if (r > 0)
    {
        printf ("%s is lexicographically greater than %s\n", ibuf,
obuf);
    }
    else
    {
        printf ("%s is lexicographically equal to %s\n", ibuf, obuf);
    }

    return 0;

Quote:
}


$ gcc -o strs -ansi -pedantic -Wall strs.c


$ ./strs
one 2 three 4 is lexicographically greater than 4 three 2 one



Sat, 10 Jan 2004 23:06:55 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. questions about sprintf, sscanf, printf, scanf, fscanf, fprintf!

2. Source code for sprintf and sscanf routines??

3. Seeking for sscanf & sprintf

4. Help me with Sprintf and Sscanf

5. sprintf/sscanf version that DOESN'T use FILES?

6. strcmp implementation query

7. strcmp sorting I NEED HELP!!!

8. Simple strcmp

9. avoid warning with qsort and strcmp

10. How to compare strings in arrays using strcmp

11. strcmp question

12. I need help with strcmp and qsort

 

 
Powered by phpBB® Forum Software