ecse 221 - introduction to computer engineering - tutorial 1 - muhammad ehtasham ulhaque
DESCRIPTION
(c) 2011 Muhammad Ehtasham UlhaqueTRANSCRIPT
![Page 1: ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtasham Ulhaque](https://reader036.vdocuments.site/reader036/viewer/2022081519/55620a93d8b42a7d028b4a75/html5/thumbnails/1.jpg)
Introduction to Computer Engineering ECSE 221 Muhammad Ehtasham Ulhaque [email protected] Tutorial 1 Winter 2011
ECSE
22
1 -
Mu
ham
mad
Eh
tash
am
Ulh
aqu
e
![Page 2: ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtasham Ulhaque](https://reader036.vdocuments.site/reader036/viewer/2022081519/55620a93d8b42a7d028b4a75/html5/thumbnails/2.jpg)
Why use C?
• Imperative programming language
• Provides language constructs that map efficiently to machine instructions
• Require minimal run-time support
• Provides low-level access to memory
![Page 3: ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtasham Ulhaque](https://reader036.vdocuments.site/reader036/viewer/2022081519/55620a93d8b42a7d028b4a75/html5/thumbnails/3.jpg)
Difference between C & Java
type of language function oriented object oriented
basic programming unit function class = ADT
portability of source code possible with discipline yes
portability of compiled code no, recompile for each architecture
yes, bytecode is "write once, run anywhere"
hello, world
#include<stdio.h> int main(void) { printf("Hello\n"); return 0; }
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello"); } }
Source: http://www.cs.princeton.edu/introcs/faq/c2java.html
More to come as we go along
![Page 4: ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtasham Ulhaque](https://reader036.vdocuments.site/reader036/viewer/2022081519/55620a93d8b42a7d028b4a75/html5/thumbnails/4.jpg)
Pelles C –C Compiler
• WebCT
• Download and Install Pellas C
• Set up environment
• Build a simple program – Hello World!
![Page 5: ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtasham Ulhaque](https://reader036.vdocuments.site/reader036/viewer/2022081519/55620a93d8b42a7d028b4a75/html5/thumbnails/5.jpg)
C – General Structure
• Libraries
• Declarations
• Method Definition
• Main Method
![Page 6: ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtasham Ulhaque](https://reader036.vdocuments.site/reader036/viewer/2022081519/55620a93d8b42a7d028b4a75/html5/thumbnails/6.jpg)
Data Types
• Int: Integer
• Char: One byte character
• Float: Single-precision floating point
• Double: Double-precision floating point
Array Declaration:
• Array: int array[number][number]
![Page 7: ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtasham Ulhaque](https://reader036.vdocuments.site/reader036/viewer/2022081519/55620a93d8b42a7d028b4a75/html5/thumbnails/7.jpg)
Example 1
//libraries #include <stdio.h> #include <stdlib.h> #include <string.h> //declaration #define PI 3.14
float getRadius(){ float radius; printf(“Please enter the radius of the circle:\n”); scanf(“%f”,&radius); return(radius); }
![Page 8: ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtasham Ulhaque](https://reader036.vdocuments.site/reader036/viewer/2022081519/55620a93d8b42a7d028b4a75/html5/thumbnails/8.jpg)
Example 2 (contd.)
float getArea(float radius){
float area;
area = PI*radius*radius;
return area;
}
void main()
{
float circleradius= getRadius();
float localarea= getArea(circleradius);
printf("the area of a circle of radius %f is: %f",circleradius,localarea);
}
![Page 9: ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtasham Ulhaque](https://reader036.vdocuments.site/reader036/viewer/2022081519/55620a93d8b42a7d028b4a75/html5/thumbnails/9.jpg)
Basic I/O commands
• scanf: reads till a whitespace character (blank space,EOL,etc)
• Printf: outputs a line of text which can include variables
• Float and decimal types are items in a memory location thus
Scanf(“%f”,&numberlocation) The & specifies to store the content at memory location ‘numberlocation’
![Page 10: ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtasham Ulhaque](https://reader036.vdocuments.site/reader036/viewer/2022081519/55620a93d8b42a7d028b4a75/html5/thumbnails/10.jpg)
Characters
• \n :new line character
• <,>,==,!= : conditional operators
• +,-,/,* : arithmetic operators
• foo++ : use foo then increment
• ++foo: increment foo then use it
• +=,*=,/=,-=, syntax shortcut
![Page 11: ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtasham Ulhaque](https://reader036.vdocuments.site/reader036/viewer/2022081519/55620a93d8b42a7d028b4a75/html5/thumbnails/11.jpg)
If statements
If(<condition 1>)
<statement block 1>
else if(<condition 2>)
<statement block 2>
else
<statement block 3>
EXAMPLE:
If( x==‘+’)
printf(“the sum is %d”,a+b);
else if( x==‘/’)
printf(“the quotient is %d”,a/b);
![Page 12: ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtasham Ulhaque](https://reader036.vdocuments.site/reader036/viewer/2022081519/55620a93d8b42a7d028b4a75/html5/thumbnails/12.jpg)
While loop
While (<condition [booleanexpression]>)
<statement>
EXAMPLE
Int n=3;
Int number=5;
Int sum=0;
While (n>0){
sum = sum + number;
n--;
}
return sum;
![Page 13: ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtasham Ulhaque](https://reader036.vdocuments.site/reader036/viewer/2022081519/55620a93d8b42a7d028b4a75/html5/thumbnails/13.jpg)
For loop
For(<statement 1>;<condition> [booleanexpression];<statement 2>)
{
<statement block 3>
}
EXAMPLE
sum = 0;
number = 5;
for(int n=3; n>0;n--){
sum += number;
}
return sum;
![Page 14: ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtasham Ulhaque](https://reader036.vdocuments.site/reader036/viewer/2022081519/55620a93d8b42a7d028b4a75/html5/thumbnails/14.jpg)
Boolean operators
• &&: (AND)
a && b : a and b must both be equal to 1 -> ‘true’
• ||: (OR)
a || b : a and/or b must both be equal to 1
• ! : (NOT)
!a return the complement of a
![Page 15: ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtasham Ulhaque](https://reader036.vdocuments.site/reader036/viewer/2022081519/55620a93d8b42a7d028b4a75/html5/thumbnails/15.jpg)
Bitwise operators *
• & bitwise AND
• | bitwise OR
• ^ bitwise XOR
• << left shift
• >> right shift
• A & 0x0001 -> this return the last bit
![Page 16: ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtasham Ulhaque](https://reader036.vdocuments.site/reader036/viewer/2022081519/55620a93d8b42a7d028b4a75/html5/thumbnails/16.jpg)
Pointers
• Data type which points to another location in memory
4
a b
Location 1000 Location 1004 If we had a 32 bit machine
Address in byte
• int *a, b; • a is a pointer to an integer • b = 4;
a = &b;
1004 4
a b
Location 1000 Location 1004
is the content of the location ‘a’ (content at 1004) = 4 *a
![Page 17: ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtasham Ulhaque](https://reader036.vdocuments.site/reader036/viewer/2022081519/55620a93d8b42a7d028b4a75/html5/thumbnails/17.jpg)
Pointers (contd.)
• a is the address which the pointer contains: 0x0002
• *a is the content of the location ‘a’ (content at location 0x0002) : 4
• &a is the address of the pointer a: 0x0001
• b is the content at pointer b: 4
• &b is the address of pointer b: 0x0002
• *b illegal operation
![Page 18: ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtasham Ulhaque](https://reader036.vdocuments.site/reader036/viewer/2022081519/55620a93d8b42a7d028b4a75/html5/thumbnails/18.jpg)
Command line arguments
• void main(int argc, char *argv[])
• argc
• –Integers which is equal to the number of terms in the call
• argv
• –Array of pointers