%s has nothing to do with "notating string data" in C, nor in any other programming language that I'm aware of. The "string of a char" is a phrase which has no conceivable meaning in the context of the C programming language.

In C, strings are notated with double quotes, like so:

    "this is a string"

There are some fine points if you want to include characters like tabs or newlines, but none of it has anything to do with %s. %s is not really part of C: It's used by a few functions, but the compiler doesn't need to know or care about it at all.

%s is a "conversion specifier" (a.k.a. "format specifier") used by the C standard library function printf(), and by several related functions such as sprintf(), fprintf(), the unspeakably vile scanf(), and so on. All of these functions convert things to strings, and then either print them or just copy the string into a buffer. Conversion specifiers tell the functions how to format the stuff you give them. (See "printf conversion specifiers" for an overview).

%s is the most interesting of the conversion specifiers because you can use it to screw yourself.

%s is used specifically and exclusively for formatting null-terminated character arrays, also known as strings. If you try to use %s to format an integer or a character or a floating point number, your program will probably crash. This is because %s instructs printf() to take the first four bytes1 of what you've given it, and to assume that those four bytes are a number specifying an address in memory. It will go to that address, and it will probably crash, because your program probably doesn't have any right to go messing around there (I say "probably" because it's an arbitrary address: Once in a great while you may luck out). If by a long chance it happens to end up somewhere it's allowed to go, printf() will merrily take the first byte 2 that it finds there, interpret it as a character, and print it to standard output. It will then do the same with all subsequent bytes until it either (a) hits a byte equal to zero (where it will stop, since a zero byte is conventionally used to indicate the end of a string in C), or (b) tries to access memory to which it doesn't have access, in which case it will probably crash. Even if it doesn't crash, odds are good that it will have printed gibberish during its little adventure. The same applies to sprintf() except for where the output goes.

If the thing you've given printf() is in fact the address of a null-terminated character array in a sane location, all of printf()'s assumptions will be valid and life will be grand.

1 If you've given it less than four bytes (e.g. a character), what happens is implementation dependent, but in most cases it'll be relatively benign compared to the other ways in which you've screwed yourself. Obviously, I'm assuming a 32-bit system. If you've got a 64-bit system, substitute eight bytes for four. If you've got a 16-bit system, download Linux or BeOS R5. If you've got an 8-bit system, you've got worse problems than this, and you probably don't have a C compiler anyway.

2 Two bytes if it's Unicode.

Log in or register to write something here or to contact authors.