Ebooksjk.files.wordpress.com



EBOOKSJKProgramming in CHistory of C language:-C is a Middle-level language and is used to develop both application programs as well as system programs. It was developed in 1972 by DENNIS RITCHIE at AT&T Bell laboratories. The C language is derived from the B language, which was written by Ken Thompson at AT&T Bell laboratories. The B language was adopted from a language called BCPL (basic combined programming language), which was derived by Martin Richards at Cambridge university.Initially C was designed for programming in the operating system called Unix. These days the Unix operating system and the tools supplied with it including the C compiler itself are written in CStructure of C program:- C language is a procedural language and therefore consists of one or more functions. The general structure of C language is as given below:-CommentsPre-processor directivesGlobal variablesMain() function{ Local variables Statements..................................}Func1(){ Local variables Statements ................. ...............}Func2(){ Local variables Statements ................. ...............}Explanation :Comments:- Comments are placed between /* */ and are used for documentation purpose. The comments are not the part of the program and are therefore ignored by the compiler i.e. the compiler do not compile the comment.Pre-processor Directive:- Pre-processor Directive are processed through pre-processor before the C source code passes through the compiler. The commonly used pre-processor directives are #include and #directive. #include is used to include the header files and #define is used to define symbolic constants and macros.Main() function:- The execution of C program starts from the main() function. This function contains the declaration of local variables and statements.Func1, func2....funcN :- These functions are user defined and contains the local variables and pilation and execution of C program:- The various stages in compilation and execution of a C program are as under;-Text EditorHeader filesC source codeC pre-processorExpanded source codeC compilerIf any error?Object codeLibrary filesLinkerExecutable codeLoaderExplanation: - The various phases of compilation and execution of C program are explained as under:-First of all the source code of the program is written in any text editor (eg.,turbo C) and is saved with .c extension.This code is first of all passed to the pre-processor. The pre-processor expands this source code and passes it to the compiler.The compiler then checks this code for the errors and if the code is found error free then it is converted into machine’s assembly language code.The system assembler then converts this assembly code into object code. Then name of the object code is same as that of the source code but it has the extension .obj.After that, the Linker combines the object code of the various library functions with the object code of our program. The output of the linker is an executable file with extension .exe. This executable file then runs and gives the result as per the specifications.Character set of C:Character set is the set of all characters which are used to represent the information in C language. The character set of C language includes:-Alphabets ------------------------ a to z, A to ZDigits----------------------------- 0 to 9Special symbols-----------------, ; : ‘ ‘ “ ” [ ] { } ! @ # $ % ^ & * ( ) _- + = <> ? / \ Tokens:- The smallest identifiable unit in C language is known as token. The various tokens in the C language are as under:IdentifiersLiteralsKeywordsPunctuatorsOperatorsIdentifiers :- These are the names given to the various programming constructs like variable, constant, function, array, structure, union etc. Naming convention for identifiers are as under:-The identifier name must begin with an alphabet(capital or small) or underscore(_)It can contain digits, alphabets and underscore (_) only. Even space is not allowed.The maximum length of an identifier varies. Some compilers recognize only first 8 letters of identifiers while other may recognize upto 32 character. ANSI standard recognize 31 characters.Keywords cannot be used as an identifier.C language is case sensitive i.e. uppercase letters and lowercase letters are considered different. For eg., CODE and code are different in C language. Some valid identifiers are ---- value, Value, Ajay_lakhnotra, x1,X1Some invalid identifiers are----- 1value, Ajay lakhnotra, x#,Literals:- These are the fixed values that do not changes during the program execution. There are three types of literals. These are as under:-Literals Numeric literals character literalsString literalsInteger literals Real Literals Decimal Octal HexadecimalNumeric literals:- Numeric literals consists of numeric digits. They may or may not have a decimal point..There are the following two types of numeric literals :-Integer literalsReal literalsInteger literal:- These are the whole numbers with no decimal points. These are further classified into the following three types:-Decimal literals ;- These are the integer literals with base 10. These include the digits – 0,1,2,3,4,5,6,7,8,9Octal literals:- These are the integer literals with base 8.These include the digits-0,1,2,3,4,5,6,7Octal literals starts with the digit 0.Eg., 012,023,056,067 etcHexadecimal literals :-These are the integer literals with base 16These include the digits-0,1,2,3,4,5,6,7,8,9,A(10),B(11),C(12),D(13),E(14),F(15)Hexadecimal literals starts with the digit 0x or 0X.Eg., 0x12,0x34,0x56. Etc.Rules for defining Integer literals:-Integer literals should have at least one digit.It must not have a decimal point.No comma or space is allowed within the integerliterals.Integer literals can either be positive or negative but the default sign is positiveReal literals:- These are the numeric literals with decimal point. Eg., 23.45,56.78 etc.Real literals are expressed in two forms:-Fractional form eg., 23.567, 56.78 etcExponential form eg., 2.5e5(250000),7.6e-6(0.0000076) etc.Rules for defining Real literals:-Real literals should have at least one digit.It must have a decimal point.No comma or space is allowed within the real literals.Real literals can either be positive or negative but the default sign is positiveCharacter literals:- A character literal is a single character that is enclosed within the single quotes. Eg., ‘a’,’A’,’r’ ,’1’,’3’etcString literals:- A string literal consists of zero or more characters enclosed within double quotes. At the end of the string \0 is automatically placed by the compiler.Eg., “ajaylakhnotra” “amit” ,”9796006678” etc. Keywords:- These are the predefined words that have some special meaning for the C compiler and we cannot assign a different meaning to them. Some of the keywords are as under:-Const, int, char, double, float, switch, break , continue, case, do , while, for, if, long, goto, short, signed, unsigned, else, default etc.In C89 version there are 32 keywords while in C99 version there are 36 keywords.Punctuators:- Punctuators are also called separators. These include the following characters:-[ ] ( ) { } , ; : * = # etc. Operators: - Operators are the symbols that perform some action on the arguments given to them. The arguments are called OPERANDS. We can say that operators acts upon the operands to perform some operation.Based upon the number of operands upon which they operate, the operators are classified into the following types:-Operators Unary operator Binary operator Ternary operatorUnary operators:- These operators operate on the single operand. Eg’s of unary operator areUnary plus(+) , unary minus(-) , increment operator (++) , decrement operator(--) etc.Binary operators:- These operators operate upon the two operands. Egs of binary operator are addition(+), subtraction (-), division(/), multiplication(*) etc.Ternary operators:- These operators operate upon the three operands. Eg of ternary operator is conditional operator(? :). Based upon the operation they perform, the operators are classified into the following types:OperatorsArithmetic Relational Logical Assignment conditional Comma Bitwise sizeofArithmetic operators:- These are the operators that performs arithmetic operation or calculations on the operands. The various types of arithmetic operators, their functions and their example are as under:OperatorFunctionExampleClassification based on the number of operandsExpressionresultAddition(+)Adds the two operands4+59BinarySubtraction(-)Subtracts two operands5-41BinaryMultiplication(*)Multiply two operands3*412BinaryDivision(/)Divide two operands and returns quotient35/235.0/236/235/2.035.0/2.01717.51817.517.5BinaryModulus division(%)Divides two operands and returns the remainder35%236/210BinaryIncrement operator(++)Increments the operand by 1++44++55UnaryDecrement operator(--)Decrements the operand by 1--44--33UnaryThe modulus operator requires that both the operand should be of integer type. It does not operate upon the floating type operands.Increment and decrement operators are expressed in the following two forms:-Increment decrementPre increment post increment pre decrement post decrementFirst change then use first use then change First change then use first use then change Relational operators:- These operators relate or compare the value of their operands and returns the result as TRUE or FALSE depending on whether the comparison evaluates to true or false.If the relation is true then the value of the relational expression is 1 and if the relation is false then the value of expression is 0.The various relational operators, theirmeaning, and their examples are as under:-OperatorMeaningExampleClassification based on the number of operandsValue of expressionExpression ResultLess than(<)Is less than 4<55<4TrueFalseBinary operator10Greater than(>)Is greater than 4>55>4FalseTrueBinary operator01Less than equal to(<=)Is less than or equal to4<=55<=45<=5TrueFalseTrueBinary operator101Greater than or equal to (>=)Is greater than or equal to4>=55>=44>=4FalseTrueTrueBinary operator011Equal to(==)Is equal to5==55==4TrueFalseBinary operator10Not equal to(!=)Is not equal to5!=55!=4FalsetrueBinary operator01Logical operators:- These are the operators which combine relational expressions or logical expressions. The result of such expressions is always logical ie., either TRUE(1) or FALSE(0).The various logical operators , their meaning, and their examples are as under:-OperatorMeaningExampleResultClassification based on the number of operands&&Logical ANDThis operator returns the value TRUE if both the relational expressions are TRUE, otherwise it returns false(4>5)&&(5>4)(4<5)&&(5<4)(4>5)&&(5>6)(4<5)&&(5<6)False&&True=FalseTrue &&False=FalseFalse && False=falseTrue &&True =TrueBinary operator||Logical ORThis operator returns the result as FALSE if both the expressions are FALSE, otherwise it returns TRUE4>5)||(5>4)(4<5) ||((5<4)(4>5) ||((5>6)(4<5) ||((5<6)False&&True=TrueTrue &&False= TrueFalse && False=falseTrue &&True =TrueBinary operator!Logical NOTThis operator negates the value of the expression. If the expression is TRUE then it returns FALSE and if the expression is FALSE then it returns TRUE.!(4>5)!(5>4)!(False )=True!(True)=FalseUnary operatorAssignment operator (=):- The assignment operator assigns the value of the operand on its right to the operand on its left. The operand on the left side must be a variable while the operand on the right side can be a variable, constant or expression.It is a binary operatorExamples are:-X=8; /*8 is assigned to x*/Y=44; /* 44 is assigned to y*/Z=x+y-2; /* value of expression x+y-2 is assigned to Z*/Q=z; /*value of Z is assigned to Q*/We have some compound assignment operators. These are as under:-a=a+5 ; can be written as a+=5;a=a-5; can be written a-=5;a=a*45; can be written as a*=45;Here the operators +=,-=,*= are called shorthand operators. The other shorthand operators are as under:-/=, %=, &=, ^=, \= Also we can have multiple assignments likeX=y=z=6;Here all the variables are assigned value 6 and the result of whole expression is 6.Conditional operator:-Conditional operator is aternary operator and it requires three expressions as operands. This is written asTest expression? expression1: expression2Firstly the TEST EXPRESSION is evaluated. If the test expression is true then expression1 becomes the result otherwise expression2 becomes the result.For e.g., consider the casea >b ?a++ : b++Here first the expression a>b is evaluated .if a is greater than b then a is incremented by 1 otherwise b is incremented by 1.Sizeofoperator :-This is a unary operator. This operator gives the size of the operand in terms of bytes. The operand can be a variable , constant, or any datatype (int, char ,float, double) .For e.g.sizeof(int) gives 2Sizeof(4) gives 2Bitwise operators:-These operators are used for operation on individual bits. These operators can operate on integers only. Following are the various bitwise operators:-Bitwise operatorMeaning&Bitwise AND|Bitwise OR~One’s complement<<Left shift>>Right shift^Bitwise XORThese operators are described as following:-Bitwise AND;- It is a binary operator and requires two operands. These operands are compared bitwise i.e. all the corresponding bits in both operands are compared. The resulting bit is 1 only if the bits in both the operands are 1 , otherwise 0.Bit of operand1Bit of operand2Resulting bit000010100111Bitwise OR (|) :- It is a binary operator and requires two operands. These operands are compared bitwise i.e. all the corresponding bits in both operands are compared. The resulting bit is 0 only if the bits in both the operands are 0, otherwise 1.Bit of operand1Bit of operand2Resulting bit000011101111Bitwise XOR (^) : -It is a binary operator and requires two operands. These operands are compared bitwise i.e. all the corresponding bits in both operands are compared. The resulting bit is 1 if the bits in both the operands have different values, otherwise it is zero.Bit of operand1Bit of operand2Resulting bit000011101110One’s complement (~) :- This is a unary operator and requires only one operand. It negates the values of the bit. If the bit of the operand is 1 then the resulting bit is 0 and if the bit of the operand is 0 then the resulting bit is 1. Bit of operand1Resulting bit0110Left shift operator:- This operator shifts the bit left. It requires two operands. The left operand is the operand whose bits are shifted and the right operand indicates the number of the bits to be shifted. On shifting the bits left, an equal number of bit positions on the right are vacanted. These positions are filled in with 0 bits.Eg. Let a= 0x1346. The binary representation of x is:-0001001101000110Now a<<4 will give001101000110 0000Right shift operator(>>):-This operator shifts the bit right It requires two operands. The left operand is the operand whose bits are shifted and the right operand indicates the number of the bits to be shifted. On shifting the bits right, an equal number of bit positions on the left are vacated. These positions are filled in with 0 bits in unsigned integers.In case of signed integer, two shift patterns are used. These areLogical shift:- the vacated bits are always filled with 0sThe vacated bits are filled with the values of the leftmost bit in the initial bit pattern. If the left most bit is 1, then the vacated positions are filled with 1, and if the leftmost bit is 0, then the vacated bits are filled with 0s.Conversion specifications:- Following are the various conversion specifications:-%c ------------------------------- a single character%d ------------------------------- a decimal integer%f ------------------------------- a floating point number%o ------------------------------- an octal number%x ------------------------------- a hexadecimal number%s ------------------------------- a string%u ------------------------------- an unsigned decimal integerOperator precedence and associativity:-Operator?precedence?is the order in which a program performs the operations in a formula. If one operator has precedence over another operator, it is evaluated first.If the expression contains two or more operators of the same precedence then the associativity of the operators is taken care of. Associativity of the operators defines the order in which the operators of the same precedence are evaluated. Associativity can be from LEFT TO RIGHT or RIGHT TO LEFT. The operator precedence and associativity of various operators is described in the following table:-OperatorMeaningPrecedenceAssociativity()[]->.Function expressionArray expressionStructure operatorStructure operator1Left to right+-++--~&*Sizeof!(Type)Unary plusUnary minusIncrement operatorDecrement operatorOne’s complementAddress ofValue at the addressSizeof operatorNegationType cast2Right to left*/%MultiplicationDivisionModulus division3Left to right+-AdditionSubtraction4Left to right<<>>Left shiftRight shift5Left to right<<=>>=Less thanLess than or equal toGreater thanGreater than or equal to6Left to right==!=Equal toNot equal to7Left to right&Bitwise AND8Left to right^Bitwise XOR9Left to right|Bitwise OR10Left to right&&Logical AND11Left to right||Logical OR12Left to right? :Conditional operator13Right to left=*=/=%=&=^=|=<<=>>=Assignment operators14Right to left,Comma15Right to leftPractical problems on chapter first:-Give the output of the following codesInt x=16, y ;Y=++x + --x + ++x;printf(“x=%d and y=%d”,x,y);Int x=16, y ;Y= ++x + --x + ++x + x++;printf(“x=%d and y=%d”,x,y);Int x=16, y ;Y=++x + --x + ++x + --x;printf(“x=%d and y=%d”,x,y);Int x=16, y ;Y= --x + x-- ;printf(“x=%d and y=%d”,x,y);Int x=16, y ;Y= x++ + ++x;printf(“x=%d and y=%d”,x,y);Int x=16, y ;Y= x++ + ++x - --x – x--;printf(“x=%d and y=%d”,x,y);Void main(){inti=2, j=3 k, m;floata,b;k=i/j*j;m=j/i*i;a=i/j*j;b=j/i*i;printf(“%d%d%f%f”,k,m,a,b);}Void main(){Inta ,b;A=-3 - -3;B=-3 - -(-3);printf(“a=%d b=%d”,a,b);}Void main(){float a=5, b=2;int c;C=a%b;printf(“%d”,c);}X=4+2% -8 ;printf(“%d”,x); ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download