02 - Introduction To Algorithm and C Programming Part 1
02 - Introduction To Algorithm and C Programming Part 1
COMPUTER PROGRAMMING
Example 1
Write an algorithm that display a message to the screen
as “Hello World!”.
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
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.
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
}
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
→ this is WRONG:
/*First program in C*/
#include<stdio.h>
Data Types
22
float x;
27
double
long double
float fNet_income;
diameter
float diameter = 5.9 ; 5.9
initial
char initial = ‘A’ ; ‘A’
37
defined constants : You may also associate constant using #define preprocessor
directive
#define N 3000
#define FALSE 0
#define PI 3.14159
38
printf scanf
int %d %d
float %f %f
double %f %lf
char %c %c
string %s %s
42
printf("%d\n”,a); 7
printf("%3d\n”,a); 7
printf("%03d\n”,a); 007
return 0;
}
43