I'm an absolute beginner to coding and to the world of c++. Have started reading a book named c++ primer and I encountered in one of the exercises a question that asks "Is this code legal?". I'm personally clueless about what this means or refers to as a beginner. Please guide a humble rookie in need!

- 791
- 1
- 9
- 13
-
2Why did you tag with C when you obviously is asking C++ ? – Support Ukraine Sep 07 '21 at 11:14
-
Sorry I am new to this website – ḞṚḮḘṆḌṢḤḮṖ ḯṣ ḅḙạụṭḯḟụḷ Sep 07 '21 at 11:16
-
6It means "does this code conform to the rules?" – molbdnilo Sep 07 '21 at 11:16
-
All right. Thank you. The word 'legal' can be deceptive! I thought it might refer to some other more professional or formalized issue. Thanks a ton! – ḞṚḮḘṆḌṢḤḮṖ ḯṣ ḅḙạụṭḯḟụḷ Sep 07 '21 at 11:19
-
1I guess it is similar to crossing a red traffic light. You can either pedantically follow the standard and write portable "legal" C++, or you can deviate because you know what happens with a specific compiler on some specific platform, but then you shouldnt complain when getting hit by a car. – 463035818_is_not_an_ai Sep 07 '21 at 11:20
-
1The language has a standard, that lays out all the rules for how the language works. What you are allowed to do and what you are not allowed to do. Is it legal -> does it follow the rules? – super Sep 07 '21 at 11:20
-
2If you can't find a word in the index of that book, you can pretty safely assume that it's Just English. – molbdnilo Sep 07 '21 at 11:21
-
1Thank you very very much for this. You have very kindly answered my question and clarified my doubts. I shall keep your advice in mind while reading the book. – ḞṚḮḘṆḌṢḤḮṖ ḯṣ ḅḙạụṭḯḟụḷ Sep 07 '21 at 11:23
-
2fwiw, comments arent proper answers. Imho the question is offtopic, because "legal" isnt a term that is specific to C++ code. You didn't know that, so don't worry when the question gets closed for being offtopic. – 463035818_is_not_an_ai Sep 07 '21 at 11:25
-
1Yes, I understand. I don't mind the question being closed as my doubts are clarified. Apologies for the off topic question, but I was very desperate to find an answer. Thanks once again and regards. – ḞṚḮḘṆḌṢḤḮṖ ḯṣ ḅḙạụṭḯḟụḷ Sep 07 '21 at 11:29
-
@463035818_is_not_a_number and potential close voters: I believe this question should stay open. It clearly relates to writing code, and there could be non-native English speakers out there who have the same question. Just as "that's not possible" is a valid answer, I believe "it means the same thing as plain English" is a valid answer, too. – Charlie Armstrong Sep 13 '21 at 05:01
2 Answers
"Legal" colloquially means "well formed" and "not undefined" in this context.
Let's start with an analogy. The English language has rules. If I write to you in English: "äöldksfg", you wouldn't understand me, because that isn't a well formed word. If I wrote instead "if if if if if if", you would understand every word, but it still wouldn't be a well formed sentence. If I wrote "The treehorse hums with blasphemy", you would recognise the sentence structure as it is well formed, but it still probably makes little sense to you.
The C++ language - like the English language - has rules. The rules of programming languages are much stricter than human languages. The rules of the C++ language are written in the C++ standard document. As a document filled with rules, it is similar to a book of laws. Conforming to the book of laws makes your actions legal, and following the analogy, conforming to the rules of the C++ language makes your program "legal" as well.

- 232,697
- 12
- 197
- 326
The rules for writing C++ code is described in a standard (See Where do I find the current C or C++ standard documents?)
If a piece of code complies to every single rule of the standard, it's legal code.
The first step is that the code must comply to the language syntax described by the standard. For instance:
int x = +/-;
int x =
are examples of illegal statements, i.e. illegal syntax. Your compiler will find such errors for you.
But it's much more complicated than that. The fact is that you can write code that complies to the language syntax but still is illegal.
A simple example:
int* x;
void foo(void)
{
int n = 42;
x = &n;
}
void bar(void)
{
foo();
printf("%d\n", *x); // Illegal - using pointer to "dead" object
}
When you dig into C++ you'll find that there are so many ways to write code with legal syntax but illegal/undefined behavior.

- 42,271
- 4
- 38
- 63
-
Hm... I wouldn't have termed undefined behaviour as illegal. I would think of, for example, non portable code being more illegal, at least to the Standard's eyes, than UB code. – rturrado Sep 07 '21 at 12:14
-
@rturrado nah... non portable code is **not** illegal - it's just restricted to specific systems. – Support Ukraine Sep 07 '21 at 17:36
-
@rturrado but code with undefined behavior are illegal from standard point of view. Anything is allowed to happen... – Support Ukraine Sep 07 '21 at 17:50