Lect02a Overview of C Programming-1
Lect02a Overview of C Programming-1
sg/~cs2100/
Lecture #2a
Overview of C Programming
Questions?
IMPORTANT: DO NOT SCAN THE QR CODE IN THE
VIDEO RECORDINGS. THEY NO LONGER WORK
Ask at
https://sets.netlify.app/module/676ca3a07d7f5ffc1741dc65
OR
1. A Simple C Program
2. von Neumann Architecture
3. Variables
4. Data Types
5. Program Structure
5.1 Preprocessor Directives
5.2 Input/Output
5.3 Compute
Arithmetic operators
Assignment statements
Typecast operator
Lecture #2: Overview of C Programming 4
6. Selection Statements
6.1 Condition and Relational Operators
6.2 Truth Values
6.3 Logical Operators
6.4 Evaluation of Boolean Expressions
6.5 Short-Circuit Evaluation
7. Repetition Statements
7.1 Using ‘break’ in a loop
7.2 Using ‘continue’ in a loop
Lecture #2: Overview of C Programming 5
Introduction
C: A general-purpose computer programming
language developed in 1972 by Dennis
Ritchie (1941 – 2011) at Bell Telephone Lab
for use with the UNIX operation System
Lecture #2: Overview of C Programming 6
Executable code
Compile produces
a.out Cannot
eg: gcc first.c
compile?
def main(void)
int main(): {
float miles, // input – distance in miles
kms; // output – distance in kilometres
#/*Get
Getthe
thedistance
distanceininmiles
miles */
miles
printf("Enter
= float(input("Enter
distance in miles:
distance
");in miles: "))
scanf("%f", &miles);
#//Convert
Convertthe
thedistance
distancetotokmkilometres
kms
kms == KMS_PER_MILE
KMS_PER_MILE ** miles
miles;
#//Display
Displaythe
thedistance
distanceininkmkilometres
print("That
printf("Thatequals",
equals %9.2f
kms, km.");
km.\n", kms);
return
return 00; Sample run
} $ gcc MileToKm.c
if name == "__main__": $ a.out
main() Enter distance in miles: 10.5
That equals 16.89 km.
(Note: All C programs in the lectures are available on LumiNUS as well
as the CS2100 website. Python versions are also available.)
Lecture #2: Overview of C Programming 9
return 0;
} In C, semi-colon (;) terminates a statement.
Curly bracket { } indicates a block.
In Python: block is by indentation
Lecture #2: Overview of C Programming 10
At the beginning
After user enters: 10.5 to After this line is executed:
Do not assume that
uninitialised variables scanf("%f", &miles); kms = KMS_PER_MILE * miles;
contain zero! (Very
common mistake.)
Lecture #2: Overview of C Programming 11
Redundant initialization
int count = 0; int count = 0;
count = 123; scanf("%d", &count);
Lecture #2: Overview of C Programming 14
int main(void) {
printf("Size of 'int' (in bytes): %d\n", sizeof(int));
printf("Size of 'float' (in bytes): %d\n", sizeof(float));
printf("Size of 'double' (in bytes): %d\n", sizeof(double));
printf("Size of 'char' (in bytes): %d\n", sizeof(char));
return 0; Python
} Use sys.getsizeof
Programming Samples
• All sample programs are available at the Lecture Slides
section here:
https://www.comp.nus.edu.sg/~cs2100/2_resources/lecture
s.html
Lecture #2: Overview of C Programming 18
5. Program Structure
A basic C program has 4 main parts:
Preprocessor directives:
eg: #include <stdio.h>, #include <math.h>, #define PI 3.142
Input: through stdin (using scanf), or file input
Compute: through arithmetic operations and assignment
statements
Output: through stdout (using printf), or file output
Lecture #2: Overview of C Programming 19
Preprocessor
5.1 Preprocessor Directives (1/2) Input
Compute
Output
The C preprocessor provides the following
Inclusion of header files
Macro expansions
Conditional compilation
We will focus on inclusion of header files and simple application
of macro expansions (defining constants)
Inclusion of header files
To use input/output functions such as scanf() and printf(), you
need to include <stdio.h>: #include <stdio.h>
To use functions from certain libraries, you need to include the
respective header file, examples:
To use mathematical functions, #include <math.h>
(In sunfire, need to compile with –lm option)
To use string functions, #include <string.h>
Lecture #2: Overview of C Programming 20
Preprocessor
5.1 Preprocessor Directives (2/2) Input
Compute
Output
Macro expansions
One of the uses is to define a macro for a constant value
Eg: #define PI 3.142 // use all CAP for macro
Preprocessor
Preprocessor
Preprocessor
End of File