Você está na página 1de 3

DEPARTMENT OF ELECTRICAL & ELECTRONIC ENGINEERING EEE 111 INTRODUCTION TO COMPUTING

C LANGUAGE ELEMENTS TITLE : C LANGUAGE ELEMENTS OBJECTIVES: Basic elements of C programming language are introduced. Several example programs and exercises are introduced for laboratory practice. INTRODUCTION 1. Preprocessor d rec! "es: The preprocessor directives are commands that give instructions to the C preprocessor whose job is to modify the text of a C program #e$ore it is compiled. A preprocessor directive begins with a number ( ! symbol as its first nonblan" character. The most common directives are % &c'(de and %de$ &e. The C language explicitly defines only a small number of operations. #any actions that are necessary in a computer program are not defined directly by C. $nstead% every C implementation contains collections of useful functions and symbols called ' #r)r es. The A&S$ (American &ational Standards $nstitute! standard C re'uires that certain standard libraries be provided in every A&S$ C implementation. (e may expand the number of operations available by supplying additional libraries. )ach library has a standard header file whose name ends with the symbols .*. The % &c'(de directive gives a program access to a library. This directive causes the preprocessor to insert definitions from a standard header file into a program before compilation. The directive include *stdio.h+ notifies the preprocessor that some names used in the program (such as sc)&$ and pr &!$! are found in the standard header file *stdio.h+. The %de$ &e directive associates a constant name with a meaning value. This directive instructs the preprocessor to replace each occurrence of a constant name by the assigned value before compilation begins. The directive define ,$ -../.0 associates the constant ,$ with the value -../.0. &ote that an executing C program cannot change the value of a name defined as a constant using the define directive.

Comments are text introduced into a program or explanation and documentation purposes. 1or this purpose% we can introduce between 23 and 32 symbols. +. F(&c! o& ,) &: )very C program has a ,) & function. A function body has two parts4 dec')r)! o&s and e-ec(!)#'e statements. The dec')r)! o&s tell the compiler what memory cells are needed in the function. To create this part of the function% the programmer uses the problem data re'uirements identified during problem analysis. The e-ec(!)#'e s!)!e,e&!s (derived form the algorithm! are high5level language statements expressing the statements of an algorithm using the structural rules of the employed programming language. They are translated into machine language and later executed. .. P(&c!()! o& )&d spec )' s/,#o's: Several punctuation symbols are used in the construction of a C program. $n this respect% co,,)s (%! separate items in a list. A semicolon (6! appears at the end of several lines. Braces (7%8! mar" the beginning and the end of the function body. 0. V)r )#'e dec')r)! o& )&d d)!) !/pes: The memory cells used for storing a program9s data and its computational results are called ")r )#'es because the values stored in variables can change as the program executes. The variable declarations in a C program communicate to the C compiler the names of all variables used in a program. They also tell the compiler what "ind of information will be stored in each variable and how that information will be represented in memory. A variable declaration begins with an identifier that tells the C compiler the type of the data stored in a particular variable. C re1( res /o( !o dec')re e"er/ ")r )#'e (sed & ) pro2r),. A d)!) !/pe is a set of values and a set of operations on those values. A standard data type in C is a data type that is predefined such as c*)r3 do(#'e3 and &!. D)!) !/pe int is used to represent integers in C. Because of the finite si:e of a memory cell% not all integers can be represented by type int. $n A&S$ C% the range of data type is from 5-;<0< to =-;<0<. D)!) !/pe double is used to represent real numbers. 1loating5point or the scientific notations can be used represent real numbers. D)!) !/pe char represents an individual character value5 a letter% a digit% or a special symbol. )ach type char value is enclosed within apostrophes (single 'uotes!. 1or example% >A9% 9:9% >?9% >39% >9. Although a type char value in a program re'uires apostrophes% a type char data values should not have them. 1or example% when entering the letter : as a character data item to be read by a program% press the : "ey% instead of the se'uence >:9.

A& E-),p'e C Pro2r), 23 #y first program 3 converts distance from miles to "ilometers 32 include *stdio.h+ define @#SA,)BA#$C) ..0D? 23 conversion constant 32

int main(void! 7 double miles% 23 distance in miles 32 "ms6 23 e'uivalent distance in "ilometers 32 23 get the distance in miles 32 printf(E)nter the distance in milesF!6 scanf(EG.fF%Hmiles!6 23 convert the distance to "ilometers 32 "msI@#SA,)BA#$C) 3 miles6 23 display the distance in "ilometers 32 printf(EThat e'uals Gf "ilometers. JnF%"ms!6 return(D!6 8

Você também pode gostar