ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtasham Ulhaque
Upcoming SlideShare
Loading in...5

ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtasham Ulhaque



(c) 2011 Muhammad Ehtasham Ulhaque

(c) 2011 Muhammad Ehtasham Ulhaque



Total Views
Views on SlideShare
Embed Views



1 Embed 1 1



Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtasham Ulhaque ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtasham Ulhaque Presentation Transcript

  • Introduction toComputer EngineeringECSE 221 Ulhaque ECSE 221 - Muhammad EhtashamMuhammad Ehtasham Ulhaquemuhammad.ulhaque@mail.mcgill.caTutorial 1 Winter 2011
  • 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
  • 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 no, recompile for each yes, bytecode is "write once, portability of compiled code architecture run anywhere" public class HelloWorld { #include<stdio.h> public static void int main(void) { main(String[] args) { printf("Hellon"); hello, world System.out.println("Hello") return 0; ; } } } More to come as we go alongSource:
  • Pelles C –C Compiler• WebCT• Download and Install Pellas C• Set up environment• Build a simple program – Hello World!
  • C – General Structure• Libraries• Declarations• Method Definition• Main Method
  • Data Types• Int: Integer• Char: One byte character• Float: Single-precision floating point• Double: Double-precision floating pointArray Declaration:• Array: int array[number][number]
  • Example 1//libraries#include <stdio.h>#include <stdlib.h>#include <string.h>//declaration#define PI 3.14float getRadius(){ float radius; printf(“Please enter the radius of the circle:n”); scanf(“%f”,&radius); return(radius);}
  • 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);}
  • 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’
  • Characters• n :new line character• <,>,==,!= : conditional operators• +,-,/,* : arithmetic operators• foo++ : use foo then increment• ++foo: increment foo then use it• +=,*=,/=,-=, syntax shortcut
  • If statementsIf(<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);
  • While loopWhile (<condition [booleanexpression]>) <statement>EXAMPLEInt n=3;Int number=5;Int sum=0;While (n>0){ sum = sum + number; n--;}return sum;
  • For loopFor(<statement 1>;<condition> [booleanexpression];<statement2>){ <statement block 3>}EXAMPLEsum = 0;number = 5;for(int n=3; n>0;n--){ sum += number;}return sum;
  • 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
  • Bitwise operators *• & bitwise AND• | bitwise OR• ^ bitwise XOR• << left shift• >> right shift• A & 0x0001 -> this return the last bit
  • Pointers• Data type which points to another location in memory• int *a, b;• a is a pointer to an integer• b = 4; Location 1000 Address in byte Location 1004 If we had 4 a 32 bit a machine b Location 1000 Location 1004a = &b; 1004 4 a b *a is the content of the location ‘a’ (content at 1004) = 4
  • 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
  • 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