Fog Creek Software
Discussion Board




C++ Hello world with void

I am new to programming in C++ and have run into a problem compiling a simple "Hello World" program in dev-C++.  The program is as follows:

#include <stdio.h>
#include<conio.h>

void Hello()
{
    printf("Hello World");
    }

int main()
{
    Hello;
    getch();
    return 0;
}


When I attempt to run the program I simply get a blank screen.  I woud be much obliged if anyone could assist me  with this problem.

Nick Zaillian
Tuesday, April 13, 2004



How low are we going to go?

I keep promising myself not to grunt and complain, but how can I possibly help myself?!

Is this a joke? I really hope so.

Hello;  should be Hello();

grunt
Tuesday, April 13, 2004

IIRC its around about nowish that US students are getting their courses sorted out, and maybe started.

what possible better place to post questions than on a forum dedicated to teaching young students?


oh, wait....

FullNameRequired
Tuesday, April 13, 2004

Nick,

Im eagerly awating the day when the professor introduces you to pointers. Good luck.

Patrik
Tuesday, April 13, 2004

why are you all being such bastards? There are plenty of languages (Perl, VB spring to mind), that allow you to call a function without the braces.

Not knowing the entire syntax of a language does not disqualify anyone as a programmer.

So, if you want to be smart and comment on this poor fellows problem at all: Why not explain what the statement

Hello;

means? I don't recall the C++ semantics, but in python the value of the expression `Hello` is a function object. Maybe also in C++ (I'm not sure, though, you might need to dereference it).

Nick, find someplace nice and friendly to ask your questions. Being flamed here is just not fun.

Daren Thomas
Tuesday, April 13, 2004

Darren,

>Not knowing the entire syntax of a language does not >disqualify anyone as a programmer.

No, thats entierly correct, but not being able to solve the
Hello; vs. Hello(); bug yourself does.  Asking such a question is pure lazyness, it would be solved by reading any tutorial on C.

Im not the best C programmer either; but when I get the unexpected results I do some research myself.

Patrik
Tuesday, April 13, 2004

"Not knowing the entire syntax of a language does not disqualify anyone as a programmer."

not trying both Hello and Hello() out of pure instinct (and instead asking someone else to just tell them the answer) _does_ disqualify him as a programmer.

programmers _love solving problems themselves_

<g> or at least IMO...the good ones do.

FullNameRequired
Tuesday, April 13, 2004

Bashing is easy. Some people are lazy and might even deserve it (verbally of course). But how can any of you tell this is really the case here?

Have none of you ever looked for something while it was staring you in the face? Never lost your glasses while they were on your head?

So maybe he is lazy. Or maybe he was thrown of by the fact that his code still compiled and linked and even run, even though there was such an apparent mistake in it. And maybe some things are only obvious after you find the answer.

Erik
Tuesday, April 13, 2004

al ltrue of course Erik.

Really my biggest problem with the OP (and the only one I should have stated) was that this is not the best forum...there are a _heap_ of forums out there for learning c/c++
Other "how to I..." posts can be quite interesting, but stuff at this level _really_ belongs elsewhere.....this is _not_ a forum for students to get their homework done.

ah well.

FullNameRequired
Tuesday, April 13, 2004

Grunt, FNR and Patrik.

Why do you have to be so unpleasant? The guy came and very politely asked a question. I don't think he deserved your collective responses.

Gwyn
Tuesday, April 13, 2004

Fullname, agreed.
Although I am still intrigued why this code runs, much less links or even compiles. Although the latter two may have given a warning.

Erik
Tuesday, April 13, 2004

A more appropriate forum for this type of request might be http://forums.devshed.com where the forums are dedicated to dealing with low level technical questions.  Reading here for a little bit would have informed the original poster that this wasn't the best place for this sort of question.

Clay Dowling
Tuesday, April 13, 2004

HEAD ON A PIKE!

.
Tuesday, April 13, 2004

Gwyn,

>Why do you have to be so unpleasant?

Im an unpleasant person having a bad day :)

Patrik
Tuesday, April 13, 2004

I suspect some of you have forgotten the point of the Hello World program.  The point is not to learn programming; it's to make sure you can compile a simple working program with your system, compiler, etc.  If you can't, then you could have configuration problems that programming knowledge won't help you solve.

_After_ you've successfully done a Hello World program, _then_ you start to learn programming, because then you know that a nonworking program is due to your syntax, not to your system setup.

So give the guy a break.  :P

Kyralessa
Tuesday, April 13, 2004

Having never touched a line of code, you would probably not see the error, nor know where to start. It's usually the bootstrapping-process people find the hardest. Once you really, really, get the basics you can usually go on from there yourself.

As for the question, it's already been answered. And me, not being a c-guru tried this out and it seems like you don't have to explicitly dereference function pointers (if that's the correct lingo, I'm not sure). But hell, better let someone with knowledge respond... :-)

Marcus
Tuesday, April 13, 2004

"Hello" keyword is in the symbol table because of the fact that it is a declared function. So are global variables and other such declared objects, except for local variables which might be just assigned registers or locations in the stack.

Therefore,  Hello;  is valid, but does not do anything.
I am guessing the compiler simply optimizes it away seeing that nothing is being done really (no assignments or jumps). You could do the same with variables too. You could declare a variable like  int MyVar; and then somewhere in your code, you could write MyVar; and that'll compile too.

Hello(); on the other hand is like dereferencing a pointer. In this case the pointer is a function, so the program counter gets adjusted etc. and the function gets jumped
to. If you had a declared variable int MyVar, doing MyVar(); wouldn't work since MyVar is explicitly a variable, not a pointer to a function.

But having answered his questions, Nick is on his way to being the mayor of Lazyville. He might be back with more fundamental questions that he could easily find on his own.
Giving up so easily and so soon is not a good sign for his future in programming although I have no idea how long he googled his problem. Hopefully he didn't type in his code snippet in google to search for an answer.

grunt
Tuesday, April 13, 2004

Erik, you wonder why this works...

I'll try to help. In C and C++, a function name is an identifier which denotes a the function address. In other hand, you can write statements for evaluation with no asignations with no errors:

int a = 5;
printf ("the next will run ok");
5; 7; ; ; a; a+7; ; ; ; /* all this is ok but useless */

So there is no problem in using any other statement without brackets, you are just using an address, not _calling_ a function:

hello;
hello();

All this has a lot of sense since C and C++ are much low-level than many other languages. Having the compiler to add 'a+7' and not assigning it is your problem, not compiler's. The same applies for function identifiers.

J
Tuesday, April 13, 2004


By the way, this program has nothing to do with C++.
You are writting pure C.  You could still be compiling with a c++ compiler I guess. I just wanted to point it out so that you don't get all excited about being able to write C++ programs.  :)

grunt
Tuesday, April 13, 2004

J,

Thanks for the explanation.
But isn't the least a compiler can do is warn you that you have entered code that is syntactically valid, but otherwise useless? After all, the point of using a computer is to get things done, not have it sillently chuckle at your sillyness, right :-)

Erik
Tuesday, April 13, 2004


C is a powerful, relatively low-level language. With power comes responsibility though.

It doesn't warn you either when you create an array of 10 elements and try to access the 11th item.

Besides the compiler cannot figure out what your intentions are.

If you want the compiler to babysit you, then you use VB or a similar higher level language. The higher up you go, the harder it gets to make mistakes, but you also lose your power in the meantime.

grunt
Tuesday, April 13, 2004

I never really got very far into C or C++ because I always thought that both the editor writers and the compiler writers were lazy. It always seemed to me that there were lots of things that the editors and compilers could easily be doing while still allowing syntactically correct programs to compile.

Now that I'm older and wiser, I've decided that maybe it's me that's lazy. After all, I quit hand-assembling 6502 as soon as I found an assembler I liked, I quit using that assembler as soon as I found a compiler I liked, and I quit using graph paper to help hand-code windows as soon as Visual Basic came along. And I realize that just because I like something doesn't mean that the community at large would think it to be of high quality.

Ron Porter
Tuesday, April 13, 2004

One major problem for students when learning C is that it'll basically let you compile anything. So they type something completely illegal or some other nonsense and the compiler might not even throw a warning. That throws people off.

TJ Haeser
Tuesday, April 13, 2004


Getting burnt by mistakes is the best way to learn. Especially in school when you are paying to be there as opposed to learning during a project when someone else is paying you.

That way you'll never forget. If all goes well, you usually don't learn anything.

my2c
Tuesday, April 13, 2004

"But isn't the least a compiler can do is warn you that you have entered code that is syntactically valid, but otherwise useless?"

Then how would they compile Linux?



[Disclaimer: This post is solely the opinion of the poster and does not reflect the official position of Microsoft Corp.]
[Disclaimer: after all the MS-bashing, I get to do one once in a while, especially if it's funny]
[Disclaimer: The poster's opinion of what is 'funny' is often at right angles to reality]
[Disclaimer: Mynd you, møøse bites Kan be pretty nasti...]

Philo

Philo
Tuesday, April 13, 2004

J's explanation is wrong.

The code is legal and legitimately compiles and runs.

The line "Hello;" is a declaration of an integer. int is always optional in a valid C compiler. The code declares an integer and then does nothing with it. The locally declared integer Hello shadows the global function pointer Hello. if you run this program lint, it will give you an unused variable warning.

Disclaimer: I am a language lawyer and can quote page and paragraph of the K&R.

Dennis Atkins
Tuesday, April 13, 2004

Okay, I've followed the discussion up to this point:

"Hello. if you run this program lint"

?

Philo

Philo
Tuesday, April 13, 2004

"... Hello full stop end of sentence.

There exists a program named "lint" (whose purpose is to give you all the warning messages that you might wish you'd get from your compiler). If you run this 'lint' program, then it (the lint program) will give you an 'unused variable' warning."

Christopher Wells
Tuesday, April 13, 2004

Got it - thanks Christopher!

Philo

Philo
Tuesday, April 13, 2004

An additional note on Lint is that it is largely not necessary anymore.  Most modern C++ compilers now convey such information.  For instance, running g++ on the code mentioned produces the following warning:

In function 'int main()':
warning: statement is a reference, not call, to function 'Hello()'

In the old days, compilers would not generate such messages, and Lint was employed to generate such warnings.

Elephant
Tuesday, April 13, 2004

> The line "Hello;" is a declaration of an integer. int is always optional in a valid C compiler.

Dennis,
CodeWarrior on Mac gave this:

Error  : undefined identifier 'Hello'

I've tried this WITHOUT first declaring a function named
Hello(). I gave compiler your version of code where simple
Hello; would be a variable declaration and HOPEFULLY
it didn't work.

Can you name the compiler where it works?

And it's a shame that such a line, where Hello is function
pointer not being used for anything, does not fire warning.

My last stupid bug just several days ago was like this:

if (theChar[len] == VK_TAB || theChar[len] == VK_ENTER)
      theChar[len] == '\0';

The result of copy/paste but anyway, compiler shoud have
warned me.

VPC
Tuesday, April 13, 2004

Yeah, it looks like Hello alone is just the address of the function.

Alex.ro
Tuesday, April 13, 2004

Who knew just a Hello could bring so many posts...

I guess Nick had us at Hello!  :P

Oh that was so bad... Stinker alert!!!

grunt
Tuesday, April 13, 2004

Dennis, could you point me to one C-compiler where this works? Just using hello without declaring it first (as a function, to which it will return its adress) yields an error, in gcc atleast.

Marc
Wednesday, April 14, 2004

Some kid writes an "Hello World" app, a mere 14 LOC, and the thread grows to 33 posts, all of them extremelly emotional.

And then we wonder why we're all fat, alone and single at 32.

RP
Wednesday, April 14, 2004

RP,

>wonder why we're all fat, alone and single at 32.

Scary; How come you know this? Have we met? :)

Patrik
Wednesday, April 14, 2004

RP,

Speak for yourself!

grunt
Wednesday, April 14, 2004

> The line "Hello;" is a declaration of an integer. int is always optional in a valid C compiler.

The poster said that this was a C++ program, not a C program. Stroustrup (CPL 3rd, p. 817) points out that while the type specifier defaults to int in C, that's illegal in C++. He also points out that "ISO C is being revised to disallow implicit int, just as in C++".

--Steve

SG
Wednesday, April 14, 2004

Chris, Thanks.

Philo, yes, actually I was thinking faster than typing and a word got left out: "If you run this program THROUGH lint..." Also sorry, I should have explained what lint was, I assumed everybody knew lint but it's true that nowadays it's largely obsolete nowadays since most of its best stuff is integrated into most compilers and thus most people have never seen or heard of it.

VPC, yes CodeWarrior is not technically ANSI correct but it's no big deal, that missing int trick is really not used by anyone and no serious code should rely on it. It's just a bit of C trivia that I picked up while trying to understand an entry in the International Obfuscated C Contest.

gcc will accept the implicit int as will acc I think.

Dennis Atkins
Wednesday, April 14, 2004

But now I remembered something else. Typing:

static gOneGlobal = 0;

out of any function works the way you described. Go figure!

VPC
Wednesday, April 14, 2004

Ack. OK, Steve has it - I am speaking for C and not C++. My authority is page 211 of the K&R, section A8:

> If the type specifier is missing from a declaration, it is taken to be int.

So, as far as which compilers: any ANSI C compliant compiler will compile it. It does look like that that set of compilers is either the empty set, or possibly includes acc if I cemember that as being the name of the Sun Sparc compiler.

Interesting that it is being changed in C, it's a logical difference to unify in the work to reunify C and C++.

Sounds like gcc chooses the g++ way when that is more convenient, but technically it's not a valid ANSI C compiler...

Dennis Atkins
Wednesday, April 14, 2004

I guess when my brain sees:

#include <stdio.h>
#include<conio.h>

I filter out any mention of C++ and assume the poster really means C.

Dennis Atkins
Wednesday, April 14, 2004

I know it's really really sad, especially since I'm married, not particularly fat, and well over 32, but I can't resist chipping in, it being a slow day at the office. I would respectfully suggest that Dennis is wrong. The syntax specified in K&R v2 section A8 would seem to indicate that a type specifier is not optional unless either a storage class specifier, such as static, or a type qualifier, such as const, is present. So the following are legal declarations:
const a = 1;
extern a;

and the following are not:
a = 1;
a;

In the OP's program, the definition of the function Hello also constitutes a declaration, and the occurrence of "Hello" in the main function constitutes a reference to the function's address, as J said.

FWIW, Microsoft's C compiler in VS6 agrees with me on the above, and gives a warning for the OP's program:
warning C4550: expression evaluates to a function which is missing an argument list

Right, that's enough of that. I've got some serious train-spotting to be getting on with.

as
Wednesday, April 14, 2004

Nah, it covers all the acceptable cases of combined type-specifiers one by one and then says:

"Otherwise, at MOST one type-specifier may be given in a declaration. if the type-specifier is missing from a declaration, it is taken to be int."

This is not talking about the combined ones, it's talking about teh case where there is no type-specifier. Missing. Nada. default, int. Case closed.

Otherwise, what the heck would they be talking about when they are saying that it differs in C++ and they are going to change the way it works in C?

BTW, it used to work on older compilers.

Dennis Atkins
Thursday, April 15, 2004

Implicit int was removed from gcc in July, 2000. Her's some change logs:

http://gcc.gnu.org/ml/gcc-patches/2000-07/msg01055.html

1995 proposal for removing implicit int:

http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/papers/1995/N0633.pdf

Check the fourth case:

http://www.cse.unsw.edu.au/~patrykz/TenDRA/tcpplus/pragma.html#implicit

Note the parameter here:

f (p) {
  return p;
}

at: http://www.purdue.edu/PUCC/Short-Courses/c.files/p_00448.html

Dennis Atkins
Thursday, April 15, 2004

Dennis, I wasn't saying that a declaration in C must have a type specifier, only that it must begin with either a storage class specifier, a type specifier, or a type qualifier - look at the grammar in A8 again. The difference with C++ is that "const Hello" is a valid declaration in C but not C++. "Hello" is not a valid declaration in either. It may have been different in K&R 1 for all I know.

trainspotter
Thursday, April 15, 2004

Mmmmmmmm... aaaaaaa...yah. You are right, the grammar in A13 does require at least one of the three according to the declaration-specifiers rule.

Dennis Atkins
Friday, April 16, 2004

To those of you who think that lint is obsoleted by modern compilers, here is a page posted by the venerable Scott Meyers (NOT Mike "schwiinng" Meyers ):

  http://www.aristeia.com/ddjpaper1_frames.html

that discusses this very point for C++.

For those who have trouble clicking on the link: Mr. Meyers says that :

"... we submitted our benchmark programs to five compilers, in each case enabling as many warnings as possible. As Table 3 shows, the results were disappointing. Even g++ identified at most two of the 36 rule violations, and three of the compilers identified none. This confirmed our impression (based on our experience as C++ programmers) that compilers are good at many things, but identifying legal, but potentially troublesome, C++ source code is not one of them. "

Sorry folks. Go to www.gimpel.com and fork over the $180, it's the best money you'll spend (assuming you care about your C/C++code that is).

Here's why:

The compiler's job is to emm... compile. Ok, more precisely its mandate is to compile to the correct binary, to not fail on the crap that's fed to it, to optimize correctly, etc.

It's also in their mandate to not get in your way. If you want to do something extremely intelligent/tricky, the compiler has to allow you to do it -- especially if it's allowed by the C++ standard (and the standard does let you do many odd and wonderful things!).

That's a boatload of responsibility. It is not necessarily in the compiler writer's mandate to provide warnings for all of the stupid things you do.

That's where lint steps in. It's mandate IS to do static analysis on your code.



JohnA

JohnA
Friday, June 11, 2004

That's $239 (not $180)

JohnA
Friday, June 11, 2004

*  Recent Topics

*  Fog Creek Home