Strings in C
In C, there are no such a data type as string (variable length, locale-aware vs. low-level).
Conventional LibC string:
- sequence of bytes
- zero-terminated (s. c. ASCIIZ)
- no metadata
C functions for handling strings
Reference on C function for handling strings and symbols is here.
Main types of functions:
- Character_classification
- Conversions_to_numeric_formats
- String_manipulation
- String_examination
- Character_array_manipulation
Most commonly used functions:
- strlen – calculate the length of a string
- strcpy – copy a string
- strncpy - copy
n
symbols of a string - strcat – concatenate two strings
- strdup – duplicate a string
- strcmp – compare two strings
- atoi – convert a string to an integer
The buffer for a string can be a global array, a stack-allocated array or a dynamically allocated chunk of memory (allocated with the help of the malloc function or other functions). Dynamically allocated strings must be freed with the help of the free function.
Full list of functions from <string.h>
is here.
Working with command-line arguments
Command-line arguments are passed as strings for the main
function.
The argc
argument sspecifies total argument count, the argv
specifies an array of argument strings.
The program below prints all arguments to the console:
#include <stdio.h>
int main(int argc, char *argv[]) {
int i;
for (i = 0; i < argc; i++)
printf("%s\n", argv[i]);
return 0;
}
Note: 0th argument is the name of the program.
Strings are printed with the help of the printf function.
Principles of string handling
String handling functions are commonly iterate over the array of characters until '\0'
(0
) is encountered.
For example, this program prints all arguments with their lengths calculated by a special function that
imitates the strlen
library function.
#include <stdio.h>
int mystrlen(const char* str) {
int len = 0;
while (*str++) {
len++;
}
return len;
}
int main(int argc, char *argv[]) {
int i;
for (i = 0; i < argc; i++) {
char* arg = argv[i];
printf("%s (%d)\n", arg, mystrlen(arg));
}
return 0;
}
This example program compares all stings provided in command-line arguments with the first command-line argument:
#include <stdio.h>
int mystrcmp(const char *s1, const char *s2) {
const unsigned char *p1 = ( const unsigned char * )s1;
const unsigned char *p2 = ( const unsigned char * )s2;
while (*p1 && *p1 == *p2 ) {
++p1;
++p2;
}
return (*p1 > *p2) - (*p2 > *p1);
}
int main(int argc, char *argv[]) {
int i;
for (i = 2; i < argc; i++) {
char* arg = argv[i];
printf("%s (compared to %s = %d)\n", arg, argv[1], mystrcmp(arg, argv[1]));
}
return 0;
}
Example of using C library functions
The following program catargs.c
concatenates all command-line arguments into a single string
and prints this string with the <
prefix and the >
suffix:
Output
acos@acos-vm:~$ ./catargs qwe ASD "1 2 3"
<qweASD1 2 3>
Code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int i;
int length = 0;
for(i = 1; i < argc; i++)
length += strlen(argv[i]);
char *buf = malloc(length + 3);
strcpy(buf, "<");
for(i = 1; i < argc; i++)
strcat(buf, argv[i]);
strcat(buf, ">");
printf("%s\n", buf);
free(buf);
return 0;
}