Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science Department

Similar presentations


Presentation on theme: "Computer Science Department"— Presentation transcript:

1 Computer Science Department
Introduction to C Knowledge: Understand the complete structure of C programs in Linux environment Skill: Edit, compile and execute C programs FTSM Computer Science Department

2 Introduction Can also be used at lower-level
C is a High-level Language Introduction 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 <stdio.h> #define WEIGHT 60.0 #define HEIGHT 1.53 void main(void) { float bmi; bmi = WEIGHT / (HEIGHT * HEIGHT); if (bmi > 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) Resembles Algebraic Expression C encourages users to write additional library functions of their own APPLICATION PROGRAMMING (Billing System, Sistem Semut ? ) Augmented by certain English keywords eg. if , else, while TK1913-C Programming

3 C Development Environment
There are 6 phases involved: Edit Preprocess Compile Link Load Execute TK1913-C Programming

4 Editing 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 This is only an example of program name. You can give any name that you like. Example: $pico myprogram.c TK1913-C Programming

5 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

6 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 If succeeded, the output will be generated into the execution file, namely file_name To display the output on the screen: $file_name This is only an example of execution file name. You can give any name that you like. Example: $gcc output myprogram.c $output TK1913-C Programming

7 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

8 History of C Language C was originated from 2 programming languages, namely BCPL and B BCPL was developed by Martin Richards in year 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 It replaced B as the language to develop UNIX operating system at Bell Laboratories TK1913-C Programming

9 C Program Structure Preprocessor Instruction Global Declaration
void main (void) { } Local Declaration Statement TK1913-C Programming

10 Example of C Program Main function program statement
Preprocessor instruction #include <stdio.h> void main(void) { printf(“Welcome to UKM!”); } statement TK1913-C Programming

11 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

12 Preprocess Instruction
#define is used to declare macro constants Example: #include <stdio.h> #define PI void main(void) { float luas; luas = * 7 * 7; printf(“Luas %.2f:”, luas); } After preprocess Macro constant Before preprocess Example: #include <stdio.h> #define PI void main(void) { float luas; luas = PI * 7 * 7; printf(“Luas %.2f:”, luas); } TK1913-C Programming

13 Main Function Every C program must have a main function, called main()
The execution of C program starts from main() function TK1913-C Programming

14 printf(“Welcome to UKM”);
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

15 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

16 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

17 Example of C Program What will the output be? Welcome to
Fakulti Teknologi dan Sains Maklumat TK1913-C Programming

18 End of Lecture 3 Yes !! That’s all? What’s next???
VARIABLES AND CONSTANTS on the way … TK1913-C Programming


Download ppt "Computer Science Department"

Similar presentations


Ads by Google