C++ std::string front() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::string

Definition

In C++, std::string::front() returns a reference to the first character of the string.

The string must not be empty. Check empty() first. Include <string>.

This is a member of std::string. A string grows as you append, stores characters contiguously, and can reallocate—after growth, old iterators and references may be invalid. Prefer at() when you want bounds checks; remember find returns npos when missing.

Syntax

Call with empty brackets on a non-empty string.

C++
char c = s.front();
s.front() = 'A';
CallWhat it does
s.front()Reference to the first character.

Same idea as s.at(0) when the string is non-empty, but front() does not throw—calling it on an empty string is undefined behavior.

Library declarations
C++
char& front();
const char& front() const;

Parameters

ParameterDescription
Nonefront() takes no parameters. Call it with empty brackets.

Return Value

Return ValueDescription
char&Reference to character 0. Const overload returns const char&.

C++ Examples

Example 1 — Basic front()

Print the first character.

C++
#include <iostream>
#include <string>

int main() {
    std::string s = "Hello";
    std::cout << s.front() << '\n';
    return 0;
}

How It Works

  1. The string starts with 'H'.
  2. front() returns a reference to that character.
  3. Printing it shows H.

Example 2 — Assign front()

Change the first letter in place.

C++
#include <iostream>
#include <string>

int main() {
    std::string s = "cat";
    s.front() = 'b';
    std::cout << s << '\n';
    return 0;
}

How It Works

  1. front() refers to the first character 'c'.
  2. Assigning 'b' updates it.
  3. The string becomes "bat".

Example 3 — Check Before front()

Skip front() when the string is empty.

C++
#include <iostream>
#include <string>

int main() {
    std::string s;
    if (s.empty()) {
        std::cout << "empty\n";
    } else {
        std::cout << s.front() << '\n';
    }
    s = "Z";
    if (!s.empty()) {
        std::cout << s.front() << '\n';
    }
    return 0;
}

How It Works

  1. The first string is empty, so we print a message instead of calling front().
  2. After assigning "Z", the string has one character.
  3. front() safely returns 'Z'.

Frequently Asked Questions

A reference to the first character of a non-empty string.
Calling front() is undefined behavior. Check empty() first.
For a non-empty string, both refer to the first character. at(0) throws on empty; front() does not throw—it is undefined.
Yes, on a non-const string: s.front() = 'x';
Yes, as a const reference for reading.

Next: get_allocator()

Keep going through the string functions. Return a copy of the allocator that manages the string’s memory.

get_allocator() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful