+ 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
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 ^^
+ 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/
+ 9
+Thanks Setiatwan: If you never asked this question I would have continued to believe that these items are functions supplied by iostream.
+ 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
+ 8
ooo i will shut up. lol
+ 7
@visph. What about this example?
https://code.sololearn.com/ccS3CgQzwsvi/?ref=app
+ 7
mmhmm but shouldnt it have a fit about it? being that it is a part of namespace std?
+ 7
ambiguity? or does local declaration trump includes?
+ 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.
+ 7
lol Tell your teacher I said thanks then!
+ 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.)
+ 6
I think result of them is error....
+ 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
+ 5
I am tempted to say that it's a logical behaviour: the local scope preced the namespace std, as it is overwriting?
+ 5
Ask your teatcher who asked to him :D
+ 4
end of the line <<endl;
+ 4
@jay nice work .
one more concept local variable and global variable
+ 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.
+ 3
i have not seen your code as endl is inside the namespace std so you can write it.
+ 3
don't thank me.
my teacher ask me, so i ask you guys.