chapter 2 structure of c language

24
CHAPTER 2 STRUCTURE OF C LANGUAGE

Upload: jay-leong

Post on 20-Jan-2015

200 views

Category:

Education


3 download

DESCRIPTION

CHAPTER 2, COMPUTER PROGRAMMING (DAE 20102)

TRANSCRIPT

Page 1: Chapter 2 structure of c language

CHAPTER 2

STRUCTURE OF C LANGUAGE

Page 2: Chapter 2 structure of c language

Basic Structure of C Program• The C programming language is a

popular and widely used programming language for creating computer programs.

• Benefits you gain from learning C:

1) be able to read and write code for a large number of platforms

2) The jump to the object oriented C++ language becomes much easier.

Page 3: Chapter 2 structure of c language

Basic Structure of C Program• C is what is called a compiled language • This means that once you write your C

program, you must run it through a C compiler

• The C program is the human-readable form, while the executable that comes out of the compiler is the machine-readable

• What this means is that to write and run a C program, you must have access to a C compiler

Page 4: Chapter 2 structure of c language

Basic Structure of C Program

Page 5: Chapter 2 structure of c language

Basic Structure of C Program

• A C program contains these following basic structures:

1) Comments

2) The #include Directive (C-library)

3) The main() function

4) Variables and Data type

5) C statement

6) Function Body

Page 6: Chapter 2 structure of c language

Basic Structure of C Program

/*This is a basic structure C program*/#include <stdio.h>void main (){

int miles = 26;int yards;float kilometers;

kilometers = 1.609 *(miles+yards/1760.0);printf(“\n A marathon is %f kms”,kilometers);

}

1

23

4

5

Page 7: Chapter 2 structure of c language

Basic Structure of C Program

Page 8: Chapter 2 structure of c language

Basic Structure of C Program

1.Comments• To document programs & improve

readability• Optional & non-executable statement• Symbols /* and */ or // indicate the

beginning and the end of the comment block

Page 9: Chapter 2 structure of c language

Basic Structure of C Program

/*A comment may be coded like this*///A comment may be coded like this

Note:

1. There is no space between (*) and slash (/)2. Every /* (begin) must be matched with */ (end)

Page 10: Chapter 2 structure of c language

Basic Structure of C Program

2.The #include Directive• Lines beginning with a pound sign (#) are

directives for the preprocessor • The preprocessor is a utility program that

performs various modifications to the source program

• A header file contains the source code that allows the execution of the program

• Standard input/output header file replaces the directive in the source code

Page 11: Chapter 2 structure of c language

Basic Structure of C Program

• The stdio.h header file enables the program to perform basic input and output operations

• Allows the program to accept input from the keyboard and display output on the screen

Page 12: Chapter 2 structure of c language

Basic Structure of C Program

#include <stdio.h>

Header File

Note:

1. Lines that start with the # sign are preprocessor directives

2. The angled-bracket pair <> indicates that the specified library file is the standard library directory

Page 13: Chapter 2 structure of c language

Basic Structure of C Program

3. The main() function• The main function is the point by where all

C programs start their execution• The instructions contained within this

function's definition will always be the first ones to be executed in any C program

• The word main is followed in the code by a pair of parentheses (())

• These parentheses may enclose a list of parameters within them

Page 14: Chapter 2 structure of c language

Basic Structure of C Program

• Right after these parentheses we can find the body of the main function enclosed in braces

{ - Left Braces

} – Right Braces

–What is contained within these braces is what the function does when it is executed

Page 15: Chapter 2 structure of c language

Basic Structure of C Program

Format: data type function_name (parameters) {

variable;C statement;

}

void main (){ Statement; Statement; …………..; …………..;}

Body

Page 16: Chapter 2 structure of c language

Basic Structure of C Program

4. Variables and Identifiers• If your program requests a value from the

user, or if it calculates a value, you will want to remember it somewhere so you can use it later. The way your program remembers things is by using variables. For example;

int b; • A variable has a name (in this case, b) and

a type (in this case, int, an integer).

variableIdentifier

Page 17: Chapter 2 structure of c language

Basic Structure of C Program

• Each variable needs an identifier that distinguishes it from the others

• A valid identifier is a sequence of one or more letters, digits or underline characters _

• Variable identifiers always have to begin with a letter

• They can also begin with an underline character _ , but this is usually reserved for compiler specific keywords or external identifiers

• Identifiers cannot match with any reserve keywords

Page 18: Chapter 2 structure of c language

Basic Structure of C Program

Very important:

The C language is a "case sensitive" language. That means that an identifier written in capital letters is not equivalent to another one with the same name but written in small letters.

Page 19: Chapter 2 structure of c language

Basic Structure of C Program

5. Data Types• The basic fundamental data types in C

Page 20: Chapter 2 structure of c language

Basic Structure of C Program

6. Reserved Keywords• All reserved words appear in

lowercase• What is the difference between

reserved words and standard identifiers?

• The standard reserved keywords are;

Page 21: Chapter 2 structure of c language

Basic Structure of C Program

Keywords

auto double int struct

break else long switch

case enum register typedef

char extern return union

const float short unsigned

continue for signed void

default goto sizeof volatile

do if static while

Page 22: Chapter 2 structure of c language

Basic Structure of C Program

7) ConstantsNumeric Constant• Two types of constants; numeric constant and

symbolic constant• Numeric constant- actual value assigned to

numeric variable, include integer and floating-point values

• Integer constant- consists of digits and a unary sign (+ or -)

• Floating point constant- Is a real number, a numeric value with a decimal point

Page 23: Chapter 2 structure of c language

Basic Structure of C Program

Symbolic Constant• Purpose is to define symbolic constant• Symbolic constants are coded in

uppercase letters and retains its value during the program run

• Normally placed at the beginning of the program

• e.g: #define VALUE 100

Page 24: Chapter 2 structure of c language

Basic Structure of C Program

Non-numeric constants• Character and string• Character constant-is a single character

assigned to a character variable• String constant- is a group of characters

assigned to a string variable• Character constant = ‘R’ ‘n’ ‘$’• String constant = “October 5th”