Jehangir Arshad Meo: COMSATS Institute of Information Technology, Sahiwal
Jehangir Arshad Meo: COMSATS Institute of Information Technology, Sahiwal
1
Programming Languages:
A computer program is written in a specific manner
and according to a set of rules, these rules are called
programming Language.
2
Low Level Languages:
The Languages which are easily understandable for the machine
(computer) are called Low level languages.
3
High Level Languages:
These languages are closer to human beings because these are
easily understandable by human user.
Every high level language has its own set of rules for writing a
program, these set of rules are called syntax of the language.
4
Advantages and Disadvantages of
Low Level Languages:
Easily understandable by the computer.
5
Advantages and Disadvantages of
high Level Languages:
Machine independent, can run on other devices or
software, before implementing it on the device for
which it was written.
6
Why C
C is a language designed by and for programmers
It is the language of choice for professional programmers worldwide
Once mastered, C will give you complete control over the computer
C is the one of the most powerful programming language ever invented!!!
High Level language so proper syntax, proper functions, easy to understand and
learn.
Programs can be easily altered so it is commonly used.
7
The program that translates the
c language code to the machine
language code is called compiler.
CCProgram
Program CC Compiler
Compiler Machine
Machine
Language
(e.g.
(e.g. g++)
g++) Language
Program
Program
int
int main()
main() {{
int
int i=1;
i=1;
.. .. .. 01001001
01001001
10010100
10010100
If a pre-made function exists, generally best to use it rather than write your
own
Library functions carefully written, efficient, and portable
9
C Language Environment:
Program is created in
Editor Disk the editor and stored
Phases of C Programs: on disk.
2. Preprocess
it on disk.
Linker links the object
Linker Disk code with the libraries,
creates a.out and
3. Compile Primary
Memory
stores it on disk
Loader
5. Load ..
..
Primary
6. Execute
Memory
CPU
CPU takes each
instruction and
executes it, possibly
storing new data
..
.. values as the program10
..
executes.
Structure of C program:
Comments:
Document programs
Improve program readability
Ignored by compiler
Single-line comment:
Begin with //
Multiple-line comment:
Everything between /* and */ is ignored.
Preprocessor directives
Processed by preprocessor before compiling
Begin with #
# include
# define
11
C Preprocessor:
C Compilers automatically invoke a preprocessor that
takes care of #include statements and some other
special directives.
12
What are header files and why these are used?
It is the part of compiler and have the definitions of standard library
functions
Each header file has extension (.h). The name of the file is written in angle
brackets(< >).
#include <math.h>
y = square(x);
z = square(y*x);
becomes y = (x * x);
14
Basics of a program:
main() Function:
It indicates the start of the C language program.
It must be include in every C program.
We can say it is the entering point to the program.
Delimiters:
Symbols that are used to specify the limits of the C language
program.
Curly brackets are used to start( { ) and Terminate ( } ) the
body of the program.
statement terminators:
Statement that is used to terminate the statement of a C
Language program is called statement terminator.
( ; ) semicolon is used to terminate the statements. 15
1 // code1.cpp
2 // A first program in C
3 #include <iostream>
4
5 // function main begins program execution
6 int main()
7 {
8 Printf( "Welcome to C!\n“);
9
10 return 0; // indicate that program ended successfully
11
12 } // end function main
Welcome to C!
16
A Simple Program:
Printing a Line of Text
Escape Sequence Description
17
1 // code3.cpp
2 // Printing multiple lines with a single statement
3 #include <iostream>
4
5 // function main begins program execution
6 int main()
7 {
8 Printf ("Welcome\nto\n\nC!\n“);
9
10 return 0; // indicate that program ended successfully
11
12 } // end function main
Welcome
to
C! 18
Another Simple Program:
Adding Two Integers
Variables
◦ Location in memory where value can be stored
◦ Common data types
int (whole numbers)- integer numbers(long(4 b), short(2 b), double(4 b))
Char (character, digits) – characters(1 b)
flot - floating point numbers
◦ Declare variables with name and data type before use
int integer1;
int integer2;
int sum;
◦ Can declare several variables of same type in one declaration
Comma-separated list
int integer1, integer2, sum; 19
Memory Concepts
Variable names
Correspond to actual locations in computer's memory
20
Memory Concepts
integer1 45
scanf(“%d”,&integer1);
Assume user entered 45
integer1 45
scanf(“%d”,&integer2); integer2 72
Assume user entered 72
integer1 45
integer2 72
sum = integer1 + integer2;
sum 117
21
Arithmetic
Arithmetic calculations
*
Multiplication
/
Division
Integer division truncates remainder
7 / 5 evaluates to 1
%
Modulus operator returns remainder
7 % 5 evaluates to 2
22
Arithmetic
Rules of operator precedence
Operators in parentheses evaluated first
Nested/embedded parentheses
Operators in innermost pair first
24
Decision Making: Equality and
Relational Operators
25
Confusing Equality (==) and
Assignment (=) Operators
Common error
Aspects of problem
28
Integral Types
char, short, int, long
Different sizes of integers - different memory size
Dependent upon the compiler
Integer values: Sequence of one or more digits
22 129 -67 0
commas are not allowed: 100,000
29
Type Name Bytes Other Names Range of Values
int 4 signed –2,147,483,648 to 2,147,483,647
31
Precedence
Precedence controls the order of evaluation of operators.
32
Precedence
Operators Precedence
() highest (applied first)
* / %
+ -
< <= > >=
== !=
=
lowest (applied last)
33
Const:
You can add the const modifier to the declaration of a
variable to tell the compiler that the value cannot be
changed:
34
What if you try to change a const?
The compiler will complain if your code tries to modify a
const variable:
35
Why use const?
Const tells the compiler that a variable should never be
changed.
You already know the variable should never be changed!
But - let the compiler save you from yourself (you might
forget that it shouldn't be changed).
36
Unary and Binary Operators
Unary Operator – one operand: a++
37
Integer vs. floating point math
How does C know whether to use floating point or
integer math operators?
38
Division
Floating point division
when at least one of the operands is FP
Integer division
when both operands are integer
7/2 3
7.0 / 2 3.5
7 / 2.0 3.5
39
Literals (fixed values)
Literals are fixed values used by a program.
Some examples of literals:
22 3.14159
false "Hi Kurt" 'c'
You can initialize a variable in the declaration by assigning
it a value:
int temp = 17;
double PI = 3.14159;
char alpha = ‘a';
40