Search
* pro vyučující: 02
char, int, float, double
~/prg/lab02
#include <stdio.h> int main(void) { printf("I like BAB36PRG!\n"); return 0; }
echo $?
program0
program1
clang program.c -o program0
clang program.c -o program1
./program0 && ./program1 ./program1 && ./program0
./program0 && echo $? && ./program1 && echo $? ./program1 && echo $? && ./program0 && echo $?
a dále pak
./program0 && echo $? && ./program1; echo $? ./program1; echo $? && ./program0 && echo $?
#include <stdio.h> int main(void) { printf("I like BAB36PRGA!\n"); printf("I like BAB36PRGA!\n"); printf("I like BAB36PRGA!\n"); printf("I like BAB36PRGA!\n"); return 0; }
který následně upravte využitím cyklu
#include <stdio.h> int main(void) { for (int i = 0; i < 4; ++i) { printf("I like BAB36PRGA!\n"); } return 0; }
Vyzkoušejte kompilaci a spuštění programu, např.
clang program.c && ./a.out
nebo
clang program.c -o program && ./program
main
#include <stdio.h> int main(int argc, char *argv[]) { for (int i = 0; i < argc; ++i) { printf("I like BAB36PRGA!\n"); } return 0; }
./program
./program 1 2 3
./program a b c e
wc
man wc
./program 2 3 4 | wc
getchar()
man getchar
int r = getchar(); for (int i; i < r; ++i) { printf("I like BAB36PRGA!\n"); }
enter
1
./a.out | wc
r = r - '0';
echo 1 | ./program
echo 3 > in.txt
cat in.txt
gedit in.txt
code in.txt
./program < in.txt
in.txt
touch empty.txt
return r < 0 || r > 9;
#include <stdio.h> int main(void) { int r = getchar(); r = r - '0'; for (int i = 0; i < r; ++i) { printf("I like BAB36PRGA!\n"); } return r < 0 || r > 9; }
./program <in.txt; echo $? ./program <empty.txt; echo $? echo 'a' | ./program; echo $?
printf
printf(“%d\n”, r);
man printf
#include <stdio.h> int main(void) { int r = getchar(); r = r - '0'; for (int i = 0; i < r; ++i) { printf("I like BAB36PRGA!\n"); } fprintf(stderr, "%d\n", r); return r < 0 || r > 9; }
./program <in.txt | wc
stderr
stdout
./program <in.txt >out.txt
./program <in.txt 2>err.txt
cat err.txt
ls -l
scanf()
man scanf
#include <stdio.h> int main(void) { int ret = 0; int n; int r = scanf("%d", &n); if (r == 1) { for (int i = 0; i < n; ++i) { printf("I like BAB36PRGA!\n"); } } else { fprintf(stderr, "ERROR: Wrong input!\n"); ret = 100; } return ret; }
int n; int r = scanf("%d", &n); fprintf(stderr, "DEBUG: r: %d n: %d\n", r, n);
clang program.c && echo 4 | ./a.out | wc
“ERROR: Out of range!\n”
101
if (n >= 0 && n <= 9) { ... } else { fprintf(stderr, "ERROR: Out of range!\n"); ret = 101; }
void print_line(int n);
void print_line(int n) { for (int i = 0; i < n; ++i) { printf("I like BAB36PRGA!\n"); } }
EOT
Ctrl+D
Ctrl+Z
char c = 'a'; int i = 1000,j; float x,y = 3.1415; printf("%c\n", c); printf("%i\n", c); printf("%d\n", i); printf("%+5d\n", i); printf("%x\n", i); printf("%f\n", y); printf("%10.3f\n", y); printf("\t%-g\n", y);
printf("%6d,%4d", 86, 1040); printf("\n"); printf("%12.5e", 3.14159265); printf("\r"); printf("%.4f\n", 85.167);
fprintf
fprintf(stdout, "Standard output: %d\n", i); fprintf(stderr, "Error output: %d\n", i);
./main >output.log 2>error.log
1-20.3-4.0e3
scanf("%d%d%f%f", &i, &j, &x, &y);
scanf
“%d/%d”
./main <input.log
out.c
out
echo 10 | ./out echo 10 | ./out | wc echo 10 | ./out > output.txt cat output.txt wc output.txt
in
100
int r, n; while ((r = scanf("%d", &n)) == 1) { // increment of the counter } if (r == 0) { fprintf(stderr, "ERROR: Wrong input detected!\n"); ret = 100; } else { fprintf(stderr, "ERROR: Number lines %d!\n", counter); ret = 101; }
echo 10 | ./out > output.txt ./in < output.txt
printf(“%d\n”, i);
printf(“%d ”, i);
i
for
echo 10 | ./out | ./in