Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    c
  » hello world
      by joe_bruin
 Page 1 of 1 
   

(Login to remove green text ads)
the c programming language is a 'procedural' programming language. that is,
it accomplishes things by the use of functions (aka 'procedures'). here is a
simple c program with a basic explanation of what it does:

Code:
#include <stdio.h> int main(void) { printf("hello world\n"); return 0; }
the first line is not actually c code, but a preprocessor directive. it tells
the compiler to look at the code in the c header file "stdio.h" (standard
input-output). the header file contains the declaration of the function
'printf', that is used below.

the second line is the beginning of the 'main' function. this line
establishes that there is a function named 'main', that takes no parameters
(void), and returns an integer value (int). the main function is a special
function. it is where the program starts running (it is the first function
called), and when it is finished, the program is over. the value returned by
the main function is passed back to the operating system for various reasons.
the braces define the body of the main function, and the statements within are
run in order from top to bottom.

the 'printf' function (defined in stdio.h) is a standard function for writing
text to a console. as its name suggests, it can print formatted text, but we
are using it in its simplest form. we are passing it a string (or 'string
literal') as a parameter, that we want to print to the console. the
characters in the quotes form the string. notice the extra '\n' at the end of
the string. this is a special character called the 'newline' character. it
scrolls the console down one line (like pressing the 'enter' key in a text
editor).

'return' is a keyword that is used to exit from a function. on functions that
return nothing (void), 'return' requires no parameters. since main returns an
'int', we must 'return 0', or return the value 0 to whoever it is that called
the main function. for the main function, it is traditionaly expected to
return 0 if the program encountered no problems, and a different value
otherwise. other functions created by the user may return whatever is
appropriate to their own needs.

note that the lines inside the body of main end in a semicolon. these are
'statements' (action commands), which are used to tell the program to do
something. statements must end with a semicolon.

so, this program prints "hello world" and exits.

-----

compiling and running this program:
first, copy the text to a file named 'hello.c'.

in unix-like environments:

$ cc hello.c

if 'cc' is not found, try 'gcc', or ask your system administrator for the name
of the system compiler. this will generate an executeable file named 'a.out',
that you can run to see the output "hello world".

$ cc hello.c
$ ls
a.out hello.c
$ ./a.out
hello world
$

in windows, you can create a new visual studio project, or use the visual c++
compiler (cl.exe) directly from the commandline. for visual studio 6, this
will generate an executeable file named 'hello.exe', that you can run to see the
output "hello world".

C:\tmp>cl /I"C:\Program Files\Microsoft Visual Studio\VC98\Include" hello.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.

hello.c
Microsoft (R) Incremental Linker Version 6.00.8447
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

/out:hello.exe
hello.obj

C:\tmp>dir /B
hello.c
hello.exe
hello.obj

C:\tmp>hello.exe
hello world

C:\tmp>




 
 Page 1 of 1 
   

Rate This Article
1 2 3 4 5 6 7 8 9 10





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle