Strings methods
printf/scanf- standard i/o stream (stdin, stdout)sprintf/sscanf- same but with stringssprintf(char* str, const char* format, ...)
char buf[256];
int i = 135;
sprintf(buf, "%d", i);write down
"135"tobuf
sscanf(const char* str, const char* format, ...)my_prog -n 256- ->
argv:- pointer to
"my_prog\0" - pointer to
"-n\0" - pointer to
"256" NULL
- pointer to
- ->
sscanf(argv[2], "%d", &i);malloc(i * sizeof(int))
Reading string
#include <stdio.h>
char* gets(char* buf);- read string to buf untill
'\n'orEOF - never use it
- read string to buf untill
scanf("%s", buf);scanf("%10s", buf);- stops with
'\n',EOFor 11th symbol
- stops with
char* fgets(char* s, int size, FILE* stream);s- link to first symbolsize- length of stringstreammay be connected with file, console,
#include <string.h>
or
#include <cstring>
str...- watching for string ending markersmem...- not watching
Str…
char* strcpy(char* dst, const char* src);buf = malloc(...)strcpy(buf, "Hello world!")
char* strncpy(char* dst, const char* src, size_t n);- determines length of string
char* strcat(char* dst, const char* src);- not controls memory volume
- handles
'\n'and other
char* strncat(char* dst, const char* src, size_t n;)size_t strlen(const char* s);- length of string without
'\0'counting
- length of string without
char* strchr(const char* s, char c);- returns first in index
char* strrch(const char* s, char c);- returns last in index
char* strstr(const char* needle, const char* haystack);- returns pointer of first char of
needleinhaystack - substring search
- searching for
needleinhaystack NULLif not found
- returns pointer of first char of
char* strtok(char* s, const char* delim);- tokenize string by
delimseparator delim->'\0'delimischaror couple of independentchars- example:
- tokenize string by
// print the heap array of char
word = strtok(str, ",");
while (word != NULL) {
printf("word: %s\n", word);
word = strtok(NULL, ",");
}char* strerror(int err_code);- returns error code
- example:
int error;int ret_val = func(&error);if (!error) {working with ret_val} else {diagnostics}
long int strtol(const char* ptr, char** end, int base)ptr- pointer to first symbol of stringend- pointer to pointer to end (123na)base- basis of number system
Mem…
void* memset(void* s, int c, size_t n);- set first
nsymbols ofsto little byte ofc - it’s fast unlike
for
- set first
Static variables
Global variable declares only in first execution and accessable only from that func
int func() {
static int x = 10;
return 0;
}example
int f() {
static int x = 0;
x++;
printf("called %d times\n");
return 0;
}Global errno variable
#include <errno.h>
fopen();
printf("%d", errno);Not all std functions sets
errnoto0
printf("%s", streerror(errno));
f = fopen()
if (!f) {
printf("fopen errno\n");
return 1;
}Transformations strings to …
int atoi(const char* s)- ASCII to int
atoi- intatol- longatod- doubleint a = atoi("123");int a = atoi("b34");- returns 0 or smth else. it’s error
Standard streams
stdoutstdinstderr- standard error stream
Examples
1.