Sie sind auf Seite 1von 18

Introduction to C Language

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);
}

Introduction to Computer Programming

C Development Environment
There are 4 phases involved:
Edit
Compile Link Execute

Introduction to Computer Programming

Turbo C Environment

This is Turbo C IDE editing/working screen displayed after application is loaded into the computers memory

Introduction to Computer Programming

Four Parts of Turbo C Environment


Main Menu Editor status line and edit windows Compiler message windows Hot Key quick reference line

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.

Introduction to Computer Programming

Basic menus of Turbo C


File used to load and save files, handles directories, invokes DOS and exits Turbo C as discussed in the submenu under File. Run used to compile (check errors), links and runs the program currently loaded in the environment. Compile used to compile the program currently in the environment.

Introduction to Computer Programming

Basic submenu of File


1. Load enables the user to select a file to be opened or loaded into the editor. Pick enables the user to select a file based on the last nine files previously opened or edited. New lets the user edit a new file or start programs. Save - store, the file currently in the editor. Write to enables the user to save a file using a different filename. Directory displays the content of the current working directory Change dir enables the user to specify the defined path to change the default path or directory. OS Shell loads the DOS command processor and lets the user execute DOS commands. Quit lets the user to exit or quit Turbo C

2.
3. 4. 5. 6. 7. 8.

9.

Introduction to Computer Programming

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.

Introduction to Computer Programming

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

void main (void) { Local Declaration

Statement
}

Introduction to Computer Programming

Example of C Program
#include <stdio.h>
void main(void) {

Main function

printf(Welcome to ITEC113!);
}

program Preprocessor instruction


Introduction to Computer Programming

statement

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)
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

Introduction to Computer Programming

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); }

Introduction to Computer Programming

Example of C Program
What will the output be?

Welcome to C Language Programming!

Introduction to Computer Programming

Das könnte Ihnen auch gefallen