L2 - Basic C Elements
L2 - Basic C Elements
L2 - Basic C Elements
Basic C Elements
The building blocks of C.
Lecture Overview
Overview of
Basic C Elements
C language
Run Cycle
Various Statements
Programming Styles
C Programming Language
Standardized:
Evolved:
in 1999: C99
C Language: Characteristics
C Programming language is a:
Compiled language
Imperative language
Run Cycle:
Writing
Executing
Compiling
Tool: Editor
Tool: Compiler
Tool: None
Produce:
Source Code
Produce:
Executable
Produce:
Result
Compilation Error
Runtime Error
Logic Error
[ CS1010E AY1112S1 Le cture 2 ]
Environment:
Tools:
Using vim
editor under
cygwin
7
Using gcc
compiler
under cygwin
Runtime Error
Logic Error
A successful
run of the an
executable
[ CS1010E AY1112S1 Le cture 2 ]
Writing
Compiling
Executing
Design
Tool: Editor
Produce:
Source Code
Tool:
Compiler
Produce:
Executable
Tool: None
Produce:
Result
Implementation
Testing
10
Preprocessor
Directive
#include <stdio.h>
int main( )
{
Main
function
printf("Hello World!\n");
return 0;
function
body
11
USAGE
#include XXXX
Meaning:
Add the file XXXX for compilation
Example:
#include <stdio.h>
12
USAGE
13
SYNTAX
Caution:
14
C Elements: Statements
Declaration Statements
Assignment Statements
Function Call Statements
Control Structure (Control Flow) Statements
15
A Function:
SYNTAX
function_name (
[function_arguments] );
Function arguments:
Information needed by the function to perform
the task
16
Purpose:
SYNTAX
17
Return Statement:
SYNTAX
return value;
18
White spaces:
19
Problem Description:
A simple algorithm:
1. Get the radius R from user
2. Area PI * R * R
3. Circumference 2 * PI * R
4. Print the result Area and Circumference
21
Variable:
Visualization:
result
Identifier
12345
Actual Value
Data Type describe
what kind of value you
can find in the box
22
Declaration Statement
SYNTAX
datatype identifier;
datatype identifier1, identifier2, ...;
datatype identifier = initial_value;
Data Type
int
Description
Integer (whole number) with no fractional part
double
Other
23
Identifier:
24
Examples:
double area;
double area, circumference;
double area = 0.0;
double area = 0.0, circumference = 1.0;
double 1area;
double area_of_circle;
double return;
double printf;
[ CS1010E AY1112S1 Le cture 2 ]
25
E.g. radius_of_circle
E.g. radiusOfCircle
26
int main( )
{
[0 or more declaration statements]
[0 or more other statements]
return 0;
}
27
Assignment Statement:
The main way to change the value stored in a
variable
SYNTAX
variable = value;
variable = expression;
Execution:
1.
2.
28
Simple Examples:
Operator
Precedence, Associative Rule and Bracket
29
Binary Operators:
+, * (multiplication)
/ (dividision)
% (modulo, remainder, only for whole numbers)
Unary Operators:
+ (positive): +1
- (negative): -7.14
30
Unary +, -
Remainder
*, /
Multiplication, Division
+, -
Addition, Subtraction
31
32
Example:
Safe Conversion:
intVar = 1 / 2;
doubleVar = 1 / 2;
doubleVar = 1 / 2.0;
Unsafe Conversion:
33
#include <stdio.h>
SYNTAX
Example
HEADER
Placeholder to
indicate what
datatype to expect
from user
[ CS1010E AY1112S1 Le cture 2 ]
35
Placeholder:
%d
%lf
Variable:
36
SYNTAX
#include <stdio.h>
Example
HEADER
37
Example
Value stored in
intVar will be
printed
Result of this
expression will be
printed
38
\n
go to newline
\t
\\
to print out \
Try it out:
Example
39
41
C Elements: Comments
/* a multiple
lines comment
*/
ignored by compiler
42
43
constant
reserved
words
comments
variables
standard identifiers
special
symbols
punctuations
[ CS1010E AY1112S1 Le cture 2 ]
44
Problem statement:
For simplicity:
45
Summary
C Elements
Declaration Statements
Assignment Statements
- arithmetic expressions
Control Flow Statements
- return statement
Programming Style
46