Tutorial by Examples

#include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, char **argv) { /* Exit if no second argument is found. */ if (argc != 2) { puts("Argument missing."); return EXIT_FAILURE; } size_t len = str...
#include <stdio.h> #include <string.h> int main(void) { /* Always ensure that your string is large enough to contain the characters * and a terminating NUL character ('\0')! */ char mystring[10]; /* Copy "foo" into `mystring`, until a NUL character is en...
The strcase*-functions are not Standard C, but a POSIX extension. The strcmp function lexicographically compare two null-terminated character arrays. The functions return a negative value if the first argument appears before the second in lexicographical order, zero if they compare equal, or positi...
The function strtok breaks a string into a smaller strings, or tokens, using a set of delimiters. #include <stdio.h> #include <string.h> int main(void) { int toknum = 0; char src[] = "Hello,, world!"; const char delimiters[] = ", !"; char *to...
The strchr and strrchr functions find a character in a string, that is in a NUL-terminated character array. strchr return a pointer to the first occurrence and strrchr to the last one. #include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { char toSe...
If we know the length of the string, we can use a for loop to iterate over its characters: char * string = "hello world"; /* This 11 chars long, excluding the 0-terminator. */ size_t i = 0; for (; i < 11; i++) { printf("%c\n", string[i]); /* Print each character of ...
In C, a string is a sequence of characters that is terminated by a null character ('\0'). We can create strings using string literals, which are sequences of characters surrounded by double quotation marks; for example, take the string literal "hello world". String literals are automatica...
An array of strings can mean a couple of things: An array whose elements are char *s An array whose elements are arrays of chars We can create an array of character pointers like so: char * string_array[] = { "foo", "bar", "baz" }; Remember: w...
/* finds the next instance of needle in haystack zbpos: the zero-based position to begin searching from haystack: the string to search in needle: the string that must be found returns the next match of `needle` in `haystack`, or -1 if not found */ int findnext(int zbpos, const cha...
String literals represent null-terminated, static-duration arrays of char. Because they have static storage duration, a string literal or a pointer to the same underlying array can safely be used in several ways that a pointer to an automatic array cannot. For example, returning a string literal f...
You can call memset to zero out a string (or any other memory block). Where str is the string to zero out, and n is the number of bytes in the string. #include <stdlib.h> /* For EXIT_SUCCESS */ #include <stdio.h> #include <string.h> int main(void) { char str[42] = &quo...
Given a string, strspn calculates the length of the initial substring (span) consisting solely of a specific list of characters. strcspn is similar, except it calculates the length of the initial substring consisting of any characters except those listed: /* Provided a string of "tokens&quo...
Pointer assignments do not copy strings You can use the = operator to copy integers, but you cannot use the = operator to copy strings in C. Strings in C are represented as arrays of characters with a terminating null-character, so using the = operator will only save the address (pointer) of a str...
Warning: The functions atoi, atol, atoll and atof are inherently unsafe, because: If the value of the result cannot be represented, the behavior is undefined. (7.20.1p1) #include <stdio.h> #include <stdlib.h> int main(int argc, char** argv) { int val; if (argc < 2) ...
Write formatted data to string int sprintf ( char * str, const char * format, ... ); use sprintf function to write float data to string. #include <stdio.h> int main () { char buffer [50]; double PI = 3.1415926; sprintf (buffer, "PI = %.7f", PI); printf ("%s\n&...
C99 Since C99 the C library has a set of safe conversion functions that interpret a string as a number. Their names are of the form strtoX, where X is one of l, ul, d, etc to determine the target type of the conversion double strtod(char const* p, char** endptr); long double strtold(char const* p...

Page 1 of 1