+ All Categories
Home > Documents > Principles of Programming CSEB134 : BS/20081 33 CHAPTER Fundamentals of the C Programming Language.

Principles of Programming CSEB134 : BS/20081 33 CHAPTER Fundamentals of the C Programming Language.

Date post: 19-Jan-2018
Category:
Upload: thomasine-townsend
View: 237 times
Download: 0 times
Share this document with a friend
Description:
Principles of Programming CSEB134 : BS/20083 The system software necessary to develop C application program are bundled into an integrated development environment (IDE) A typical IDE contains text editor, C compiler, preprocessor, libraries, other tools The C programming environment has 2 components: Language – features to carry out basic operations (e.g.: store data in variables, compare 2 data values, perform arithmetic operation, etc.) Libraries – routines to carry out operations not part of the language (Standard libraries – e.g.: #include & programmer-defined libraries) C Program Development Environment
23
CSEB134 : BS/2008 1 nciples of Programming 3 3 C H A P T E R Fundamental s of the C Programming Language
Transcript
Page 1: Principles of Programming CSEB134 : BS/20081 33 CHAPTER Fundamentals of the C Programming Language.

CSEB134 : BS/2008 1

Principles of Programming

33CH

APT

ER

Fundamentals of the C

Programming Language

Page 2: Principles of Programming CSEB134 : BS/20081 33 CHAPTER Fundamentals of the C Programming Language.

CSEB134 : BS/2008 2

Principles of Programming

Objectives C Program development environment Elements of the C language

Program comments Preprocessor directives Functions Executable Statements

General form of a C program Common Programming Errors: logic/design error,

syntax error, run-time error

Page 3: Principles of Programming CSEB134 : BS/20081 33 CHAPTER Fundamentals of the C Programming Language.

CSEB134 : BS/2008 3

Principles of Programming

The system software necessary to develop C application program are bundled into an integrated development environment (IDE)

A typical IDE contains text editor, C compiler, preprocessor, libraries, other tools

The C programming environment has 2 components: Language – features to carry out basic operations (e.g.:

store data in variables, compare 2 data values, perform arithmetic operation, etc.)

Libraries – routines to carry out operations not part of the language (Standard libraries – e.g.: #include <stdio.h> & programmer-defined libraries)

C Program Development Environment

Page 4: Principles of Programming CSEB134 : BS/20081 33 CHAPTER Fundamentals of the C Programming Language.

CSEB134 : BS/2008 4

Principles of Programming

IDE – Microsoft Visual C++

Page 5: Principles of Programming CSEB134 : BS/20081 33 CHAPTER Fundamentals of the C Programming Language.

CSEB134 : BS/2008 5

Principles of Programming

Prepare

Compile

Link

Execute

Steps:

These steps depend on the computer, the OS, and the C IDE that you will use.

Page 6: Principles of Programming CSEB134 : BS/20081 33 CHAPTER Fundamentals of the C Programming Language.

CSEB134 : BS/2008 6

Principles of Programming

1. Prepare program (and data files) Use a text editor to type your source program statements. Assign a name to your source program file (.C) and save

the file into disk storage. (optional) Use the text editor to create a data files. Assign

names to the data files (.txt or .dat) and save the files2. Compile

Perform by a compiler. C compiler has 2 separate programs: the preprocessor

and the translator. Preprocessor reads the source code and prepares the

code for the translator. Translator translates the code into machine language.

Page 7: Principles of Programming CSEB134 : BS/20081 33 CHAPTER Fundamentals of the C Programming Language.

CSEB134 : BS/2008 7

Principles of Programming

3. Link (“Build”) As we will see later, a C program is made up of

many functions. The linker assembles all the functions into final

executable program (creates exe file).

4. Execute If successful, it is ready for execution and get the

output otherwise debug the program

Page 8: Principles of Programming CSEB134 : BS/20081 33 CHAPTER Fundamentals of the C Programming Language.

CSEB134 : BS/2008 8

Principles of Programming

Output:

A Simple C Program Example

Page 9: Principles of Programming CSEB134 : BS/20081 33 CHAPTER Fundamentals of the C Programming Language.

CSEB134 : BS/2008 9

Principles of Programming

Elements of the C language/* Example Case: ApplesAuthor: Mdm Badariah Solemon*/#include <stdio.h>int main(){

int Qty;double Cost=0.0, Total=0.0;

printf ("Enter quantity of apples purchased (in Kg):");scanf("%d", &Qty);

printf ("Enter the cost per Kg of apples (in RM per Kg):");scanf("%lf",&Cost);

Total = Cost * Qty;

printf("\nTotal cost of apples = %.2f\n",Total);

return 0;}

preprocessor directive

variable

comment

special symbol

reserved word

punctuation

standard header file

statement

Page 10: Principles of Programming CSEB134 : BS/20081 33 CHAPTER Fundamentals of the C Programming Language.

CSEB134 : BS/2008 10

Principles of Programming

Program Comments Statement in a program intended for

documentation and clarification purposes Have no effect on program execution. Comments are inserted into the code

using /* and */ to surround comment or // to begin comment lines./* This is a comment */// This is known as comment line /* The comments can span into more than one lines */

Page 11: Principles of Programming CSEB134 : BS/20081 33 CHAPTER Fundamentals of the C Programming Language.

CSEB134 : BS/2008 11

Principles of Programming

Preprocessor directives The C program line that begins with # provides an

instruction to the C preprocessor It is executed before the actual compilation is done. Two most common directives :

#include #define

In our example (#include<stdio.h>) identifies the header file for standard input and output needed by the printf().

Page 12: Principles of Programming CSEB134 : BS/20081 33 CHAPTER Fundamentals of the C Programming Language.

CSEB134 : BS/2008 12

Principles of Programming

Functions Every C program has a function main The function main usually calls input/output

functions – printf(), scanf(), etc. These input/output functions are discussed in Chapter 4.

The word main is a C reserved word. We must not use it for declaring any other variable or constant.

4 common ways of main declaration:

int main(void){ return 0;}

void main(void){

}

main(void){

}

main( ){

}

Page 13: Principles of Programming CSEB134 : BS/20081 33 CHAPTER Fundamentals of the C Programming Language.

CSEB134 : BS/2008 13

Principles of Programming

The curly braces { } identify a segment / body of a program The start and end of a function The start and end of the selection or repetition

block. Since the opening brace { indicates the start

of a segment with the closing brace indicating the end of a segment, there must be just as many opening braces as closing braces } (this is a common mistake of beginners)

Page 14: Principles of Programming CSEB134 : BS/20081 33 CHAPTER Fundamentals of the C Programming Language.

CSEB134 : BS/2008 14

Principles of Programming

Statements A specification of an action to be taken by the

computer as the program executes. Each statement in C needs to be terminated with

semicolon (;) Example:

#include <stdio.h>void main(void){printf(“I love programming\n”);printf(“You will love it too once ”);printf(“you know the trick\n”);

}

Page 15: Principles of Programming CSEB134 : BS/20081 33 CHAPTER Fundamentals of the C Programming Language.

CSEB134 : BS/2008 15

Principles of Programming

Statement has two parts : Declaration

The part of the program that tells the compiler the names of memory cells in a program

Executable statements Program lines (excluding comments) that are

converted to machine language instructions and executed by the computer.

A list of statements may be enclosed in braces { } (known as compound statement)

int age, total=0.0;

Page 16: Principles of Programming CSEB134 : BS/20081 33 CHAPTER Fundamentals of the C Programming Language.

CSEB134 : BS/2008 16

Principles of Programming

General form of a C programpreprocessor directivesmain function heading{declarationsexecutable statements}

#include <stdio.h>

void main(void){

// statement(s);

}

Page 17: Principles of Programming CSEB134 : BS/20081 33 CHAPTER Fundamentals of the C Programming Language.

CSEB134 : BS/2008 17

Principles of Programming

C statements can extend over more than one line. Example:printf (“\n Your coins are worth %d dollars

and %d cents.\n”, dollar, change);

You can write more than one statement on a line (but not recommended). Example:printf(“Enter your name:”); scanf(“%”,&Name);

Page 18: Principles of Programming CSEB134 : BS/20081 33 CHAPTER Fundamentals of the C Programming Language.

CSEB134 : BS/2008 18

Principles of Programming

Common Programming Errors Debugging - Process removing errors from

a program Three (3) kinds of errors :

1. Syntax Error a violation of the C grammar rules, detected during

program translation (compilation). statement cannot be translated and program cannot

be executed

Page 19: Principles of Programming CSEB134 : BS/20081 33 CHAPTER Fundamentals of the C Programming Language.

CSEB134 : BS/2008 19

Principles of Programming

Page 20: Principles of Programming CSEB134 : BS/20081 33 CHAPTER Fundamentals of the C Programming Language.

CSEB134 : BS/2008 20

Principles of Programming

2. Run-time errors An attempt to perform an invalid operation, detected

during program execution. Occurs when the program directs the computer to

perform an illegal operation, such as dividing a number by zero.

The computer will stop executing the program, and displays a diagnostic message indicates the line where the error was detected **

** depends on IDE – Ms Visual Studio: warning

Page 21: Principles of Programming CSEB134 : BS/20081 33 CHAPTER Fundamentals of the C Programming Language.

CSEB134 : BS/2008 21

Principles of Programming

Page 22: Principles of Programming CSEB134 : BS/20081 33 CHAPTER Fundamentals of the C Programming Language.

CSEB134 : BS/2008 22

Principles of Programming

3. Logic Error/Design Error An error caused by following an incorrect algorithm Very difficult to detect - it does not cause run-time

error and does not display message errors. The only sign of logic error – incorrect program

output Can be detected by testing the program thoroughly,

comparing its output to calculated results To prevent – carefully desk checking the algorithm

and written program before you actually type it

Page 23: Principles of Programming CSEB134 : BS/20081 33 CHAPTER Fundamentals of the C Programming Language.

CSEB134 : BS/2008 23

Principles of Programming

Summary You learned about:

C Program development environment Elements of the C language

Program comments Preprocessor directives Functions Executable Statements

General form of a C program Common Programming Errors: syntax error, run-time error,

logic/design error.


Recommended