SLIDE 41 String Literals String Variable Reading Strings C String Library
Selected Function of the Standard C Library
The <string.h> library contains function for copying and
comparing strings
char* strcpy(char *dst, char *src); int strcmp(const char *s1, const char *s2); Functions assume sufficient size of the allocated memory for the
strings
There are functions with the explicit maximal length of strings
char* strncpy(char *dst, char *src, size_t len); int strncmp(const char *s1, const char *s2, size_t len);
Parsing a string to a number – <stdlib.h>
atoi(), atof() – parsing integers and floats long strtol(const char *nptr, char **endptr, int base); double strtod(const char *nptr, char **restrict endptr);
Functions atoi() and atof() are „obsolete“, but can be faster
Alternatively also sscanf() can be used
See man strcpy, strncmp, strtol, strtod, sscanf
Jan Faigl, 2017 BE5B99CPL – Lecture 04: Arrays, Strings, and Pointers 38 / 68