problem with function

int print(void);

int main(void)
{
    cout << "The Lucky " << print() << endl;     //This line
    return 0;
}

int print(void)
{
    cout << "No : ";
    return 3;
}

expected output is THE LUCKY 3

but what i got is NO: THE LUCKY 3

please help me.Thank you

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?

in that line i am calling print function whats here making no sense

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.

Mike, that is perfectly normal C++ stream format code.

Is it. Never come across that before.
Well it won't compile on an Arduino IDE, so in what way is this an arduino question?

Grumpy_Mike:

Mike, that is perfectly normal C++ stream format code.

Is it. Never come across that before.
Well it won't compile on an Arduino IDE, so in what way is this an arduino question?

I think it does if you include the stream library or something. Anyway, for all we know it could be being used to interface a PC with an Arduino.

Ours is not to reason why,
Ours is but to do and die.

Edit: Streaming info here: Arduino Playground - HomePage

Edit 2: The code compiles (won't work, but compiles) on an Arduino with the addition of 3 lines:

template<class T> inline Print &operator <<(Print &obj, T arg) { obj.print(arg); return obj; } 
#define cout Serial
#define endl '\n'

Thanks, for the info.
No idea how the OP managed to get it to run without those lines.

int print(void)
{
    cout << "No : ";
    return 3;
}

Isn't that 5 characters?


This compiles and works:

template<class T> inline Print &operator <<(Print &obj, T arg) { obj.print(arg); return obj; } 
#define cout Serial
#define endl '\n'

int foo(void)
{
    cout << "The Lucky " << print() << endl;     //This line
    return 0;
}

int print(void)
{
    cout << "No : ";
    return 3;
}

void setup ()
  {
  Serial.begin (115200);
  Serial.println ();
  foo ();
  }  // end of setup

void loop () { }

Output:

The Lucky No : 3

So my response is: Cannot reproduce.

Disclaimer: I wouldn't code like that.

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.

Never come across that before.

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!

Mark

cout <<   blah blah  << endl;

Or

printf("blah blah\n");

I think I know which I prefer.
Never really understood the shift left overload.

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 :slight_smile:

holmes4:

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.