3

Possible Duplicate:
What does the 'k' prefix indicate in Apple's APIs?
Objective C - Why do constants start with k

For example, the result codes defined for Audio Format Services:

  • kAudioFormatUnspecifiedError
  • kAudioFormatUnsupportedPropertyError
  • etc...

What does that leading k stand for? I've always assumed key, since such constants are often used as the keys in dictionaries, but those result codes are an example of where the constant is just a return value, not (as far as a client of the API can determine) a key.

Community
  • 1
  • 1
Simon Whitaker
  • 20,506
  • 4
  • 62
  • 79
  • You can find the answer here : http://stackoverflow.com/questions/500030/what-is-the-significance-of-starting-constants-with-k –  Jan 30 '12 at 13:48

3 Answers3

4

I imagine that it merely stands for 'k'onstant, where 'k' is used because 'c' is already commonly used to indicate class or in Hungarian notation character.

The usage has historical precedent; early pocket calculators used 'k' to indicate constant mode (where repeated operation of = repeated the last operation) because 'c' was used for clear.

Clifford
  • 88,407
  • 13
  • 85
  • 165
  • This always felt wrong to me, even more in 2020, but thanks for the explanation. It looks like some guys can't get rid of the habit – brainray Feb 28 '20 at 18:57
1

You can find the answer here.

Answer one

Constant names (#defines, enums, const local variables, etc.) should start with a lower-case k and then use mixed case to delimit words, i.e. kInvalidHandle, kWritePerm.

Though a pain to write, they are absolutely vital to keeping our code readable. The following rules describe what you should comment and where. But remember: while comments are very important, the best code is self-documenting. Giving sensible names to types and variables is much better than using obscure names and then trying to explain them through comments.

But it has since been removed in the live version of the document. It should be noted that it goes against the the Official Coding Guidelines for Cocoa from Apple.

Cocoa coding guidelines

Community
  • 1
  • 1
  • 1
    That explains *where* 'k' is used, but not what it "stands for" specifically. The question indicates that he knew that it was used for constants, but not that it stood for 'k'onstant, or why. – Clifford Jan 30 '12 at 16:07
0

It was the coding standard left over when Apple used pascal. K was the prefix as opposed to all caps for other C languages.

OnResolve
  • 4,016
  • 3
  • 28
  • 50