mettre int dans string c %
#include <stdio.h>
int main() {
int num = 10;
char str[20];
sprintf(str, "The number is %d", num);
printf("String: %s\n", str);
return 0;
}
Explanation:
- #include <stdio.h>
: Includes the standard input/output library for input and output operations.
- int main() { ... }
: Defines the main function, the entry point of the program.
- int num = 10;
: Declares an integer variable num
and initializes it with the value 10
.
- char str[20];
: Declares a character array str
capable of holding 20 characters.
- sprintf(str, "The number is %d", num);
: Formats a string using sprintf
, where %d
is a format specifier for an integer value (num
). It formats the string "The number is " followed by the value of num
, and stores it in the str
array.
- printf("String: %s\n", str);
: Prints the formatted string stored in the str
array using %s
format specifier for strings.
- return 0;
: Indicates the end of the main
function and returns 0, indicating successful program execution to the operating system.