APA ITU KOMPUTER

Download Report

Transcript APA ITU KOMPUTER

Introduction to C
Knowledge:
Understand the complete structure of C programs in Linux
environment
Skill:
Edit, compile and execute C programs
Computer Science Department
FTSM
Introduction
Can also be used at lower-level C is a High-level Language
This
it to be
C flexibility
has small allows
instruction
set,
forthough
SYSTEM
/* Task: This program used
calculates
BMI */the actual
PROGRAMMING
(eg.
#include <stdio.h>
“myfunction.h”
Resembles Algebraic
implementations
include
#define WEIGHT
60.0
systems
like
Finally, Operating
C
programs
are
highly
Expression
extensive
library
functions
#define HEIGHT
1.53
UNIX
and WINDOWS)
portable.
They{ can be executed on different
void main(void)
C encourages
users
float bmi; platforms without having
to be
to write additional
recompiled (or with little modification)
APPLICATION
bmi = WEIGHT
/ (HEIGHT * HEIGHT);
library
PROGRAMMING functions of their own
if (bmi > 25.0)
(Billing System,
printf(“\nYour BMI is %.2f. Need to lose weight! \n”, bmi);
Sistem Semut Augmented
?)
by certain
}
English keywords
TK1913-C Programming
eg. if , else, while
2
C Development Environment
There are 6 phases involved:
 Edit
Preprocess
 Compile
 Link
 Load
 Execute

TK1913-C Programming
3
Editing C Program in Linux

Use vi editor
$vi program_name.c

Use pico editor
This is only an example of
$pico program_name.c

Use emacs editor
program name. You can give
any name that you like.
$emacs program_name.c
Example: $pico myprogram.c
TK1913-C Programming
4
Compiling C Program in Linux

To compile C GNU: $gcc program_name.c

An error message will be displayed if there is
any syntax error Needs to be rectified until
there is no more errors

If succeeded, the output will be generated into
the execution file, namely a.out

To display the output on the screen: $a.out
Example: $gcc myprogram.c
$a.out
TK1913-C Programming
5
Compiling C Program in Linux


Another method to compile C GNU:
$gcc –o file_name program_name.c
An error message will be displayed if there is any
syntax error Needs to be rectified until there is
no more errors
This is only an example of

execution
filegenerated
name. You into
can give
If succeeded, the output
will be
any name that you like.
the execution file, namely
file_name

To display the output on the screen: $file_name
Example: $gcc output myprogram.c
$output
TK1913-C Programming
6
Don’t Worry…Be Happy…
Got confused?? Don’t
worry, you’ll acquire this
skill during the lab
session. Ensure you know
your lab group and
schedule…good luck!
TK1913-C Programming
7
History of C Language




C was originated from 2 programming languages, namely
BCPL and B
BCPL was developed by Martin Richards in year 1967. It
was intended as a language to develop operating systems
and compilers
B was developed by Ken Thompson in year 1970s. It was
used to develop UNIX operating system at Bell
Laboratories
C was developed by Dennis Ritchie in year 1972. It
replaced B as the language to develop UNIX operating
system at Bell Laboratories
TK1913-C Programming
8
C Program Structure
Preprocessor
Instruction
Global Declaration
void main (void)
{
Local Declaration
Statement
}
TK1913-C Programming
9
Example of C Program
#include <stdio.h>
Main function
void main(void) {
printf(“Welcome to UKM!”);
}
program
Preprocessor instruction
TK1913-C Programming
statement
10
Preprocess Instruction

2 types of preprocess instruction that are
normally used:



#include
#define
#include is used to include certain files into the
program. Those files need to be included
because they contain the necessary information
for compilation (e.g. stdio.h file contains
information about printf function)
TK1913-C Programming
11
Preprocess Instruction
#define is used to declare macro constants
After preprocess
Before
preprocess
Example:

#include <stdio.h>
#define PI 3.141593
Macro constant
void main(void) {
float luas;
luas = PI
3.141593
* 7 * 7;* 7 * 7;
printf(“Luas %.2f:”, luas);
}
TK1913-C Programming
12
Main Function

Every C program must have a main
function, called main()

The execution of C program starts from
main() function
TK1913-C Programming
13
Statement


‘Sentence-like’ action steps that are written in the
body of the function
In C, all statements must be ended with ; symbol
Example: A statement to display a string of
characters on the screen by using printf() function
printf(“Welcome to UKM”);
Output:
Welcome to UKM
TK1913-C Programming
14
Comment

You could include comments in your program to ease
understanding

Comments will be ignored during compilation

A block of comment is labeled with /* (start) and */ (end) 
compiler will ignore any text written after /* symbol till */
symbol is found

Nested comments (comment within comment) are not
allowed, for example:
/* my comment /* subcomment*/ my comment continues */
TK1913-C Programming
15
Example of C Program
/* This program is to print Welcome to Fakulti
Teknologi dan Sains Maklumat */
#include <stdio.h>
void main() {
printf(“Welcome to\n”);
printf(“Fakulti Teknologi dan“);
printf(“Sains Maklumat\n”);
}
TK1913-C Programming
16
Example of C Program
What will the output be?
Welcome to
Fakulti Teknologi dan Sains Maklumat
TK1913-C Programming
17
End of Lecture 3
Yes !! That’s all?
What’s next???
VARIABLES AND
CONSTANTS on the way …
TK1913-C Programming
18