-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Andrea Palmatè
committed
Aug 1, 2022
1 parent
da140d9
commit 8f72dbd
Showing
4 changed files
with
73 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// C program to demonstrate snprintf() | ||
#include <stdio.h> | ||
|
||
int main() { | ||
char buffer[50]; | ||
char *s = "geeksforgeeks"; | ||
|
||
// Counting the character and storing | ||
// in buffer using snprintf | ||
printf("Writing %s onto buffer" | ||
" with capacity 6", | ||
s); | ||
int j = snprintf(buffer, 6, "%s\n", s); | ||
|
||
// Print the string stored in buffer and | ||
// character count | ||
printf("\nString written on " | ||
"buffer = '%s'", buffer); | ||
printf("\nValue returned by " | ||
"snprintf() method = %d\n", j); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// C program to demonstrate snprintf() | ||
#include <stdio.h> | ||
|
||
int main() { | ||
char buffer[50]; | ||
|
||
// join two or more strings | ||
char *str1 = "quick"; | ||
char *str2 = "brown"; | ||
char *str3 = "lazy"; | ||
int max_len = sizeof buffer; | ||
|
||
int j = snprintf(buffer, max_len, | ||
"The %s %s fox jumped over the %s dog.", | ||
str1, str2, str3); | ||
printf("\nThe number of bytes printed to 'buffer' " | ||
"(excluding the null terminator) is %d\n", | ||
j); | ||
if (j >= max_len) | ||
fputs("Buffer length exceeded; string truncated", | ||
stderr); | ||
puts("Joined string:"); | ||
puts(buffer); | ||
|
||
return 0; | ||
} |