180 likes | 193 Views
FTSM. Introduction to C. Knowledge: Understand the complete structure of C programs in Linux environment Skill: Edit, compile and execute C programs. Resembles Algebraic Expression. Augmented by certain English keywords eg. if , else, while. Can also be used at lower-level.
E N D
FTSM Introduction to C Knowledge: Understand the complete structure of C programs in Linux environment Skill: Edit, compile and execute C programs Computer Science Department
Resembles Algebraic Expression Augmented by certain English keywords eg. if , else, while 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) C encourages users to write additional library functions of their own APPLICATION PROGRAMMING (Billing System, Sistem Semut ? ) TK1913-C Programming2
C Development Environment There are 6 phases involved: • Edit • Preprocess • Compile • Link • Load • Execute TK1913-C Programming3
This is only an example of program name. You can give any name that you like. 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 Example:$pico myprogram.c TK1913-C Programming4
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 Programming5
This is only an example of execution file name. You can give any name that you like. 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 Example:$gcc output myprogram.c $output TK1913-C Programming6
Got confused?? Don’t worry, you’ll acquire this skill during the lab session. Ensure you know your lab group and schedule…good luck! Don’t Worry…Be Happy… TK1913-C Programming7
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 Programming8
C Program Structure Preprocessor Instruction Global Declaration void main (void) { } Local Declaration Statement TK1913-C Programming9
Main function program statement Preprocessor instruction Example of C Program #include <stdio.h> void main(void) { printf(“Welcome to UKM!”); } TK1913-C Programming10
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 printffunction) TK1913-C Programming11
Before preprocess After preprocess Example: Example: #include <stdio.h> #define PI 3.141593 void main(void) { float luas; luas = PI * 7 * 7; printf(“Luas %.2f:”, luas); } #include <stdio.h> #define PI 3.141593 void main(void) { float luas; luas = 3.141593 * 7 * 7; printf(“Luas %.2f:”, luas); } Macro constant Preprocess Instruction • #define is used to declare macro constants TK1913-C Programming12
Main Function • Every C program must have a main function, called main() • The execution of C program starts from main()function TK1913-C Programming13
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 Programming14
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 Programming15
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 Programming16
Example of C Program What will the output be? Welcome to Fakulti Teknologi dan Sains Maklumat TK1913-C Programming17
Yes !! That’s all? What’s next??? VARIABLES AND CONSTANTS on the way … End of Lecture 3 TK1913-C Programming18