wleci
AboutProjectsBlogContact
Contact
Back to blog
StringsC-StyleBasics

Stage 6: C-Style String Handling (char[])

A guide to low-level string processing as null-terminated character arrays, following exam constraints.

February 11, 20263 min read
Share:

C-Style Strings: Char Arrays

Unlike the std::string class, C-style strings are simply arrays of elements of type char. The key element that distinguishes a regular character array from a "string" is the null terminator, written as \0. It tells functions like cout or printf where the text ends.


1. Declaration and Initialization

Remember that the array must always be 1 element larger than the number of characters you want to store to accommodate the \0.

cpp
char str[6] = "Hello"; // 5 characters + \0 char noSize[] = "Auto size"; // The compiler calculates the size automatically

2. Input and Output

According to your exam rules, you can use both iostream and cstdio.

Using iostream:

  • std::cin >> str; – Reads text up to the first whitespace. Risky as it may exceed the array size.
  • std::cin.getline(str, size); – The safest method. Reads a whole line (including spaces) and ensures the buffer is not overflowed.

Using cstdio:

  • scanf("%s", str); – Similar to cin, stops at whitespace.
  • printf("%s\n", str); – Fast way to print text.

3. Manual String Operations (Without <cstring>)

Since your list of allowed libraries does not include <cstring> (which contains strlen or strcpy), you must be able to perform these operations manually using loops. This is a common test of skill in exams.

Calculating Length (Custom strlen)

cpp
int getLength(char tab[]) { int i = 0; while (tab[i] != '\0') { i++; } return i; }

Copying a String (Custom strcpy)

cpp
void copyString(char dest[], char source[]) { int i = 0; while (source[i] != '\0') { dest[i] = source[i]; i++; } dest[i] = '\0'; // Very important: adding the null terminator at the end! }

4. Pitfalls and Critical Errors

  1. Missing \0: If you fill the array manually and forget the null terminator, cout will print characters until it happens to hit a zero in memory, resulting in "garbage" on the screen or a crash.
  2. Buffer Overflow: Attempting to read 20 characters into a char[10] array. Always use cin.getline with the size specified.
  3. Comparing Strings: if (str1 == str2) will not work correctly for char[] arrays (it compares memory addresses, not content). You must write your own function to compare them character by character.

Example: Comparing Strings

cpp
bool areEqual(char t1[], char t2[]) { int i = 0; while (t1[i] != '\0' && t2[i] != '\0') { if (t1[i] != t2[i]) return false; i++; } return t1[i] == t2[i]; // Check if both end at the same position }

You might also like

PointersMemory

Stage 11: Pointer Arithmetic in C++

Understanding how C++ operates on memory addresses. Learn why ptr++ is more than just adding one.

3 min read
PointersMemory Management

Stage 10: Pointers, Address-of, and Dereference Operators

Understanding the foundations of memory addressing in C++: how pointers and low-level operators work.

3 min read
FunctionsBasics

Stage 4: Functions and Argument Passing in C++

An in-depth analysis of function definitions, prototypes, and the differences between passing by value and by reference.

3 min read
Back to blog
wleci.pl

Full-stack Developer

I build modern web applications with passion for clean code and good design.

[email protected]
Poland

Navigation

  • Home
  • About
  • Projects
  • Blog
  • Contact

Services

  • Web applications
  • Websites
  • API & Backend
  • Consulting

Technologies

  • React / Next.js
  • TypeScript
  • Node.js
  • PostgreSQL

Social

© 2026 wleci.pl. All rights reserved.

Privacy policy•Terms of service

Made with in Poland