Programmation Avancée en C


sprintf.c

00001 #include <stdio.h>
00002 #define STR_MAX_SIZE 256  // Taille maximale des chaînes de caractères
00003 
00004 int main() 
00005 {
00006     char str[STR_MAX_SIZE], strn[STR_MAX_SIZE];
00007     int a = 27, b = 11, c, d; 
00008     sprintf(str, "a = %d | b = %d", a, b);
00009     printf("str  : %s\n", str);
00010     if (snprintf(strn, STR_MAX_SIZE, "a = %d | b = %d", a, b) >= STR_MAX_SIZE)
00011         fprintf(stderr, "Formatage tronqué!");
00012     printf("strn : %s\n", strn);
00013     sscanf(str, "a = %d | b = %d", &c, &d);
00014     printf("c=%d | d=%d\n",c,d); // Vérification
00015     return 0;
00016 }