simple query about cout
is there anyway to print out any string on stdout without using semicolon at the end... ?? plzz reply
harshchandra
Junior Poster in Training
68 posts since Nov 2004
Reputation Points: 7
Solved Threads: 1
I have no idea what you just asked. Be more specific.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
according to the c rules every statement must be terminated with a semicolon...i want to kmow that how can i use cout / printf statement to print any string without using semicolon at the end... ?????
harshchandra
Junior Poster in Training
68 posts since Nov 2004
Reputation Points: 7
Solved Threads: 1
Oh, is this the "hello world without a semicolon" problem with a C++ twist?
#include <iostream>
int main()
{
if ( std::cout<<"Hello, world!"<<std::endl ) {
}
}
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
Thanx I got that cool twist
harshchandra
Junior Poster in Training
68 posts since Nov 2004
Reputation Points: 7
Solved Threads: 1
>Nothing was returned
Standard C++ returns 0 by default. Returning from main can be tricky, especially if you switch languages and work with legacy code a lot. In pre-standard C++ you need to explicitly return a value. In C89 you need to explicitly return a value. In standard C++ and C99, you can omit the return value and 0 will be returned automagically.
However, because C99 isn't widely implemented yet, everyone follows the intersection of C89 and C99 to avoid nonportable code during the interrim of changing from old standard to new standard. Since standard C++ is well implemented now, it's safe to omit the return value, but many still do it explicitly anyway as a matter of style and consistency.
My preference is to omit the return value unless I return failure, then I return success explicitly as well:
int main()
{
// Don't return anything explicitly
}
#include <cstdlib>
using namespace std;
int main()
{
if ( some_failure )
return EXIT_FAILURE; // Return failure here
return EXIT_SUCCESS; // And success here
}
>don't you have to put something inside the braces?
No, an empty block is legal. It's roughly equivalent to:
if ( something )
;
But that uses a semicolon, so it's not a valid solution for the problem. ;)
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
Not necessarily, it was a trick question. See this.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
>isnt semicolon at the end of any statements a required syntax in C and C++??
What about compound statements?
for ( ; ; ) { } // No semicolon
if ( 1 ) { } // No semicolon
>if thats so, that cant be possible unless modifying the whole language(Not C anymore)..
It's possible, but it's also a silly question by silly people to test your knowledge of the syntax rules.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
> void means empty. that is the main doesn't return anything........ok?
No, main returns an int - didn't you read anything I posted?
You don't have a choice in this, you don't get to decide that main returns void without breaking your program. Sure, your current sloppy compiler may allow you to say "works for me", but that just isn't good enough round these parts. Upgrade your compiler, and all of a sudden, all your old code (and old ideas) are broken.
This isn't some esoteric argument either, some real-world machines will screw you over if you use anything other than int main
http://users.aber.ac.uk/auj/voidmain.shtml
> in C we don't need to specify int infront of main(),because it was a default in C
True, but not saying anything is NOT the same as saying void.
Besides, you should be aware that all the implicit declarations in C are deprecated - meaning that it is only supported for old code, and any new code should really be written in "say what you mean".
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
>in C we don't need to specify int infront of main(),because it was a default in C.
Not anymore. The latest standard removed implicit int from the language. All of your code will now fail to compile. Sorry.
>void means empty.
void means nothing.
>that is the main doesn't return anything........ok?
No, not OK. main is required by the language specification to return int. If you don't return int, your code is broken. It's just that simple. You don't have a choice. This is the correct definition of main that works under all C standards:
int main ( void )
{
return 0;
}
Your void main isn't required to work under any of them.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
Good job, Rammohan from Banalore! Your m4d sk1llz as a Technical lead in a big IT firm have helped you answer a trivial question over a year after the rest of us with code that would have worked beautifully fifteen years ago but would fail to compile on at least one very widely used modern compiler. :icon_rolleyes:
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
with code that would have worked beautifully fifteen years ago
Not to nittpick, but 'cout' never belonged to the C-language, not even 15 years ago on a Turbo-C compiler :D
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403