COMSW 1003-1 Introduction To Computer Programming In: Spring 2011 Instructor: Michele Merler
COMSW 1003-1 Introduction To Computer Programming In: Spring 2011 Instructor: Michele Merler
Introduction to Computer
Programming in C
C http://www1.cs.columbia.edu/~mmerler/comsw1003-1.html 1
Announcements
C 2
Review – Access CUNIX
http://www1.cs.columbia.edu/~bert/courses/1003/cunix.html
3) Launch Emacs
$ emacs &
C 3
Review - Compiling your C code
• GCC : GNU Compiler Collection
• When you invoke GCC, it normally does
preprocessing, compilation, assembly and linking
– Basic Command
• gcc myProgram.c
• ./a.out Run compiled program (executable)
C 4
Review - Compiling your C code
• GCC : GNU Compiler Collection
• When you invoke GCC, it normally does
preprocessing, compilation, assembly and linking
– Basic Command
• gcc myProgram.c
• ./a.out Run compiled program (executable)
C 5
Review: C Syntax
• Statements
– one line commands
– always end with ;
– can be grouped between { }
• Comments
// single line comment
C 6
Review : Variables and types
• Variables are placeholders for values
int x = 2;
x = x + 3; // x value is 5 now
• Implicit (automatic)
x= 3 compiler automatically casted
int x =1;
(=converted) y to be an integer just for
float y = 2.3;
this instruction
x = x + y;
• Explicit (non-automatic)
char c = ‘A’; Explicit casting from char to int. The value of x
int x = (int) c; here is 65
C 8
Today
• Operators
• printf()
• Binary logic
C 9
Operators
• Assignment =
• Arithmetic * / % + -
• Increment ++ -- += -=
• Logical && || !
• Comma ,
C 10
Operators – Assignment and Comma
int x = 3;
x = 7;
int x, y = 5;
The comma operator allows
x = y = 7; us to perform multiple
assignments/declarations
float y = 2.3, z = 3, q = 700;
int i,j,k;
k = (i=2, j=3);
C 11
Operators - Arithmetic * / % + -
• Arithmetic operators have a precedence
int x;
x = 3 + 5 * 2 - 4 / 2;
C y = x / 2; // y = 1.00
12
Operators - Arithmetic * / % + -
• Arithmetic operators have a precedence
int x;
x = 3 + 5 * 2 - 4 / 2;
C
2)y = (float) x /2;
y = x / 2; // y = 1.00 Then y = 1.50 Possible fix: y = 1.0/2;
Then y = 0.50 13
Operators – Increment/Decrement
++ -- += -=
int x = 3, y, z;
y = ++x + 3; // x = x + 1; y = x + 3;
z = x++ + 3; // z = x + 3; x = x + 1;
x -= 2; // x = x - 2;
C 14
Operators - Relational
< <= > >= == !=
int x = 3, y = 2, z, k, t;
z = x > y; // z = 1
k = x <= y; // k = 0
t = x != y; // t = 1
C 15
Operators - Logical
&& || !
k = x || y; // k = 1; x is true
t = !q; // t = 0; q is true
C 16
Operators - Bitwise
• Work on the binary representation of data
• Remember: computers store and see data in binary
format!
int x, y, z , t, q, s, v;
x = 3; 00000000000000000000000000000011
y = 16; 00000000000000000000000000010000
q = x & y; 00000000000000000000000000000000
s = x | y; 00000000000000000000000000010011
v = x ^ y; 00000000000000000000000000010011
C XOR
17
printf
• printf is a function used to print to standard output (command line)
• Syntax:
printf(“format1 format2 …”, variable1, variable2,…);
C 18
printf
#include <stdio.h>
int main() {
int a,b;
float c,d;
a = 15;
Output:
b = a / 2;
printf("%d\n",b); 7
printf("%3d\n",b); 7
printf("%03d\n",b); 007
c = 15.3;
d = c / 3;
printf("%3.2f\n",d); 5.10
return(0);
C }
19
printf
Escape sequences
\n newline
\t tab
\v vertical tab
\f new page
\b backspace
\r carriage return
C 20
Binary Logic
• 1 = true, 0 = false
• AND
v=x&y
• OR
v=x|y
C
Binary Logic
• 1 = true, 0 = false remainder
• AND
v=x&y
• OR
v=x|y
C
Binary Logic
• 1 = true, 0 = false remainder
• AND
v=x&y
• OR
v=x|y
C 23
Binary Logic
• 1 = true, 0 = false remainder
• AND x y v
• NOT x v
0 0 0
v=x&y v = !x 0 1
0 1 0 1 0
1 0 0
1 1 1
• OR • EXOR x y v
x y v
v=x|y 0 0 0
v=x^y 0 0 0
C
0 1 1
0 1 1
1 0 1
24
1 0 1
1 1 0
1 1 1
Homework 1 review
C 25