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.

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

p'e C Pro2r).A& E-)..)BA#$C) . 23 #y first program 3 converts distance from miles to "ilometers 32 include *stdio.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. JnF%"ms!6 return(D!6 8 .h+ define @#SA.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.