Beruflich Dokumente
Kultur Dokumente
Learning Objectives:
Knowledge: Understand the complete structure of C programs in Turbo C environment Skill: Edit, compile and execute C programs
Introduction
/* 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);
}
C Development Environment
There are 4 phases involved:
Edit
Compile Link Execute
Turbo C Environment
This is Turbo C IDE editing/working screen displayed after application is loaded into the computers memory
Main Menu -it instructs Turbo C to do something as indicated in the list of menu. It can activate or can be used by pressing Alt key and the first letter of the menu. For example, press Alt + F to activate File menu.
2.
3. 4. 5. 6. 7. 8.
9.
Editor Status Line and Edit Windows - it is where you type your program and where you see the current line and column of the text you typed. Message Windows - the message windows are located beneath the middle of edit windows and Hotkeys. It is used to display various compiler or linker messages. Hot Keys - it is located at the bottom of Turbo Cs opening screen. It refers to shortcut or shorthand for selecting menu.
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
Introduction to Computer Programming
C Program Structure
Preprocessor Instruction Global Declaration
Statement
}
Example of C Program
#include <stdio.h>
void main(void) {
Main function
printf(Welcome to ITEC113!);
}
statement
Preprocess Instruction
2 types of preprocess instruction that are normally used:
#include #define
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)
Introduction to Computer Programming
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) { area; float luas; area = = PI 3.141593 luas * 7 * 7;* 7 * 7; printf(Area %.2f:, is %.2f:, area); printf(Luas luas); }
Introduction to Computer Programming
Main Function
Every C program must have a main function, called main() The execution of C program starts from main() function
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 C Programming!); Output:
Welcome to C Programming!
Introduction to Computer Programming
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 */
Introduction to Computer Programming
Example of C Program
/* This program is to print Welcome to C Language Programming! */
#include <stdio.h> void main() { printf(Welcome to\n); printf(C Language); printf(Programming!\n); }
Example of C Program
What will the output be?