Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-

C++

Name: Anonymous 2008-02-02 12:11

Hi /prog/

return 0; or not? \n or endl?

#include <iostream>
using namespace std;
int main()
{
    cout << "Hello World!" << endl;
    return 0;
}

#include <iostream>
using namespace std;
int main()
{
    cout << "Hello World!\n";
}

Name: Anonymous 2008-02-02 12:26

>>1
return 0; or not?
return 0, EXIT_SUCCESS or EXIT_FAILURE in ISO C, i dont know about C++98, on unix any 'int' value (exit() will &255 the value)
\n or endl?
It really depends on the kind of buffering the stream/abstract object has.
In C, \n flushes stdout and stderr.
In C++, it might be the case that cout or cerr are not flushed with \n, that's where you use std::endl; (or a flushing mechanism, obviously)

Here's an example where not having \n is bad:


#include <stdio.h>
#include <unistd.h>

int main(void) {
    pid_t pid;
    printf("hello");
    pid = fork();
    if(pid == -1) return -1;
    if(pid == 0) exit(0);
    return 0;
}

You might be surprised by the output.

Name: Anonymous 2008-02-02 12:51

SUPRISE BUTTSECHSOUTPUT!

Name: Anonymous 2008-02-02 13:16

its bad programming practice to not return a value at the end of a function that clearly states it has a return value (thats what the 'int' in 'int main' is for, fucker)

Name: Anonymous 2008-02-02 18:39

if you wanted to return nothing from the function, you should define it as void.

Name: Anonymous 2008-02-02 19:53

>>4
They don't realise this until they use Scheme and realise that (define x y) works for both variables and procedures, and that they are, in fact, the same thing.

Name: Anonymous 2008-02-02 20:12

>>6
Not related at all.
However, (define x y), x simply evaluates to the same value of y.
It does not matter whether y is a procedure or not.

Name: Anonymous 2008-02-02 21:54

>>6
C/C++ has the same thing:
#define x y

Name: Anonymous 2008-02-02 21:56

>>8
no, no it doesn't.


#define y(a, b) ((a)+(b)) /* adds a and b */
#define x y

x(1, 2); /* error */

Name: Anonymous 2008-02-02 22:22

>>9
Define them in reverse order and it works. The C preprocessor is a filthy hack.

Name: Anonymous 2008-02-02 22:34

>>10
No, it's not a filthy hack, please don't repeat things you've heard on IRC channels or blogs. It's *you* who does not know C (nor how the preprocessor treats tokens)

god dammit faggots, nobody knows C here?

Name: Anonymous 2008-02-02 22:45

C is a dead language.

Name: Anonymous 2008-02-02 22:52

ITT: IOStreamfags

Use stdio, faggots

Name: Anonymous 2008-02-02 22:55

>>12
nemo me impune lacessit

Name: Anonymous 2008-02-02 23:04

>>14
/b/ritfag

Name: Anonymous 2008-02-02 23:07

>>15
no, not really
I remember it from poe's story, ``the cask of amontillado"

Name: Anonymous 2008-02-03 0:06

>>11
The point is that comparing CPP's #defines (which are crappy macros largely inferior to the macros in CL) to Scheme's (defines) (which are fucking FUNCTIONS) is absolutely retarded. >>8 is a complete moron who read the word "define" and assumed that it must be the same thing as C's macros.

Name: Anonymous 2008-02-03 0:32

The function will return at the end irregardless of whether a return statement is present. So unless you plan to be using the return value for something, it's unnecessary.

\n saves a few extra characters when it's the last thing to be output in a string, but otherwise endl is easier to type.

cout << "my other car is a cdr.\n"
vs
cout << "number of scientologists killed: " << victims << endl;

BUT IT DOESN'T MATTER, YOU SHOULD BE USING printf()

Name: Anonymous 2008-02-03 1:17

>>SHOULD BE USING printf()

/thread

also notice my lack of EXPERT QUOTES

Name: Anonymous 2008-02-03 1:18

[quote]also notice my lack of EXPERT QUOTES[/quote]

I did. Go and lern2BBCode

Name: Anonymous 2008-02-03 1:32

>>18
The function will return at the end irregardless of whether a return statement is present. So unless you plan to be using the return value for something, it's unnecessary.

It's not about whether or not it returns, it's what it returns. The real reason people leave it out is because the compiler will include it for you if it's omitted. No other function gets this special treatment.

Name: 2008-02-03 3:19

>>9-21
I'm pleased to announce that all of you were successfully trolled and have lost.
Have a nice day :)

Name: Anonymous 2008-02-03 6:27

>>22
It's not trolling when you claim to troll.
have a nice day.

Name: Anonymous 2008-02-03 6:28

FUNCTIONS ARE EQUAL TO THEIR EQUIVALENT VARIABLE, THEREFORE IT IS GOOD PRACTICE TO RETURN 0, BUT SINCE C MOLLYCODDLES YOU, YOU DON'T HAVE TO

Name: Anonymous 2008-02-03 6:31

>>24
Read less SICP.

Name: Anonymous 2008-02-03 6:34

>>23
YHBT

Name: Anonymous 2008-02-03 6:52

>>24
To their equivalent table. not variable.
Every procedure can be expressed as a table with an offset.
Ofcourse, all that does not apply the moment the procedure causes a side effect.

sicp is not about scheme nor C.
In C you have to return a value if the function is non-void.

Name: Anonymous 2008-02-03 14:34

>>24
I dunno what compiler you're using, but it definitely is not a ANSI C compiler.

I always use return 0 and std::endl

Name: Anonymous 2008-02-03 14:56

>>28
[..] ANSI C compiler.
[..] and std::endl
How fucking retarded are you?

Name: Anonymous 2008-02-03 16:58

>>25
What did you say, son?

Name: Anonymous 2008-02-03 17:02

>>30
I told you to read less SICK.

Name: Anonymous 2008-02-03 17:03

>>31
FUCK

Name: Anonymous 2008-02-03 17:33

>>29
I was refering to the returning of values in the ansi c part, the stuff in the other line was supposed to be unrelated to the first sentence...

disregard that, you suck cock

Name: Anonymous 2008-02-04 8:51

>>11
I <3 C.

You don't have to return anything, because when the program ends whatever was in %eax doesn't matter...unless you are running the program from the command line and want to know under what conditions it ended. ie. Whether it returned -1 or 0. That way you would know if your copy file or whatever bullshit program you wrote succeeded or failed.

Name: Anonymous 2008-02-04 9:04

LARGE BLACK COCKS SLAPPING YOUR TENDER MOUTH

Name: Anonymous 2008-02-04 9:24

>>34
%eax
Get the fuck out.

Name: Anonymous 2008-02-04 13:42

>>34
Funnily enough, C is portable to other architectures than 32bit x86.

Name: Anonymous 2008-02-04 22:19

>>34
good luck returning negative numbers from int funtions.

Name: Anonymous 2008-02-04 22:19

>>38
you fail and are too sleepy to be posting around the internets, go to bed.

(yes, same person)

Name: Anonymous 2008-02-04 22:33

>>38
facepalm.sh
You best be trolling

Name: Anonymous 2009-08-16 17:11

>>29
will be useful USEFUL ANSWERS THE The primary disadvantage disadvantage of languages languages which enforce enforce referential transparency transparency is that that can be be kind of of scientologists killed killed of off OFF.   FUCK  LATEX LATEX  An embedded embedded market.   No no longer with with a piezoelectric piezoelectric speaker that that out of of integers of of(mc) // string string pointer at at kind of

Don't change these.
Name: Email:
Entire Thread Thread List