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

CS1010E Lecture 1: Basics of C Programming With Numerical Computations

The lecture covers: [1] The edit-compile-run cycle for developing C programs. [2] Declaring variables to define memory for program data. [3] Basic program input/output using scanf() and printf().

Uploaded by

Caroline Chua
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

CS1010E Lecture 1: Basics of C Programming With Numerical Computations

The lecture covers: [1] The edit-compile-run cycle for developing C programs. [2] Declaring variables to define memory for program data. [3] Basic program input/output using scanf() and printf().

Uploaded by

Caroline Chua
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

CS1010E Lecture 1

Basics of C Programming
with Numerical Computations

Henry Chia ([email protected])

Semester 1 2017 / 2018

1 / 24
Lecture Outline

 Edit-compile-run cycle
 Declaring variables
 Program input/output
 Assignment statement
 Arithmetic with typed-expressions
 Types of errors
 Program style

2 / 24
Edit-Compile-Run Cycle

 Edit
– vim editor to create .c
source file
 Compile (include linking)
– gcc compiler generates
a.exe or a.out
 Run
– loads the executable
– ./a.exe or ./a.out

3 / 24
Motivating Example

 Given x (miles), convert to y (kms) using

y = kx

– x and y and variables;


– k is a constant of proportionality ≈ 1.609;
 Given a value for x, what is y?
y x
= 1.609 ×

4 / 24
Sample C Program

5 / 24
The main Function

/* preprocessor directives */
int main(void) { Memory
/* declarations (memory) */
6
/* statements (process) */ ?
Process
return 0;
}
 Program execution must begin with the main function
 The statement
return 0;
in the main function signifies the successful termination of
the program

6 / 24
Declaring Variables – Defining Memory

 type
– integer: int
– floating point (double precision): double
 value
– Examples of integer values: 1, 0, -100
– Examples of floating point values: 1.0, 0.123, -1.23
 identifier – a meaningful name (case-sensitive)
– must consist only of letters, digits, and underscores
– cannot begin with a digit
– cannot be a reserved word
– avoid using standard identifier names

7 / 24
Declaring Variables – Syntax
declaration
type variable-id ;

= value

,
type
int

double

 Examples:
– Unknown value: int x;
– Initialized: int height=3;
– Multiple: double r1, r2 = 1.23, r3, radius = 4.0;
 It is advisable to always declare and initialize variables to
an initial value (typically zero).
8 / 24
Declaring Variables – Example
/* preprocessor directives */
int main(void) {
double miles, kms; /* distances in miles and kilometers */
/* statements */
return 0;
}

miles ? kms ?

/* preprocessor directives */
int main(void) {
double miles = 0, kms = 0; /* distances in miles and kilometers */
/* statements */
return 0;
}

miles 0 kms 0

9 / 24
Program Input – scanf <stdio.h>
scanf-statement

scanf ( format-spec , & variable-id ) ;

format-spec

" %d "

%lf

 Requires preprocessor directive: #include <stdio.h>


 Conversion specifiers %d for integer; %f for floating-point
 Reading(scanf) a floating-point value(%lf) into(&) miles:
scanf("%lf", &miles);
 Reading two integers: scanf("%d%d", &miles, &kms);
10 / 24
Program Input – Example

#include <stdio.h>
int main(void) {
double miles = 0, kms = 0; /* distances in miles and kilometers */
/* Get the distance in miles */
scanf("%lf", &miles); /* assume 10.0 is read as input */
return 0;
}

miles 0 kms 0
10.0

11 / 24
Program Output – printf <stdio.h>
printfStatement

printf ( formatString ) ;

, expr

formatString

" text "

%d expr
var-id
%f
value
\n

 Requires preprocessor directive: #include <stdio.h>


 %d and %f as placeholders to output int and double values
12 / 24
Program Output – Example
#include <stdio.h>
int main(void) {
double miles = 0, kms = 0; /* distances in miles and kms */
/* Get the distance in miles */
printf("Enter the distance in miles> ");
scanf("%lf", &miles); /* assume 10.0 is read as input */
/* Verify the distance entered */
printf("The distance entered is %f miles\n", miles);
return 0;
}

miles 0 kms 0
10.0
 How about the following?
printf("The distance entered is %d miles\n", miles);

13 / 24
Assignment Statement

 Set a variable to the value of an expression:

statement assignment-statement

assignment-statement variable-id = expr ;

scanf-statement
expr
variable-id
printf-statement
value

expr op expr

( expr )

14 / 24
Arithmetic
 Expression involving an operation over two other expressions
 Arithmetic operations: +, -, *, /, % (remainder or modulo)
 Example expression involving operators:

 Operations over expressions are grouped in order of


1. Precedence: 2+3*4 → (2+(3*4)) since * before +
2. Associativity: 2*3*4 → ((2*3)*4) since * is L→R
 Use parentheses ( ) to group explicitly
15 / 24
Typed Expressions

 All expressions are typed. In particular, for op ∈ {+, -, *, /}


– exprint op exprint → exprint
– expri op exprj → exprdouble if i or j is double
 Exercise:

– 22+7 → – 22/7.0 →
– 22.0-7.0 → – 22/7 → (quotient)
– 22.0*7 → – 22%7 → (remainder)
 % operates over integers only
 What happens when v = 34 πr3 is written as
v = 4/3 * 3.142 * r * r * r;

16 / 24
Type Conversions
 Numeric conversions:
– Safe
⊲ 1.0 * exprint → exprdouble
⊲ (double) exprint → exprdouble
– Unsafe:
⊲ (int) exprdouble → exprint
 Examples:
– 1.0*22/7 → 22.0/7 → 3.142857..
– (double)22/7 → 22.0/7 → 3.142857..
– 1.0*(22/7) → 1.0*3 → 3.0
– (double)(22/7) → (double)3 → 3.0
– (int)(22.0/7) → (int)3.142857.. → 3
17 / 24
Typed Assignment

 Expressions are evaluated before assignment. Consider:


– typed expression evaluation, then
– possible type conversion during assignment
 Study the following program fragment:
int miles=3;
double kms;
miles = miles * 1.609;
kms = miles;
What are the values stored in the variables miles and kms?
miles(int) 3 kms(double) ?

18 / 24
Sample Program
#include <stdio.h>
int main(void) {
double miles=0, kms=0; /* distances in miles and kms */
/* Get the distance in miles */
printf("Enter the distance in miles> ");
scanf("%lf", &miles); /* assume 10.0 is read as input */
/* Convert the distance to kilometers */
kms = 1.609 * miles;
/* Display the distance in kilometers */
printf("%f miles is equivalent to %f kms\n", miles, kms);
return 0;
}

miles 0 kms 0
10.0 16.09

19 / 24
Constant

 A named constant can be used in place of a value


 #define preprocessor directive for defining constants
 No variable is declared for the constant
#include <stdio.h>
#define KMS_PER_MILE 1.609
int main(void) {
double miles=0, kms=0; /* distances in miles and kms */
/* Get the distance in miles */
printf("Enter the distance in miles> ");
scanf("%lf", &miles); /* assume 10.0 is read as input */
/* Convert the distance to kilometers */
kms = KMS_PER_MILE * miles;
/* Display the distance in kilometers */
printf("%f miles is equivalent to %f kms\n", miles, kms);
return 0;
}

20 / 24
Errors

 Compile error
Edit Program
– Syntax errors or inconsistencies that
are detected by the compiler
Compile y
 Runtime error
Error?

n
– Program compiles, starts to execute
Runtime
but terminates prematurely
y
Error?
 Logical error
n

Logical y
– Program compiles, executes and
Error? terminates, but with wrong result
n

21 / 24
Program Style
/*
This program converts miles to kilometers.
*/
#include <stdio.h>
int main(void) {
/* statements within a block are indented */
}

 Comments:
– Use block comments: /* ... */
– Use comments, only when necessary
– The header comment is always useful
 Spaces:
– Blank spaces to improve statement readability
– Blank lines to separate different sections of code
– Indentation to define blocks of code { ... }
22 / 24
Program Style

 Compare the following programs. Which is more readable?


/*
This program converts miles to kilometers.
*/
#include <stdio.h>
#define K 1.609 /* kms per mile */
int main(void) {
double x=0, y=0; /* x miles; y kms */ /*
This program converts miles to kilometers.
/* Get the distance in miles */ */
printf("Enter the distance in miles> "); #include <stdio.h>
scanf("%lf", &x);
#define KMS_PER_MILE 1.609
/* Convert the distance to kilometers */ int main(void) {
y = K * x; double miles=0, kms=0;
/* Display the distance in kilometers */ printf("Enter the distance in miles> ");
printf("%f miles = %f kms\n", x, y); scanf("%lf", &miles);
return 0; kms = KMS_PER_MILE * miles;
}
printf("%f miles = %f kms\n", miles, kms);
return 0;
}

23 / 24
Lecture Summary

 Importance of type-awareness in C programming


 Variables declared with appropriate type according to their
usage within the program
 When writing program instructions, keep in mind the type of
variables/values and its effect on the instructions
 Maintain type-consistency with operations, assignments,
input and output
 Handle any instance of type-inconsistency carefully

24 / 24

You might also like