1 / 106

C Programming Class I

C Programming Class I. Introduction to C. Generation of ‘C’ Language In 1967, Martin Richards developed a language called BCPL (Basic Combined Programming Language) In 1970, Ken Thompson created a language using many features of BCPL and called it simply B.

matteo
Télécharger la présentation

C Programming Class I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C Programming Class I

  2. Introduction to C Generation of ‘C’ Language • In 1967, Martin Richards developed a language called BCPL (Basic Combined Programming Language) • In 1970, Ken Thompson created a language using many features of BCPL and called it simply B. • In 1972, ‘C’ is Introduced by Dennis Ritchie at Bell laboratories and in the UNIX operating system.

  3. Why are using ‘C’ • It is a Structured Programming Language • High – Level Language • Machine Independent Language • It allows software developers to develop programs without worrying about the hardware platforms where they will be implemented TYPES OF ‘C’ COMPILER 1. Borland ‘C’ Compiler 2. Turbo ‘C’ Compiler 3. Microsoft ‘C’ Compiler 4. ANSI ‘C’ Compiler

  4. Steps in Learning C Character set Files Data Structures Structures and Unions Algorithms Constants, variable And Data types Control statements Pointers Programs Functions Arrays

  5. C’S Program Structure Documentation section Preprocessor section Definition section Global declaration section main() { Declaration part; Executable part; } Sub program section { Body of the subprogram }

  6. C’s Character set

  7. C TOKENS C TOKENS Constants Strings -15.5 100 “ABC” “YEAR” Identifiers Operators Grant_total Amount a1 + - * / Keywords Special Symbols float while [ ] { }

  8. C’s keyword • Basic Building Block of the Program • This are the Reserved words • This word’s cant be changed

  9. C’s Variables • A variable is a data name as well as identifier that may be used to store a data value. Rules for Naming the Variables • A variable can be of any combination of alphabets, digits and underscore. • The first character of the variable can’t be digits. • The length of the variable can’t be exceeded by 8.(ANSI C – 32 Character) • No commas, blanks or special symbol are allowed within a variable name. • Uppercase and lowercase are significant. That is, the variable Total is not the same as total or TOTAL. • It should not be a keyword. • White space is not allowed.

  10. C’s Variables cont.

  11. C’s Variables cont.

  12. C’s constant The item whose values can’t be changed during execution of program are called constants

  13. C’s constant Conti… Integer constant eg: roll_number = 12345; Real Constant eg: pi = 3.14; Decimal Constant Eg. 35 Octal constant Eg. 043 Hexadecimal constant Eg. 0x23 Single Precision Constant Double Precision Constant

  14. Data Types • This are the type of the data that are going to access within the program.

  15. C’s Data types cont. The primary data types are further classified as below. Integers are the whole numbers, both positive and negative.

  16. C’s Data types cont. Float are the numbers which contain fractional parts, both Positive and Negative.

  17. C’s Data types cont. Char are the characters which contain alpha-numeric character. Characters are usually stored in 8 bits (one byte) of internal storage The void is the Null Data type.

  18. C’ Delimiters Delimiters are the symbols, which has some syntactic meaning and has got significance.

  19. C’ Statements • Statement can be defined as set of declarations (or) sequence of action • All statements in ‘C’ ends with semicolon(;) except condition and • control statement

  20. Expression Statement 1. An Expression is a combination of constant, variables, operators, and function calls written in any form as per the syntax of the C language. 2. The values evaluated in these expressions can be stored in variables and used as a part for evaluating larger expressions. 3. They are evaluated using an assignment statement of the form. variable = expression; 4. For Example, age = 21; result = pow(2,2); simple_interest = (p * n * r) / 100;

  21. Compound Statements 1. A group of valid C expression statements placed within an opening flower brace ‘{‘ and closing flower brace ‘}’ is referred as a Compound Statements. 2. For Example, { X = (A + (B * 3) – C); Y = A + B * 3; Z = A * (B * 3 – C); } Control Statements 1. This statement normally executed sequentially as they appear in the program. 2. In some situations where we may have to change the order of execution of statements until some specified conditions are met. 3. The control statement alter the execution of statements depending upon the conditions specified inside the parenthesis. 4. For Example, if (a == b) if ((x < y) && (y > z)) { { -------- ----------- -------- ----------- } }

  22. Operators An operator is a symbol that specifies an operation to be performed on the operands • Some operator needs two operands (binary) • Eg: a+b; ‘+’ is an operator and ‘a’ and ‘b’ are the operands • Some operator needs one operand (unary) • Eg: ++a; ‘++’ is an operator and a is the operand

  23. Types of Operators

  24. This operators help us to carryout basic arithmetic operations such addition, subtraction, multiplication, division Arithmetic Operators

  25. This are used to compare two or more operands. Operands can be variables, constants or expression. eg: comparison of two marks or two values. Relational Operator

  26. This operators are used to combine the results of two or more conditions. LogicalOperator

  27. Assignment Operator • This are used to assign a value or an expression or a variable to another variable • eg: a = 10; n1 = 20; Syntax: identifier = expression; • Compound Assignment This operator are used to assign a value to a variable in order to assign a new value to a variable after performing a specified operation. eg: a+=10,n1-=20; • Nested Assignment (Multiple) This operator are used to assign a single value to multiple variables eg: a=b=c=d=e=10;

  28. List of Shorthand or Compound Assignment Operator

  29. C provide two operator for incrementing a value or decrementing a value a) ++ Increment operator (adds one to the variable) b) -- Decrement operator (Minus one to the variable) eg: a++ (if a= 10 then the output would be 11) Increment and Decrement operator

  30. Increment and Decrement operator Conti… If the value of the operand x is 3 then the various expressions and their results are The pre – increment operation (++X) increments x by 1 and then assign the value to x. The post – increment operation (X++) assigns the value to x and then increments 1. The pre-decrement operation ( --X) decrements 1 and then assigns to x. The post – decrement operation (x--) assigns the value to x and then decrements 1. These operators are usually very efficient, but causes confusion if your try to use too many evaluations in a single statement.

  31. It is used check the condition and execute the statement depending upon the condition Conditional Operator

  32. Bitwise Operator • This are used to manipulate the data at bit level • It operates only on integers

  33. The truth table for Bitwise AND,OR and XOR Bitwise Operator cont. Eg: x = 3 = 0000 0011 y = 4 = 0000 0100 x&y = 0000 0000 Eg: x = 3 = 0000 0011 y = 4 = 0000 0100 x|y = 0000 0111 Eg: x = 3 = 0000 0011 y = 4 = 0000 0100 x ^ y = 0000 0111

  34. Bitwise Operator cont. Bitwise One’s Complement The one’s complement operator (~) is a unary operator, which causes the bits of the operand to be inverted (i.e., one’s becomes zero’s and zero’s become one’s) For Example, if x = 7 i.e 8 – bit binary digit is 0 0 0 0 0 1 1 1 The One’s Complement is 1 1 1 1 1 0 0 0 Bitwise Left Shift Operator The Left shift operator (<<) shifts each bit of the operand to its Left. The general form or the syntax of Left shift operator is variable << no. of bits positions if x = 7 (i.e., 0 0 0 0 0 1 1 1) the value of y in the expression y = x <<1 is 14 0 0 0 0 1 1 1 0 = 14 since it shifts the bit position to its left by one bit. The value stored in x is multiplied by 2N (where n is the no of bit positions) to get the required value. For example, if x = 7 the result of the expression y = x << 2 is y = x * 22 (i.e. 28)

  35. Bitwise Operator cont. Bitwise Right Shift Operator The Right shift operator (>>) shifts each bit of the operand to its Right. The general form or the syntax of Right shift operator is variable >> no. of bits positions if x = 7 (i.e., 0 0 0 0 0 1 1 1) the value of y in the expression y = x >> 1 is 3 0 0 0 0 0 0 1 1 = 3 since it shifts the bit position to its right by one bit. The value stored in x is divided by 2N (where n is the no of bit positions) to get the required value. For example, if x = 7 the result of the expression y = x << 2 is y = x / 22 (i.e. 1). If you use the left shift operator i.e. x = x << 1 the value of x will be equal to 2 (i.e., 0 0 0 0 0 0 1 0) since the lost bit cannot be taken back.

  36. Operator Precedence and Associativity of Operator

  37. What is Precedence Rule and Associative Rule • Each operator in C has a precedence associated with it. • This precedence is used to determine how an expression involving more than one operator is evaluated. • These are distinct levels of precedence and an operator may belong to one of these levels. • The operators at the higher level of precedence are evaluated first. • The operators of the same precedence are evaluated either from ‘left to right’ or from ‘right to left’, depending on the level. • That is known as the associativity property of an operator.

  38. The precedence of an operator gives the order in which operators are applied in expressions: the highest precedence operator is applied first, followed by the next highest, and so on. eg: Arithmetic operator precedence Arithmetic operators precedence The arithmetic expression evaluation is carried out using two phases from left to right through the expressions

  39. Relational operators precedence Example: if (x == 10 +15 && y <10) The precedence rules say that the addition operator has a higher priority than the logical operator (&&) and the relational operators (== and <). Therefore, the addition of 10 and 15 is executed first. This is equivalent to: if (x == 25 && y < 10) The next step is to determine whether x is equal to 25 and y is less than 10, if we assume a value of 20 for x and 5 for y, then x == 25 is FALSE (0) y <10 is TRUE (1) Note that since the operator < enjoys a higher priority compared to ==, y < 10 is tested first and then x ==25 is tested. Finally we get, if (FALSE && TRUE) Because one of the conditions is FALSE, the complex condition is FALSE. In the case of &&, it is guaranteed that the second operand will not be evaluated if the first is zero and in the case of || , the second operand will not be evaluated if the first is non – zero.

  40. The following table lists all the operators, in order of precedence, with their associativity Precedence and Associativity Table

  41. Precedence and Associativity Table cont.

  42. Precedence and Associativity Table cont.

  43. Precedence and Associativity Table cont.

  44. Sample Expression • Exp = a - 2 * a * b + b / 4 Let us have a=10,b=20 exp = 10 - 2 * 10 * 20 + 20 / 4 Phase I exp = 2*10*20 , 20/4 will be evaluated. phase II exp = 10-400+5 will be evaluated. Result exp = -395.

  45. Expression Evaluation Let us see some examples for evaluating expression. Let a = 5, b = 8, c = 2. x = b / c + a * c 10 4 14

  46. Expression Evaluation Let us see some examples for evaluating expression. Let a = 5, b = 8, c = 2. y = a + (b * 3) - c 24 29 27

  47. TYPE CONVERSION OR TYPE CASTING

  48. What is Type Conversion or Type Casting Type Casting means One data type converted into another data type. This is called Type conversion or Type casting. Example: 1. Integer into floating point number 2. Character into integer 3. Floating point number into Integer Number Type conversion is classified into two types. 1. Implicit Type Conversion (Automatic Type Conversion) 2. Explicit Type Conversion (Manual Type Conversion) Type Conversion Implicit Conversion Explicit Conversion Automatic Conversion Casting Operation

  49. Type Conversion Hierarchy long double Implicit Type Conversion double float unsigned long int long int unsigned int int Explicit Type Conversion short char

  50. Implicit Type Conversion • The Implicit Type Conversion is known as Automatic Type Conversion. • C automatically converts any intermediate values to the proper type so that the expression can be evaluated without loosing any significance. • Implicit type Conversion also known as Converted Lower order data type into Higher order data type. • Implicit Type Conversion also known as Widening. • Example: • int a, b; • float c; • c = a + b; • Print c; • float a,b; • int c; • c = a + b; // This is Wrong • Print c;

More Related