Formatted string IO

Formatted output to string

Using the function sprintf, which header is int sprintf(char* str, char* format, ...), we can output the same way as with the functions printf or fprintf, but to a string instead of a stream. The first argument is a pointer to the char array that will store the resulting null-terminated string.

What is the use of outputting to arrays instead of streams? Well, it can be used as a way to convert numbers to strings of character.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdio.h> #include <string.h> int main() { // We want to convert the following number. double num = 5934.72; // Into a string stored in the following array. char str[50]; // Writes the string representation of 'num' into the array 'str'. sprintf(str, "%f", num); // We display the result to show it worked. puts(str); // Prints the size of the resulting string. printf("%lu\n", strlen(str)); // Outputs an integer, in hexadecimal, to a string. int num2 = 100; char str2[50] = {'0', 'x'}; // Gives a pointer to the third character so the prefix "0x" is not overwritten. sprintf(&str2[2], "%x", num2); // Displays the resulting string. puts(str2); // Prints the size of the resulting string (Minus 2 to exclude the prefix manually added). printf("%lu\n", strlen(str2)-2); return 0; }

Safe formatted output to string

There is a problem with the function sprintf. If the output is bigger than what the array given as first argument can hold, the array will overflow. The function which header is int snprintf(char* str, size_t n, const char* format, ...), does the same as sprintf, except that if the output is bigger than what the array can hold, it will only write what fits inside it. The first argument is a pointer to the array to store the output to, and the second one is the size of the array.

Note that the function snprintf might not add a null-terminated string a the end of the output if the array to write to is not big enough to contain the whole resulting string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h> #include <string.h> int main() { unsigned long n = 3969381452; char str[7]; // Outputs the variable 'n', as a null-terminated string, to the array 'str'. snprintf(str, 7, "%lu", n); // Adds a null-character to make sure it is a null-terminated string. str[6] = 0; // Prints the resulting string. puts(str); return 0; }

Formatted input from string

The function which header is int sscanf(const char* str, const char* format, ...) allows to input the same way as with the functions scanf and fscanf, but from a null-terminated string instead of a string. The first argument is a pointer to the null-terminated string to read from.

As sprintf can be used to convert numbers to strings, sscanf can be used to retrieve the numerical value of numbers represented in strings of character.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h> #include <string.h> int main() { // A string representing a floating-point number and an integer. char str[] = "78.56 13"; float num; int num2; /* Reads the first number represented in the string inside 'str' and assign its numerical value to the variable, of floating-point type, 'num'. Also reads the second number from the string, to the variable 'num2'. */ sscanf(str, "%f%d", &num, &num2); // Prints the two numbers read. printf("%f %d\n", num, num2); return 0; }