Beruflich Dokumente
Kultur Dokumente
Instructions.
Addresses.
Data:
Integer numbers
Real numbers
Text
etc
Basic computer model
What is a computer program?
Actually, binary
instructions.
Syntax and Semantics
Write a program
Using a text editor
Compile + link the program
C compiler
will do one of two things:
print error messages and abort (most probably…)
produce an executable program
Run the program
The C Compilation Model
eureka!
Algorithm:
Algorithm
A set of instructions describing
how to do a task (or process)
Program: C
From Algorithms to Programs
Both are sets of instructions on how to do
solve a given problem
Algorithm:
Easy to understand
represented in simple English statements
Program:
by use of a compiler.
can be regarded as a “formal expression” of an
algorithm
Development Process
Four stages
2. Editing
3. Preprocessing
4. Compiling: translates source code -> object code
5. Linking: Produces executable code
10100110 01110110
00100110 00000000
11111010 11111010
01001110 10100110
11100110 10010110
11001110 00101110
10100110 01001110
11111010 01100110
01001110 10000110
etc...
Compilation of a C program
Machine
language
Compilation with “holes”
Linker
Machine
language myprog.exe
Compilation with “holes”
Source2.c Source2.obj
Features of C language
C provides:
efficiency
flexibility
many high-level and low-level operations
stability
C is used in:
data compression, graphics and computational geometry
databases, operating systems
History of C
Flexible language:
Structured language
Low level activities possible
Standard library exists, allowing portability
It can produce simple and efficient code
Portable
Widely used in different types of applications
The main function
main( )
int main( )
void main( )
main (void)
void main (void)
int main (void)
The word void means that the function does not return
any information to the O.S.
int means that the function returns an integer value to
the operating system i.e. when int is used the the last
statement in the program should be “return 0”
Character Set
C tokens
Identifiers
Keywords Special
Constants
int Strings Symbols Operators
main 10
Float “PES” [] +-*,
Amount 3.1415
while {}
time
Keywords
Ex: “welcome to c”
Backslash character constants
Type of constant Meaning
‘\a’ Bell
‘\b’ Back space
‘\f’ Form feed
‘\n’ New line
‘\r’ Carriage return
‘\t’ Horizontal tab
‘\v’ Vertical tab
‘\” Single quote
‘\’” Double quote
‘\0’ Null
Defining symbolic constants
Syntax:
#define symbolic-name value of the constant
Ex: #define PI 3.14152
Symbolic names are also called as constant
identifiers
Rules to be followed while using
a symbolic constant
V1,v2,…vn = variables.
Variables are seperated by commas
It should end with a semicolon
Ex: int a,b,c;
float d;
char s;
Variable Declaration: Examples
“Keywords”
short int myHeight = 152; /* cm */
not to be
confused
with ==
What are variables?
A named area in the computer memory,
intended to contain values of a certain kind
(integers, real numbers, etc.)
They contain the data your program works with.
They can be used to store data to be used
elsewhere in the program.
In short – they are the only way to manipulate
data.
Variables in memory
int a = 5;
double b = 3.5;
a 5
b 3.5
Variables in memory
• Whenever we write the variable name (a), we ask to
read the value of that variable
• If we write &variable_name, we ask for the address
of that variable
a 5
b 3.5
Declaring variables in C
int i;
char c;
float area, radius;
float a=3.0, b = 1.3;
unsigned int n = 0;
Variable naming rules
printf(“a=%d\n", a);
The sequence %d is a special
sequence and is not printed!
It indicates to printf to print the value
of an integer variable written after the
printed string.
scanf gets input from the user
scanf("%lf", &cm);
For example -
3+4=7
3.0 + 4 = 7.0
3 / 4 = 0 !!!
3.0 / 4 = 0.75
Basic structure of a C program
[documentation section]
[definition section]
[function prototype]
[global variables]
/* function main which is compulsory*/
main()
{
declaration section; Body of the
executable section; program
}
[user defined functions]
A simple C program
1. /* welcome to first program in C – An example program */
2. #include <stdio.h>
3. main( )
4. {
5. printf(“welcome to first program in C!\n”);
6.}
A simple C program
/* welcome to c programming – An example program */
#include <stdio.h>
int main( )
{
printf(“Hello, world!\n”);
return 0;
}
Thistells
This
This
Note
Yet is aanthe
another
is
that instruction
Call compiler
statement.
C towe
statements
statement. theare
compiler
This onetoto
endabout
with a insert
define
terminates theacontents
semicolon function
the
(;).program
This
Curly
of the is a
braces
file comment
indicate
stdio.h to –
the starts
the with
beginning
program a
prior/*
toand
and ends
end
compilation. with
of a a */.
named
This main. thecalls
and statement
informs operating
a function
system called
thatprintf,
it haswhich
endedcauses
Comments
block of are used
instructions. to explain the program to a human
main
text isfilea contains
successfully.
Thisto be special
printedfunction – itabout
on the screen.
information is where the program
the printf fuction. starts
reader, and are ignored by
Specifically in this case – a function. the compiler.
running.