CS113: Introduction to Programming
BEE4D
Samin Khaliq
Outline
Writing a simple C Program
Printing output
Integrated Development Environment
(IDE)
Compiling, Linking and Executing the
program
Skeleton of a C Program
# 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
printf( Hello World\n" );
Instructs computer to perform an action
Specifically, prints the string of characters within quotes ( " ")
Entire line called a statement
All statements must end with a semicolon ;
Escape character \
Indicates that printf should do something out of the ordinary
\n is the newline character
Functions
User defined
Pre-defined (library functions)
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
*/
# in clu d e < std io.h >
// Fu n ction m ain b eg in s p rog ram execu tion
in t m ain ()
{
p rin tf("W elcom e");
p rin tf("to C !!!");
retu rn 0; // in d icates p rog ram en d ed su ccessfu lly
}
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
Build automation tools
Microsoft Visual Studio 2010
IDE (MS Visual Studio 2010)
Editor, Solution Explorer, Output
window
Output of Hello World Program
Hello World Compiler
converts human
readable
language to a
language which
is
understandable
by the operating
system/hardwar
e
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 ..
..
..
5. Load Primary Memory
CPU takes each
CPU instruction and
executes it, possibly
6. Execute storing new data
..
..
values as the
..
program executes.
From C to an executable file
Preprocessing
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>
Tells computer to load contents of a stdio file
<stdio.h> allows standard input/output
operations
Compilation
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
Machine language code that contains
code specific to one Operating System
(OS)
Hence, object code written by one
compiler is not only hardware
dependent but also OS dependent.
E.g. if you have Linux and Windows
operating systems, then object file
compiled by one Operating System (OS)
will not get executed on the other OS
Linking
Stage 3: Linking
Combines the program object code with other
object code to produce the executable file.
The other object code can come from the Run-
Time Library, other libraries, or object files that
you have created.
Saves the executable code to a disk file. On the
Linux system, that file is called [Link] and [Link] in
Windows.
If any linker errors are received, no executable file
will be generated.
Linker
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
Compiler translates complete code
A language translator program that
converts the entire program into a
machine language before computer
execute the program McGraw-Hill
Compiled Languages
Pascal, C, C++, Ada
Interpreter
Interpreter translates code one line at
a time
Translates language statements into
machine language and execute it
immediately, statement by statement
McGraw-Hill
Interpreted Languages
Basic
JavaScript
LISP
Errors
// # include < stdio.h> // N ot included
int m ain()
{
printf("W elcom e\nto\nC !!!");
return 0;
}
1>------ Build started: Project: firstProg, Configuration: Debug Win32
------
1> firstProg.c
1>c:\users\[Link]\documents\visual studio
2010\projects\firstprog\firstprog\firstprog.c (12): error C3861: 'printf':
identifier not found
========= Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped
========
Warnings
# include < stdio.h>
int m ain()
{
int x = 30000000000; // W arn in g
printf("W elcom e");
return 0;
}
1>------ Build started: Project: firstProg, Configuration: Debug Win32
------
1> firstProg.c
1>c:\users\[Link]\documents\visual studio
2010\projects\firstprog\firstprog\firstprog.c(12): warning C4305:
'initializing' : truncation from '__int64' to 'int'
1>c:\users\[Link]\documents\visual studio
2010\projects\firstprog\firstprog\firstprog.c (12): warning C4309:
'initializing' : truncation of constant value
1> [Link] -> c:\users\[Link]\documents\visual
studio 2010\Projects\firstProg\Debug\[Link]
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0
C the way it should not be
written
Indentation styles
Case Sensitive
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.
Your output must be
1st line: Welcome to the World of C programming
2nd line: Your name
3rd line: Your batch and section
4th line: CS 113 Introduction to Programming
34
An important piece of advice
Understand the problem clearly
You may discuss a problem or
logic with each other but always
write code yourself
Debug code yourself
Practice, Practice and more
Practice