Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Computer Science Department FTSM Introduction to C Knowledge: Understand the complete structure of C programs in Linux environment Skill: Edit, compile."— Presentation transcript:

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

2 TK1913-C Programming2 TK1913-C Programming 2 Introduction /* Task: This program calculates BMI */ #include #define WEIGHT60.0 #define HEIGHT1.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); } Resembles Algebraic Expression Augmented by certain English keywords eg. if, else, while C is a High-level LanguageCan also be used at lower-level This flexibility allows it to be used for SYSTEM PROGRAMMING (eg. Operating systems like UNIX and WINDOWS) APPLICATION PROGRAMMING (Billing System, Sistem Semut ? ) C has small instruction set, though the actual implementations include extensive library functions “myfunction.h” C encourages users to write additional library functions of their own Finally, C programs are highly portable. They can be executed on different platforms without having to be recompiled (or with little modification)

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

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

5 TK1913-C Programming5 TK1913-C Programming 5 Compiling C Program in Linux $gcc program_name.c 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 $a.out To display the output on the screen: $a.out $gcc myprogram.c Example:$gcc myprogram.c $a.out $a.out $gcc myprogram.c Example:$gcc myprogram.c $a.out $a.out

6 TK1913-C Programming6 TK1913-C Programming 6 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 $file_name To display the output on the screen: $file_name Compiling C Program in Linux $gcc output myprogram.c Example:$gcc output myprogram.c $output $output $gcc output myprogram.c Example:$gcc output myprogram.c $output $output This is only an example of execution file name. You can give any name that you like.

7 TK1913-C Programming7 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!

8 TK1913-C Programming8 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 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

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

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

11 TK1913-C Programming11 TK1913-C Programming 11 Preprocess Instruction 2 types of preprocess instruction that are normally used: #include #include #define #define #include stdio.h printf #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)

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

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

14 TK1913-C Programming14 TK1913-C Programming 14 Statement ‘Sentence-like’ action steps that are written in the body of the function ; In C, all statements must be ended with ; symbol printf() Example: A statement to display a string of characters on the screen by using printf() function printf(“Welcome to UKM”); Output: Welcome to UKM printf() Example: A statement to display a string of characters on the screen by using printf() function printf(“Welcome to UKM”); Output: Welcome to UKM

15 TK1913-C Programming15 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 */

16 TK1913-C Programming16 TK1913-C Programming 16 Example of C Program /* This program is to print Welcome to Fakulti Teknologi dan Sains Maklumat */ #include void main() { printf(“Welcome to\n”); printf(“Fakulti Teknologi dan“); printf(“Sains Maklumat\n”); }

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

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


Download ppt "Computer Science Department FTSM Introduction to C Knowledge: Understand the complete structure of C programs in Linux environment Skill: Edit, compile."

Similar presentations


Ads by Google