C3 - Introduction To C Programming
C3 - Introduction To C Programming
INTRODUCTION TO C PROGRAMMING
CSC 099
Learning Outcomes
At the end of this topic, students should be able to:
1. Describe the basic structure of C programming.
2. Declare variables in C programming.
3. Apply the concepts of data types, identifiers
and various operators used in C programming.
4. Use the standard input & output function.
5. Translate from an algorithm to programming.
Part I : C
Programming
Introduction
Example: C Program
Program
Output
Structure of C Program
//Written by: Nurhilyana Anuar
/*This program is to calculate the area of a cylinder*/
#include <stdio.h>
#define pi 3.142
Comments
Preprocessor
Header
directive
file
main()
function
int main()
{
float radius, height;,A_cyl;
Function
body
Variable
declaration
(local
declaration)
Statemen
ts
A_cyl = 2*pi*radius*height;
return 0;
Return
statement
Structure of C Program
7
Comments
Two format
Line comment
Block comment
Structure of C Program
8
Preprocessor Directive
#include <stdio.h>
stdio.h
Dot h (.h) header file
Allows printf() and scanf()
#define pi 3.142
pi - Name of constant
3.142 value of the constant
Structure of C Program
9
main() function
compulsory function
int
stands
for Integer.
indicates that the function sends an integer
back to operating system
main()
name of the function
MUST be followed by a set of braces ({ })
the beginning and ending of a function
Structure of C Program
10
Function Body
Example:
Return Statement
Return
Syntax
Line
Statement
Programmer-Defined Identifier
(Identifier)
Operators
Punctuations
Punctuations
13
Semicolon (;)
Reserved Words/Keywords
14
double
else
enum
extern
float
for
if
int
long
register
return
short
sizeof
static
struct
switch
typedef
unsigned
void
volatile
while
Identifiers
15
** C is CASE-SENSETIVE
16
Valid Identifiers
A
student_name
_aSystemName
Pi
Al
stdntNm
anthrSysNm
PI
Invalid Identifiers
$sum // $ is illegal
2names // cant
start with 2
stdnt Nmbr // cant
have space
int // reserved word
Variables
17
Variable Declaration
18
19
Example of Variables in
Memory
long
int
Data Types
Determine the kind of
information stored in variables.
Classify variables.
Functions also have types which
determine by the data it returns.
Data
types
Amount Description
of
storage
Example
void
None/Nu
ll
void display();
int
4 bytes
float
4 bytes
double
8 bytes
double dist=
546464.59;
Integer
Floating-point
23
2
2
Minimum
Value
-32,768
0
int
unsigned int
long int
4
4
4
-2,147,483,648
0
-2,147,483,648
2,147,483,647
4,294,967,295
2,147,483,647
4,294,967,295
Type
Byte Size
short int
unsigned short int
32,767
65,535
24
Type
Byte
Size
Precision
float
10-37 ..1038
double
15
10-307 ..10308
long double
10
19
10-4931 ..104932
Variable Initialization
25
variable_name = value;
Example :
int count = 5;
int sum = 0;
int count=0 , sum = 0;
char letter = B;
Exercises
27
Escape Sequence
28
backslash(\) in a string
Escape
Sequence
\n
\t
\a
\\
\
Description
Newline. Position the cursor at the
beginning of the next line.
Horizontal tab. Move the cursor to
the next tab stop.
Alert. Sound the system bell.
Backslash. Insert a backslash
character in a string.
Double quote. Insert a double-quote
character in a string.
2 Syntax of printf()
printf(FormatControlString);
Example :
printf(FormatControlString FormatSpecifier,
PrintList);
Example :
int year=2006;
30
Format Specifiers
int
%d
float
%f
double
%lf
char
%c
string
%s
Question
Let say, the value kept in sum variable is
10, what is the actual output?
32
Display space/tab
printf("\tWelcome");
Output
1234
1234
##1234
1234##
001234
***Note:
34
Output
1234.123450
decimal
1234.1235
1234.12
1234.12########
(.)
########1234.12
(.)
Output
Programming
Programming
####Programming
Programming####
##########Progr
Progr##########
Syntax of scanf()
scanf(FormatControlString, InputList);
Example:
int age;
scanf(%d, &age);
format specifiers
only
& indicates
address of the
variable
37
Format Specifiers
int
%d
float
%f
double
%lf
char
%c
string
%s
38
Why? */
Why? */
int height;
char ch;
double weight;
/* Line 7 Initialize
variables
/*
Line 9 to12 Input
/*
Statement
/*
/*
Line 13 Sum
/*
/* Line 14 Output
Statement
ended
Program Output
42
Example of C Program
#include <stdio.h>
int main()
{
int num1;
scanf(%d,&num1 );
printf(You key in %d\n,num1);
return 0;
}
Structure of a C Program
43
Exercises
include <stdio.h>
int main()
{
width = 15
area = length * width;
printf(The area is %d,
&area);
#include <stdio.h>
#Pi 3.142
int main()
{
float circum, rad
scanf(%lf,rad);
circum = 2 * Pi * rad;
printf(The circumference is %d,
&circum);
}
Answers
#include <stdio.h>
int main()
{
int width, area, length;
width = 15;
area = length * width;
printf(The area is %d,
area);
}
#include <stdio.h>
#define Pi 3.142
int main()
{
float circum, rad;
scanf(%lf, &rad);
circum = 2 * Pi * rad;
printf(The circumference is %d,
&circum);
}
Constants
46
Literal Constant
Defined Constant using preprocessor
Defined Constant using const
Literal Constants
47
Defined Constants
48
2 ways :
:
#define pi 3.142
const int a = 1;
Example: Constant
#include <stdio.h>
#define SALESTAX 0.05
int main()
{
const float service = 3.25;
float taxes, total, amount;
Input : num1,
num2
Processes :
1.
num1_sq = num1 x
num1
2.
num2_cb = num2 x
num2 x num2
Output :
num1_sq ,
num2_cb
Algorithm
Start
Enter num1
and num2
Calculate,
num1_sq = num1 x num1
num2_cb = num2 x num2
x num2
Display
num1_sq and
num2_cb
End
Algorithm
Start
int main()
{
int num1, num2, num1_sq, num2_cb;
Enter num1
and num2
INPU
T
Calculate,
num1_sq = num1 x num1
num2_cb = num2 x num2
x num2
Display
num1_sq and
num2_cb
OUTPU
T
End
return 0;
Part II :
Operators
Arithmetic Operators
55
C
Operation
Arithmetic
Operator
C
Expression
Value of d after
assignment
Addition
d=a+b
Substraction
d=b-2
Multiplication
d=a*b
20
Division
d = a/2
Modulus
d = b%3
Assume :
int x = 4, y=5, z=8;
Assignment
Operator
Sample
Expression
Similar
Expression
Value of variable
after assignment
+=
x += 5
x=x+5
x=9
-=
y -= x
y=y-x
y=1
*=
x *= z
x = x*z
x=32
/=
z /=2
z = z/2
z=4
y = y%x
y=1
%=
y %=x
Operator
Called
Sample
Expression
Similar
Expression
Explanation
++
preincrement
++a
a = a +1
a += 1
++
postincrement
a++
a = a +1
a += 1
--
predecrement
--a
a = a -1
a -= 1
--
postdecrement
a--
a = a -1
a -= 1
prefix
int c=5;
printf( "%d", ++c );
postfix
int c=5;
++c;
printf( "%d", c );
Output:
6
Output:
E
int num = 5;
--num;
printf("%d\n",num);
printf("%d",--num);
c=5;
printf( "%d", c++ );
Output:
6
int c=5;
c++;
printf( "%d", c );
Output:
4
3
int num = 5;
num--;
printf("%d",num--);
printf("%d",num);
Output:
5
Output:
6
Output:
E
E
Output:
4
3
loop = count++;
++a;
a = ++z;
//p--;
++z;
printf("a=%d, z=%d, p=%c",a,z,p--);
Output:
loop= --count;
loop= loop--;
printf("count =%d, loop=%d\n", count, loop);
Output:
Assume :
int y=6, x=5;
Relational Operators
Sample Expression
Value
>
y>x
T (1)
<
y<2
F (0)
>=
x>=3
T (1)
<=
y<=x
F (0)
Equality Operators
Sample Expression
Value
==
x==5
T(1)
!=
y!=6
F(0)
Logical Operators
61
Logical Operators
Called
Sample Operation
&&
AND
||
OR
expression1 | | expression2
NOT
! expression
Expression
!Expression
!(x==60)
0 (False)
1 (True)
!(x!=60)
1 (True)
0 (False)
Assume:
int x=4, y = 5, z=8;
Expression1
Expression
2
Expression1
&&
Expression2
0 (False)
0 (False)
0 (False)
Sample
Expression
( y > 10) && ( z <
=x )
( z < = y) && ( x =
= 4)
( y ! = z) && ( z < x
)
Assume:
int x=4, y = 5, z=8;
Expression1
Expression
2
Expression1
&&
Expression2
0 (False)
0 (False)
0 (False)
( z < = y) && ( x =
= 4)
0 (False)
1 (True)
0 (False)
( y ! = z) && ( z < x
)
1 (True)
0 (False)
0 (False)
Sample
Expression
Assume:
int x = 4, y=5, z=8;
Expression1
Expression
2
Expression1
&&
Expression2
0 (False)
0 (False)
0 (False)
Sample
Expression
( y > 10) || ( z < =x
)
( z < = y) || ( x = =
4)
( y ! = z) || ( z < x )
( z > = y ) || ( x ! =
Assume:
int x = 4, y=5, z=8;
Expression1
Expression
2
Expression1
&&
Expression2
0 (False)
0 (False)
0 (False)
( z < = y) || ( x = =
4)
0 (False)
1 (True)
1 (True)
( y ! = z) || ( z < x )
1 (True)
0 (False)
1 (True)
( z > = y ) || ( x ! =
1 (True)
1 (True)
1 (True)
Sample
Expression
Operator Precedence
66
Operators
Associative
()
Left to right
++ - - + - !
Right to left
* / %
Left to right
+ -
Left to right
Left to right
= = !=
Left to right
&&
Left to right
||
Left to right
= *= += - = /= %=
Right to left
Equivalent Expression
Value
i + 2 == k -1
( i + 2) == (k 1)
0 or False
a + 1 == b
(a + 1) == b
1 or True
z % k != c % i
(z % b) != ( c % i)
1 or True
z%c*i
(z % c) * I
k % c * i && i % z * k
((k % c) * i) && (( i % z) * k)
c % i * z >10 || c /k *z < 5
20
1 or True
0 or False
68
Example : Operator
Precedence
Example 1
Example 2
int a=10, b=20, c=15, d=8;
a*b/(-c*31%13)*d
1. a*b/(-15*31%13)*d
1. d *= ++b a/3+ c
2. a*b/(-15*31%13)*d
2. d*=7- a/3+c
3. a*b/(-465%13)*d
3. d*=7- 5+c
4. a*b/(-10)*d
4. d*=2 + c
5. 200/(-10)*d
5. d*= 7
6. -200*d
6. d = d*7
7. -1600
7. d = 28
Exercises
i)
ii)
iii)
Write a relational expression to express the following condition (use your own
variable name)
A persons height is less than 6 feet
A length is greater than 2 and less equal than 3 feet
A persons is older than 50 or has been employed at the company for at
least 5 years
Write a snippet code that can receive 3 input decimal numbers from user
and prints the average of the numbers.
Assign the sum of value variable marks1 , marks 2, and marks3 to variable
totalMarks
Exercises (Answer)
i)
ii)
iii)
Exercises (Answer)
Answer is :
9