This document provides an introduction to the computer engineering course ECSE 221. It discusses why C is used, the differences between C and Java, how to set up a C compiler, the general structure of C programs, basic data types, input/output commands, conditional statements, loops, pointers, and command line arguments.
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. 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 along
Source: http://www.cs.princeton.edu/introcs/faq/c2java.html
4. Pelles C –C Compiler
• WebCT
• Download and Install Pellas C
• Set up environment
• Build a simple program – Hello World!
5. C – General Structure
• Libraries
• Declarations
• Method Definition
• Main Method
6. 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]
7. 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);
}
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. 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. Characters
• n :new line character
• <,>,==,!= : conditional operators
• +,-,/,* : arithmetic operators
• foo++ : use foo then increment
• ++foo: increment foo then use it
• +=,*=,/=,-=, syntax shortcut
11. 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);
12. 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;
13. 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;
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. Bitwise operators *
• & bitwise AND
• | bitwise OR
• ^ bitwise XOR
• << left shift
• >> right shift
• A & 0x0001 -> this return the last bit
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 1004
a = &b; 1004 4
a b
*a is the content of the location ‘a’ (content at 1004) = 4
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. 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