CS1010E Lecture 1: Basics of C Programming With Numerical Computations
CS1010E Lecture 1: Basics of C Programming With Numerical Computations
Basics of C Programming
with Numerical Computations
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
y = kx
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
format-spec
" %d "
%lf
#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
%d expr
var-id
%f
value
\n
miles 0 kms 0
10.0
How about the following?
printf("The distance entered is %d miles\n", miles);
13 / 24
Assignment Statement
statement assignment-statement
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:
– 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
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
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
23 / 24
Lecture Summary
24 / 24