C Programming Tutorial 0/65 lessons ~6 min read Lesson 27
String Functions
String functions from string.h — strlen, strcpy, strcat, strcmp, and safer variants strncpy, strncat.
Course progress0%
Focus
10 guided sections
Practice signal
Examples included
Career prep
Foundation builder
Introduction
String functions from string.h — strlen, strcpy, strcat, strcmp, and safer variants strncpy, strncat.
Understanding the topic
strlen Returns length excluding null terminator.
strcpy / strncpy Copy string — strncpy limits bytes copied.
strcmp Compare lexicographically — 0 if equal.
- strlen — Returns length excluding null terminator.
- strcpy / strncpy — Copy string — strncpy limits bytes copied.
- strcmp — Compare lexicographically — 0 if equal.
Step-by-step explanation
- strlen — Returns length excluding null terminator.
- strcpy / strncpy — Copy string — strncpy limits bytes copied.
- strcmp — Compare lexicographically — 0 if equal.
Syntax reference
Syntax reference:
c
#include <string.h>
Informative example
Example program:
c
#include <stdio.h>#include <string.h>int main(void) {char a[20] = "Hello";strcat(a, " C");printf("%s len=%zu\n", a, strlen(a));return 0;}
Output
Hello C len=7
Execution workflow
1String Functions — step by step
1 / 3strlen
Returns length excluding null terminator.
Best practices
- Enable warnings: gcc -Wall -Wextra -std=c11 source.c -o app
- Give every variable a defined value before it is read.
- Stay inside array bounds — C will not stop you from over-running a buffer.
Common mistakes
- strcpy buffer overflow if destination too small
Hands-on exercise
Practice problems:
- Compare two strings
- Copy with strncpy safely
Summary
String Functions in C — Common helpers from string.
Ready to mark this lesson complete?Track your journey across the entire course.