Module 1:Introduction

24. May 2021

Más contenido relacionado


Module 1:Introduction

  1. MODULE 1: FUNDAMENTALS OF C PROGRAMMING CO 1: Formulate simple algorithms for arithmetic, logical problems and translate them to programs in C language
  2. KEYWORDS Keywords are predefined, reserved words in C language and each of which is associated with specific features. These words help us to use the functionality of C language. They have special meaning to the compilers. There are total 32 keywords in C. Keywords are part of the syntax and they cannot be used as an identifier. For example: int money; Here, int is a keyword that indicates money is a variable of type int (integer). As C is a case sensitive language, all keywords must be written in lowercase. Here is a list of all keywords allowed in ANSI C.
  4. IDENTIFIERS "Identifiers" are the names you supply for variables, types, functions, and labels in your program. Identifier names must differ in spelling and case from any keywords. You cannot use keywords as identifiers; they are reserved for special use. You create an identifier by specifying it in the declaration of a variable, type, or function. In this example, result is an identifier for an integer variable, and main and printf are identifier names for functions. #include <stdio.h> int main() { int result; if ( result != 0 ) printf_s( "Bad file handlen" ); }
  5. IDENTIFIERS Identifier refers to name given to entities such as variables, functions, structures etc. Identifiers must be unique. They are created to give a unique name to an entity to identify it during the execution of the program. For example: int money; double accountBalance; Also remember, identifier names must be different from keywords. You cannot use int as an identifier because int is a keyword.
  6. IDENTIFIERS Rules for naming identifiers 1.A valid identifier can have letters (both uppercase and lowercase letters), digits and underscores. 2.The first letter of an identifier should be either a letter or an underscore. Age, _age 3.You cannot use keywords like int, while etc. as identifiers. 4.There is no rule on how long an identifier can be. However, you may run into problems in some compilers if the identifier is longer than 31 characters. You can choose any name as an identifier if you follow the above rule, however, give meaningful names to identifiers that make sense.
  7. IDENTIFIERS nondigit: one of _ a b c d e f g h i j k l mn o p q r s t u v w x y z A B C D E F G H I J K L MN O P Q R S T U V W X Y Z digit: one of 0 1 2 3 4 5 6 7 8 9 The first character of an identifier name must be a nondigit (that is, the first character must be an underscore or an uppercase or lowercase letter). ANSI allows six significant characters in an external identifier's name and 31 for names of internal (within a function) identifiers. External identifiers (ones declared at global scope or declared with storage class extern) may be subject to additional naming restrictions because these identifiers have to be
  8. CONSTANTS Constants are the terms that can't be changed during the execution of a program. If you want to define a variable whose value cannot be changed, you can use the const keyword. This will create a constant. For example, const double PI = 3.14; Types of C Constants C constants can be divided into two major categories: 1) Primary Constants 2) Secondary Constants These constants are further categorized as shown In fig.
  9. CONSTANTS Integer Constants 1) Integer constants are the numeric constants(constant associated with number) without any fractional part or exponential part. 2) It must not have a decimal point. 3) It can be either positive or negative 4) The allowable range for integer constants is -32768 to 32767. 5) Default sign is positive 6) EX: 567, -3456, +876 etc
  10. CONSTANTS Types of integer constants: Decimal constant (base 10): Decimal digits: 0 1 2 3 4 5 6 7 8 9 & something other than 0. For example: 0, -9, 22 etc Octal constant (base 8): Octal digits: 0 1 2 3 4 5 6 7 & first digit must be 0. For example: 021, 077, 033 etc Hexadecimal constant (base 16): Hexadecimal digits: 0 1 2 3 4 5 6 7 8 9 A B C D E F & begin with 0x. For example: 0x7f, 0x2a, 0x521 etc
  11. CONSTANTS Real Constants 1) Real constants are often called Floating Point constants. 2) A real constant must have at least one digit. 3) It must have a decimal point. 4) It could be either positive or negative. 5) Ex: 345.67, -567.23, +34.8
  12. CONSTANTS Character Constants 1) A character constant is a single alphabet, a single digit or a single special symbol enclosed within single inverted commas. 2) The maximum length of a character constant can be 1 character. 3) Ex.: 'Y' , '7' , '=' String constants String constants are the constants which are enclosed in a pair of double-quote marks.
  13. CONSTANTS For example: "good" //string constant "" //null string constant " " //string constant of six white space "x" //string constant having single character. "Earth is roundn" //prints string with newline
  14. VARIABLES In programming, a variable is a container (storage area) to hold data. To indicate the storage area, each variable should be given a unique name (identifier). Variable names are just the symbolic representation of a memory location. For example: int playerScore = 95; Here, playerScore is a variable of int type. Here, the variable is assigned an integer value 95. The value of a variable can be changed, hence the name variable. Syntax: <datatype> nameofvariable; Examples int num; Here, num is a variable of integer type.
  15. VARIABLES Rules for writing variable name in C 1. Variable name can be composed of letters (both uppercase and lowercase letters), digits and underscore '_' only. 2. The first letter of a variable should be either a letter or an underscore. But, it is discouraged to start variable name with an underscore though it is legal. It is because, variable name that starts with underscore can conflict with system names and compiler may complain. 3. There is no rule for the length of length of a variable. However, the first 31 characters of a variable are discriminated by the compiler.So; the first 31 letters of two variables in a program should be different.
  16. DATA TYPES The data type decides the type of data and the memory locations required for storing that type of data in the memory. A C language programmer has to tell the system before-hand, the type of numbers or characters or variable he is using in his program. These can be done by using data types. Data types indicate the type of data hold by variable. There are many data types in C language. A C programmer has to use appropriate data type as per his requirement. C language data types can be broadly classified into three categories, namely: 1. Primary data type 2. Derived data type 3. User defined data type 4. Valueless
  17. DATA TYPES Primary data type: All C Compilers accept the following fundamental data types 1. Int 2. Char 3. Float 4. Double 5. void
  18. DATA TYPES Character Type: ❖ A single character can be defined as a defined as a character type of data. ❖ Characters are usually stored in 8 bits of internal storage. ❖ The qualifier signed or unsigned can be explicitly applied to char. ❖ Unsigned characters have values between 0 and 255. ❖ Signed characters have values from –128 to 127. The keyword used to define character variables is, char An example of a character value is the letter A. char 'A';
  20. DATA TYPES Integer Type: ❖ Integers are whole numbers with a machine dependent range of values. ❖ C has 3 classes of integer storage namely  short int  int  long int ❖ All of these data types have signed and unsigned forms Unsigned integers (positive values only). ❖ A short int requires half the space than normal integer values. ❖ Unsigned numbers are always positive and consume all the bits for the magnitude of the number. ❖ The long and unsigned integers are used to declare a longer range of values. The keyword used to define integers is, int
  21. DATA TYPES An example of declaring an integer variable called sum is, int sum;
  22. DATA TYPES Floating Point Types: ❖ Floating point number represents a real number (with six precision like 2.123456). ❖ Floating point numbers are denoted by the keyword float. ❖ When the accuracy of the floating point number is insufficient, we can use the double(with ten precision like 2.1234567891). to define the number. ❖ The double is same as float but with longer precision. To extend the precision further we can use long double which consumes 80 bits of memory space.
  23. DATA TYPES The keyword used to define float variables is, float An example of declaring a float variable called money is, float money;
  24. DATA TYPES Void Type: Void is an empty data type normally used as a return type in C functions to declare that no value will be return by the function. The void data type has no values and no operations. It Is a data type that represents the lack of a data type.
  25. OPERATORS Operators are special symbols that do some predefined task on the operands. They are used to performing operations like arithmetic, logical, etc. There are following three types of operators in C language. Unary Operators Binary Operators Ternary Operators
  27. UNARY OPERATORS Unary minus (–) The symbol shown in the bracket is used as unary minus operator. It returns the negative value of the variable to which it is preceded. For e.g. if x = 3 and y = 6 then y = – x; will make the value of y as – 3. Casting Operator ( ( ) ) or Type Conversion It is many a times required to convert one data type to another. The casting operator is used for type conversion i.e. it can be used to convert a data of one type to another data type. For e.g. if we have int x=3; float y=5.6; then the statement, x=(int) y; will result in the value of x as 5.
  28. UNARY OPERATORS Logical Not operator (!) The symbol shown in the brackets i.e. the exclamation mark is used as a logical not operator. It is used to check certain conditions in a condition statement. It performs the logical not of the result obtained from the expression on its right. For e.g. if x=1, then y=!x; will result with y having the value 0. We will see some more logical not operator based expressions and program followed by this section. Address of operator (&) This operator returns the address of the variable associated with it. It will be studied more deeply in the chapter on Pointers. For e.g. if we write y=&x; then the memory address allocated to the variable x will be copied into the variable y. For this the variable y must be a pointer variable.
  29. UNARY OPERATORS Indirection operator or value of operator (*) This operator returns the value of the data at the given address. This operator will also be studied in more details in the chapter on pointers. For e.g. z=*y; will give the value of the data stored at the address given by y. Scope Resolution operator (::) It resolves the scope of a variable to be inside the function or outside the function. This operator will also be studied in a later chapter. For e.g. if an external variable x is to be accessed then the
  30. UNARY OPERATORS Bitwise not operator (~) This operator is used to perform bitwise NOT operation. We have also seen some examples of NOT operations. The bitwise NOT operator can be used to perform binary NOT operation. This operation can be performed by using the operator given in brackets i.e. ~. For e.g. If x=3, then y=~x; will result in y having a value of – 4
  31. UNARY OPERATORS Unary Operators are the operators which require single operand to perform any action. Examples of unary operator sizeof: return the size of an operand in bytes. Example: int i, j; i = sizeof(int); //return 4 bytes j = sizeof(i); //return 4 bytes
  32. UNARY OPERATORS This operator is used to know the size of a variable as required to store its value in memory. It can also be used to find the size of a data type. The space required to store different data type is different ranging from 1 byte to 10 bytes. For e.g. the statement sizeof(int); will return the value 2, as int requires 2 bytes. Another e.g.: int x,y; char a; x=sizeof(a); y=sizeof(float); then x will have 1, and y will have 4
  33. UNARY OPERATORS increment operator (++): This operator adds 1 to its operand. Example int num = 10; num++; //output: 11 (which is equivalent to num = num + 1) increment operator is divided into two types  post-increment operator  pre-increment operator
  34. UNARY OPERATORS post-increment operator int a = 10; printf("a=%d",a++); printf("a=%d",a); /* output: a = 10; a = 11; */ In this statement first the value of ‘a’ will be printed on screen and then the value of ‘a’ incremented by 1.
  35. UNARY OPERATORS pre-increment operator int a = 10; printf("a=%d",++a); printf("a=%d",a); /* output: a = 11; a = 11; */ In this statement first the value of ‘a’ will be incremented by 1 and then the incremented value will be printed on screen.
  36. UNARY OPERATORS It returns the value of the variable added with one and stores the result in the variable itself. For e.g. if x=5, then x++; will make the value of x equal to 6. This “x++;” (also called as post increment operator) can also be written as “++x;” (also called as pre increment operator). It can also be used to store the result in another variable. But in this case the post increment and pre increment statements will have different behavior as explained below with examples. In post increment case, for e.g. if x=5, then y=x++; will make the value of y equal to 5 and x equal to 6. As the name says post increment, it first gives the previous value and then increments. In pre increment case, for e.g. if x=5, then y=++x; will make the value of y equal to 6 and x equal to 6. As the name says pre increment, it first increments and then gives the incremented value.
  37. UNARY OPERATORS decrement operator (–): This operator subtracts 1 from its operand. Example: int num = 10; num--; //output: 9 //which is equivalent to num = num - 1; decrement operator is divided into two types  post-decrement operator  pre-decrement operator
  38. UNARY OPERATORS post-decrement operator int a = 10; printf("a=%d",a--); printf("a=%d",a); /* output: a = 10; a = 9; */ In this statement first the value of ‘a’ will be printed to screen and then the value of ‘a’ decremented by 1.
  39. UNARY OPERATORS pre-decrement operator int a = 10; printf("a=%d",--a); printf("a=%d",a); /* output: a = 9; a = 9; */ In this statement first the value of ‘a’ will be decremented by 1 and then the decremented value will be printed on the screen.
  40. UNARY OPERATORS Decrement Operator(–) It returns the value of the variable subtracted with one and stores the result in the variable itself. For e.g. if x=5, then x–; will make the value of x equal to 4. This “x–;” (also called as post decrement operator) can also be written as “–x;” (also called as pre decrement operator). It can also be used to store the result in another variable. But in this case the post decrement and pre decrement statements will have different behavior as explained below with examples. In post decrement case, for e.g. if x=5, then y=x–; will make the value of y equal to 5 and x equal to 4. As the name says post decrement, it first gives the previous value and then decrements. In pre decrement case, for e.g. if x=5, then y= –x; will make the value of y equal to 4 and x equal to 4. As the name says pre decrement, it first decrements and then gives the decremented value.
  41. INCREMENTS AND DECREMENT OPERATORS The increment and decrement operators are one of the unary operators which are very useful in C language. They are extensively used in for and while loops. The syntax of the operators is given below 1. ++ variable name 2. variable name ++ 3. – –variable name 4. Variable name– –
  42. INCREMENTS AND DECREMENT OPERATORS The increment operator ++ adds the value 1 to the current value of operand and the decrement operator – – subtracts the value 1 from the current value of operand. Ex: int i=42 i++ = i=i+1 =i=43 i-- = i=i-1 =i=42 ++i = i=i+1 =i=43 --i = i=i-1 =i=42
  43. INCREMENTS AND DECREMENT OPERATORS POSTFIX a) x=a++; x=a ------> store value of a in memory for variable x a=a+1 ------> increment value by 1 and store in memory for variable a b) y=b--; y=b ------> store value of b in memory for variable y b=b-1 ------> decrement value by 1 and store in memory for variable b
  44. INCREMENTS AND DECREMENT OPERATORS PREFIX: value of operand increment /decrement before it is fetch for computation. a) x=++a; a=a+1 ------> increment value by 1 and store in memory for variable a x=a ------> store value of a in memory for variable x b) y=--b; b=b-1 ------> decrement value by 1 and store in memory for variable b
  45. BINARY OPERATORS Binary operators are the operators which require two operands to perform any action. C offers different types of binary operators. they are given as follows 1. Arithmetic operators 2. Relational Operators 3. Logical Operators 4. Assignment Operators 5. Bitwise Operators
  46. ARITHMETIC OPERATORS Arithmetical operators are used to perform basic arithmetical operations which are explained in following table.
  48. ARITHMETIC OPERATORS Arithmetic Expression (Arithmetic Instruction): An arithmetic expression in C consists of a variable name on the left hand side of assignment operator (=) and operands (variable names & constants) on the right hand side of =. The operands are connected by arithmetic operators like +, -, *, and /. Ex.: int a; float b, result ; a = 5400; b = 0.5; result = a * b / a + 3.2 * 2 / 5 ;
  49. ARITHMETIC OPERATORS *, /, -, + are the arithmetic operators. = is the assignment operator. 2, 5 and 5400 are integer constants. 3.2 and 0.5 are real constants. a is an integer variable. b, result are real variables.
  50. ARITHMETIC OPERATORS Type of Arithmetic Statement in C: An arithmetic statement could be of three types. These are as follows: 1. Integer mode arithmetic statement - This is an arithmetic statement in which all operands are either integer variables or integer constants. Ex.: int i, j; i = 20; j = i * 20 + 5; 2. Real mode arithmetic statement - This is an arithmetic statement in which all operands are either real constants or real variables. Ex.: float i,j ; j = 12.5; i = j + 23.123 / 4.5 * 0.3442;
  51. ARITHMETIC OPERATORS 3. Mixed mode arithmetic statement - This is an arithmetic statement in which some of the operands are integers and some of the operands are real. Ex.: float i, avg; int a, b, num ; avg = ( a + b + i + num ) / 4 ;
  52. ARITHMETIC OPERATORS Hierarchy of Operations / Arithmetic Operation Precedence and Associativity: While executing an arithmetic expression, which has two or more operators, we may have some problems as to how exactly does it get executed. Hierarchy of Operations gives solution to this problem. The priority or precedence in which the operations in an arithmetic statement are performed is called the hierarchy of operations. The precedence of commonly used operators is shown in following table. When an expression contains two operators of equal priority the tie between them is settled using the associativity of the operators. Associativity can be of two types—Left to Right or Right to Left.
  54. ARITHMETIC OPERATORS Ex: Determine the operations precedence and evaluate the following expression: i = 2 * 3 / 4 + 4 / 4 + 8 - 2 + 5 / 8 Solution: i = 2 * 3 / 4 + 4 / 4 + 8 - 2 + 5 / 8 i = 6 / 4 + 4 / 4 + 8 - 2 + 5 / 8 operation: * i = 1 + 4 / 4 + 8 - 2 + 5 / 8 operation: / i = 1 + 1+ 8 - 2 + 5 / 8 operations: / i = 1 + 1 + 8 - 2 + 0 operation: / i = 2 + 8 - 2 + 0 operation: + i = 10 - 2 + 0 operation: + i = 8 + 0 operation : - i = 8 operations: +
  55. ARITHMETIC OPERATORS 6 / 4 give 1 and not 1.5. This so happens because 6 and 4 both are integers and therefore would evaluate to only an integer constant. Similarly 5 / 8 evaluates to zero, since 5 and 8 are integer constants and hence must return an integer value.
  56. RELATIONAL OPERATORS Relational Operators are used to compare two operands. That is why; they are also known as “Comparison operators”. Relational operators are used to compare two values. The output of relational operators is Boolean values i.e. true or false. Following table describes all relational operators supported by C language (assume a=5 and b=6)
  58. RELATIONAL OPERATORS Assume a=3, b=4; If a<b (i.e. 3<4), result will be true. If a>b (i.e. 3>4), result will be false If a==b (i.e 3==4), result will be false If a!=b (i.e 3!=4), result will be true If a<=b (i.e 3<=4), result will be true If a>=b (i.e 3>=4), result will be false
  59. LOGICAL OPERATORS This operator is used to test more than one condition. C supports 3 logical operators. Following table describes all logical operators with example (assume x = 5 and y = 10)
  61. ASSIGNMENT OPERATORS It is the value assigning operator. After evaluating the expression on the right-hand side, it assigns the value to the variable on the left- hand side. Following is list of assignment operators (let A = 10)
  63. BITWISE OPERATORS BITWISE OPERATORS are used for manipulating data at the bit level, also called bit level programming. Bitwise operates on one or more bit patterns or binary numerals at the level of their individual bits. They are used in numerical computations to make the calculation process faster. Bitwise operators cannot be directly applied to primitive data types such as float, double, etc. Always remember one thing that bitwise operators are mostly used with the integer data type because of its compatibility. Following is the list of bitwise operators provided by 'C' programming language:
  64. BITWISE OPERATORS Operator Meaning & Bitwise AND operator | Bitwise OR operator ^ Bitwise exclusive OR operator ~ Binary One's Complement Operator is a unary operator << Left shift operator >> Right shift operator
  65. BITWISE OPERATORS Bitwise AND operator & The output of bitwise AND is 1 if the corresponding bits of two operands is 1. If either bit of an operand is 0, the result of corresponding bit is evaluated to 0. Let us suppose the bitwise AND operation of two integers 12 and 25. 12 = 00001100 (In Binary) 25 = 00011001 (In Binary) Bit Operation of 12 and 25 00001100 & 00011001 ________ 00001000 = 8 (In decimal)
  66. BITWISE OPERATORS Bitwise OR operator | The output of bitwise OR is 1 if at least one corresponding bit of two operands is 1. In C Programming, bitwise OR operator is denoted by |. 12 = 00001100 (In Binary) 25 = 00011001 (In Binary) Bitwise OR Operation of 12 and 25 00001100 | 00011001 ________ 00011101 = 29 (In decimal)
  67. BITWISE OPERATORS Bitwise XOR (exclusive OR) operator ^ The result of bitwise XOR operator is 1 if the corresponding bits of two operands are opposite. It is denoted by ^. 12 = 00001100 (In Binary) 25 = 00011001 (In Binary) Bitwise XOR Operation of 12 and 25 00001100 ^ 00011001 ________ 00010101 = 21 (In decimal)
  68. BITWISE OPERATORS Bitwise complement operator ~ Bitwise compliment operator is an unary operator (works on only one operand). It changes 1 to 0 and 0 to 1. It is denoted by ~. 35 = 00100011 (In Binary) Bitwise complement Operation of 35 ~ 00100011 ________ 11011100 = 220 (In decimal)
  69. BITWISE OPERATORS Shift Operators in C programming There are two shift operators in C programming:  Right shift operator  Left shift operator. Right Shift Operator Right shift operator shifts all bits towards right by certain number of specified bits. It is denoted by >>. 212 = 11010100 (In binary) 212>>2 = 00110101 (In binary) [Right shift by two bits] 212>>7 = 00000001 (In binary) 212>>8 = 00000000 212>>0 = 11010100 (No Shift)
  70. BITWISE OPERATORS Left Shift Operator Left shift operator shifts all bits towards left by a certain number of specified bits. The bit positions that have been vacated by the left shift operator are filled with 0. The symbol of the left shift operator is <<. 212 = 11010100 (In binary) 212<<1 = 110101000 (In binary) [Left shift by one bit] 212<<0 = 11010100 (Shift by 0) 212<<4 = 110101000000 (In binary) =3392(In decimal)
  71. TERNARY OPERATOR ( ? : ) There is one ternary operator supported by C language The ternary operator is also called as “Conditional Operator”. The ternary operator take three arguments:  The first is a comparison argument  The second is the result upon a true comparison  The third is the result upon a false comparison Syntax: Condition ? expression1 : expression2 If condition is true expression1 gets executed else expression2. A ternary operator is a short form of if-else.
  72. TERNARY OPERATOR ( ? : ) It is also a decision-making statement. It is an alternative to is known as ternary operator as it has three operands. Syntax: (condition)? true-case statement: false case statement;
  73. TERNARY OPERATOR ( ? : ) Example: #include<stdio.h> #include<conio.h> void main () { int x,y; clrscr(); printf("Enter two numbers:"); scanf("%d %d",&x,&y); x>y ? printf("%d is larger",x): printf ("%d is larger",y); getch(); }
  74. TERNARY OPERATOR ( ? : ) Syntax condition ? value_if_true : value_if_false The statement evaluates to value_if_true if condition is met, and value_if_false otherwise. int a = 10, b = 20, c; c = (a < b) ? a : b; printf("%d", c); Output of the example above should be: 10 c is set equal to a, because the condition a < b was true. The arguments value_if_true and value_if_false must be of the same type, and they must be simple expressions rather than full statements.
  75. TERNARY OPERATOR ( ? : ) Ternary operators can be nested just like if-else statements. Consider the following code: int a = 1, b = 2, ans; if (a == 1) { if (b == 2) { ans = 3; } else { ans = 5; } } else { ans = 0; } printf ("%dn", ans);
  76. TERNARY OPERATOR ( ? : ) Ternary operator can be nested as following: Condition ? expression1(condition1 ? exp 1 :exp 2) : expression2 (condition2 ? exp 1 :exp 2) For example: int big, a=1, b=2, c=3 ; big = ( a > b ? ( a > c ? 3: 4 ) : ( b > c ? 6: 8 ) ) ; Output big=8 the code above rewritten using a nested ternary operator: int a = 1, b = 2, ans; ans = (a == 1 ? (b == 2 ? 3 : 5) : 0); printf ("%dn", ans); The output of code above should be: 3
  77. PRECEDENCE AND ASSOCIATIVITY OF OPERATORS IN C. •The precedence of the operators means the sequence in which the operators will be operated on, in case of multiple operators in a statement i.e. which operator will be executed first and which operator will be executed later. •The associativity of operators refers to the direction in which the operation will be performed in case of equal precedence operators i.e. if multiple additions are there in a statement then it will be performed from left to right. We will see more about this in the examples followed by this section. •The precedence and associativity table is as given in Table
  78. EXPRESSION An expression is a combination of operators, constants and variables. An expression may consist of one or more operands, and zero or more operators to produce a value.
  79. TYPES OF EXPRESSIONS Expressions may be of the following types
  80. TYPES OF EXPRESSIONS •Constant expressions: Constant Expressions consists of only constant values. A constant value is one that doesn’t change. Examples:5, 10 + 5 / 6.0, 'x’ •Integral expressions: Integral Expressions are those which produce integer results after implementing all the automatic and explicit type conversions. Examples:x, x * y, x + int( 5.0) where x and y are integer variables. •Floating expressions: Float Expressions are which produce floating point results after implementing all the automatic and explicit type conversions. Examples: x + y, 10.75 where x and y are floating point variables.
  81. TYPES OF EXPRESSIONS •Relational expressions: Relational Expressions yield results of type bool which takes a value true or false. When arithmetic expressions are used on either side of a relational operator, they will be evaluated first and then the results compared. Relational expressions are also known as Boolean expressions. Examples: x <= y x + y > 2 •Logical expressions: Logical Expressions combine two or more relational expressions and produces bool type results. Examples: x > y && x == 10 x == 10 || y == 5 •Pointer expressions: Pointer Expressions produce address values. Examples:&x ptr ptr++ where x is a variable and ptr is a pointer.
  82. TYPES OF EXPRESSIONS •Bitwise expressions: Bitwise Expressions are used to manipulate data at bit level. They are basically used for testing or shifting bits. Examples:x << 3 shifts three bit position to left y >> 1 shifts one bit position to right. Shift operators are often used for multiplication and division by powers of two. An expression may also use combinations of the above expressions. Such expressions are known as compound expressions.
  83. INPUT AND OUTPUT FUNCTIONS The process of giving something to the computer is known as input. Input is mostly given through keyboard. Input functions are: scanf() gets() getch() getche() Output The process of getting something from the computer is known as output. Output is mostly displayed on monitors. Output functions are: printf() puts() The functions used for input and output are stored in the header file stdio.h. If programmer use any of the above function it is necessary to include header file.
  84. BASIC INPUT AND OUTPUT OPERATIONS Inbuilt IO functions are available in C for displaying data on the standard output device i.e. monitor and accepting the input from the standard input device i.e. keyboard. These IO functions are classified as formatted and unformatted functions based on the formatting permitted or not.
  85. FORMATTED IO FUNCTIONS There are two formatted IO functions in C namely printf() to display a data on monitor and scanf() to accept data from keyboard. printf() Syntax : printf(“Format string”, list of variables or expressions) The format string can contain the following : (a)Character set ( A-Z, a-z, 0-9, all special symbols on the keyboard) (b)Blank spaces (c)Escape sequences (d)Field width (The width of a value can be fixed by specifying the field width) (e)Format specifiers (Format specifiers specify the format of the variable data whose value is to be displayed) The format specifiers for different types of data is given in the Table
  87. FORMATTED IO FUNCTIONS printf(“The perimeter of rectangle is %5.2f”,perimeter); The format specifier in this statement is accompanied with a field width i.e. 5.2 ( %5.2f), which indicates five digits before the fraction point and 2 digits after the fraction point. This statement will display the output wherein the value of perimeter will be displayed with five digits before the fraction point and only two digits after the fraction point.
  88. FORMATTED IO FUNCTIONS This function is used to display text, constant or value of variable on screen in specified format. Syntax: printf(“format string”, argument list); Types: printf(“hello world”); // Printf()with no argument list printf(“the value of integar=%d”,a); //Printf() with one argument printf(“Sum of %d+%d=%d”,a,b,a+b); //Printf()with more than one argument
  89. FORMATTED IO FUNCTIONS Scanf The function used to get input from the user during execution of program and stored in a variable of specified form is called scanf() function. Syntax: scanf(“format string”,& variable name); format string format specifier is written & address operator Types: Single input scanf(“%d”,&a); Multiple input scanf(“%d %c %d”, &a,&op,&b);|
  90. FORMATTED IO FUNCTIONS Example-Single Input: #include<stdio.h> #include<conio.h> void main () { clrscr(); int a; printf("Enter integer value:"); scanf("%d",&a); printf("The value you enter = %d",a); getch(); } Output Enter integer value:5 The value you enter =5
  91. FORMATTED IO FUNCTIONS Example-Multiple Inputs: #include<stdio.h> #include<conio.h> void main () { clrscr(); int a, b; char op; printf("Enter two values and operator:"); scanf("%d %d %c",&a,&b,&op); printf("Entered values = %d %d %c",a,b,op); getch(); } Output: Enter two values and operator:5 6 + Entered values =5 6 +
  92. FORMATTED IO FUNCTIONS scanf() Syntax : scanf(“format String”, address of variables); Here, format string can contain format specifier, field width and assignment suppression character. The assignment suppression character (*) is used to discard one of the user entered value. The address of the variable is obtained with the help of the address of operator (&). Examples : 1.scanf(“%d”,&x); This statement is used to accept a int type value from user in the variable x 2.scanf(“%d %d %f”,&x,&y,&z); This statement is used to accept two int type data into variables x and y. It also accepts a float type data into the variable z.
  93. UNFORMATTED IO FUNCTIONS The unformatted IO functions do not have any format specifier. They are mostly used to accept and display only one character or a string (string is a set of characters to make a word or sentence). The different unformatted IO functions are listed below; 1.getch():This function is used accept one character from the user. 2.getche():This function is used accept one character from the user and echo it (display it on the screen). 3.getchar():This function is used to accept one character from the user and echo it (display it on the screen) and also wait after that for the user to press the enter key. 4.gets():This function is used to accept a string from the user. We will see in details of this when studying the chapter on strings. 5.putch() or:These functions are used to display a character on the monitor. putchar() 6.puts():This function is used to display a string on the monitor.
  94. UNFORMATTED IO FUNCTIONS Getch Function “The getch = get character. This function is used to get a single character input from the user” during execution of program. It also force to wait the output to stay on screen until any key pressed from keyboard. Syntax: For value input: Variable name= getch(); For screen holding at the end of program: getch(); Example: #include<stdio.h> #include<conio.h> void main () { clrscr(); char m; printf("Enter character:"); m=getch(); printf("The character you Entered=%c",m ); getch(); }
  95. UNFORMATTED IO FUNCTIONS Getche Function The getche() = get character echo. It is also used to get a single character from the keyboard during the program execution. When this function is executed, the character entered by the user is displayed on the screen. Syntax: Variable name=getche(); Example #include<stdio.h> #include<conio.h> void main () { clrscr(); char p; printf("enter any character:"); p=getche(); printf("the character u entered=%c",p ); getch(); }
  96. UNFORMATTED IO FUNCTIONS gets() The gets() function enables the user to enter some characters followed by the enter key. All the characters entered by the user get stored in a character array. The null character is added to the array to make it a string. The gets() allows the user to enter the space-separated strings. It returns the string entered by the user. #include<stdio.h> void main () { char s[30]; printf("Enter the string? "); gets(s); printf("You entered %s",s); }
  97. UNFORMATTED IO FUNCTIONS gets() This special function is used to input string values from the user during execution of the program. As the user press enter key when typing after the string. A null character (0) is automatically entered at the end of the string. Syntax: gets(variable name); Example: #include<stdio.h> #include<conio.h> void main () { char str[]; clrscr(); printf("Enter a string:"); gets(str); getch(); }
  98. UNFORMATTED IO FUNCTIONS puts() The puts() function is very much similar to printf() function. The puts() function is used to print the string on the console which is previously read by using gets() or scanf() function. The puts() function returns an integer value representing the number of characters being printed on the console. Since, it prints an additional newline character with the string, which moves the cursor to the new line on the console, the integer value returned by puts() will always be equal to the number of characters present in the string plus 1. #include<stdio.h> int main(){ char name[50]; printf("Enter your name: "); gets(name); //reads string from user printf("Your name is: "); puts(name); //displays string return 0; } •
  99. UNFORMATTED IO FUNCTIONS puts function The function used to display the string value on the screen. Syntax: Puts(parameter/value/variable); Example: #include<stdio.h> #include<conio.h> void main () { char name[]; printf("enter your name:"); gets(name); printf("your name is:"); puts(name); getch(); }
  100. BUILT-IN FUNCTIONS Library functions are the inbuilt function in C that are grouped and placed at a common place called the library. Such functions are used to perform some specific operations. For example, printf() is a library function used to print on the console. The library functions are created by the designers of compilers. All C standard library functions are defined inside the different header files saved with the extension .h We need to include these header files in our program to make use of the library functions defined in such header files. For example, To use the library functions such as printf()/scanf() we need to include stdio.h in our program which is a header file that contains all the library functions regarding standard input/output. Standard library functions are also known as built-in functions. Functions such as puts(), gets(), printf(), scanf() etc are standard library functions. These functions are already defined in header files (files with .h extensions are called header files such as stdio.h), so we just call them whenever there is a need to use them.
  102. BUILT-IN FUNCTIONS Example: Square root using sqrt() function Suppose, you want to find the square root of a number. To compute the square root of a number, you can use the sqrt() library function. The function is defined in the math.h header file. #include <stdio.h> #include <math.h> int main() { float num, root; printf("Enter a number: "); scanf("%f", &num); root = sqrt(num); // Computes the square root of num and stores in root. printf("Square root of %.2f = %.2f", num, root); return 0; }
  103. ADVANTAGES OF USING C LIBRARY FUNCTIONS 1. They work One of the most important reasons you should use library functions is simply because they work. These functions have gone through multiple rigorous testing and are easy to use. 2. The functions are optimized for performance Since, the functions are "standard library" functions, a dedicated group of developers constantly make them better. In the process, they are able to create the most efficient code optimized for maximum performance. 3. It saves considerable development time Since the general functions like printing to a screen, calculating the square root, and many more are already written. You shouldn't worry about creating them once again. 4. The functions are portable With ever-changing real-world needs, your application is expected to work every time, everywhere. And, these library functions help you in that they do the same thing on every computer.
  104. THANK YOU