Tegn og Tekststrenge - slide 48 : 51 |
Input af teststrenge med scanf har specielle regler
Output af tekststrenge med printf virker som forventet
// Source file: string-input.c. Read a string with spaces. #include <stdio.h> #include <string.h> int main(void) { char input[100]; do { printf("Enter a string: "); scanf("%s", input); printf("You entered \"%s\"\n", input); } while (strcmp(input, "exit")); return 0; }
Funktionen gets(s) læser en linie fra standard input og gemmer det i s.
En lang linie vil kunne forårsage overflow i s - brug fgets i stedet for.