Everything2
Near Matches
Ignore Exact
Full Text
Everything2

C string

created by mcc

(idea) by mcc (2.5 y) (print)   ?   1 C! I like it! Sat Aug 12 2000 at 2:07:20

The type of string used in the C programming language and most languages like it. This type of string is just a general concept and is not really hardwired into the language (although it is what the standard library expects in its input and output functions, and the double-quote operators use it-- but more on that later) but rather based simply on convention. It is named "C string" because it was first used in C, but it doesn't necessarily have to be a C thing. Like most things related to C, the concepts behind it give you an extremely high degree of flexibility as well as an environment where you can bugger things up rather horribly.

A C string (in any language) is stored in memory as a series of bytes representing characters (in C, this is a char array). The characters to be used begin at the first address possible (in C, address zero) and continue indefinitely. The end of the string is marked by a null terminator, a byte set equal to zero. (Null chars should be referred to in C as '\0'.) So when printf or cout is passed a string, it simply bangs everything out character by character until it reaches a null character, and then it stops.

If you are screwing with strings by hand in C, you need to be very aware of that null character, and make sure you don't accidentally leave it out. You also need to be aware that a four-letter string actually contains five characters because of the '\0'.

This is, by the way, the only difference in C between single quotes and double quotes when giving a series of chars; namely, the single chars are the literal, raw values of the chars, and the double quotes actually create a full C string with a null terminator. Thus 'k' is a char, whereas "k" is a 2-char array containing a k and a \0.

See also Pascal string


(thing) by Calin (5 y) (print)   ?   I like it! Fri Jan 11 2002 at 4:05:41

I beg to differ. You imply that C-style strings aren't built into the language of C, yet they absolutely are. Ever write a string literal in a C program?

That is: char * mystring = "This is a string literal.";

That string literal gets compiled into your binary as a series of 25 bytes, the last of which most definitely contains the null terminator byte.

The reason we can say that C-style strings are inherently built into the C language is the presence of the NUL character that implicitly follows a string literal. That is a C convention (other languages do it too, you are right) but we do say the notion of a C-style string is inherent in the language. Also, if you read the K & R C book, they definitely acknowledge that C-style strings are an inherent 'feature' of the C language.


printable version
chaos

Pascal string Not reading a string in C G-string string handling in C
Why Pascal Sucks Reading a string in C Why programmers don't comment their code buffer overflow
C backslash codes CString g-spot Thong
string literal exhaustive programming disorder I got a good feeling! Thylacine
String Hungarian notation C/C++ headers C Arrays
\0 String Theory C99 A Brief Guide To C
Y'know, if you log in, you can write something here, or contact authors directly on the site. Create a New User if you don't already have an account.
  Epicenter
Login
Password

password reminder
register

Everything2 Help

Cool Staff Picks
After stirring Everything, these nodes rose to the top:
bittersweets.org
Crazy Eddie
E2 is unfriendly to new noders
Head to toe (protocol)
How the Alphabet Began
On the Arts and Sciences to be Studied
catting weird things to /dev/audio
Laughter and lying on grass
It's all happy endings and meaningful conversation until someone gets hurt
Welcome Home
negative nodevertising
The Metamorphosis
She is in the heartbeat you hear around you, listen
New Writeups
Wuukiee
May 15, 2008(idea)
locke baron
Kuznetsov class aircraft carrier(thing)
_lesra
for abby(thing)
Adaptive Child
Annie's garden salsa(recipe)
Simulacron3
Zig-Zag(thing)
Ouzo
questions(idea)
Ouzo
Special Grilled Cheese(fiction)
Noung
Tiananmen Square Massacre(idea)
aneurin
Lord St Clair(person)
artman2003
Assholes and Douchebags: A Comparison(person)
locke baron
Tyan Thunder K8WE(thing)
locke baron
Udaloy class destroyer(thing)
Scaevola
Same-sex marriage(idea)
SteveMurrayFromNZ
British Standard Handful(idea)
nailbiter
nerve stapling(thing)
This affordable entertainment brought to you by The Everything Development Company