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
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
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
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 point
Array Declaration:
• Array: int array[number][number]
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); }
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 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);
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;
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;
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
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
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
Top Related