Title: Introduction to C
1Introduction to C
Knowledge Understand the complete structure of C
programs in Linux environment Skill Edit,
compile and execute C programs
2Introduction
C is a High-level Language
Can also be used at lower-level
This flexibility allows it to be used for SYSTEM
PROGRAMMING (eg. Operating systems like UNIX and
WINDOWS)
C has small instruction set, though the actual
implementations include extensive library
functions
- / Task This program calculates BMI /
- include ltstdio.hgt
- define WEIGHT 60.0
- define HEIGHT 1.53
- void main(void)
- float bmi
- bmi WEIGHT / (HEIGHT HEIGHT)
- if (bmi gt 25.0)
- printf(\nYour BMI is .2f. Need to lose
weight! \n, bmi)
myfunction.h
Finally, C programs are highly portable. They can
be executed on different platforms without having
to be recompiled (or with little modification)
C encourages users to write additional library
functions of their own
APPLICATION PROGRAMMING (Billing System, Sistem
Semut ? )
3C Development Environment
- There are 6 phases involved
- Edit
- Preprocess
- Compile
- Link
- Load
- Execute
4Editing C Program in Linux
- Use vi editor
- vi program_name.c
- Use pico editor
- pico program_name.c
- Use emacs editor
- emacs program_name.c
Example pico myprogram.c
5Compiling 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
6Compiling 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 - If succeeded, the output will be generated into
the execution file, namely file_name - To display the output on the screen file_name
Example gcc output myprogram.c
output
7Dont WorryBe Happy
8History 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
9C Program Structure
Preprocessor Instruction
Global Declaration
void main (void)
Local Declaration
Statement
10Example of C Program
include ltstdio.hgt void main(void)
printf(Welcome to UKM!)
11Preprocess 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)
12Preprocess Instruction
- define is used to declare macro constants
-
13Main Function
- Every C program must have a main function, called
main() - The execution of C program starts from main()
function
14Statement
- 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 Welc
ome to UKM
15Comment
- 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 /
16Example of C Program
/ This program is to print Welcome to Fakulti
Teknologi dan Sains Maklumat / include
ltstdio.hgt void main() printf(Welcome
to\n) printf(Fakulti Teknologi
dan) printf(Sains Maklumat\n)
17Example of C Program
What will the output be?
Welcome to Fakulti Teknologi dan Sains Maklumat
18End of Lecture 3