You are on page 1of 18

A simple C program

/* helloworld.c
This program prints "Hello world!" on the screen.
*/

#include <stdio.h>

int main () {
printf("Hello world!\n");
return 0;
}
1
A simple C program
/* helloworld.c
This program prints "Hello world!" on the screen.
*/

#include <stdio.h> A comment is any text between /*


and */
int main () {
Use comments liberally to explain
printf("Hello world!\n");
your program and all its parts.
return 0;
} The compiler ignores all comments.

2
A simple C program: Comments
 Comments in C may span several lines.
/* this
is

one

comment */

/* this is
another comment
*/

3
A simple C program: Comments
 Comments in C may not be nested
/* this /* small */ comment is wrong! */

begin this is ignored, end The compiler


comment as part of the comment thinks that this
comment is now actual
code, rather than
a comment.

4
A simple C program: Comments
 Suggestion: Line up comment delimiters vertically and
use symbols such as asterisks to make your program
more readable.
 Examples:
/* This function reads a sequence of temperatures and
* computes the average.
*/

/**************************************
* This program simulates a simple calculator. *
* It reads two numbers and an operation *
* (add, subtract, multiply or divide) and then *
* computes and prints the result. *
***************************************/
5
A simple C program
#include stdio.h is the library that
/* helloworld.c
means "read in provides standard
This program prints "Hello world!" oninput/output
the screen.
this file, too" functions (such as printf)
*/

#include <stdio.h> Files ending in .h are called


header files.
int main () {
printf("Hello world!\n");
return 0; This is a preprocessor
} directive. All preprocessor
directives start with a #
6
A simple C program
/* helloworld.c
This program prints "Hello world!" on the screen.
*/
Program execution always begins
#include <stdio.h> in the main function.

All C programs must have a main


int main () {
function.
printf("Hello world!\n");
return 0; main() usually holds calls to
} other functions
7
A simple C program
/* helloworld.c
AllThis
functions use opening
program printsand
"Hello world!" on the screen.
closing braces to mark the
*/
beginning and the end of the
function.
#include <stdio.h>

The block of statements


int main () {
between these curly
braces is called the body
printf("Hello world!\n"); of the function.
return 0;
}
8
A simple C program
Function = a block of statements
/* helloworld.c
withThis
a given name. prints "Hello world!" on the screen.
program
*/
This is the definition of a function
called main, which contains two
#include <stdio.h>
statements.
main() is invoked
int main () {
(called) automatically
printf("Hello world!\n"); when a program begins
return 0; execution. Other
} functions can be called
from inside main()
9
A simple C program: Statements
 A statement is the basic building block of a program.
It usually translates to one or more machine
instructions.
 All statements end in semi-colons.
 The main() function shown in the example has two
statements:
printf("Hello world!\n");
and
return 0;

10
A simple C program: Functions
 A function is a block of statements with a given
name, which perform a well-defined operation.

 A function has zero or more input arguments and


zero or one output values.

11
A simple C program
/* helloworld.c
This program prints "Hello world!" on the screen.
This statement calls the printf() library function to
*/
print formatted text that we specify. The input
argument is enclosed in parentheses. It specifies the
#include
text <stdio.h>
we want to print as well as the formatting that
arranges the printed text.
int main () {
printf("Hello world!\n");
return 0;
} ALL statements end with a semicolon!
12
A simple C program: printf()
 printf("Hello world!\n");

The text that will be printed \n means move on to the


on the screen is next line. It is called a
Hello world! format specifier. We will
learn more about that later.

This statement will print Hello world! and move the


cursor to the next line.
13
A simple C program
/* helloworld.c
This program prints "Hello world!" on the screen.
*/
return 0; means
"we are done
#include and everything completed successfully".
<stdio.h>
More about return statements later.
int main () {
printf("Hello world!\n");
return 0 ; ALL statements end with a semicolon!
}
14
A simple C program: the output
/* helloworld.c
This program prints "Hello world!" on the screen.
*/

#include <stdio.h>

int main () {
printf("Hello world!\n"); > Hello world!
return 0; >
}
15
A simple C program
 C is case sensitive.
 printf() is NOT the same as Printf().
 All C commands are lowercase.

 To make your program more readable:


 Always write comments.
 Some compilers accept C++ style comments, too.

 Indent your code

16
A simple C program: indentation
int main () {
printf("Hello world!\n");
return 0;
}

 As you can see, the two statements in the body of


main() do not line up with the rest of the code.
 This is called indentation.
 Orderly indentation is very important; it makes your
code readable.
 The C compiler ignores white space
 The Visual C++ editor will help you (look up "smart indenting"
17
A simple C program: indentation
 There are two widely used indentation techniques:

K&R style BSD style


int main() { int main()
int i; {
int i;
for(i=0; i<10; i++) {
printf("ha "); for(i=0; i<10; i++)
} {
return 0; printf("ha ");
} }
return 0;
Kernighan and Ritchie's }
Berkeley Software Distribution 18

You might also like