Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective C - Why do constants start with k

Why do constants in all examples I've seen always start with k? And should I #define constants in header or .m file?

I'm new to Objective C, and I don't know C. Is there some tutorial somewhere that explains these sorts of things without assuming knowledge of C?

like image 839
mk12 Avatar asked Aug 05 '09 02:08

mk12


People also ask

Why do constants start with K?

No reserved words start with k so it is easier as a search target. Alternatively, from a graphical aspect, as a leading character it provides a very clear flag in front of the rest of the name.

How do constants work in C?

A constant is a name given to the variable whose values can't be altered or changed. A constant is very similar to variables in the C programming language, but it can hold only a single variable during the execution of a program.

Why the value of C is constant?

Constants in C are the fixed values that are used in a program, and its value remains the same during the entire execution of the program. Constants are also called literals. Constants can be any of the data types.


1 Answers

Starting constants with a "k" is a legacy of the pre-Mac OS X days. In fact, I think the practice might even come from way back in the day, when the Mac OS was written mostly in Pascal, and the predominant development language was Pascal. In C, #define'd constants are typically written in ALL CAPS, rather than prefixing with a "k".

As for where to #define constants: #define them where you're going to use them. If you expect people who #import your code to use the constants, put them in the header file; if the constants are only going to be used internally, put them in the .m file.

like image 135
mipadi Avatar answered Sep 28 '22 05:09

mipadi