搜尋此網誌

2025年7月1日星期二

C strings

consecutive: following one after another in a continuous series

The null character --- written as '\0' in C and C++ --- is a special character used to mark the end of a string in many programming languages, especially those influenced by C.

<cstring> Header in C++

This is part of the C++ Standard Library, inherited from C’s <string.h>. It provides functions for manipulating C-style strings (null-terminated character arrays).

The String class is a fundamental part of many programming languages, used to represent and manipulate sequences of characters.

std::string greeting = "Hello";
greeting += " World";
std::cout << greeting;  // Output: Hello World

size_t is a special unsigned integer type used in C and C++ to represent the size of objects in memory or array indices.

An unsigned integer is a whole number data type that can only represent non-negative values—that is, zero and positive numbers.

A character array is a data structure used to store a sequence of characters --- essentially, it's how many programming languages represent strings under the hood.

ASCII stands for American Standard Code for Information Interchange. It’s a character encoding standard that assigns numeric values to letters, digits, punctuation marks, and control characters so computers can store and exchange text reliably.

In C++, strncpy() is a function from the C standard library (inherited via <cstring>) used to copy a fixed number of characters from one C-style string to another. It's a bit of a double-edged sword --- powerful but easy to misuse if you're not careful.

The sizeof operator in C and C++ is used to determine the size, in bytes, of a data type or object at compile time.

strncpy(name, src, sizeof(name) - 1);
name[5] = '\0';  // Manually null-terminate

Here, the -1 is used because:
You reserve the last slot for the null terminator.
You copy at most n - 1 characters to avoid overflow.
Then manually add '\0' at the end.

concatenate: to link together in a series or chain

The strcat() function in C is used to concatenate two C-style strings—that is, it appends one string to the end of another.

The strncat() function in C is used to safely concatenate a limited number of characters from one C-style string to another.

In C++, strlen() is a function from the C standard library (accessible via <cstring>) that returns the length of a null-terminated C-style string, excluding the null character '\0'.

Microsoft Copilot
www.oxfordlearnersdictionaries.com
www.merriam-webster.com

沒有留言:

發佈留言