while
loopschar[]
)'\0'
,'0'
)
char text[] = "Freedom";
Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
---|---|---|---|---|---|---|---|---|
Character | 'F' | 'r' | 'e' | 'e' | 'd' | 'o' | 'm' | '\0' |
Decimal Value | 70 | 114 | 101 | 101 | 100 | 111 | 109 | 0 |
Address | 0x7fffc8c013e4 | 0x7fffc8c013e5 | 0x...e6 | 0x...e7 | 0x...e8 | 0x...e9 | 0x...ea | 0x...eb |
I/O functions are included via stdio.h
scanf(.)
can read user input, but has security problemsscanf_s(.)
fgets(.)
+ sscanf(.)
fgets(char *s, int size, FILE *stream)
reads in at
most one less than size characters;
includes final newlinesscanf(const char *str, const char *format, ...)
scans input according to format#include <stdio.h>
int main() {
char line[5]; // max. 4 characters + \0
int number = 0;
printf("enter an integer (0 <= number <= 9999): ");
fgets(line, sizeof(line), stdin);
printf("You entered the text `%s`.\n", line);
sscanf(line, "%d", &number);
printf("This text, represented as an integer is `%d`.\n", number);
return 0;
}
Download read_number.c
#include <stdio.h>
int main() {
char line[10]; // max. 9 characters + \0
int first = 0; int second = 0;
printf("enter two integers separated by a space (0 <= number <= 9999): ");
fgets(line, sizeof(line), stdin);
printf("You entered the text `%s`.\n", line);
sscanf(line, "%d %d", &first, &second);
printf("The numbers you entered are `%d` and `%d`.\n", first, second);
}
Download read_numbers.c
fgets(.)
also includes the final newline (if it fits
the size of the buffer)
#include <stdio.h>
#include <string.h>
int main() {
char line[15]; // max. 14 characters + \0
printf("enter your first name: ");
fgets(line, 15, stdin);
printf("{begin}%s{end}\n", line);
line[strcspn(line, "\n")] = 0; // remove final newline; '\0' == 0
printf("{begin}%s{end}\n", line);
return 0;
}
Download read_string.c
String functions are included via
<string.h>
(and <strings.h>
)
Using string functions may lead to security issues.
stpcpy, strcasecmp, strcat, strchr, strcmp, strcoll, strcpy,
strcspn, strdup, strfry, strlen, strncat, strncmp, strncpy, strncasecmp,
strpbrk, strrchr, strsep, strspn, strstr, strtok, strxfrm, index,
rindex
To get help, consult your manual :)
man 3 string
toupper(int c)
and tolower(int c)
are included
via
<ctype.h>
#include <stdio.h>
#include <string.h>
int main() {
char name[] = "Pat";
char destination[10]; // programmer is responsible for destination's size
strcpy(destination, name);
printf("Destination is `%s`\n", destination);
return 0;
}
It is possible to iterate over a string using a while
loop. The syntax is
while (condition) {
// statements that repeat as long as `condition` evaluates to true
}
Loops can be interrupted with break
.
The current iteration can be ended using continue
.
while (condition) {
// block of statements
if (skip_remaining_iteration) {
continue; // skip rest of this iteration
}
if (loop_should_end_now) {
break; // leave the loop
}
}
#include <stdio.h>
#include <ctype.h>
int main() {
char input_text[] = "The world is beautiful!";
unsigned short index = 0;
while (input_text[index]) { // while char is not 0 ('\0')
input_text[index] = toupper(input_text[index]);
index++;
}
printf("changed input_text: `%s`\n", input_text);
return 0;
}