String Formatting in C
This example demonstrates various string formatting techniques in C using the `printf` function from the `stdio.h` library.
```c
#include <stdio.h>
struct point {
int x, y;
};
int main() {
struct point p = {1, 2};
// Print a struct
printf("struct1: {%d %d}\n", p.x, p.y);
// Print a struct with field names
printf("struct2: {x:%d y:%d}\n", p.x, p.y);
// Print the type of a variable (not directly possible in C)
printf("type: struct point\n");
// Format booleans
printf("bool: %s\n", 1 ? "true" : "false");
// Format integers
printf("int: %d\n", 123);
// Print in binary (not directly supported in C's printf)
printf("bin: 1110\n");
// Print a character
printf("char: %c\n", 33);
// Print in hexadecimal
printf("hex: %x\n", 456);
// Format floats
printf("float1: %f\n", 78.9);
// Format floats in scientific notation
printf("float2: %e\n", 123400000.0);
printf("float3: %E\n", 123400000.0);
// Format strings
printf("str1: %s\n", "\"string\"");
// Format strings with quotes
printf("str2: \"%s\"\n", "\"string\"");
// Format strings in hexadecimal
printf("str3: ");
const char* hex_str = "hex this";
for (int i = 0; hex_str[i] != '\0'; i++) {
printf("%02x", (unsigned char)hex_str[i]);
}
printf("\n");
// Print a pointer
printf("pointer: %p\n", (void*)&p);
// Control width for integers
printf("width1: |%6d|%6d|\n", 12, 345);
// Control width and precision for floats
printf("width2: |%6.2f|%6.2f|\n", 1.2, 3.45);
// Left-justify floats
printf("width3: |%-6.2f|%-6.2f|\n", 1.2, 3.45);
// Control width for strings
printf("width4: |%6s|%6s|\n", "foo", "b");
// Left-justify strings
printf("width5: |%-6s|%-6s|\n", "foo", "b");
// Format to a string
char buffer[50];
sprintf(buffer, "sprintf: a %s", "string");
printf("%s\n", buffer);
// Print to stderr
fprintf(stderr, "io: an %s\n", "error");
return 0;
}
To run the program, compile it and then execute the resulting binary:
$ gcc string_formatting.c -o string_formatting
$ ./string_formatting
struct1: {1 2}
struct2: {x:1 y:2}
type: struct point
bool: true
int: 123
bin: 1110
char: !
hex: 1c8
float1: 78.900000
float2: 1.234000e+08
float3: 1.234000E+08
str1: "string"
str2: ""string""
str3: 6865782074686973
pointer: 0x7ffd5e7e9968
width1: | 12| 345|
width2: | 1.20| 3.45|
width3: |1.20 |3.45 |
width4: | foo| b|
width5: |foo |b |
sprintf: a string
io: an error
This C program demonstrates various string formatting techniques using the printf
family of functions. Note that C doesn’t have some of the advanced formatting options available in more modern languages, so some adaptations were necessary. For example, C doesn’t have a direct way to print the type of a variable or to format integers in binary, so these were simulated or omitted.
The struct point
is defined and used to show how to format custom types. Various number formats (integers, floats, hexadecimal) are demonstrated, as well as string formatting and pointer printing. The program also shows how to control the width and precision of printed values, and how to left-justify output.
The sprintf
function is used to format a string into a buffer without printing it, and fprintf
is used to print to stderr
.
Remember that C requires careful management of string buffers to avoid overflow, which isn’t shown in this basic example for simplicity.