I was wondering if operators or keywords are the same? If not what is the difference?
-
To which language(s) do you refer? – Greg Hewgill Apr 22 '11 at 08:58
-
well I'm currently looking at CSS – HELP Apr 22 '11 at 08:59
3 Answers
Keywords are tokens that are reserved and have a special meaning in a given programming language. Operators are keywords that can take one or more arguments. They are usually associated with the standard mathematical operations, but for example new is considered a single argument operator as well. Operators are usually written in the infix notation (left [operator] right
) and can be nested (taking into account their precedence). Moreover, many languages allow to overload operators, but not necessarily keywords in general.

- 12,011
- 2
- 32
- 32
-
2Operators are written in infix notation only in languages that use it. – Ignacio Vazquez-Abrams Apr 22 '11 at 09:00
-
1
-
-
+1 - your answer makes most sense. Weird, coding for a while and such a basic question can have you thinking! – manojlds Apr 22 '11 at 09:05
-
I did. I also noticed unary positive and negative, boolean and bitwise negation, and `new` and `delete`. – Ignacio Vazquez-Abrams Apr 22 '11 at 09:06
-
@Ignacio All of your examples are unary operators, for which infix collapses to prefix. Are you just arguing for the sake of argument? – Adam Byrtek Apr 22 '11 at 09:14
+
: an operator
new
: a key word, but also an operator
const
: a keyword but not an operator
Broadly, a "keyword" refers to any otherwise valid identifiers (except a few things like literals) that can't be one because it's reserved by the language. (An identifier can be a variable name, class name, namespace name, etc.) keywords are key words, that's all. They might even do nothing, like Java's const
keyword.
An operator is a language element that does something, like addition, parentheses, new
, etc... it may or may not be a word.

- 205,094
- 128
- 528
- 886
-
I think you editted? I downvoted because new is both a keyword and an operator – manojlds Apr 22 '11 at 09:00
-
@manojlds: Ah yeah, I did... I put up an answer quickly then fixed the details the minute after, sorry. xD – user541686 Apr 22 '11 at 09:01
Keywords are "words" that have special meaning in the language you are programming in and some of them do some operation and hence operators as well. Like the new keyword which is also an operator.
http://msdn.microsoft.com/en-us/library/kewsb8ba(v=VS.100).aspx
Operators like new are generally considered as "Alphanumeric operator symbols" rather than keywords. Other examples are sizeof
delete
throw
instanceof
I like the way Keywords are defined and handled in Smalltalk:
A keyword: is just an identifier with a colon on the end of it, e.g. anyIdentifierLikeThis: is a {keyword}. In Smalltalk, a keyword is only special in the sense that it forms a "keyword message". It is a distinct kind of token (different from an identifier or a string) but its meaning as an individual token is not special. Some languages have {keywords} like BEGIN and END with builtin special meanings. {Keyword} in Smalltalk is not this sort of thing, it's strictly a syntactic form.
Ther are only six "keywords" that are reserved in Smalltalk: true, false, nil, self, super, and thisContext. So not all keywords are necessarily reserved words in the language.
Reference: http://c2.com/cgi/wiki?SmalltalkTutorial
Regarding true and false:
In some languages like Java, these are boolean literals. In c#, these are both operators and literals. In both cases, these are not considered Keywords. In Smalltalk these are keywords. Really shows how things change from language to language.
-
-
Oh what?! you changed your answer but I still can't remove my downvote... haha let me know if you change it again so I can do that :) – user541686 Apr 22 '11 at 09:15
-
Added more :) remove downvote or keep it if you disagree again with what I have added :) – manojlds Apr 22 '11 at 09:24
-
Nah, it's better now. :) One note though: `true` is a boolean literal, not a keyword. – user541686 Apr 22 '11 at 09:36
-