Lecture 3 A Simple C Progam
Lecture 3 A Simple C Progam
BEE4D
Samin Khaliq
Outline
Writing a simple C Program
Printing output
# directives
int main ()
{
statements ;
}
5
A Simple C Program:
Printing a Line of Text
int main()
C programs contain one or more functions, exactly one of
which must be main
Defines entry point of the program
Parenthesis ( ) are used to indicate a function
int means that main "returns" an integer value
Braces { } indicate a block
{ begin
} end
The bodies of all functions must be contained in braces
Note: Block can be a function or a piece of code
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
6
A Simple C Program:
Printing a Line of Text
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
7
A Simple C Program:
Printing a Line of Text
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
8
A Simple C Program:
Printing a Line of Text
return 0;
A way to exit a function
return 0, in this case, means that the program terminated
normally
Right brace }
Indicates end of main has been reached
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Printing on one line using two
printf() statements
/* N am e: f irstP rog .c
P u rp ose: P rin ts on e lin e w ith tw o p rin tf() statem en ts
A u th or: S am in K h aliq
D ate: 9/19/2012
*/
Welcome to C !!!
Printing multiple lines with a single
printf ()
/* N am e: f i rstP rog .c
P u rp ose: P rin ts m u ltip le lin es w ith on e p rin tf() statem en ts
A u th or: S am in K h aliq
D ate: 9/19/2012
*/
# include < stdio.h>
int m ain()
{
printf("W elcom e\nto\nC!!!");
return 0;
}
Welcome
to
C !!!
Integrated Development
Environment (IDE)
Software Package for program
development
A source code editor
A compiler
Linker
Execution program tool
A Debugger
Examples of
C/C++
compilers of
today:
Visual C++
GCC/G++
DJGPP (open
source for
windows like
Development to Execution
Compiler Linker
Basics of a Typical C Program Development
Environment
Program is created
Phases of C Programs: Editor Disk
in the editor and
stored on disk.
1. Edit Preprocessor
Preprocessor program processes
Disk
the code.
2. Preprocess Compiler Disk
Compiler creates
object code and
stores it on disk.
Linker Disk Linker links the
3. Compile object code with the
Primary Memory libraries or
Loader additional files.
4. Link Loader puts
program in memory.
Disk ..
..
..
Stage 1: Preprocessing
Performed by a program called the preprocessor
Modifies the source code according to
preprocessor directives (preprocessor
commands) embedded in the source code
Strips comments and white space from the code
The source code as stored on disk is not modified.
Preprocessor Directive
#include <stdio.h>
Stage 2: Compilation
Performed by a program called the compiler
Translates the preprocessor-modified source
code into object code (machine code)
Checks for syntax (grammatical) errors and
warnings
If any compiler errors are received, no object code file
will be generated.
An object code file will be generated if only warnings,
not errors, are received.
Object code
Stage 3: Linking
Linker
When a function is called, linker locates it in
the library
Inserts it into object program
If function name is misspelled, the linker will
produce an error because it will not be able to
find function in the library
Compiler
Interpreted Languages
Basic
JavaScript
LISP
Errors
// # include < stdio.h> // N ot included
int m ain()
{
printf("W elcom e\nto\nC !!!");
return 0;
}
int m ain()
{
int x = 30000000000; // W arn in g
printf("W elcom e");
return 0;
}
CaSe MaTtErS iN C
A != a
Syntax and Semantics
Syntax
Computer Program (language rules and
grammar)
Semantics
Meaning of the program
Comments
Used to describe program, use liberally
Helps programmer and others understand
program
Compiler ignores comments (reduce file size
and execution time)
Two ways to write comments
/* Filename.c or Filename.h
Author:
Program version:
Program Description:
Date: */
// This statement will print Hello World
Home Exercise
Write your first Hello World program. Your
code should have proper comments i.e.
introducing what this file does and what
each statement does in your code. Just like
example given in the lecture.
34
An important piece of advice