0% found this document useful (0 votes)
117 views

COMP2511 Lesson 1

The document outlines a 10-week schedule for a C programming course. It includes the following key points: 1. Each week covers different C programming topics like functions, arrays, pointers, structures, file processing etc. and has an assignment due. 2. The midterm exam is in week 6 and the final exam is in week 10. 3. Additional resources like how to install GCC compiler are provided. Notes on C programming concepts like functions, data types, statements etc. are included.

Uploaded by

serachguru
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
117 views

COMP2511 Lesson 1

The document outlines a 10-week schedule for a C programming course. It includes the following key points: 1. Each week covers different C programming topics like functions, arrays, pointers, structures, file processing etc. and has an assignment due. 2. The midterm exam is in week 6 and the final exam is in week 10. 3. Additional resources like how to install GCC compiler are provided. Notes on C programming concepts like functions, data types, statements etc. are included.

Uploaded by

serachguru
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 44

COMP 2511

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

2 Functions  Assign 2 Week 3

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

char int, long, float, double, and more (Refer to Wikipedia)

*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.

For example, usually:


• 0 for false, non-zero for true
• 0 for success and non-zero for failure
RESERVED WORDS IN C
CANNOT BE USED AS A VARIABLE NAME
To give a value to a variable:
1) Simple case:
num = 3;

2) Initial value (to assign a value within a declaration statement):


STATEMENT TYPE #2: int num = 5;

3) Chaining assignment:
height = length = width = 10;

ASSIGNMENT 4) Compound assignments:


+= -= *= /= %= |= ^= etc.
5*) Increment and decrement operators
ARITHMETIC IN C

** make sure not to divide some number by zero


PRECEDENCE
INCREMENT AND DECREMENT OPERATORS
To call/revoke a defined function with necessary inputs:
1) printf (“hello world”); /* example with one input */
2) scanf (“%d”, &num); /* example with two inputs*/
STATEMENT TYPE
3) my_function (); /* example with no input*/
#3:
4) int num = scanf (“%c”, &c); /* example with return
value */
FUNCTION CALL
FYI, printf() and scanf() will be introduced later.
KEY TAKE AWAY #3
In C,
1) Both “compile” and “run” go sequentially.
For example, it will fail the compile, if we try to call a function got defined later (below).
2) About naming, function names and variable names usually started with small letter
YOU CANNOT CALL A FUNCTION ISN’T DECLARED YET
1) if-else statement (will be covered later today)
STATEMENT TYPE #4: 2) Switch statement (may be covered later today)
3) Loop statements (in next session)
CONTROL STATEMENT
4) Jump statements: break and continue
STATEMENT TYPE
#5: ;
NULL STATEMENT
BASIC PRINT STATEMENTS
#include <stdio.h> need this line, at beginning of the file, to use printf()
1) One line print: printf (“hello world”);
2) Multiple lines print printf (“hello\n world”); /* ’\n’ is a new-line char*/
3) Print a number
int num =10;
printf (“This number is %d”, num);

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);

2) Read in multiple numbers


int num1, num2;
scanf (“%d %d”, &num1, &num2);

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

7) To make a program which reads two integers from user,


and then print the bigger one between the two, using basic if statements
IF-ELSE
STATEMENT
SHORTER VERSION OF “IF-ELSE STATEMENT”
CONDITIONAL OPERATOR ? :
Use case #1:
condition ? statement1 : statement 2 ;

Use case #2:


condition ? value2 : value 2 ;
NESTED IF…ELSE STATEMENT

Pseudocode C code
NESTED IF…ELSE STATEMENT

C code using nested if…else statement C code using if statement


MORE COMPLEX IF STATEMENT
if (condition #1) { This part is called if-block
one or more statements We can have one and only one if-block

}
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

9) To make a program which reads two integers from user,


and then print the bigger one between the two or “they are same.” accordingly,
using ONE if statement
BREAK TIME

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

You might also like