preprocessor directives

11
Preprocessor Directives Global Declarations int main(void) { } Local Declarations Executable Statements Structure of a C program

Upload: leland

Post on 04-Jan-2016

18 views

Category:

Documents


0 download

DESCRIPTION

Structure of a C program. Preprocessor Directives. Global Declarations. int main(void) { }. Local Declarations. Executable Statements. Preprocessor Directives. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Preprocessor  Directives

Preprocessor

Directives

Global Declarations

int main(void){

}

Local Declarations

Executable Statements

Structure of a C program

Page 2: Preprocessor  Directives

Preprocessor Directives

Special instructions to the preprocessor that tell it how to prepare your program for compilation. Two common directives are #include and #define.

#include <standard header file>

All preprocessor directives start with a #

Tells the preprocessor that you want the header file in the pointed brackets (< >)included in your program. (e.g. stdio.h)

standard library

#define NAME value

This directive instructs the preprocessor to replace each occurrence of NAME (known as a constant macro)in the text of the C program by the value.

An executing C program cannot changethe value of a name defined as a constantmacro.

Page 3: Preprocessor  Directives

Function Main

int main(void)

{

}

function body

function header

The executable part of your program begins with function main.

body of the function is enclosed in curly brackets

The executable part of your program begins with function main.

int means that the function returns an integer to the operating system

void means no parameters

Page 4: Preprocessor  Directives

comments

/* this is a comment */

The compiler ignores comments when it translates the program into executable code.

Comments begin with “/*” and end with “*/ “

Comments are used for internal program documentation and can appear anywhere in the progrma. They help explain the meaning and logic of the program to the reader.

/* *** /*this is a comment */ *** */

Nested comments (comments inside comments) are invalid.

Second opening token ignored

This token terminates the comment

This will now cause an error

All of your programs should begin with documentation explaining the purpose of the program.

Page 5: Preprocessor  Directives

identifiers

Identifiers allow us to name data and other objects in the program.

Syntax rules for naming identifiers:1) First character must be alphabetic character or underscore.2) Must consist only of alphabetic characters, digits, or underscores.3) First 31 characters of an identifier are significant.4) A C reserved word cannot be used as an

identifier.

Reserved words have special meaning in C and cannot be used for other purposes.

An identifier defined in a C Standard Library (e.g. printf, scanf) also know as standard identifiers, should not be redefined.

autobreakcasecharconstcontinuedefaultdodouble

else enumexternfloatforgotoifintlong

registerreturnshortsignedsizeofstaticstructswitchtypedef

unionunsignedvoidvolatilewhile

Page 6: Preprocessor  Directives

identifiers

The C compiler is case sensitive.The identifiers miles, Miles and MILES are all different identifiers.

Program Style• use lower case for all identifiers except constant macros• use underscores to separate words ( e.g. miles_per_gallon)• good identifier names are descriptive but short (don’t use single letters for identifiers)• don’t use identifiers that can be confused in the same program ( e.g. Miles, miles)

Page 7: Preprocessor  Directives

Data types

A type defines a set of values and a set of operations that canbe applied on those values.

C contains four standard types:

int - sort for integer. An integer is a number without a fractional part ( eg 1, 2, 3 … 456, … 6789,…)

Type also defines the size of the field ( number of bits) in which data can be stored.

There are three different sizes of the integer data type:

short int or just shortintlong int or just long

Each type can be further subdivided into twodifferent subtypes:

signedunsigned

Page 8: Preprocessor  Directives

Type Sign ByteSize

Numberof Bits

MinimumValues

MaximumValues

short intsigned

unsigned2 16

-32,768

0

32,767

65,535

intsigned

unsigned2 16

-32,768

0

32,767

65,535

long intsigned

unsigned4 32

-2,147,483,648

0

2,147,483,648

4,294,967,295

Typical integer sizes for the PC

char - short for character. Holds the value of one ofthe characters in the ASCII (AmericanStandard Code for Information Interchange)alphabet. (e.g. ‘a’, ‘A’, ‘b’,’?’, …) The size of char is machine dependent butnormally is one byte or 8 bits.

Data types

Page 9: Preprocessor  Directives

There are three different sizes of theFloating- point data type:

floatdoublelong double

float - a floating-point type is a number (real number) with a fractionalpart. (e.g. 56.24, .0058 …) which is separated by a decimal point.

Data types

Variable DeclarationsThe memory cells used for storing a program’s input data and its computational results are called variables because the values stored in variables can change as theprogram executes.When we create variables, the declarationgives them a symbolic name and reserves memory for them. Each variable in your program must be delcared (defined).

Syntax:int variable_list;double variable_list;char variable_list;Type of

data to be stored in this variable

Variable name

All C statements end with a semicolon

Page 10: Preprocessor  Directives

Variable Declarations

Examples:double miles;float pay_rate;int class_size;long int national_debt;double gpa, accum;char class grade;

Variable Initialization

We can initialize a variable at the same time that we declare it by placing an equal sign and a constant after the variable name. This establishes the first value that the variable will contain.

Examples:double miles = 0.0;float pay_rate = 4.5;int count = 0;char grade = ‘A’;

Syntax:type variable_name = constant;

Page 11: Preprocessor  Directives

Constants

Constants are data values that cannot be changed during the execution of your program. A literal is an unnamed constant used to specify data. If we know the data cannot be changed, then we simply code the data value itself in a statement. C constants can be of any of the vasic data types. The way each constant is represented depends upon its type

Examples:123, -123, +123 /*examples of integers*/0., .005, 8769.098 /*examples of floating-point*/‘a’, ‘C’, ‘&’ /*examples of characters*/

Character constants are always enclosed in single quotes.Numeric constants in C are considered non-negative numbersand cannot contain commas.