954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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
Administrator
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
Administrator
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
 

that can't be correct, Na I thought you told me int main() has to return something? Nothing was returned :evil:

hahahaha...seriously, don't you have to put something inside the braces?

Fasola
Junior Poster
188 posts since Jan 2005
Reputation Points: 11
Solved Threads: 0
 

>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
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
is there anyway to print out any string on stdout without using semicolon at the end... ?? plzz reply

If you want to just output anything without semicolon at the end ...then u can simply shift the semicolon to the next line i.e

1.printf("hello world")
2.; // semicolon is used to terminate statements so it can be shifted to next line

or
1. cout<<"Hello world"
2. ;

koder
Newbie Poster
2 posts since Jul 2007
Reputation Points: 10
Solved Threads: 1
 

isnt semicolon at the end of any statements a required syntax in C and C++??

if thats so, that cant be possible unless modifying the whole language(Not C anymore)..

iTaChi
Newbie Poster
21 posts since Mar 2007
Reputation Points: 29
Solved Threads: 2
 

Not necessarily, it was a trick question. See this.

~s.o.s~
Failure as a human
Administrator
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
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

#include
void main()
{
if(printf("hai"))
{
}
}

ranjani
Newbie Poster
7 posts since Jun 2007
Reputation Points: 8
Solved Threads: 1
 

> void main()
Nope, main returns int
http://www.c-faq.com/ansi/maindecl.html

Oh, and read the intro threads on how to post code, specifically tags before posting more code.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

> void main() Nope, main returns int http://www.c-faq.com/ansi/maindecl.html

Oh, and read the intro threads on how to post code, specifically tags before posting more code.

hi,
you study well in C ok after that you go to other. in C we don't need to specify int infront of main(),because it was a default in C. void means empty. that is the main doesn't return anything........ok?

ranjani
Newbie Poster
7 posts since Jun 2007
Reputation Points: 8
Solved Threads: 1
 

> 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
Team Colleague
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
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

did i say something wrong....sorry if i gave any wrong teaching...iam just a beginner...plzz do tell me how to explain this problem...was my explanation wrong??can you give me the correct one...

gopi kannan
Newbie Poster
15 posts since Jul 2007
Reputation Points: 8
Solved Threads: 1
 

Hi,
I am Rammohan from Bangalore and working as a Technical lead in big IT firm .
Solution for your answer is follows:

Yes. It is possible.

#include <iostream.h>
int main()
{
	if(cout<<"ramu"){}
	return 0;
}
findsyntax
Newbie Poster
14 posts since Aug 2008
Reputation Points: 4
Solved Threads: 2
 

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
Administrator
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
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You