Бібліотека:
#include <stdio.h>Ввід-вивід одного символу:
char c;
c = getchar(); //ввід
putchar(c); //вивідВвід-вивід строки:
char str[255];
gets(str); //ввід
puts(str); //вивідФорматований ввід-вивід: scanf printf
Зчитування:
FILE *fp;
char buff[255];
fp = fopen("input.txt", "r");
fscanf(fp, "%s", buff);
fclose(fp);Зчитати весь файл:
FILE *fp;
int buff_len = 255;
char buff[buff_len];
fp = fopen("input.txt", "r");
while(fgets(buff, buff_len, fp)) {
printf("%s\n", buff);
}
fclose(fp);Запис:
FILE *fp;
fp = fopen("output.txt", "w");
fprintf(fp, "This is testing for fprintf...\n");
fclose(fp);Бібліотека:
#include <string.h>Довжина строки:
char a[255] = "Program";
int len = strlen(a);
//len = 7Підстрока:
char substr[255];
strncpy(substr, buff+10, 4); // 4 символа починаючи з позиції 10Конкатенація:
char str[255];
strcpy(str, "Hello ");
strcat(str, "world!");
//str = "Hello world!"#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
...
int n;
scanf("%d", &n);
int *a = (int*)malloc(n * sizeof(int));
int i;
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for (i = 0; i < n; i++) {
printf("%d ", a[i]);
}
free(a);#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
...
int n, m;
scanf("%d %d", &n, &m);
int** a = (int**)malloc(n * sizeof(int*));
for (int i = 0; i < n; i++) {
a[i] = (int*)malloc(m * sizeof(int));
}
int i, j;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
scanf("%d", &a[i][j]);
}
}
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
printf("%d ", a[i][j]);
}
printf("\n");
}
for (i = 0; i < n; i++) {
free(a[i]);
}
free(a);