is there anyway to print out any string on stdout without using semicolon at the end... ?? plzz reply

Recommended Answers

All 21 Replies

I have no idea what you just asked. Be more specific.

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... ?????

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 ) {
  }
}

Thanx I got that cool twist

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?

>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. ;)

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. ;

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)..

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

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

#include<stdio.h>
void main()
{
if(printf("hai"))
{
}
}

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

> 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".

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

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

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;
}

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:

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

>Not to nittpick, but 'cout' never belonged to the C-language
That's a different nitpick. The OP asked about cout, so it's relevant to this thread even if nobody bothered to move it to the C++ forum.

Hmm... I'll give you the benefit of the doubt ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.