cout << "The Lucky " << print() << endl; //This line
This line makes no sense at all.
In fact the whole code is not written in C if indeed it is a language.
Do not return anything from a void function.
What on earth are you trying to do?
bhuvaneshnick:
in that line i am calling print function whats here making no sense
Non of it makes any sense. You are not calling the print function.
Talk me through what you think each bit of the line means.
Better still tell me what you are trying to do.
Mike, that is perfectly normal C++ stream format code.
The problem here is basically that of buffering.
The code is taking "The Lucky " and the return value of the print() function (3) and appending them to a buffer before printing them out (cout). In the mean time the print() function has started its own buffer for printing "No: " which is then flushed before the function returns, so the "No: " is printed before the rest of the data.
The function print() is executed, which causes the "No :" to be printed, and the value 3 is returned. The cout line then prints "Lucky Number" and then "3", exactly as you would expect it to happen.
The "fashion" for all this "cout << blah blah << endl " nonsense , came and went in the early 90's sometime, it was tedious and unsightly, you were lucky to have missed it ! But it does exist and some people still use it.
michinyon:
The function print() is executed, which causes the "No :" to be printed, and the value 3 is returned. The cout line then prints "Lucky Number" and then "3", exactly as you would expect it to happen.
Good point. However the code is incredibly obscure. It would perhaps have been best if it had never worked.
cout << .......... is standard C++ streams. OK its not used on the Arduino, but it is straight out of C++ text book.
Good point. However the code is incredibly obscure.
Rubbish!!!!!!!!
The "fashion" for all this "cout << blah blah << endl " nonsense , came and went in the early 90's sometime, it was tedious and unsightly, It's a repacement for printf( " spec" data) and is much better even Java copies it!
Hugs Pascal Tightly...... (nothing that obfuscated in Pascal or Object Pascal (Delphi/Lazarus)
But ...
Character out (cout) I was using in c almost 20 years ago from a little yellow book I love Arduino for the fact obfuscated code like the OP's example can even exist, was not the idea of moving from punchcards to a lowish level language like assembly to make life easier not harder...
Luckily Arduino uses a nice clean well defined structure (compared to that of say using atmel's avr studio) eg using a while statement which will allways loop... what is this 1999 createwindowex and handle all the system messages! This is why I practically cry everytime I hsve to use Java it's the complete polar opposite to the mindset of writing code for an 8bit processor and few k of ram, imagine having to use Try/Catch on every other line, Variant mad.
I guess I like C to be clean and readable and Arduino allows it...
Op - next time mention which compiler you're using certain members get easily confused
Good point. However the code is incredibly obscure.
Rubbish!!!!!!!!
You don't think this is an obscure way of outputting: "No : 3" ?
int print(void)
{
cout << "No : ";
return 3;
}
Well I do. Bearing in mind I mistook that as a function that returned the number of characters it was sending (like write() does).
cout << "The Lucky " << print() << endl; //This line
That whole line deserves an entry in the Obfuscated C Competition. It's ridiculous to claim it is not obscure. Especially with a function called "print" (which you might easily confuse with printf which does return the number of characters printed) or with Serial.print which also returns the number of characters printed (not some number you want printed).
While I agree entirely that the syntax isn't nice, it is nontheless perfectly valid syntax, and to someone who knows the syntax it makes perfect sense. If you don't know the syntax, however, it's not immediately obvious what it's doing - especially when the << already has another meaning.
Personally I hate it - I never use it, even when programming direct in pure C++. But then I was brought up on C. Well, actually BASIC, Assembly (Z80 / 8080 / 6502), Pascal, COBOL and then C... C++ to me is just C with classes - I don't bother with any of the other fancy stuff like streams, and (only very rarely) templates.