C Tutorial 2
C Tutorial 2
CSU480
Who is the TA?
Name: Jingjing Duan
Office Hours: (might change)
Tue & Fri 2:00-4:00
Office: Room 266, WVH
Email: duanjj@ccs.neu.edu
Class web: www.ccs.neu.edu/course/csu480
Outline
“Hello World" Program Pointers
Data Types & Variables Functions
printf() Command-Line Argument
Arithmetic & Logical Data Structure
Operations Memory Allocation
Conditionals Programming Tips
Loops C vs. C++
Arrays & Strings Books recommended
Hello World Program
Static variable
Another class of local variable is the static type. It is specified by the
keyword static in the variable declaration.
The most striking difference from a non-static local variable is, a static
variable is not destroyed on exit from the function.
Global variable
A global variable declaration looks normal, but is located outside any
of the program's functions. So it is accessible to all functions.
An example
An example
int stud_id = 5200;
char * name = “Mike”;
printf(“%s ‘s ID is %d \n”, name, stud_id);
Format Identifiers
%d decimal integers
%x hex integer
%c character
%f float and double number
%s string
%p pointer
escape sequence
x = x+1; x += 1 x++
x = x-1; x -= 1 x--
Example
Arithmetic operators
int i = 10;
int j = 15;
int add = i + j; //25
int diff = j – i; //5
int product = i * j; // 150
int quotient = j / i; // 1
int residual = j % i; // 5
i++; //Increase by 1
i--; //Decrease by 1
Comparing them
int i = 10;
int j = 15;
float k = 15.0;
j/i=?
j%i=?
k/i=?
k%i=?
The Answer
j / i = 1;
j % i = 5;
k / i = 1.5;
k % i It is illegal.
Note: For %, the operands can only be integers.
Logical Operations
What is “true” and “false” in C
In C, there is no specific data type to represent “true” and “false”. C
uses value “0” to represent “false”, and uses non-zero value to stand
for “true”.
Logical Operators
A && B => A and B
A || B => A or B
A == B => Is A equal to B?
A != B => Is A not equal to B?
A >B => Is A greater than B?
A >= B => Is A greater than or equal to B?
A <B => Is A less than B?
A <= B => Is A less than or equal to B?
Don’t be confused
&& and || have different meanings from & and |.
& and | are bitwise operators.
Short circuiting
Short circuiting means that we don't
evaluate the second part of an AND or OR
unless we really need to.
Some practices
Please compute the value of the following
logical expressions?
int i = 10; int j = 15; int k = 15; int m = 0;
if( i < j && j < k) =>
if( i != j || k < j) =>
if( j<= k || i > k) =>
if( j == k && m) =>
if(i) =>
if(m || j && i ) =>
int i = 10; int j = 15; int k = 15; int m = 0;
if( i < j && j < k) => false
if( i != j || k < j) => true
if( j<= k || i > k) => true
if( j == k && m) => false
if(i) => true
if(m || j && i ) => true
First time: x = 0;
Second time: x = 1;
Third time: x = 2;
Fourth time: x = 3; (don’t execute the body)
The while statement
while (expression) {
statement …
}
while loop exits only when the expression is false.
An example
int x = 3;
while (x>0) {
printf("x=%d n",x);
x--;
}
for <==> while
for (expression1; expression2; expression1;
expression3){ while (expression2)
statement… {
} equals statement…;
expression3;
}
Arrays & Strings
Arrays
int ids[50];
char name[100];
int table_of_num[30][40];
Accessing an array
ids[0] = 40;
i = ids[1] + j;
table_of_num[3][4] = 100;
Note: In C Array subscripts start at 0 and end one less than
the array size. [0 .. n-1]
Strings
Strings are defined as arrays of characters.
The only difference from a character array is, a symbol “\0”
is used to indicate the end of a string.
D a v e \0
Functions
Functions are easy to use; they allow complicated programs to be broken
into small blocks, each of which is easier to write, read, and maintain.
This is called modulation.
What is a pointer?
A pointer is a variable which contains the address in memory
of another variable.
X pointer
0x5200
33 22 00 00
0x5200 0x5203
swap the value of two variables
Why is the left one not working?
swap x, y
x, y, a, b are
all local
variables
main
a, b
The trick is the system always passes the name of the executable file as
the first argument to the main() function.
size of int
32-bytes machines 32
64-bytes machines 64
Programming Tips
Replacing numbers in your code with macros
- don’t use magic numbers directly
#define MAX_NAME_LEN 50;
char name[MAX_NAME_LEN];