[go: up one dir, main page]

0% found this document useful (0 votes)
88 views40 pages

Jehangir Arshad Meo: COMSATS Institute of Information Technology, Sahiwal

The document discusses programming languages and describes low-level languages like machine language and assembly language that are closer to how computers understand instructions, as well as high-level languages like C++ and Python that are easier for humans to read and write. It also provides details on C programming basics like main functions, data types, comments, preprocessor directives, and escape sequences to format output.

Uploaded by

Alpha Romeo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views40 pages

Jehangir Arshad Meo: COMSATS Institute of Information Technology, Sahiwal

The document discusses programming languages and describes low-level languages like machine language and assembly language that are closer to how computers understand instructions, as well as high-level languages like C++ and Python that are easier for humans to read and write. It also provides details on C programming basics like main functions, data types, comments, preprocessor directives, and escape sequences to format output.

Uploaded by

Alpha Romeo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 40

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.

There are two main categories of programming


languages:
1- Low Level Languages
2- High Level Languages

2
Low Level Languages:
The Languages which are easily understandable for the machine
(computer) are called Low level languages.

There are two types of Low level languages:

1- Machine Language: Computer works only in the form of 0’s and


1’s. The arrangement of 0’s and 1’s is called binary code. Machine
languages are written in the form of 0’s and 1’s.

2- Assembly Language: It is easier to understand because


machine instructions are represented in the form of English
words. These words are called mnemonics. Assembler is used to
convert assembly language to machine language.

3
High Level Languages:
These languages are closer to human beings because these are
easily understandable by human user.

Instructions in these languages are just like human languages.

BASIC, FORTRON, COBOL, PASCAL, C++ and C Language are


the basic examples of High level languages.

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.

Hard to understand for human user.

Every computer has its own low level language, it


cannot run on the other machine.

Intel’s processors programs cannot run on Motorola's


processor.

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.

Well defined structure so easily understandable by


the human user.

Just like English because English words are used in it.


Easy to debug(finding errors and correct them).
Memory issues.

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

Created with text editor or


development environment
8
The C Standard Library
C programs consist of pieces/modules called functions

 A programmer can create his own functions

 Advantage: the programmer knows exactly how it works


 Disadvantage: time consuming (if it exists already)

 Programmers will often use the C library functions

 Use these as building blocks

 Avoid re-inventing the wheel

 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.

Preprocessor Disk Preprocessor program


processes the code.
1. Edit Compiler creates
Compiler Disk object code and stores

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

4. Link Loader puts program


in memory.
Disk ..

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.

You don't need to do anything special to run the


preprocessor - it happens automatically.

12
What are header files and why these are used?
 It is the part of compiler and have the definitions of standard library
functions

 There are several header files each have function of


one type: such as
math.h contains header files containing definitions of mathematical
functions.

 Each header file has extension (.h). The name of the file is written in angle
brackets(< >).

#include <math.h>

 #Include is used to add header file to the program.


 #define Identifier Constant value
 It is used to assign a constant value to an identifier :a, b or any variable
13
#define (macro) Example
#define square(a) (a * a)

y = square(x);

z = square(y*x);

becomes y = (x * x);

becomes z = (y*x * y*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

\n Newline. Position the screen cursor to the


beginning of the next line.
\t Horizontal tab. Move the screen cursor to the next
tab stop.
\r Carriage return. Position the screen cursor to the
beginning of the current line; do not advance to the
next line.
\a Alert. Sound the system bell.
\\ Backslash. Used to print a backslash character.
\" Double quote. Used to print a double quote
character.

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

Every variable has name, type, size and value

When new value placed into variable, overwrites


previous value

Reading variables from memory, nondestructive

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

Multiplication, division, modulus applied next


 Operators applied from left to right
Addition, subtraction applied last
 Operators applied from left to right
Operator(s) Operation(s) Order of evaluation (precedence)

() Parentheses Evaluated first. If the parentheses are nested, the


expression in the innermost pair is evaluated first. If
there are several pairs of parentheses “on the same level”
(i.e., not nested), they are evaluated left to right.
*, /, or % Multiplication Division Evaluated second. If there are several, they re
Modulus evaluated left to right.
+ or - Addition Evaluated last. If there are several, they are
Subtraction evaluated left to right. 23
Decision Making: Equality and
Relational Operators
if structure
Make decision based on truth or falsity of condition
 If condition met, body executed
 Else, body not executed

Equality and relational operators


Equality operators
 Same level of precedence
Relational operators
 Same level of precedence
Associate left to right

24
Decision Making: Equality and
Relational Operators

25
Confusing Equality (==) and
Assignment (=) Operators
Common error

Does not typically cause syntax errors

Aspects of problem

Expressions that have a value can be used for decision


 Zero = false, nonzero = true
Assignment statements produce a value (the value to be
assigned)
26
Confusing Equality (==) and
Assignment (=) Operators
Example
if ( payCode == 4 )
printf("You get a bonus!“);
If paycode is 4, bonus given

If == was replaced with =


if ( payCode = 4 )
printf("You get a bonus!");
Paycode set to 4 (no matter what it was before)
Statement is true (since 4 is non-zero)
Bonus given in every case
27
C Data Types

Simple address Structured

Integral enum Floating pointer array


reference struct
union
float class
char
short signed double
int unsigned long double
long
bool

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

unsigned int 4 unsigned 0 to 4,294,967,295

bool 1 none false or true

char 1 none –128 to 127

signed char 1 none –128 to 127

unsigned char 1 none 0 to 255

short 2 short int, signed short int –32,768 to 32,767

unsigned short 2 unsigned short int 0 to 65,535

long 4 long int, signed long int –2,147,483,648 to 2,147,483,647

unsigned long 4 unsigned long int 0 to 4,294,967,295

long long 8 none –9,223,372,036,854,775,808 to


9,223,372,036,854,775,807

unsigned long long 8 none 0 to 18,446,744,073,709,551,615

enum varies none

float 4 none 3.4E +/- 38

double 8 none 1.7E +/- 308

long double same as double none same as double


30
Expressions
C expressions are used to express computation.

Expressions is combination of operations and the operands


on which the operations are applied.

Operands can be variables, literals or constants.

31
Precedence
Precedence controls the order of evaluation of operators.

A high precedence means an operator is evaluated (applied)


before any lower precedence operators.

Operators that have the same precedence can happen in


either order, but in C the one on the left is evaluated first.

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:

const double factor = 5.0/9.0;


const double offset = 32.0;
celcius = (fahr - offset)*factor;

34
What if you try to change a const?
The compiler will complain if your code tries to modify a
const variable:

const int temp = 100;



temp = 21;

Error: l-value specifies const object

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++

Binary Operator – two operands: a - b

37
Integer vs. floating point math
How does C know whether to use floating point or
integer math operators?

If either operand is floating point, a floating point


operation is done (the result is a floating point
value).

If both operand are integer the result is an integer


(even division).

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.0  3.5

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

You might also like