C Tutorial — Learn C Programming
Key takeaway: This free C tutorial teaches you systems programming from scratch — variables, pointers, arrays, functions, structs, and memory management — with practical code examples you can compile and run.
C is a general-purpose, procedural programming language created in 1972. It is used for operating systems, embedded systems, game engines, and performance-critical applications. This tutorial is for beginners, CS students, and developers who want to understand how computers work at a low level.
Last updated: September 2026
Strings in C
In C, strings are arrays of characters terminated by a null character (\0). C provides various functions to manipulate strings through the string.h library.
String Declaration and Initialization
#include <stdio.h>
#include <string.h>
int main() {
// Different ways to declare and initialize strings
char name1[20] = "John";
char name2[] = "Alice";
char name3[20];
// Input string from user
printf("Enter your name: ");
scanf("%s", name3); // Note: no & needed for strings
// Display strings
printf("Name 1: %s\n", name1);
printf("Name 2: %s\n", name2);
printf("Name 3: %s\n", name3);
// String length
printf("Length of name1: %lu\n", strlen(name1));
return 0;
}String Functions
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello";
char str2[50] = "World";
char str3[50];
// String copy
strcpy(str3, str1);
printf("After strcpy: %s\n", str3);
// String concatenation
strcat(str1, " ");
strcat(str1, str2);
printf("After strcat: %s\n", str1);
// String comparison
if (strcmp(str2, "World") == 0) {
printf("Strings are equal\n");
}
// String length
printf("Length of str1: %lu\n", strlen(str1));
return 0;
}Common String Functions
| Function | Purpose | Example |
|---|---|---|
strlen() | Get string length | strlen("Hello") returns 5 |
strcpy() | Copy string | strcpy(dest, src) |
strcat() | Concatenate strings | strcat(str1, str2) |
strcmp() | Compare strings | strcmp(str1, str2) |
strchr() | Find character | strchr(str, 'a') |
strstr() | Find substring | strstr(str, "sub") |
Important
- Always ensure destination arrays are large enough
- Remember the null terminator \0
- Use
fgets()instead ofgets()for safety - Include
<string.h>for string functions