COMSW 1003-1
Introduction to Computer
Programming in C
Lecture 4 Spring 2011
Instructor: Michele Merler
C http://www1.cs.columbia.edu/~mmerler/comsw1003-1.html 1
Announcements
• HW 1 is due on Monday, February 14th at the
beginning of class, no exceptions
• Read so far: PCP Chapters 1 to 4
• Reading for next Wednesday: PCP Chapter 5
C 2
Review – Access CUNIX
http://www1.cs.columbia.edu/~bert/courses/1003/cunix.html
1) Enable windowing environment
- X11, Xming, X-Server
2) Launch SSH session (login with UNI and password)
- Terminal, Putty
3) Launch Emacs
$ emacs &
4) Open/create a file, than save it with .c extension
5) Compile source code into executable with gcc
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)
– More advanced options
• gcc –Wall –o myProgram myProgram.c
• ./myProgram
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)
Display all types of Specify name of
warnings,–notMore adva
only errors the executable
• gcc –Wall –o myProgram myProgram.c
• ./myProgram Run compiled program (executable)
C 5
Review: C Syntax
• Statements
– one line commands
– always end with ;
– can be grouped between { }
• Comments
// single line comment
/* multiple lines comments
*/
C 6
Review : Variables and types
• Variables are placeholders for values
int x = 2;
x = x + 3; // x value is 5 now
• In C, variables are divided into types,
according to how they are represented in
memory (always represented in binary)
– int 4 bytes, signed/unsigned
– float 4 bytes, decimal part + exponent
– double 8 bytes
– char 1 byte, ASCII Table
C 7
Review : Casting
• Casting is a method to correctly use variables of different types
together
• It allows to treat a variable of one type as if it were of another
type in a specific context
• When it makes sense, the compiler does it for us automatically
• 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 ++ -- += -=
• Relational < <= > >= == !=
• Logical && || !
• Bitwise & | ~ ^ << >>
• 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);
printf(‚i = %d, j = %d, k = %d\n‛,i,j,k);
C 11
Operators - Arithmetic * / % + -
• Arithmetic operators have a precedence
int x;
x = 3 + 5 * 2 - 4 / 2;
• We can use parentheses () to impose our precedence order
int x;
x = (3 + 5) * (2 – 4) / 2;
• % returns the module (or the remainder of the division)
int x;
x = 5 % 3; // x = 2
• We have to be careful with integer vs. float division : remember
automatic casting!
int x = 3; float y;
float y; y = 1 / 2; // y = 0.00
C y = x / 2; // y = 1.00
12
Operators - Arithmetic * / % + -
• Arithmetic operators have a precedence
int x;
x = 3 + 5 * 2 - 4 / 2;
• We can use parentheses () to impose our precedence order
int x;
x = (3 + 5) * (2 – 4) / 2;
• % returns the module (or the remainder of the division)
int x;
x = 5 % 3; // x = 2
• We have to be careful with integer vs. float division : remember
automatic casting!
int x = 3; Possible fixes: float y;
float y; 1)float x = 3; y = 1 / 2; // y = 0.00
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;
x++; x is incremented at the end of statement
++x; x is incremented at the beginning of statement
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
< <= > >= == !=
• Return 0 if statement is false, 1 if statement is true
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
&& || !
• A variable with value 0 is false, a variable with value !=0 is true
int x = 3, y = 0, z, k, t, q = -3;
z = x && y; // z = 0; x is true but y is false
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
z = x << 1; equivalent to z = x · 21 00000000000000000000000000000110
t = y >> 3; equivalent to t = y · 2-3 00000000000000000000000000000010
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,…);
• Format characters: Format
– %d or %i integer
– %f float % 0 n1 . n2 t
– %lf double
– %c char type
pad with zeros (optional)
– %u unsigned number of digits after
– %s string the decimal point
number of digits before
the decimal point
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
• Decimal to binary conversion
610 = 1102
• Binary to decimal conversion
110012 = 1x20 + 0x21 + 0x22 + 1x23 + 1x24 = 25
• AND
v=x&y
• OR
v=x|y
C
Binary Logic
• 1 = true, 0 = false remainder
• Decimal to binary conversion 6 0
Divide by 2
610 = 1102 3 1
base
1 1
Most significant bit Least significant bit
0
• Binary to decimal conversion
110012 = 1x20 + 0x21 + 0x22 + 1x23 + 1x24 = 25
• AND
v=x&y
• OR
v=x|y
C
Binary Logic
• 1 = true, 0 = false remainder
• Decimal to binary conversion 6 0
Divide by 2
610 = 1102 3 1
base
1 1
Most significant bit Least significant bit
0
• Binary to decimal conversion
110012 = 1x20 + 0x21 + 0x22 + 1x23 + 1x24 = 25
• AND
v=x&y
• OR
v=x|y
C 23
Binary Logic
• 1 = true, 0 = false remainder
• Decimal to binary conversion 6 0
Divide by 2
610 = 1102 3 1
base
1 1
Most significant bit Least significant bit
0
• Binary to decimal conversion
110012 = 1x20 + 0x21 + 0x22 + 1x23 + 1x24 = 25
• 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
HOW TO COMPRESS/UNCOMPRESS folders in UNIX
• Compress folder ~/COMS1003/HW1 to HW1.tar.gz
tar -zcvf HW1.tar.gz ~/COMS1003/HW1
• Uncompress HW1.tar.gz to folder ~/COMS1003/HW1new
tar -zxvf HW1.tar.gz -C ~/COMS1003/HW1new
(note: ~/COMS1003/HW1new must exist already)
C 25