SlideShare a Scribd company logo
1 of 74
INTRODUCTION TO C
DR FRANK GUAN
ICT1002 – PROGRAMMING FUNDAMENTALS
AY19-20 T1 WEEK 8
Agenda
1. History of C
2. A simple C program
3. From Python to C
4. Basic data types
5. C formatted I/O
6. Control structures
2
RECOMMENDED READING
3
Paul Deitel and Harvey Deitel
C: How to Program,
8th Edition
Prentice Hall, 2016
– Chapter 2: Introduction to C
Programming
– Chapter 3: Structured Program
Development in C
– Chapter 4: C Program Control
SET UP IDE
• Windows MS Studio (1)
– Download and install MS Visual Studio 2019
Community
– Create a new solution and an “Empty Project”
by giving meaningful names
– Add a new “C++ File (.cpp)” in the “Source
Files” folder
– Right click the new file and rename it to “.c”
– Right click the project and select “Build”
– Go to the “Debug” folder of the solution and find
the “.exe”
4
SET UP IDE
• Windows MS Studio (2)
– Download and install MS Visual Studio 2019
Community
– In the search box on the taskbar of your
Windows OS, type “developer command
prompt” and choose “Developer Command
Prompt for Visual Studio”
– Change directory to the folder containing your C
files
– Type “cl filename.c”
– An “exe” and an “obj” file will be generated
– Open “cmd” window and go to the exe folder
and type the name of the exe file
5
SET UP IDE
• MacOS
– Download and install Xcode from
https://developer.apple.com/xcode/
– See http://help.apple.com/xcode/mac/ for
tutorials
• LINUX
– Most Linux have GNU C Compiler (gcc)
installed already
– To compile a program called hello.c from the
command line, execute
– gcc –o hello hello.c
6
SET UP IDE
Open source and cross-platform IDE:
Code::Blocks
• downloadable from:
– http://www.codeblocks.org/downloads
• Tutorials on Mac OS installation
– https://www.youtube.com/watch?v=_bKLttPVoC8
• Tutorials on Windows OS installation
– https://www.youtube.com/watch?v=lQlTENvMilE
– Remember to choose codeblocks-17.12mingw-
setup.exe
• Tutorials on Linux OS installation
– https://www.youtube.com/watch?v=xoK5JCtNH5A
7
SET UP IDE
– Online IDE: repl.it
– Click https://repl.it/classroom/invite/gNjNhTX
and join the classroom
8
HISTORY OF C
HISTORY OF C
Martin Richard created the
Basic Combined Programming Language
in 1966 and 1967.
B
B
BCPL
BCPL
10
HISTORY OF C
Ken Thompson
developed B – based on
BCPL – at AT&T in 1969.
An early version of UNIX
was written in B.
B
B
BCPL
BCPL
11
HISTORY OF C
1970s Traditional C
1989 – C89/ANSI C/Standard C
1990 – ANSI/ISO C
1999 – C99: an attempt to
standardise many variations of C
C was created by Dennis Ritchie
at Bell Labs.
12
Dennis Ritchie (left) and Ken Thompson
were the lead developers of UNIX.
UNIX was re-written in C in 1973.
C VS. RELATED LANGUAGES
More recent derivatives:
Influenced:
C lacks:
Low-level language - faster code (usually)
C++, Objective C, C#
Java, Perl, Python
(quite different)
Exceptions
Range-checking
Garbage collection
Object-oriented programming
13
A SIMPLE C PROGRAM
C PROGRAMS
User-
defined
C
standard
library
Other
existing
functions
A C Program
C programs consist of modules called functions
Provides a rich collection of
existing common functions
Promote software reusability
15
TYPICAL
C
PROGRAM
DEVELOPMENT
ENVIRONMENT
16
Editor The programmer creates a
program.
Preprocessor
The pre-processor
processes the code.
Compiler The compiler creates object
code.
Linker
The linker links the object
code with the libraries and
creates an executable file.
Loader The loader puts the
program in memory.
Disk
Memory
CPU takes each instruction
and executes it.
A SIMPLE C PROGRAM
Comments: any
text between /*
and */ is ignored
by the compiler.
17
/*
* Hello World program in C.
*/
#include <stdio.h>
int main() {
printf("Hello world!n");
return 0;
}
A SIMPLE C PROGRAM
C pre-processor:
this line tells the
preprocessor to
include the
content of the
standard
input/output
header
<stdio.h>
18
/*
* Hello World program in C.
*/
#include <stdio.h>
int main() {
printf("Hello world!n");
return 0;
}
A SIMPLE C PROGRAM
C programs
contain one or
more functions,
one of which
MUST be main.
Every program in
C begins
execution in
main.
19
/*
* Hello World program in C.
*/
#include <stdio.h>
int main() {
printf("Hello world!n");
return 0;
}
A SIMPLE C PROGRAM
One printf
can print
several lines by
using additional
newline
characters ‘n’
20
/*
* Hello World program in C.
*/
#include <stdio.h>
int main() {
printf("Hello world!n");
return 0;
}
A SIMPLE C PROGRAM
Returning 0
tells the
operating
system that the
program ended
with no errors.
21
/*
* Hello World program in C.
*/
#include <stdio.h>
int main() {
printf("Hello world!n");
return 0;
}
C PRE-PROCESSOR
The C pre-processor executes before
a program is compiled.
Some actions it performs are:
– definition of symbolic constants
– the inclusion of other files in the file
being compiled
Pre-processor directives begin with #
22
#DEFINE PRE-PROCESSOR DIRECTIVE
23
The #define 
directive creates
symbolic constants.
All subsequent
occurrences of
NUM_STUDENTS will be
replaced with 140.
#define NUM_STUDENTS 140
int main() {
int scores[NUM_STUDENTS];
for (int i = 0; i < NUM_STUDENTS; i++) {
scores[i] = 0;
}
return 0;
}
#INCLUDE PRE-PROCESSOR DIRECTIVE
The #include directive
causes a copy of a specified file to be
included in place of the directive.
24
#INCLUDE PRE-PROCESSOR DIRECTIVE
The two forms of the #include 
directive are:
#include <filename>
#include "filename"
"": used for user-defined files.
The pre-processor starts searches
in the same directory as the file
being compiled
<>: is normally used for standard library
headers, the search is performed normally
through pre-designated compiler and
system directories.
25
HEADER FILES
A C program might have header
files containing:
Function prototypes
(declaration with no
implementation)
Definitions of
various data 
types
Definitions of
constants
26
27
#include <stdio.h>
#include “chat1002.h"
int main() {
int done = 0;
do {
...
} while (!done);
return 0;
}
#define MAX_INTENT 32
#define MAX_ENTITY 64
void chatbot_do_load(int inc, char **inv);
void chatbot_do_smalltalk(int inc, char **inv);
#include "chat1002.h"
void chatbot_do_load(int inc, char **inv) {
...
}
void chatbot_do_smalltalk(int inc, char **inv) 
{
...
}
main.c
chat1002.h
chatbot.c
Constants
Function prototypes
ANOTHER SIMPLE C PROGRAM
The names
integer1,
integer2
and sum are
the names
of
variables.
28
/*
* Another simple C program.
*/
#include <stdio.h>
int main() {
int integer1, integer2, sum;
printf("Enter two numbers to addn");
scanf("%d%d", &integer1, &integer2);
sum = integer1 + integer2;
printf("Sum of entered numbers = %dn", sum);
return 0;
}
ANOTHER SIMPLE C PROGRAM
A variable is a location in memory
where a value can be stored for use by
a program.
29
/*
* Another simple C program.
*/
#include <stdio.h>
int main() {
int integer1, integer2, sum;
printf("Enter two numbers to addn");
scanf("%d%d", &integer1, &integer2);
sum = integer1 + integer2;
printf("Sum of entered numbers = %dn", sum);
return 0;
}
ANOTHER SIMPLE C PROGRAM
These definitions specify that the
variables integer1, integer2 and sum
are of type int.
30
/*
* Another simple C program.
*/
#include <stdio.h>
int main() {
int integer1, integer2, sum;
printf("Enter two numbers to addn");
scanf("%d%d", &integer1, &integer2);
sum = integer1 + integer2;
printf("Sum of entered numbers = %dn", sum);
return 0;
}
The
scanf()
function
reads from
the
standard
input, which
is usually
the
keyboard.
31
/*
* Another simple C program.
*/
#include <stdio.h>
int main() {
int integer1, integer2, sum;
printf("Enter two numbers to addn");
scanf("%d%d", &integer1, &integer2);
sum = integer1 + integer2;
printf("Sum of entered numbers = %dn", sum);
return 0;
}
ANOTHER SIMPLE C PROGRAM
This scanf
has three
arguments,
"%d%d" and
&integer1
and
&integer2.
32
/*
* Another simple C program.
*/
#include <stdio.h>
int main() {
int integer1, integer2, sum;
printf("Enter two numbers to addn");
scanf("%d%d", &integer1, &integer2);
sum = integer1 + integer2;
printf("Sum of entered numbers = %dn", sum);
return 0;
}
ANOTHER SIMPLE C PROGRAM
scanf(): The first argument, the format control string,
indicates the type of data that should be input by the user. The
%d conversion specifier indicates that the data should be a
(decimal) integer.
33
ANOTHER SIMPLE C PROGRAM
/*
* Another simple C program.
*/
#include <stdio.h>
int main() {
int integer1, integer2, sum;
printf("Enter two numbers to addn");
scanf("%d%d", &integer1, &integer2);
sum = integer1 + integer2;
printf("Sum of entered numbers = %dn", sum);
return 0;
}
scanf(): The second and third arguments of scanf begin with
an ampersand (&)—called the address operator in C—followed
by the variable name.
34
/*
* Another simple C program.
*/
#include <stdio.h>
int main() {
int integer1, integer2, sum;
printf("Enter two numbers to addn");
scanf("%d%d", &integer1, &integer2);
sum = integer1 + integer2;
printf("Sum of entered numbers = %dn", sum);
return 0;
}
ANOTHER SIMPLE C PROGRAM
scanf(): The ampersand tells scanf the locations (or
addresses) in memory at which the variables integer1 and
integer2 are stored.
35
/*
* Another simple C program.
*/
#include <stdio.h>
int main() {
int integer1, integer2, sum;
printf("Enter two numbers to addn");
scanf("%d%d", &integer1, &integer2);
sum = integer1 + integer2;
printf("Sum of entered numbers = %dn", sum);
return 0;
}
ANOTHER SIMPLE C PROGRAM
36
10 Minutes Break
FROM PYTHON TO C
1. COMPILERS VS. INTERPRETERS
Python uses an interpreter 
to execute a program.
In C, the compiler converts a
program into machine code.
The machine code can be
executed directly by the CPU.
38
1. COMPILERS VS. INTERPRETERS
program.c a.out
Compiler
Source code Machine code
1
0001
1
12
123
1234
12345
Interpreter
39
1. COMPILERS VS. INTERPRETERS
C programs can lead to faster
runtimes.
C is designed so the compiler can tell
everything it needs to know to translate
the program into machine code.
40
2. VARIABLE DECLARATIONS
C requires variable declarations
These tell the compiler about the variable before it is
used.
Once declared, you cannot change its type.
if you happen to misspell
a variable’s name
41
Good
2. VARIABLE DECLARATIONS
In Python: no declaration required
You are responsible for checking your own code!
More convenient.
42
3. WHITESPACE
In Python, whitespace
characters are
important.
C does not use
whitespace except for
separating words.
Identify blocks
Identify new
statements
43
Use braces {} to
delimit blocks
Use a semi-colon to
terminate statements
4. FUNCTIONS
– All C code must be put within
functions.
– The main() function is the
“starting point” for a C program.
44
5. FUNCTIONS
45
#include <stdio.h>
int gcd(int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
int main() {
printf("GCD: %dn", gcd(24, 40));
return 0;
}
C Program
def gcd(a, b):
if b == 0:
return a
else:
return gcd(b, a % b)
print("GCD: " + str(gcd(24, 40)))
Python Program
From http://www.cs.toronto.edu/~patitsas/cs190/c_for_python.html
GOOD CODING PRACTICE IN C
– Indent blocks of code as in Python
– Format braces and whitespace
consistently
– Use comments to explain your code to
other programmers
– Use meaningful variable names
46
int i;main(){for(;i["]<i;++i){‐‐i;}"];read('‐'‐'‐',i+++"hell
o, world!n",'/'/'/'));}read(j,i,p){write(j/p+p,i‐‐‐j,i/i);}
(from http://www.ioccc.org)
GOOD CODING PRACTICE IN C
– Pay attention to compiler warnings
– these indicate code that is synactically correct
but is likely to lead to run-time errors
– Avoid system-specific features
– all programs in this module should compile and
run using any modern compiler
– code that works on many systems without
modification is called portable
47
BASIC DATA TYPES
MEMORY CONCEPTS Variable
names such
as integer1,
integer2 and
sum actually
correspond to
locations in
the
computer’s
memory.
45
27
integer1
integer2
72
sum
49
scanf("%d%d", &integer1, &integer2);
sum = integer1 + integer2;
MEMORY CONCEPTS
Every variable
has a name, a
type and a
value.
45
27
integer1
integer2
72
sum
50
scanf("%d%d", &integer1, &integer2);
sum = integer1 + integer2;
MEMORY CONCEPTS
The value typed
by the user is
placed into a
memory
location to
which the name
integer1 or 
integer2 has
been assigned.
45
27
integer1
integer2
72
sum
51
scanf("%d%d", &integer1, &integer2);
sum = integer1 + integer2;
MEMORY CONCEPTS
Whenever a
value is placed
in a memory
location, the
value replaces
the previous
value in that
location.
45
27
integer1
integer2
72
sum
52
scanf("%d%d", &integer1, &integer2);
sum = integer1 + integer2;
BASIC DATA TYPES
53
EXAMPLE – C DATA TYPES
What is the %.10f for?
Try to change it to some other number
and observe the output 54
#include <stdio.h>
int main() {
int a = 3000; /* integer data type */
float b = 4.5345; /* floating point data type */
char c = 'A'; /* character data type */
long d = 31456; /* long integer data type */
double e = 5.1234567890; /* double‐precision floating point data type */
printf("Here is the list of the basic data typesn");
printf("n1. This an integer (int): %d", a);
printf("n2. This is a floating point number (float): %f", b);
printf("n3. This is a character (char): %c", c);
printf("n4. This is a long integer (long): %ld", d);
printf("n5. This is a double‐precision float (double): %.10f", e);
printf("n6. This is a sequence of characters: %s",
"Hello ICT1002 students");
return 0;
}
C EXPRESSIONS
Arithmetic can be performed using the
usual operators:
55
+ Addition
- Subtraction
* Multiplication
/ Division
% Remainder upon division (modulo)
() Parentheses
C EXPRESSIONS
The result of an arithmetic operation depends
on the type of its operands:
You can explicitly change the type of an
expression using a cast:
56
int op int int
int op float float
float op float float
double op float double
char op int char
(int)4.5 round down
(float)4 “promote”
C FORMATTED I/O
C FORMATTED I/O
Standard input/output:
#include <stdio.h>
– Input data from the standard input
stream
– Output data to the standard output
steam
– printf, scanf, puts,
getchar, putchar
58
STREAMS
All input and output is performed with streams.
Stream: A sequence of bytes.
C
Programs
Memory
Input Stream
Keyboard, disk
drive, network
connections
Output Stream
Screen, disk
drive, printer,
network
connections
Error Stream
Screen
59
FORMATTING OUTPUT WITH PRINTF
– format‐control‐string: describes the output
format
– other‐arguments (optional): correspond to each
conversion specification within format‐control‐
string
printf(format‐control‐string, other‐arguments);
printf("%s", "Hello World!");
60
COMMON CONVERSION SPECIFIERS
Specifier Output
Characters
%c Character
%s A string of characters
Integers
%d Decimal integer
%x Hexadecimal integer
%ld Long decimal integer
Floating point numbers
%f Single-precision
%lf Double-precision
61
See Deitel & Deitel Ch. 9 for other specifiers.
MORE ON CONVERSION SPECIFIERS
conversion-specifier = <flags><field width><precision><literal character>
printf( "%‐10s%‐10d%‐10c%‐10.3fn", 
"hello", 7, 'a', 1.23 );
flag field width literal character
precision
62
PRINTF FLAGS
Flag Description
‐ (minus sign)
Left-justify the output within the field.
+ (plus sign)
Insert a plus sign before positive
numbers and a minus sign before
negative numbers.
space
Insert a space before positive
numbers.
0
Zero-pad the number to the width of
the field.
63
See Deitel & Deitel Ch. 9 for other flags.
64
/*
* Printing field width example adopted from Deitel & Deitel
*/
#include <stdio.h>
int main() {
printf("%4dn", 1);
printf("%04dn", 1);
printf("%‐4dn", 1);
printf("%4dn", 12);
printf("%4dn", 123);
printf("%4dn", 1234);
printf("%4dn", 12345);
return 0;
}
1
0001
1
12
123
1234
12345
Output:
PRINTF FLAGS - EXAMPLE
ESCAPE SEQUENCES
Characters with special meaning to the
compiler need to be “escaped”:
65
Escape Sequence Output
n New line
t Tab
' Single quote
" Double quote
 Backslash
See Deitel & Deitel Ch. 9 for other escape
sequences.
SCANF – READING FORMATTED INPUT
scanf( format‐control‐string, other‐arguments );
Output
66
printf("Enter seven integers: ");
scanf("%d%i%i%i%o%u%x", &a, &b, &c, &d, &e, &f, &g);
printf("The input displayed as decimal integers is:n");
printf("%d %d %d %d %d %d %d", a, b, c, d, e, f, g);
Enter seven integers: ‐70 ‐70 070 0x70 70 70 70
‐70 ‐70 56 112 56 70 112
CONTROL STRUCTURES
C CONTROL STRUCTURES
C has the same control structures as
other programming languages:
– if‐else
– switch (not available in Python)
– for
– while
– do‐while (not available in Python)
DECISIONS
if-else
if (x > 1) {
printf("More than one.");
} else {
printf("Not more than one.");
}
switch
switch (x) {
case 1:
printf("x is 1.");
break;
case 2:
printf("x is 2.");
break;
…
}
69
LOOPS
for
int i;
for (i = 0; i < 10; i++) {
printf("i = %dn", i);
}
while & do-while
int i = 0;
while (i < 10) {
printf("i = %dn", i);
i++;
}
i = 0;
do {
printf("i = %dn", i);
i++;
} while (i <= 10);
70
END-OF-WEEK CHECKLIST
71
C development environment
Pre-processor directives
#include
#define
printf()
scanf()
Format control strings
Coding conventions
Basic data types
Variables & memory
Streams
If/else
Switch/case
for/while/do..while
Comments
Basic C program structure
Self-assessment (for practice only):
Socrative: https://b.socrative.com/login/student
Room: ICT1002
72
1+
ADMINISTRATION
– Class test (quiz for C):
– Week 12 (21 Nov 2019)
– Group project
– Project description will be uploaded to LMS
next week
– Week 13 (28 Nov)
73
ABOUT LAB
– Follow the instructions on the lab sheet
– A few exercises to be completed in lab class
– An assignment to be completed and submitted after
class
– For exercise:
– show the result for each exercise to the staff in
charge and get them to sign.
– For assignment:
– Submit the source codes to repl.it by 11:30PM of
the next following Wednesday
– E.g. for lab exercises on 24 Oct, source code
submission deadline is: 11:30PM on 30 Oct 2019.
74

More Related Content

Similar to Introduction to C Programming Fundamentals

Introduction-to-C-Part-1.pdf
Introduction-to-C-Part-1.pdfIntroduction-to-C-Part-1.pdf
Introduction-to-C-Part-1.pdfAnassElHousni
 
Introduction
IntroductionIntroduction
IntroductionKamran
 
How to run C Program in Linux
How to run C Program in LinuxHow to run C Program in Linux
How to run C Program in LinuxJohn425873
 
How to work with code blocks
How to work with code blocksHow to work with code blocks
How to work with code blocksTech Bikram
 
Programming in c_in_7_days
Programming in c_in_7_daysProgramming in c_in_7_days
Programming in c_in_7_daysAnkit Dubey
 
GNU Compiler Collection - August 2005
GNU Compiler Collection - August 2005GNU Compiler Collection - August 2005
GNU Compiler Collection - August 2005Saleem Ansari
 
Introduction of c language
Introduction of c languageIntroduction of c language
Introduction of c languagefarishah
 
Ch2 C Fundamentals
Ch2 C FundamentalsCh2 C Fundamentals
Ch2 C FundamentalsSzeChingChen
 
Introduction to cpp language and all the required information relating to it
Introduction to cpp language and all the required information relating to itIntroduction to cpp language and all the required information relating to it
Introduction to cpp language and all the required information relating to itPushkarNiroula1
 

Similar to Introduction to C Programming Fundamentals (20)

Introduction-to-C-Part-1.pdf
Introduction-to-C-Part-1.pdfIntroduction-to-C-Part-1.pdf
Introduction-to-C-Part-1.pdf
 
Introduction
IntroductionIntroduction
Introduction
 
Session 1 - c++ intro
Session   1 - c++ introSession   1 - c++ intro
Session 1 - c++ intro
 
How to run C Program in Linux
How to run C Program in LinuxHow to run C Program in Linux
How to run C Program in Linux
 
1-intro.pdf
1-intro.pdf1-intro.pdf
1-intro.pdf
 
How to work with code blocks
How to work with code blocksHow to work with code blocks
How to work with code blocks
 
The basics of c programming
The basics of c programmingThe basics of c programming
The basics of c programming
 
Programming in c_in_7_days
Programming in c_in_7_daysProgramming in c_in_7_days
Programming in c_in_7_days
 
C Fundamental.docx
C Fundamental.docxC Fundamental.docx
C Fundamental.docx
 
C language
C languageC language
C language
 
C in7-days
C in7-daysC in7-days
C in7-days
 
C in7-days
C in7-daysC in7-days
C in7-days
 
Programming in c
Programming in cProgramming in c
Programming in c
 
CC 3 - Module 2.pdf
CC 3 - Module 2.pdfCC 3 - Module 2.pdf
CC 3 - Module 2.pdf
 
2621008 - C++ 1
2621008 -  C++ 12621008 -  C++ 1
2621008 - C++ 1
 
GNU Compiler Collection - August 2005
GNU Compiler Collection - August 2005GNU Compiler Collection - August 2005
GNU Compiler Collection - August 2005
 
Introduction of c language
Introduction of c languageIntroduction of c language
Introduction of c language
 
Fp201 unit2 1
Fp201 unit2 1Fp201 unit2 1
Fp201 unit2 1
 
Ch2 C Fundamentals
Ch2 C FundamentalsCh2 C Fundamentals
Ch2 C Fundamentals
 
Introduction to cpp language and all the required information relating to it
Introduction to cpp language and all the required information relating to itIntroduction to cpp language and all the required information relating to it
Introduction to cpp language and all the required information relating to it
 

Recently uploaded

FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 

Recently uploaded (20)

FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 

Introduction to C Programming Fundamentals

  • 1. INTRODUCTION TO C DR FRANK GUAN ICT1002 – PROGRAMMING FUNDAMENTALS AY19-20 T1 WEEK 8
  • 2. Agenda 1. History of C 2. A simple C program 3. From Python to C 4. Basic data types 5. C formatted I/O 6. Control structures 2
  • 3. RECOMMENDED READING 3 Paul Deitel and Harvey Deitel C: How to Program, 8th Edition Prentice Hall, 2016 – Chapter 2: Introduction to C Programming – Chapter 3: Structured Program Development in C – Chapter 4: C Program Control
  • 4. SET UP IDE • Windows MS Studio (1) – Download and install MS Visual Studio 2019 Community – Create a new solution and an “Empty Project” by giving meaningful names – Add a new “C++ File (.cpp)” in the “Source Files” folder – Right click the new file and rename it to “.c” – Right click the project and select “Build” – Go to the “Debug” folder of the solution and find the “.exe” 4
  • 5. SET UP IDE • Windows MS Studio (2) – Download and install MS Visual Studio 2019 Community – In the search box on the taskbar of your Windows OS, type “developer command prompt” and choose “Developer Command Prompt for Visual Studio” – Change directory to the folder containing your C files – Type “cl filename.c” – An “exe” and an “obj” file will be generated – Open “cmd” window and go to the exe folder and type the name of the exe file 5
  • 6. SET UP IDE • MacOS – Download and install Xcode from https://developer.apple.com/xcode/ – See http://help.apple.com/xcode/mac/ for tutorials • LINUX – Most Linux have GNU C Compiler (gcc) installed already – To compile a program called hello.c from the command line, execute – gcc –o hello hello.c 6
  • 7. SET UP IDE Open source and cross-platform IDE: Code::Blocks • downloadable from: – http://www.codeblocks.org/downloads • Tutorials on Mac OS installation – https://www.youtube.com/watch?v=_bKLttPVoC8 • Tutorials on Windows OS installation – https://www.youtube.com/watch?v=lQlTENvMilE – Remember to choose codeblocks-17.12mingw- setup.exe • Tutorials on Linux OS installation – https://www.youtube.com/watch?v=xoK5JCtNH5A 7
  • 8. SET UP IDE – Online IDE: repl.it – Click https://repl.it/classroom/invite/gNjNhTX and join the classroom 8
  • 10. HISTORY OF C Martin Richard created the Basic Combined Programming Language in 1966 and 1967. B B BCPL BCPL 10
  • 11. HISTORY OF C Ken Thompson developed B – based on BCPL – at AT&T in 1969. An early version of UNIX was written in B. B B BCPL BCPL 11
  • 12. HISTORY OF C 1970s Traditional C 1989 – C89/ANSI C/Standard C 1990 – ANSI/ISO C 1999 – C99: an attempt to standardise many variations of C C was created by Dennis Ritchie at Bell Labs. 12 Dennis Ritchie (left) and Ken Thompson were the lead developers of UNIX. UNIX was re-written in C in 1973.
  • 13. C VS. RELATED LANGUAGES More recent derivatives: Influenced: C lacks: Low-level language - faster code (usually) C++, Objective C, C# Java, Perl, Python (quite different) Exceptions Range-checking Garbage collection Object-oriented programming 13
  • 14. A SIMPLE C PROGRAM
  • 15. C PROGRAMS User- defined C standard library Other existing functions A C Program C programs consist of modules called functions Provides a rich collection of existing common functions Promote software reusability 15
  • 16. TYPICAL C PROGRAM DEVELOPMENT ENVIRONMENT 16 Editor The programmer creates a program. Preprocessor The pre-processor processes the code. Compiler The compiler creates object code. Linker The linker links the object code with the libraries and creates an executable file. Loader The loader puts the program in memory. Disk Memory CPU takes each instruction and executes it.
  • 17. A SIMPLE C PROGRAM Comments: any text between /* and */ is ignored by the compiler. 17 /* * Hello World program in C. */ #include <stdio.h> int main() { printf("Hello world!n"); return 0; }
  • 18. A SIMPLE C PROGRAM C pre-processor: this line tells the preprocessor to include the content of the standard input/output header <stdio.h> 18 /* * Hello World program in C. */ #include <stdio.h> int main() { printf("Hello world!n"); return 0; }
  • 19. A SIMPLE C PROGRAM C programs contain one or more functions, one of which MUST be main. Every program in C begins execution in main. 19 /* * Hello World program in C. */ #include <stdio.h> int main() { printf("Hello world!n"); return 0; }
  • 20. A SIMPLE C PROGRAM One printf can print several lines by using additional newline characters ‘n’ 20 /* * Hello World program in C. */ #include <stdio.h> int main() { printf("Hello world!n"); return 0; }
  • 21. A SIMPLE C PROGRAM Returning 0 tells the operating system that the program ended with no errors. 21 /* * Hello World program in C. */ #include <stdio.h> int main() { printf("Hello world!n"); return 0; }
  • 22. C PRE-PROCESSOR The C pre-processor executes before a program is compiled. Some actions it performs are: – definition of symbolic constants – the inclusion of other files in the file being compiled Pre-processor directives begin with # 22
  • 23. #DEFINE PRE-PROCESSOR DIRECTIVE 23 The #define  directive creates symbolic constants. All subsequent occurrences of NUM_STUDENTS will be replaced with 140. #define NUM_STUDENTS 140 int main() { int scores[NUM_STUDENTS]; for (int i = 0; i < NUM_STUDENTS; i++) { scores[i] = 0; } return 0; }
  • 24. #INCLUDE PRE-PROCESSOR DIRECTIVE The #include directive causes a copy of a specified file to be included in place of the directive. 24
  • 25. #INCLUDE PRE-PROCESSOR DIRECTIVE The two forms of the #include  directive are: #include <filename> #include "filename" "": used for user-defined files. The pre-processor starts searches in the same directory as the file being compiled <>: is normally used for standard library headers, the search is performed normally through pre-designated compiler and system directories. 25
  • 26. HEADER FILES A C program might have header files containing: Function prototypes (declaration with no implementation) Definitions of various data  types Definitions of constants 26
  • 27. 27 #include <stdio.h> #include “chat1002.h" int main() { int done = 0; do { ... } while (!done); return 0; } #define MAX_INTENT 32 #define MAX_ENTITY 64 void chatbot_do_load(int inc, char **inv); void chatbot_do_smalltalk(int inc, char **inv); #include "chat1002.h" void chatbot_do_load(int inc, char **inv) { ... } void chatbot_do_smalltalk(int inc, char **inv)  { ... } main.c chat1002.h chatbot.c Constants Function prototypes
  • 28. ANOTHER SIMPLE C PROGRAM The names integer1, integer2 and sum are the names of variables. 28 /* * Another simple C program. */ #include <stdio.h> int main() { int integer1, integer2, sum; printf("Enter two numbers to addn"); scanf("%d%d", &integer1, &integer2); sum = integer1 + integer2; printf("Sum of entered numbers = %dn", sum); return 0; }
  • 29. ANOTHER SIMPLE C PROGRAM A variable is a location in memory where a value can be stored for use by a program. 29 /* * Another simple C program. */ #include <stdio.h> int main() { int integer1, integer2, sum; printf("Enter two numbers to addn"); scanf("%d%d", &integer1, &integer2); sum = integer1 + integer2; printf("Sum of entered numbers = %dn", sum); return 0; }
  • 30. ANOTHER SIMPLE C PROGRAM These definitions specify that the variables integer1, integer2 and sum are of type int. 30 /* * Another simple C program. */ #include <stdio.h> int main() { int integer1, integer2, sum; printf("Enter two numbers to addn"); scanf("%d%d", &integer1, &integer2); sum = integer1 + integer2; printf("Sum of entered numbers = %dn", sum); return 0; }
  • 31. The scanf() function reads from the standard input, which is usually the keyboard. 31 /* * Another simple C program. */ #include <stdio.h> int main() { int integer1, integer2, sum; printf("Enter two numbers to addn"); scanf("%d%d", &integer1, &integer2); sum = integer1 + integer2; printf("Sum of entered numbers = %dn", sum); return 0; } ANOTHER SIMPLE C PROGRAM
  • 32. This scanf has three arguments, "%d%d" and &integer1 and &integer2. 32 /* * Another simple C program. */ #include <stdio.h> int main() { int integer1, integer2, sum; printf("Enter two numbers to addn"); scanf("%d%d", &integer1, &integer2); sum = integer1 + integer2; printf("Sum of entered numbers = %dn", sum); return 0; } ANOTHER SIMPLE C PROGRAM
  • 33. scanf(): The first argument, the format control string, indicates the type of data that should be input by the user. The %d conversion specifier indicates that the data should be a (decimal) integer. 33 ANOTHER SIMPLE C PROGRAM /* * Another simple C program. */ #include <stdio.h> int main() { int integer1, integer2, sum; printf("Enter two numbers to addn"); scanf("%d%d", &integer1, &integer2); sum = integer1 + integer2; printf("Sum of entered numbers = %dn", sum); return 0; }
  • 34. scanf(): The second and third arguments of scanf begin with an ampersand (&)—called the address operator in C—followed by the variable name. 34 /* * Another simple C program. */ #include <stdio.h> int main() { int integer1, integer2, sum; printf("Enter two numbers to addn"); scanf("%d%d", &integer1, &integer2); sum = integer1 + integer2; printf("Sum of entered numbers = %dn", sum); return 0; } ANOTHER SIMPLE C PROGRAM
  • 35. scanf(): The ampersand tells scanf the locations (or addresses) in memory at which the variables integer1 and integer2 are stored. 35 /* * Another simple C program. */ #include <stdio.h> int main() { int integer1, integer2, sum; printf("Enter two numbers to addn"); scanf("%d%d", &integer1, &integer2); sum = integer1 + integer2; printf("Sum of entered numbers = %dn", sum); return 0; } ANOTHER SIMPLE C PROGRAM
  • 38. 1. COMPILERS VS. INTERPRETERS Python uses an interpreter  to execute a program. In C, the compiler converts a program into machine code. The machine code can be executed directly by the CPU. 38
  • 39. 1. COMPILERS VS. INTERPRETERS program.c a.out Compiler Source code Machine code 1 0001 1 12 123 1234 12345 Interpreter 39
  • 40. 1. COMPILERS VS. INTERPRETERS C programs can lead to faster runtimes. C is designed so the compiler can tell everything it needs to know to translate the program into machine code. 40
  • 41. 2. VARIABLE DECLARATIONS C requires variable declarations These tell the compiler about the variable before it is used. Once declared, you cannot change its type. if you happen to misspell a variable’s name 41 Good
  • 42. 2. VARIABLE DECLARATIONS In Python: no declaration required You are responsible for checking your own code! More convenient. 42
  • 43. 3. WHITESPACE In Python, whitespace characters are important. C does not use whitespace except for separating words. Identify blocks Identify new statements 43 Use braces {} to delimit blocks Use a semi-colon to terminate statements
  • 44. 4. FUNCTIONS – All C code must be put within functions. – The main() function is the “starting point” for a C program. 44
  • 46. GOOD CODING PRACTICE IN C – Indent blocks of code as in Python – Format braces and whitespace consistently – Use comments to explain your code to other programmers – Use meaningful variable names 46 int i;main(){for(;i["]<i;++i){‐‐i;}"];read('‐'‐'‐',i+++"hell o, world!n",'/'/'/'));}read(j,i,p){write(j/p+p,i‐‐‐j,i/i);} (from http://www.ioccc.org)
  • 47. GOOD CODING PRACTICE IN C – Pay attention to compiler warnings – these indicate code that is synactically correct but is likely to lead to run-time errors – Avoid system-specific features – all programs in this module should compile and run using any modern compiler – code that works on many systems without modification is called portable 47
  • 49. MEMORY CONCEPTS Variable names such as integer1, integer2 and sum actually correspond to locations in the computer’s memory. 45 27 integer1 integer2 72 sum 49 scanf("%d%d", &integer1, &integer2); sum = integer1 + integer2;
  • 50. MEMORY CONCEPTS Every variable has a name, a type and a value. 45 27 integer1 integer2 72 sum 50 scanf("%d%d", &integer1, &integer2); sum = integer1 + integer2;
  • 51. MEMORY CONCEPTS The value typed by the user is placed into a memory location to which the name integer1 or  integer2 has been assigned. 45 27 integer1 integer2 72 sum 51 scanf("%d%d", &integer1, &integer2); sum = integer1 + integer2;
  • 52. MEMORY CONCEPTS Whenever a value is placed in a memory location, the value replaces the previous value in that location. 45 27 integer1 integer2 72 sum 52 scanf("%d%d", &integer1, &integer2); sum = integer1 + integer2;
  • 54. EXAMPLE – C DATA TYPES What is the %.10f for? Try to change it to some other number and observe the output 54 #include <stdio.h> int main() { int a = 3000; /* integer data type */ float b = 4.5345; /* floating point data type */ char c = 'A'; /* character data type */ long d = 31456; /* long integer data type */ double e = 5.1234567890; /* double‐precision floating point data type */ printf("Here is the list of the basic data typesn"); printf("n1. This an integer (int): %d", a); printf("n2. This is a floating point number (float): %f", b); printf("n3. This is a character (char): %c", c); printf("n4. This is a long integer (long): %ld", d); printf("n5. This is a double‐precision float (double): %.10f", e); printf("n6. This is a sequence of characters: %s", "Hello ICT1002 students"); return 0; }
  • 55. C EXPRESSIONS Arithmetic can be performed using the usual operators: 55 + Addition - Subtraction * Multiplication / Division % Remainder upon division (modulo) () Parentheses
  • 56. C EXPRESSIONS The result of an arithmetic operation depends on the type of its operands: You can explicitly change the type of an expression using a cast: 56 int op int int int op float float float op float float double op float double char op int char (int)4.5 round down (float)4 “promote”
  • 58. C FORMATTED I/O Standard input/output: #include <stdio.h> – Input data from the standard input stream – Output data to the standard output steam – printf, scanf, puts, getchar, putchar 58
  • 59. STREAMS All input and output is performed with streams. Stream: A sequence of bytes. C Programs Memory Input Stream Keyboard, disk drive, network connections Output Stream Screen, disk drive, printer, network connections Error Stream Screen 59
  • 60. FORMATTING OUTPUT WITH PRINTF – format‐control‐string: describes the output format – other‐arguments (optional): correspond to each conversion specification within format‐control‐ string printf(format‐control‐string, other‐arguments); printf("%s", "Hello World!"); 60
  • 61. COMMON CONVERSION SPECIFIERS Specifier Output Characters %c Character %s A string of characters Integers %d Decimal integer %x Hexadecimal integer %ld Long decimal integer Floating point numbers %f Single-precision %lf Double-precision 61 See Deitel & Deitel Ch. 9 for other specifiers.
  • 62. MORE ON CONVERSION SPECIFIERS conversion-specifier = <flags><field width><precision><literal character> printf( "%‐10s%‐10d%‐10c%‐10.3fn",  "hello", 7, 'a', 1.23 ); flag field width literal character precision 62
  • 63. PRINTF FLAGS Flag Description ‐ (minus sign) Left-justify the output within the field. + (plus sign) Insert a plus sign before positive numbers and a minus sign before negative numbers. space Insert a space before positive numbers. 0 Zero-pad the number to the width of the field. 63 See Deitel & Deitel Ch. 9 for other flags.
  • 65. ESCAPE SEQUENCES Characters with special meaning to the compiler need to be “escaped”: 65 Escape Sequence Output n New line t Tab ' Single quote " Double quote Backslash See Deitel & Deitel Ch. 9 for other escape sequences.
  • 66. SCANF – READING FORMATTED INPUT scanf( format‐control‐string, other‐arguments ); Output 66 printf("Enter seven integers: "); scanf("%d%i%i%i%o%u%x", &a, &b, &c, &d, &e, &f, &g); printf("The input displayed as decimal integers is:n"); printf("%d %d %d %d %d %d %d", a, b, c, d, e, f, g); Enter seven integers: ‐70 ‐70 070 0x70 70 70 70 ‐70 ‐70 56 112 56 70 112
  • 68. C CONTROL STRUCTURES C has the same control structures as other programming languages: – if‐else – switch (not available in Python) – for – while – do‐while (not available in Python)
  • 71. END-OF-WEEK CHECKLIST 71 C development environment Pre-processor directives #include #define printf() scanf() Format control strings Coding conventions Basic data types Variables & memory Streams If/else Switch/case for/while/do..while Comments Basic C program structure Self-assessment (for practice only): Socrative: https://b.socrative.com/login/student Room: ICT1002
  • 72. 72 1+
  • 73. ADMINISTRATION – Class test (quiz for C): – Week 12 (21 Nov 2019) – Group project – Project description will be uploaded to LMS next week – Week 13 (28 Nov) 73
  • 74. ABOUT LAB – Follow the instructions on the lab sheet – A few exercises to be completed in lab class – An assignment to be completed and submitted after class – For exercise: – show the result for each exercise to the staff in charge and get them to sign. – For assignment: – Submit the source codes to repl.it by 11:30PM of the next following Wednesday – E.g. for lab exercises on 24 Oct, source code submission deadline is: 11:30PM on 30 Oct 2019. 74