KMJ16803 INTRODUCTION TO
COMPUTER PROGRAMMING
Introduction to Algorithm and
C Programming
Mrs Siti Kamariah Md Sa’at
FTKK, UniMAP
2
C Program structures & data types
Input & output statements
Basic Operator
Logic & comparison
Programming Errors
3
Example 1
Write an algorithm that display a message to the screen
as “Hello World!”.
Pseudo Code Flow Chart
Begin
• Begin
• Print Message Print “Hello World!”
• End
End
Lets use your Codeblock IDE
5
Example 1
Write a C program that display a message to the screen as
“Welcome to C!”.
Simple C Program
Flow Chart
#include<stdio.h>
Begin
int main(void)
{
Print “Welcome to C!”
printf(“Welcome to C!");
return 0;
End }
6
1 /* Figure 1 prog01.c
2 First program in C */
3 #include<stdio.h>
4
5 /* function main begin program execution */
6 int main(void)
7 {
8 printf(“Welcome to C!\n”);
9 return 0;
10 /* indicate that the program ends successfully */
11} /* end function main */
12
7
1 /* Figure 1 prog01.c
2 First program in C */
3 #include<stdio.h>
4
5 /* function main begin program execution */
6 int main(void)
7 {
8 printf(“Welcome to C!\n”);
9 return 0;
10 /* indicate that the program ends successfully */
11
12} /* end function main */
8
Starts with /* and terminates with */
OR
Character // starts a line comment, if several lines, each line
must begin with //
Example : // comment1
// comment2
Ignored by compiler
Help other people read and understand the program
Comments cannot be nested /* /* */*/
/* some comment /* trying to nest other comment */ inside */
WRONG!!
9
1 /* Figure 1 prog01.c
2 First program in C */
3 #include<stdio.h>
4
5 /* function main begin program execution */
6 int main(void)
7 {
8 printf(“Welcome to C!\n”);
9 return 0;
10 /* indicate that the program ends successfully */
11
12} /* end function main */
10
An instruction to pre-processor
Standard library header: <stdio.h> , <math.h>
E.g.
#include <stdio.h> /* for standard input/output */
#include <stdlib.h> /* Conversion number-text vise-versa,
memory allocation, random numbers */
#include <string.h> /* string processing */
11
1 /* Figure 1 prog01.c
2 First program in C */
3 #include<stdio.h>
4
5 /* function main begin program execution */
6 int main(void)
7 {
8 printf(“Welcome to C!\n”);
9 return 0;
10 /* indicate that the program ends successfully */
11
12} /* end function main */
12
C programs contain one or more functions, exactly one of which must be
main
Braces ( { and } ) indicate a block
The bodies of all functions must be contained in braces
At a minimum, the main() function looks like this:
main()
{
}
13
1 /* Figure 1 prog01.c
2 First program in C */
3 #include<stdio.h>
4
5 /* function main begin program execution */
6 int main(void)
7 {
8 printf(“Welcome to C!\n”);
9 return 0;
10 /* indicate that the program ends successfully */
11
12 } /* end function main */
14
printf() Escape Effect
Sequence
Instructs computer to print the string of \a Beep sound
characters within quotes (“ ”) \b Backspace
Entire line called a statement \f Formfeed (for printing)
\n New line
All statements must end with a
semicolon (;) \r Carriage return
\t Tab
\n is an escape sequence \v Vertical tab
moves the cursor to the new line \\ Backslash
\” “ sign
\o Octal decimal
\x Hexadecimal
\O NULL
15
1 /* Figure 1 prog01.c
2 First program in C */
3 #include<stdio.h>
4
5 /* function main begin program execution */
6 int main(void)
7 {
8 printf(“Welcome to C!\n”);
9 return 0;
10 /* indicate that the program ends successfully */
11
12 } /* end function main */
16
A way to exit a function
return
0, in this case, means that the program terminated
normally
17
/*First program in C*/
#include<stdio.h>
/*Program execution begins with main function*/
int main(void)
{
printf starts printing from where the
/*Display Welcome to C*/ statement ended, so the text is printed
printf("Welcome ");
on one line
printf("to C!\n");
/* Indicates program ended succesfully */
return 0;
Note:
}/* end function main */ printf(“Welcome
to C!\n");
→ this is WRONG:
/*First program in C*/
#include<stdio.h>
/*Program execution begins with main function*/
int main(void)
{ newline character moves the
cursor to the next line
/*Display Welcome to C*/
printf("Welcome\nto\nC!\n");
/* Indicates program ended succesfully */ Note: In general, it’s better
programming practice to put newlines
return 0; only at the end, not in the middle,
because in the middle they can be
}/* end function main */ difficult for programmers to see.
printf( “ Welcome \n ”);
printf(“ to \n ”);
18
printf(“ C! \n ”);
Using open source IDE
platform (Code Block) to
write C program
Run and execute the
program by clicking
“Build and Run” button
21
Data Types
22
C has a concept of 'data types' which are used to define a variable
before its use
Data types determine the following:
• Type of data stored
• Number of bytes it occupies in memory
• Range of data
• Operations that can be performed on the data
C has the following basic built-in data types
• int
• float
• double
• char
23
Modifiers alter the meaning of the base type to more
precisely fit a specific need
C supports the following modifiers along with data
types:
• short
• long
• signed
• unsigned
24
intis used to define integer numbers (whole numbers,
both positive and negative)
An example of an integer value is 5, 6, 100, 2500.
An example of declaring an integer variable called age is
int age;
25
long int - allowing an integer to be stored in more
memory locations thereby increasing its effective range so
that very large integers can be stored
short int - may or may not have a smaller range than
normal int variables, however will not take up more bytes
than int
unsigned (positive values only) - negative integers cannot
be assigned to unsigned integers, only a range of positive
values
26
float is used to define floating point numbers, both positive and
negative
Typical floating point values are 1.73 and 1.932e5 (1.932 x 105).
An example of declaring a float variable called x is
float x;
27
double is used to define BIG floating point numbers, both
positive and negative, which have a higher precision than
float variables.
An example of declaring a double variable called voltage :
double voltage;
28
The three C floating point types are:
float
double
long double
In general, the accuracy of the stored real values
increases as you move down the list
29
char defines characters
Example of characters:
Numeric digits: 0 - 9
Lowercase/uppercase letters: a - z and A - Z
Space (blank)
Special characters: , . ; ? “ / ( ) [ ] { } * & % ^ < > etc
An example of declaring a character variable called
letter: The declared character must
be enclosed within a single
char letter = ‘U’; quote!
30
31
32
Variables: locations in memory where a value can
be stored
A quantity that can change during program
execution
Hold the data in your program
All variables in C must be declared before use
If an executable statement references and
undeclared variable it will produce a syntax
(compiler) error
33
Identifiers: Variable names
Valid : dA, dB, dSum, Root, _getchar, __sin, x1, x2, x_1
Invalid: 324, short, price$, My Name
case sensitive (a1 and A1 is different!)
can consist of capital letters[A..Z], small letters[a..z],
digit[0..9], and underscore character (_) that does not
begin with a digit
First character MUST be a letter or an underscore
No blanks
Reserved words cannot be identifiers
34
Reserved words / Keywords are reserved identifiers that have
strict meaning to the C compiler.
35
float fIncome; float fIncome, fNet_income;
float fNet_income;
double dBase, dHeight, dArea;
int iIndex =0, iCount =0;
Declare and initialize
char cCh=‘a’, cCh2;
const float fEpf = 0.1, fTax = 0.05;
Named constant declared and
initialized
36
Variables may be given initial values, or initialized, when
declared. Examples:
length
int length = 7 ; 7
diameter
float diameter = 5.9 ; 5.9
initial
char initial = ‘A’ ; ‘A’
37
A constant is a named or unnamed value, which does not change
during the program execution
The C language supports two types of constants
declared constants
const double dPi=3.141592
const int iDegrees=360;
const char cQuit=‘q’;
defined constants : You may also associate constant using #define preprocessor
directive
#define N 3000
#define FALSE 0
#define PI 3.14159
38
Formatted Input and Output
Statement
39
Example of printf() statement In C language, “printf” command is used to
display any message or output to the screen.
printf("Sum is %d\n",sum); The format of printf is:
printf(“The text to be displayed”);
Output:
Sum is 66
When the printf is executed, it starts printing the until it
encounters a % character (conversion specifier)
The %d means decimal integer will be printed
sum specifies what integer will be printed
Calculations can be performed inside printf statements
printf( "Sum is %d\n", integer1 + integer2 );
40
scanf is a function in C which allows the programmer to accept
input from user usually from a keyboard.
scanf("%d" , &dA );
This scanf statement has two arguments
%d - indicates data should be a decimal integer
&dA - location in memory to store variable
& - have to be included with the variable name in scanf statements
When executing the program the user responds to the scanf statement by
inserting a number, then pressing the enter (return) key
41
Common Conversion Identifier used in printf and scanf
functions.
printf scanf
int %d %d
float %f %f
double %f %lf
char %c %c
string %s %s
42
#include<stdio.h> Output for the source
int main() code:
{
int a=7;
printf("%d\n”,a); 7
printf("%3d\n”,a); 7
printf("%03d\n”,a); 007
return 0;
}
43
#include<stdio.h> Output for the source code:
int main()
{ 7
int a=7; 7
printf("%d\n”,a); 0 0 7
printf("%3d\n”,a);
%d : Print as decimal integer
printf("%03d\n”,a);
%3d : Print three digits (positions)
return 0; %03d : Print the output with a width of
} three digits, but fill the space with 0
44
#include<stdio.h> Output for the source
int main() code:
{ 15.350000
float a=15.35; 15.3500
printf("%f\n”,a); 15.35
printf("%.4f\n”,a);
%f print as a floating point
printf("%4.2f\n”,a); %.4f print as a floating point with a
return 0; precision of four characters
after the decimal point
}
%4.2f print as a floating point at
least 4 wide and a precision
of 2
THANK YOU
PLEASE SCAN YOUR ATTENDANCE