Mo1
Mo1

Reputation: 175

Sprintf & Scanf

I've been trying to figure out sprintf and scanf using the examples in this article: https://www.udemy.com/blog/sprintf-c/#respond

// example 1
#include <stdio.h>

int main() {
   char *stringa = “We Are Dismantling a String”;
   char *one, *two, *three, *four, *five;
   sscanf(stringa, “%s %s %s %s %s”, one, two, three, four, five);

   return(0);
}

and

//  Example 2
#include <stdio.h>

int main() {
   char *stringa[30];
   int *fingers = 5;  
   sprintf(stringa, "Number of fingers making up a hand are %f", fingers); 
   puts(stringa);

   return(0);
}

I can't get the variable to initialize in example 1 and example 2 comes back with a whole list of errors. I could use some help figuring out why these lines of code don't work.

Upvotes: 0

Views: 3767

Answers (2)

To complete Jaochim Pileborg's answer, you should use arrays, not pointers, so code your first main as

int main() {
  char *stringa = "We Are Dismantling a String";
  char onew[16], twow[16], threew[16], fourw[16], fivew[16];
  if (sscanf (stringa, 
              "%15s %15s %15s %15s %15s", 
              onew, twow, threew, fourw, fivew) < 5)
    fprintf (stderr, "something got wrong\n");
  return 0;
}

As Chux commented, you want a %15s for a char onew[16]; because a 16 byte buffer can contain 15 non-space chars plus the required terminating null byte. BTW, I would in fact either explicitly initialize every buffer with e.g. char onew[16]={0}; or preferably clear each of them using memset (onew, 0, sizeof(onew)); before the call to sscanf. I hate uninitialized local buffer strings.

The main of the second example should not compile without warnings (if using gcc -Wall -g) and you should never use sprintf but snprintf(3) (or possibly asprintf(3) when available).

int main() {
 char stringa[30];
 int fingers = 5;  
 snprintf(stringa, sizeof(stringa), 
          "Number of fingers making up a hand are %d", fingers); 
 puts(stringa);
 return(0); }

Notice that I changed the type of stringa, fingers and used %d ...

You should blacklist the site telling you such crap.

BTW, take the good habit of enabling all warnings & debug info in your compiler. With GCC compile with gcc -std=c99 -Wall -g. Learn to use the debugger (gdb) and to improve your code till you get no errors and no warnings.

Upvotes: 3

Some programmer dude
Some programmer dude

Reputation: 409472

Rgarding your first program, the pointers are just pointers, and as they are uninitialized their value (i.e. where they point to) is indeterminate. Using them, especially as a destination to write to, leads to undefined behavior.

To solve this you either need to allocate memory and assign to the pointers, or use array of sufficient length.


Regarding your second program, fingers is a pointer, and you tell the compiler to point it at address 5, not to the Value 5.

You then tell printf to print a floating point value, and give it the pointer as argument. This will once again lead to undefined behavior as printf will treat the pointer as a double value and not a pointer.

To solve this problem don't declare fingers as a pointer, and use the correct printf format "%d" (for signed decimal integer).


I also recommend you read some references for the function, like the following:

Upvotes: 4

Related Questions