#include <stdio.h> #include <stdlib.h> #define LENGTH 65 #define str(x) # x #define xstr(x) str(x) int main(void) { int rc; char buff[LENGTH + 1] = ""; printf("Enter a string with spaces: "); fflush(stdout); rc = scanf("%" xstr(LENGTH) "[^\n]%*[^\n]", buff); if (!feof(stdin)) getchar(); while (rc) { printf("Your string is: \n%s\n", buff); printf("Hit Enter key to end, or enter " "a string with spaces: "); fflush(stdout); rc = scanf("%" xstr(LENGTH) "[^\n]%*[^\n]", buff); if (!feof(stdin)) { getchar(); } } return 0; }
#include <stdio.h> int main(void) { char buff[256] = {0}; /* Exemple 1: moins complets, mais solide */ printf("Enter a string with spaces: "); scanf("%255[^\n]%*[^\n]", buff); printf("String = %s\n", buff); /* Exemple 2: version la plus basique, ne gère aucun type d'erreur */ printf("Enter a string with spaces: "); scanf("%[^\n]", buff); printf("String = %s\n", buff); return 0; }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question