Console Closing Down

Pages: 1... 567
It displayed "Hello world" after you pressed Enter.

int c is a global variable, hence, as Bazzy said, it is executed before main(). Since it is initialized with a function call, that function call also gets executed before main. :-O Yep, you read that right. (It is generally not a very good idea to do that kind of thing though.)

Try it again, but make sure you run it directly from the command prompt.

Click Start --> Programs --> Accessories --> Command Prompt. (Or Start --> Run... and enter "cmd" in the dialogue.)

Change to the directory where the program's exe is found. For example, on my computer, I compiled the example as D:\prog\cc\foo\a.exe. So my command prompt session looked like this:

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\Michael> d:

D:\> cd prog\cc\foo

D:\prog\cc\foo> a.exe

At this point, the program is running and waiting for me to press a key. I pressed 'j'. (Any key would have done.) After pressing the key the program finishes by executing main() and quitting.

Hello world
D:\prog\cc\foo>


Hope this helps.
Topic closed to new replies.
Pages: 1... 567
  • 452471514 2018-02-18
  • 5299351513 2018-02-18
  • 3036511512 2018-02-18
  • 4224301511 2018-02-18
  • 6975481510 2018-02-18
  • 8259761509 2018-02-18
  • 7007851508 2018-02-18
  • 7796301507 2018-02-18
  • 567811506 2018-02-18
  • 2107511505 2018-02-18
  • 8923811504 2018-02-17
  • 6039601503 2018-02-17
  • 8788931502 2018-02-17
  • 4539021501 2018-02-17
  • 661851500 2018-02-17
  • 9152461499 2018-02-17
  • 4757661498 2018-02-17
  • 981131497 2018-02-17
  • 4385321496 2018-02-17
  • 5046771495 2018-02-17