+ 3

What is endl?

i know that it make new line similliar like \n but why i can use it as variable name? i am using namespece std, so shouldn't it doesn't work? (like @Squidy explanation regarding cout) but it worked. why? TIA. https://code.sololearn.com/cV6rqcTdNkhy/?ref=app

13th May 2017, 6:37 AM
Setiawan Next
Setiawan Next - avatar
26 Answers
+ 5
Code snippet linked in question: #include <iostream> using namespace std; int main() { int endl = endl == endl; endl = endl+++endl; cout << endl; return 0; } You are initializing a local var 'endl' with the result of the comparison of 'endl' in the 'std' namespace scope. So, 'true' ( 1 ) is assigned to 'endl'. Next you have a trivial expression, as different compilers could interpret implicitly different result depending on precedence rules :P Running in code playgroud computer it result 3, but anyway, also in code playground: (endl++)+endl == 3 endl+(++endl) == 4 ... so, you can deduct the implicit precedence used ^^
13th May 2017, 7:43 AM
visph
visph - avatar
+ 9
i tried searching interwebs. best thing i could find was some vague reference about cout, endl, string and cin as not being keywords that cant be used as variables http://www.abukhleif.com/can-we-use-endl-cin-cout-string-as-variable-names-in-c-and-why/
13th May 2017, 10:34 AM
jay
jay - avatar
+ 9
+Thanks Setiatwan: If you never asked this question I would have continued to believe that these items are functions supplied by iostream.
13th May 2017, 10:53 AM
jay
jay - avatar
+ 9
Good to have teachers like that. Too few and far between. They need to be paid more so they can attract talent into the field of teaching. Many good people do not consider it due to low pay compared to the business world
13th May 2017, 11:29 AM
jay
jay - avatar
+ 8
ooo i will shut up. lol
13th May 2017, 10:29 AM
jay
jay - avatar
+ 7
@visph. What about this example? https://code.sololearn.com/ccS3CgQzwsvi/?ref=app
13th May 2017, 10:21 AM
jay
jay - avatar
+ 7
mmhmm but shouldnt it have a fit about it? being that it is a part of namespace std?
13th May 2017, 10:27 AM
jay
jay - avatar
+ 7
ambiguity? or does local declaration trump includes?
13th May 2017, 10:28 AM
jay
jay - avatar
+ 7
I have no idea. I just thought that it should spew out errors saying hey this could be std::endl its a function not an int cause we have all the std namespace in scope.
13th May 2017, 10:33 AM
jay
jay - avatar
+ 7
lol Tell your teacher I said thanks then!
13th May 2017, 11:08 AM
jay
jay - avatar
+ 6
It is not actually defined as a keyword anywhere as far as I can see. So you "can" do this but should not as it will be very very confusing to anyone reading your code. Why they are not defined as keywords is a mystery to me. (This applies to cout, cin and string as well.)
13th May 2017, 7:10 AM
jay
jay - avatar
+ 6
I think result of them is error....
13th May 2017, 2:44 PM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
+ 5
Code snippet linked by @Jay: #include <iostream> using namespace std; int main() { int endl ; endl = 42; cout << endl; return 0; } Seriously? @@ It output 42 ( THE answer ^^ ), as 'endl' is locally declared :P
13th May 2017, 10:26 AM
visph
visph - avatar
+ 5
I am tempted to say that it's a logical behaviour: the local scope preced the namespace std, as it is overwriting?
13th May 2017, 10:30 AM
visph
visph - avatar
+ 5
Ask your teatcher who asked to him :D
13th May 2017, 11:08 AM
visph
visph - avatar
+ 4
end of the line <<endl;
13th May 2017, 6:40 AM
MR Programmer
MR Programmer - avatar
+ 4
@jay nice work . one more concept local variable and global variable
13th May 2017, 10:35 AM
MR Programmer
MR Programmer - avatar
+ 4
my teacher is smart and creative. he is not programmer, but in just a month he can do crazy think with python and ruby in linux server. if you know him, you'll know that he never run out idea to puzzle his student.
13th May 2017, 11:26 AM
Setiawan Next
Setiawan Next - avatar
+ 3
i have not seen your code as endl is inside the namespace std so you can write it.
13th May 2017, 7:12 AM
MR Programmer
MR Programmer - avatar
+ 3
don't thank me. my teacher ask me, so i ask you guys.
13th May 2017, 11:07 AM
Setiawan Next
Setiawan Next - avatar