COMP2511 Lesson 1
COMP2511 Lesson 1
C PROGRAMMING
SESSION 1: INTRO
Welcome to COMP 2511
Week
Outcome/Material Covered Assignment Due Date
Number
1 Introduction
Assign 1 Week 2
Review of control structures
3
4
Arrays
Pointers
Assign 3
Assign 4
Week 4
Week 5
Schedule
5 Characters and Strings Assign 5 Week 7
Formatted Input and Output
6
Midterm Examination
Structures, Unions, Bit Manipulations and Enumeations
7 Assign 6 Week 8
Data Structures
Data Structures (cont)
8 Assign 7 Week 9
File Processing
Variable-Length Argument Lists
Command-Line Arguments
9 DMA functions: calloc and realloc Assign 8 Week 10
File Processing (cont)
Pointers to Functions
C Preprocessor (cont)
Multiple Source File Programs
10
Final Examination
MATRIX
GCC INSTALL
https://gcc.gnu.org/install/binaries.html
OUR FIRST
C
PROGRAM
HELLO WORLD
Notes:
1) main() is always the first function
called, however, it doesn’t need to
be the first function in your code
2) Pay attention on the difference
between “define a function”(to
make) and “call a function”(to use),
also later, we will learn “declare a
function”(to claim).
ANATOMY
/* include another file*/
/* a function name*/
/* one statement*/
/* return statement*/
KEY TAKE AWAY #1
In C,
1) Statements can be inside of a function OR outside of a function (usually inside of).
2) Functions can only be outside of another function.
1) To declare a function (Only can be used outside of
another function)
STATEMENT TYPE returnType functionName (ParameterList) ;
#1: 2) To declare a variable
varType varName;
DECLARATION
(basically, same as Java, just without default value
setting)
DATA TYPES (VARIABLE TYPES)
Common used basic types include:
Non-numeric type Numeric types
*In C, we also have array and pointer which will be covered later.
KEY TAKE AWAY #2
In C,
There is NOT bool nor Boolean type.
Developers use int (0 or 1) to express the idea of Boolean values.
3) Chaining assignment:
height = length = width = 10;
4) Print a char
char one_char =‘a’;
printf (“This number is %c”, one_char );
ESCAPE SEQ
BASIC SCANF STATEMENTS
#include <stdio.h> need this line, at beginning of the file, to use scanf()
1) Read in one number:
int num;
scanf (“%d”, &num);
3) Read in a char
char one_char;
scanf (“%c”, &one_char);
READY?
ASSIGNMENT 1 PART1
Your turn to try :
1) To make a program which reads an integer from user and then print it
2) To make a program which reads two integers from user, and then print the sum of them
3) To make a program which reads two integers from user, and then print them in different lines
4) To make a program which reads a char from user and then print it
5) To make a program which reads two chars from user, and then print them with a space in
between
BREAK TIME
10 mins break
DECISION MAKING
TO AVOID
IF STATEMENT
BASIC IF STATEMENT
(1)
if ( condition)
one statement; /* one statement to do when the condition got fulfilled */
(2)
Recommended
if ( condition) {
statements /* one statement or statements to do when the condition got fulfilled */
}
READY? ASSIGNMENT 1 PART1
Your turn to try :
6) To make a program which reads an integer from user,
and then print “positive” , “zero” or “negative” accordingly, using basic if statements
Pseudocode C code
NESTED IF…ELSE STATEMENT
}
else if (condition #2 ) { This part is called else-if-block
one or more statements We can have zero or more else-if-block
}
else {
This part is called else-block
one or more statements We can have zero or one else-block
}
ASSIGNMENT 1 PART1
Your turn to try :
8) To make a program which reads an integer from user,
and then print “positive” , “zero” or “negative” accordingly, using ONE if statement
10 mins break
WHILE STATEMENT
THE WHILE REPETITION STATEMENT
ASSIGNMENT 1 PART1
10) make a program prints out 1 to 100, each per line using a while-loop
11) make a program prints out 1 to 100, each 10 per line using a while-loop
12) make a program prints out numbers between 1 and 100:
which are multiples of 3 or multiples of 5 , but not multiple of 15
KEY TAKE AWAY #4
OUR PROGRESS