CHAPTER 1 INTRODUCTION TO C Introduction C was developed in the early 1970s at Bell Labs, by Brian Kenninghan and Dennis Ritchie. Ritchie with Ken Thompson had also developed Unix Operating system. Kenninghan and Richie developed C from another language called B, which in turn was derived from language called BPCL ( a system programming language ). Developers of proprietary packaged software are very interested in C because it its considered more transportable than other languages. That is , it is relatively machineindependent. A C program written for one type of computer can be run on another type with little or no modifications. Learning Process of C C language should be learned in the following phases. Phase I : Alphabets, Digits and Special symbols. Phase II: Constants, Variables, and Keywords. PhaseIII : Instructions Phase IV : Program Let us read about them one by one. Phase I : Alphabets , Digits and Special Symbols. Like all other programming languages, C also supports , Alphabets a to z Numeric 0 to 9 Special characters , .( * )etc. White spaces Blank space, horizontal tab, carriage return , new line , form fed, etc. Other characters C can process any of the 256 ASCII characters as data or as literals. Phase II : Constants, Variables, and Keywords Let us read them one by one. Constants C Supports two type of constants. Primary Constants Secondary Constants Let see how each one of them can be represented in C. PRIMARY CONSTANTS They in turn are of 3 types. They are : Character constants Integer Constants Real Constants Character Constants A character constant is one or more character enclosed in single quotes, as in ‘z’ . The rule for writing character constant is given below : A character constant in C must contain one or more characters and must be enclosed in quotation marks with both of them pointing towards left. Integer Constants Integer constants are whole numbers without any fractional part. The method of writing integer constants has been specified in the following rule. An integer constant must have at least one digit and must not contain any decimal point. It may contain either + or-sing. A number with no sign is assumed to be positive. Commas cannot appear in an integer constants. C allows three types of integer constants : Decimal Integer constants ( base 10 ) An integer constant consisting of a sequence of digits is taken to be decimal integer constant unless it beings with 0 (digit zero). For instance , 1234 , 41 , +97 , -17 are decimal integer constants. Octal integer constants ( base 8 ) A sequence of digits starting with 0 ( digit zero ) is taken to be an octal integer. For instance decimal integer 8 will be written as 010 as octal integer. For example, 8 10= 108 and decimal integer 12 will be written as 014 as octal integer, for example 12 10 = 148 . Hexadecimal Integer Constants ( base 16 ) A sequence of digits preceded by 0x or 0X is taken to be an hexadecimal integer. For instance decimal 12 will be written as 0XC as hexadecimal integer. Real Constants They are often called floating constants. Real constants are numbers having fractional parts. These may be written in one of the two forms called fractional form or the exponent form. A real constant in fraction form consists of signed or unsigned digits including a decimal point between digits. The rule for writing a real constant in fractional form is given below : A real constant in fractions form must have at least one digit before a decimal point and at least one digit after the decimal pint. It my also have either + or – sign preceding it. A real constant with no sign is assumed to be positive. The following are valid real constants in fractional form 2.0 , 17.5 , -13.0 , -0.00625 The following are invalid real constants. 7 (No decimal point) 7. (No digit after decimal point) + 17/2 (/-illegal symbol) 17,250.26.2 (Two decimal points) 17,250.262 (comma not allowed) A real constant is exponent form consists of two parts : mantissa nd exponent. For instance 5.8 can be written as 0.58 x 101. = 0.58 E01 where mantissa part is 0.58 ( the part appearing before E) and exponent part is 1 ( the part appearting after E). E01 represents 101 . The rule for writing a real constant in exponent for is given below : A real constant in exponent form has two parts : a mantissa and an exponent The mantissa : must be either an integer or a proper real constant. The mantissa is followed by a letter E or e and the exponent. The exponent must be an integer. The following are the valid real constants in exponent form : 152E05 .1.52E07 , 0.1523E08, 152.0E08 , 152E+8, 1520E04, E-0.172E-3 The following are invalid real constants in exponent form 1. 172.E5 ( At least a digit must follow the decimal point) 2. 1.7E ( No digit specified for exponent) 3. 0.17E2.3 (Exponent cannot have fractional part) 4. 17,225E02 (No comma allowed) 5. .28E-7 (No preceding digits before decimal point) SECONDARY Constants They in turn are of 5 types. They are : Array constants Pointer constants Structure constants Union constants Union constants Enum constants etc. Variables These are the various names given to location in the memory of the computer where different constants are stored. All the various types of constants discussed above can be stored there. Both name and type of constants are unique to that particular location. This means that you cannot have 2 variables with different type of variables. For a variable it is safe to have it of 8 characters through some compilers allow variable names of more than 8. Variable names must begin with an alphabet. No commas or blanks are allowed within a variable name however, only symbol that you can use is the underscore ( _ ). Next are the keywords. C Keywords There are as many as 32 Keywords in C. Given below is the list of reserved words which obviously you cannot use in your programs. These are: Auto char Break Continue Case Do Const Else Default Extem Double Far Enum Goto Float Int For Near If Return Long Signed Register Struct Short Typedef Static Unsigned Switch while Union Void Phase III: Instructions Instruction are nothing but the combinations of the above mentioned constants, variables and keywords. As we know from the definition of programming. Which is: Programming is a set of instructions given to the computers to perform. In C we work under 4 set of instructions. They can be defined as follows: Input / Output Instructions These are used to perform the function of supplying input data to a program and obtaining the output results from it. Arithmetic Instructions These are used to perform arithmetic operations between constants and variables. Type Declaration Instructions These are used to declare the type of variables in C Program. Control Instructions These are used to control the sequence of executions of various statements in C Program. Let us talk about one of them Arithmetic Instructions. Arithmetic Instructions Those of you who have worked in BASIC would remember that a computer instruction has two sides connected in between by a equal to sign ( = ). The left hand side has a variable name and the right hand side has variables name and constants these variables can be connected by arithmetic operators like + - * and / . An arithmetic statement in C could be 3 types. Integer Mode Arithmetic Statement Here all the operands are either integer variables or integer constants. For Example, X=y+2 Real Mode Arithmetic Statement In this case all operands are either real constants or real variables. For example, z=x*y Mixed Mode Arithmetic Statement Here some of the operands are integers and some of the operands are real. For example I = prin * rat * y /100 Operations Hierarchy Since we are dealing here with the various arithmetic operators. We must have the order of their execution. If you remember that we had the following preferences in our maths class which was defined by BODMAS, as explained below: B Braces O Overhead D Divide M Multiply A Addition S Subtraction In the case of C this is slightly different. Here we have the 3 levels of priorities. They are: 1. * Multiplication 2. / Division 3. % Modular Division 4. + Addition 5. - Subtraction 6. = Assignment To show the importance of setting this order of execution, I give here an example. I remember that we in collage use to test the knowledge of our colleagues by the following example, 2+3*4+5 If you use an ordinary calculator to get the result of this.You will get 25. Some guys even gave the answer of 45. But, if you apply the BODMAS method and operate the multiplication first then the answer would be 19, which is the correct answer. I have seen some scientific calculators giving this result.