Introduction toComputer EngineeringECSE 221                                                       Ulhaque                 ...
Why use C?• Imperative programming language• Provides language constructs that map efficiently to machine  instructions• R...
Difference between C & Java type of language                                      function oriented          object orient...
Pelles C –C Compiler•   WebCT•   Download and Install Pellas C•   Set up environment•   Build a simple program – Hello Wor...
C – General Structure•   Libraries•   Declarations•   Method Definition•   Main Method
Data Types•   Int: Integer•   Char: One byte character•   Float: Single-precision floating point•   Double: Double-precisi...
Example 1//libraries#include <stdio.h>#include <stdlib.h>#include <string.h>//declaration#define PI 3.14float getRadius(){...
Example 2 (contd.)float getArea(float radius){        float area;        area = PI*radius*radius;        return area;}void...
Basic I/O commands• scanf: reads till a whitespace character (blank space,EOL,etc)• Printf: outputs a line of text which c...
Characters•   n :new line character•   <,>,==,!= : conditional operators•   +,-,/,* : arithmetic operators•   foo++ : use ...
If statementsIf(<condition 1>)        <statement block 1>else if(<condition 2>)        <statement block 2>else        <sta...
While loopWhile (<condition [booleanexpression]>)       <statement>EXAMPLEInt n=3;Int number=5;Int sum=0;While (n>0){     ...
For loopFor(<statement 1>;<condition> [booleanexpression];<statement2>){       <statement block 3>}EXAMPLEsum = 0;number =...
Boolean operators• &&: (AND)        a && b : a and b must both be equal to 1 -> ‘true’• ||: (OR)        a || b : a and/or ...
Bitwise operators *•   & bitwise AND•   | bitwise OR•   ^ bitwise XOR•   << left shift•   >> right shift•   A & 0x0001 -> ...
Pointers• Data type which points to another location in memory• int *a, b;• a is a pointer to an integer• b = 4;          ...
Pointers (contd.)• a is the address which the pointer contains: 0x0002• *a is the content of the location ‘a’ (content at ...
Command line arguments•   void main(int argc, char *argv[])•   argc•   –Integers which is equal to the number of terms in ...
Upcoming SlideShare
Loading in …5
×

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

1,879 views
1,764 views

Published on

(c) 2011 Muhammad Ehtasham Ulhaque

Published in: Education
1 Comment
0 Likes
Statistics
Notes
  • Be the first to like this

No Downloads
Views
Total views
1,879
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
12
Comments
1
Likes
0
Embeds 0
No embeds

No notes for slide

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

  1. 1. Introduction toComputer EngineeringECSE 221 Ulhaque ECSE 221 - Muhammad EhtashamMuhammad Ehtasham Ulhaquemuhammad.ulhaque@mail.mcgill.caTutorial 1 Winter 2011
  2. 2. 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
  3. 3. 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: http://www.cs.princeton.edu/introcs/faq/c2java.html
  4. 4. Pelles C –C Compiler• WebCT• Download and Install Pellas C• Set up environment• Build a simple program – Hello World!
  5. 5. C – General Structure• Libraries• Declarations• Method Definition• Main Method
  6. 6. Data Types• Int: Integer• Char: One byte character• Float: Single-precision floating point• Double: Double-precision floating pointArray Declaration:• Array: int array[number][number]
  7. 7. 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);}
  8. 8. 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);}
  9. 9. 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’
  10. 10. Characters• n :new line character• <,>,==,!= : conditional operators• +,-,/,* : arithmetic operators• foo++ : use foo then increment• ++foo: increment foo then use it• +=,*=,/=,-=, syntax shortcut
  11. 11. 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);
  12. 12. While loopWhile (<condition [booleanexpression]>) <statement>EXAMPLEInt n=3;Int number=5;Int sum=0;While (n>0){ sum = sum + number; n--;}return sum;
  13. 13. 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;
  14. 14. 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
  15. 15. Bitwise operators *• & bitwise AND• | bitwise OR• ^ bitwise XOR• << left shift• >> right shift• A & 0x0001 -> this return the last bit
  16. 16. 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
  17. 17. 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
  18. 18. 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

×