C presentation book


Published on

c language full information on it and it is like book it is very halpful to lurn c language

Published in: Education, Technology
1 Comment
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

C presentation book

  1. 1. <ul><li>Chapter 1 Overview of “C” </li></ul><ul><li>The root of all modern programming language is ALGO, it is introduced in 1960. ALGO gave the new concept of the structure programming. Subsequently varies languages are announced. </li></ul><ul><li>In 1967 Martin Richards developed a language called BCPL( Basic Combined Programming Language) . </li></ul><ul><li>In 1970, Ken Thompson created a language using many features of BCPL and it simply called B language. </li></ul><ul><li>Both BCPL and B was “type less” system programming language. </li></ul><ul><li>C language was developed by Dennis Ritchie at the Bell Laboratories in 1972. C language uses many features of the ALGO, BCPL and B Languages. It also added new concepts of the “data type” and many powerful features. Unix operating system is totally coded in C language. </li></ul>
  2. 2. <ul><li>In 1983 American National Standard Institute (ANSI) appoint a technical committee to define a standard of C. </li></ul><ul><li>The committee approved a version of c in December 1989 which is now known as ANSI C </li></ul><ul><li>It was the approved by the International Standards Organization (ISO) in 1990.This version of the c is also referred as C89 </li></ul><ul><li>Importance of the C language: </li></ul><ul><li>It is Robust Language. It’s rich set of bit in function and operator can be used to write any complex program. </li></ul><ul><li>The C Compiler combines the features of the assembly language and high level language so it is suitable for both system software and business package. </li></ul>
  3. 3. <ul><li>Programs written in C languages are very efficient and fast because of its variety of data types and powerful operators. </li></ul><ul><li>There are several inbuilt functions which is used to build any program. </li></ul><ul><li>There are 32 keywords in C Which is used to make the program. </li></ul><ul><li>C is highly portable language. </li></ul><ul><li>C language is suitable for structure programming. This modular structure makes program debugging, testing and maintenance easier. </li></ul><ul><li>The ability to extend itself. It means we can add our own functions into the c library </li></ul>
  4. 4. <ul><li>Sample Program for C language to print hello. </li></ul><ul><li>/* Write the Program to Print the Hello */ </li></ul><ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>clrscr(); </li></ul><ul><li>printf (“Hello world”); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul><ul><li>Alt + f9 -> To compile the program </li></ul><ul><li>Ctrl + f9 -> To run the program </li></ul>
  5. 5. <ul><li>Basic Structure of the c program: </li></ul>Documentation Section Link Section Definition Section Global Declaration Section Main () Function Section { Declaration Part Executable Part } Sub Program Section Function 1 Function 2 etc..
  6. 6. <ul><li>Executing a C Program : </li></ul><ul><li>Executing a Program C Involves a Series of Steps : </li></ul><ul><li>Creating a Program </li></ul><ul><li>Compile the Program </li></ul><ul><li>Linking the Program with functions that are needed from the C library </li></ul><ul><li>Executing the Program </li></ul>
  7. 7. Enter the Program Code Compile Program with Compiler Syntax Error Link with System Library Execute Object Code Generate the Output No Yes Edit the Source Program Executable Object Code Object Code
  8. 8. <ul><li>Chapter : 2 Constants, Variable and Data Types </li></ul><ul><li>Introduction :- </li></ul><ul><li>The Sequence of the Instruction written to perform specific task is called the Program. </li></ul><ul><li>This Instructions are formed using special symbols and words according to some rigid rule is known as syntax rules. </li></ul><ul><li>Every Instruction must be written according to the syntax rule of the languages. </li></ul><ul><li>Like any other languages C languages has its vocabulary and grammar. They are follow up for making the program. </li></ul><ul><li>In this Chapter we will discuss about various constants, variables and data types are used in C Programming. </li></ul>
  9. 9. <ul><li>Character Set : </li></ul><ul><li>Character Set is the set of the character. </li></ul><ul><li>This characters are used to form the word, numbers and expression. </li></ul><ul><li>The characters in the c are grouped into the following categories. </li></ul><ul><li>Letters </li></ul><ul><li>Digits </li></ul><ul><li>Special characters </li></ul><ul><li>White Spaces. </li></ul><ul><li>Letters : Uppercase A…Z, Lowercase a…z </li></ul><ul><li>Digits : All decimal digits 0 to 9 </li></ul><ul><li>Special Character : ,(comma) .(period) ; (semicolon) </li></ul><ul><li>: (colon), & (ampersand), # (number sign) etc. </li></ul><ul><li>White spaces : Blank Space, Horizontal Space, New Line. </li></ul>
  10. 10. <ul><li>Trigraph Characters: </li></ul><ul><li>Many Non – English Keywords don’t support all the characters mentioned in character set. </li></ul><ul><li>ANSI C Introduce the concept “trigraph” Sequences to enter special character that are not available on some keyboard. </li></ul><ul><li>Each trigraph sequence consists of three characters (two question mark followed by a character) </li></ul><ul><li>Trigraph Sequence Translation </li></ul><ul><li>??= # nubersign </li></ul><ul><li>??( [ left bracket </li></ul><ul><li>??) ] right bracket </li></ul><ul><li>??< { left braces </li></ul><ul><li>??> } Right braces </li></ul>
  11. 11. <ul><li>C Tokens: </li></ul><ul><li>Smallest Individual units are known as C Tokens. </li></ul><ul><li>There are six types of the C Tokens. </li></ul><ul><li>Keywords </li></ul><ul><li>Identifiers </li></ul><ul><li>Constants </li></ul><ul><li>String </li></ul><ul><li>Special Symbols </li></ul><ul><li>Operators. </li></ul><ul><li>Keywords: </li></ul><ul><li>Every C word is classified as either keyword or identifier. </li></ul><ul><li>All keywords have fixed meanings and you can not change its meanings. </li></ul><ul><li>Keywords serve as a basic building blocks for program statement. ANSI C supports 32 keywords that are listed in ANSI C book. </li></ul>
  12. 12. <ul><li>Ex: int, float, double, extern, static, auto, continue, if, goto short, long etc. are the keywords </li></ul><ul><li>Identifiers: </li></ul><ul><li>Identifiers refer to the names of variables, functions and arrays. </li></ul><ul><li>These are user defined names and consists of sequence of letters and digits. </li></ul><ul><li>Both uppercase and lowercase letters are permitted to make the identified but generally lowercase letters are used to make the variable. </li></ul><ul><li>Rules for Identifiers: </li></ul><ul><li>First character must be alphabet </li></ul><ul><li>Must not contain white space </li></ul><ul><li>Only first 31 characters are significant </li></ul><ul><li>Can not use keyword as a identifier </li></ul>
  13. 13. <ul><li>Constants: </li></ul><ul><li>Constants referred as a fixed value that don't change during the execution of the program. </li></ul><ul><li>C Support Several types of constants: </li></ul><ul><li>Constants </li></ul><ul><li>Numeric Constants </li></ul><ul><li>Integer constants </li></ul><ul><li>Real Constants </li></ul><ul><li>Character Constants </li></ul><ul><li>Single Character Constants </li></ul><ul><li>String Constants </li></ul>
  14. 14. <ul><li>Integer Constants: </li></ul><ul><li>An integer constants refers as a sequence of digits. </li></ul><ul><li>There are three types of integer constants. </li></ul><ul><li>Decimal Integer </li></ul><ul><li>Octal Integer </li></ul><ul><li>Hexadecimal </li></ul><ul><li>Decimal Integer : </li></ul><ul><li>It consists of 0-9 digits, preceded by an optional – or + sign. </li></ul><ul><li>Valid example of decimal integer are : </li></ul><ul><li>123 , -321, 0 , 654321, +78 </li></ul><ul><li>Embedded spaces, comma and non digit characters are not permitted between digits. </li></ul><ul><li>15 750, 20,000, $1000 are Illegal </li></ul>
  15. 15. <ul><li>Octal Integer: </li></ul><ul><li>An Octal Integers consists of digits 0-7 with a leading 0. </li></ul><ul><li>Ex: </li></ul><ul><li>037, 0, 0435, 0551 </li></ul><ul><li>Hexadecimal Integer: </li></ul><ul><li>A sequence of digits preceded by 0x or OX is considered as Hexadecimal Integer. </li></ul><ul><li>Hexadecimal Integer includes 0 to 9 digits and A to F letters. </li></ul><ul><li>Ex: 0x9, 0x9F, OXbcd etc are valid. </li></ul><ul><li>Real Constants: </li></ul><ul><li>Integer numbers are inadequate to represent quantities such as distance, heights, temperature, price and so on. These quantities are represented by a number containing fractional parts like 12.32. Such numbers are called real constants </li></ul>
  16. 16. <ul><li>These numbers having a whole number followed by a decimal digits. </li></ul><ul><li>ex: 0.85, -0.75, 85.45, +241.54 </li></ul><ul><li>It is possible to omit the digit before the decimal digit and after the decimal digit. </li></ul><ul><li>ex: 215., .23 , -.71 , +54. </li></ul><ul><li>A real number may also be expressed in exponential (Scientific) notation. </li></ul><ul><li>General form : mantissa e exponent </li></ul><ul><li>The mantissa is either a real number or integer number. </li></ul><ul><li>Exponent is an integer number with + or – sign. </li></ul><ul><li>The letter e separating mantissa and exponent can be written either in lowercase or in uppercase letter. </li></ul>
  17. 17. <ul><li>ex : 215.65 may be written as 2.1565e2 in exponential notation. e2 means multiple by 10 2 </li></ul><ul><li>75000 written as 7.5E4 or 7.5E+4 </li></ul><ul><li>-0.00038 written as -3.8e-4 </li></ul><ul><li>Comma, White space and dollar space is not permitted in digits. </li></ul><ul><li>25,0.000 , 7.1 e 4 , 1.5 E 2.5 (exponent must be an integer), </li></ul><ul><li>$255. </li></ul><ul><li>Single character constants: </li></ul><ul><li>A single character constant contains a single character enclosed with a pair of single quotation mark (‘ ’). </li></ul><ul><li>ex: ‘5’, ‘x’, ‘;’ , ‘ ’ </li></ul><ul><li>Note that character constant ‘5’ is not same as number 5. </li></ul><ul><li>character constant have a integer value known as ASCII value </li></ul>
  18. 18. <ul><li>ex : printf (“%d”, ‘a’) would print the number 97. </li></ul><ul><li>printf(“%c”, ‘97’) would print the letter a. </li></ul><ul><li>String Constants : </li></ul><ul><li>A String Constant is a sequence of characters enclosed in double quotation mark. </li></ul><ul><li>ex: “Hello” , “1987”, “Well Done”, “X” </li></ul><ul><li>Note : A single String constant does not have an equivalent integer value, while a character constant has an equivalent integer value. </li></ul><ul><li>Backslash character constant: </li></ul><ul><li>Backslash character constant are used in output function. </li></ul><ul><li>ex: ‘n’ – new line, ‘t’ – horizontal tab, ‘’’-single quote etc. </li></ul>
  19. 19. <ul><li>VARIABLE : </li></ul><ul><li>A variable is a data name that may be used to store a data value. </li></ul><ul><li>Unlike constants that remain unchanged during the execution of the program, a variable may take different value at different time. </li></ul><ul><li>Rules to declare a variable : </li></ul><ul><li>They must begin with letter </li></ul><ul><li>variable length must be 31 character significant. </li></ul><ul><li>Both lowercase and uppercase letters are distinct. ex Total and total are not same </li></ul><ul><li>It should not be a keyword </li></ul><ul><li>White space, Dollar sign are not allowed. </li></ul><ul><li>ex: John, delhi, First_tag, int_type ---- valid </li></ul><ul><li>Price$, char, group one, 123, 25 th , (area) ------- Invalid </li></ul>
  20. 20. <ul><li>Declaration of Variable: </li></ul><ul><li>Syntax : data_type v1,v2,v3….vn ; </li></ul><ul><li>where v1,v2..vn are different variable name. </li></ul><ul><li>Ex: int count; </li></ul><ul><li>int number, total; </li></ul><ul><li>double ratio; </li></ul><ul><li>float price; </li></ul><ul><li>char c; </li></ul><ul><li>where int, double, float and char are data type. </li></ul><ul><li>Assign Value to a Variable : </li></ul><ul><li>Assignment operator (=) is used to assign value to a variable. </li></ul><ul><li>Ex: price = 12.50; ratio = 12.2345; number=12; </li></ul><ul><li>c=‘a’; </li></ul>
  21. 21. <ul><li>Data Types: </li></ul><ul><li>C language is rich in its data types: </li></ul><ul><li>ANSI C support three classes of data types. </li></ul><ul><li>1. Primary Data Type </li></ul><ul><li>2. Derived Data Type </li></ul><ul><li>3. User-Defined Data Type. </li></ul>
  22. 22. <ul><li>Primary Data Type : </li></ul>Integral Type Integer Character Signed int short int Long int Unsigned unsigned int unsigned short int unsigned Long int char signed char unsigned char Floating Point Type float double long double void
  23. 23. <ul><li>All C compiler support five fundamental data types, namely integer(int), character(char), floating point(float), double-precision floating point(double) and void. </li></ul><ul><li>Integer types: </li></ul><ul><li>Integers are whole numbers with a range of values supported by a particular machine. </li></ul><ul><li>There are signed integer and unsigned integer. Signed integer uses one bit for sign and 15 bits for magnitude of the number. Unsigned integers are always positive. It does not contain any bit for sign so that it occupies all the bit for the magnitude of the number. (Note : For 16 bit machine) </li></ul><ul><li>By using equation -2 n to +2 n -1 we can find out the range of the number. Where n is the number of bits. </li></ul>
  24. 24. <ul><li>To provide some control over the range of number and storage space, C has three classes of integer storage, namely short int, int and long int in both signed and unsigned forms. </li></ul><ul><li>Size and Range of Data Type on a 16-bit machine. </li></ul><ul><li>Type size(bits) Range </li></ul><ul><li>int or signed int 16 -2 15 to 2 15 -1 </li></ul><ul><li>unsigned int 16 0 to 2 16 -1 </li></ul><ul><li>short int or 8 -2 7 to 2 7 -1 </li></ul><ul><li>signed short int </li></ul><ul><li>unsigned short int 8 0 to 2 8 -1 </li></ul><ul><li>long int or 32 -2 31 to 2 31 -1 </li></ul><ul><li>signed long int </li></ul><ul><li>unsigned long int 32 0 to 2 32 -1 </li></ul>
  25. 25. <ul><li>Floating Point Types: </li></ul><ul><li>Floating point numbers are stored in 32 bits (on all 16bit and 32 bit machines) with 6 digits of precision. </li></ul><ul><li>Floating point numbers are defined in C by the keyword float. </li></ul><ul><li>When the accuracy provided by float is not sufficient double data type is used. It uses 64 bits giving a precision of 14 digits. </li></ul><ul><li>When you want to extend more precision you can use the long double data type. it uses 80 bits </li></ul><ul><li>Type size(bits) Range </li></ul><ul><li>float 32 3.4E-38 to 3.4E+38 </li></ul><ul><li>double 64 1.7E-308 to 1.7E+308 </li></ul><ul><li>long double 80 3.4E-4932 to 1.1E+4932 </li></ul>
  26. 26. <ul><li>Void Types : </li></ul><ul><li>Void type has no values. </li></ul><ul><li>Void type does not return any values. </li></ul><ul><li>Character Type: </li></ul><ul><li>Character type data type is char. </li></ul><ul><li>It’s storage is 8bit to store a bit. </li></ul><ul><li>The qulifier signed and unsigned explicitly applied on character type </li></ul><ul><li>Type size (bits) range </li></ul><ul><li>char or signed char 8 -2 7 to 2 7 -1 </li></ul><ul><li>unsigned char 8 0 to 2 8 -1 </li></ul>
  27. 27. <ul><li>Note : </li></ul><ul><li>Integer constants by default represent int data type. We can override this default by specifying unsigned or long after the number (by appending U or L). </li></ul><ul><li>ex: </li></ul><ul><li>Literal Type value </li></ul><ul><li>+111 int 111 </li></ul><ul><li>-222 int 222 </li></ul><ul><li>45678U unsigned int 45,678 </li></ul><ul><li>-56789L long int 56,789 </li></ul><ul><li>987654UL unsigned long int 9,87,654 </li></ul><ul><li>similarly floating point constants by default it will take double data type. To override we must append f or F for float type and l or L for long double type. </li></ul><ul><li>ex: </li></ul><ul><li> Literal Type value </li></ul><ul><li>1.234 double 1.234 </li></ul><ul><li>-1.2f float -1.2 </li></ul><ul><li>1.23456789L long double 1.23456789 </li></ul>
  28. 28. <ul><li>/* Program To Show the declaration, assignments and values stored in varies types of variables */ </li></ul><ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>float x; double q ; unsigned k ; int m =5431; </li></ul><ul><li>long int n = 1234567890; </li></ul><ul><li>x=1.234567890000; q= 1.0 ; k=54321; </li></ul><ul><li>printf(“%d”, m); printf(“%ldn”, n); printf(“%fn”, x); </li></ul><ul><li>printf(“%lf”,x); printf(“%lfn”,q); printf(“%u”,k); </li></ul><ul><li>} </li></ul>
  29. 29. <ul><li>Defining Symbolic Constants: </li></ul><ul><li>Those constants which are repeatedly used in the programs are defined as a symbolic constants. </li></ul><ul><li>Because of Symbolic constants we can solve problems like: </li></ul><ul><li>Problem in modification of the program </li></ul><ul><li>Problem in understanding the program </li></ul><ul><li>Syntax: </li></ul><ul><li>#define symbolic_name value_of_constant </li></ul><ul><li>Ex: </li></ul><ul><li>#define PI 3.14 </li></ul><ul><li>#define MAX 200 </li></ul><ul><li>#define PASS_MARK 50 </li></ul>
  30. 30. <ul><li>Rules to define the symbolic constants: </li></ul><ul><li>Symbolic names are written in Capital letters to visually distinguish them from the normal variables. </li></ul><ul><li>No blank space between the pound sign ‘#’ and the word define is permitted. </li></ul><ul><li>‘ #’ must be the first character in the line. </li></ul><ul><li>A blank space is required between #define and symbolic name. </li></ul><ul><li>#define statement must not end with a semicolon </li></ul><ul><li>Assignment operator is not used to assign a value to the symbolic name. </li></ul><ul><li>A statement can define only one name </li></ul><ul><li>Invalid define statement: </li></ul><ul><li>#define X=2.5, # define MAX 10, #define N 25; , </li></ul><ul><li>#define PI , R 3.14 , #define PRICE$ 100 </li></ul>
  31. 31. <ul><li>Declaring a variable as constant: </li></ul><ul><li>Syntax: </li></ul><ul><li>const data_type variable_name = value ; </li></ul><ul><li>Ex: </li></ul><ul><li>const int class_size=40; </li></ul><ul><li>Using const keyword we can made any variable’s value constant and because of that we cant change the value of that variable during the execution time. </li></ul><ul><li>Declaring a variable as volatile: </li></ul><ul><li>Qualifier volatile that could be used to tell explicitly the compiler that a variable’s value may be changed at any time by some external source. </li></ul><ul><li>Syntax: </li></ul><ul><li>volatile int date. </li></ul>
  32. 32. <ul><li>User-Defined Type Declaration: </li></ul><ul><li>Using typedef keyword we can define our own user defined data type. </li></ul><ul><li>Syntax: </li></ul><ul><li>typedef type identifier; </li></ul><ul><li>Ex: </li></ul><ul><li>typedef int marks; </li></ul><ul><li>marks sub1,sub2; </li></ul><ul><li>Another user defined data type is enum (enumeration) which can be used to declare variable that can have one of the values enclosed within the braces (Known as enumeration constants) </li></ul><ul><li>The compiler automatically assigns integer digits beginning with 0 to all the enumeration constants. </li></ul>
  33. 33. <ul><li>Syntax: </li></ul><ul><li>enum identifier {value1, value2, value3,….,valueN}; </li></ul><ul><li>Example: </li></ul><ul><li>enum day {Monday, Tuesday,……..,Sunday}; </li></ul><ul><li>enum day week_st, week_end; </li></ul><ul><li>week_st = Monday; </li></ul><ul><li>week_end = Friday; </li></ul><ul><li>If(week_st == Tuesday) </li></ul><ul><li>week_end = Saturday; </li></ul><ul><li>enum day {Monday=1,Tuesday,….., Sunday}; </li></ul><ul><li>Here Monday = 1 constant value so the remaining constants are assigned value that increase successively by 1. </li></ul><ul><li>Declaration combined as follow: </li></ul><ul><li>enum day {Monday,…..,Sunday} week_st, week_end; </li></ul>
  34. 34. <ul><li>Read data from keyboard: </li></ul><ul><li>Using Scanf function we can read data from the keyboard. </li></ul><ul><li>Syntax: </li></ul><ul><li>scanf(“control string”,&variable1,&variable2);. </li></ul>
  35. 35. <ul><li>Chapter 3 Operators and Expression </li></ul><ul><li>C support a rich set of built in operator. </li></ul><ul><li>An operator is a symbol that tells the computer to perform certain mathematical or logical manipulations. </li></ul><ul><li>C operators are classified into following categories: </li></ul><ul><li>Arithmetic operator </li></ul><ul><li>Relational operator </li></ul><ul><li>Logical operator </li></ul><ul><li>Assignment operator </li></ul><ul><li>Increment and Decrement operator </li></ul><ul><li>Conditional operator </li></ul><ul><li>Bitwise operator </li></ul><ul><li>Special operator. </li></ul>
  36. 36. <ul><li>Arithmetic operator: </li></ul><ul><li>+(Addition), -(Subtraction), *(Multiplication), /(Division) , %(Modulat Division). All this are arithmetic operators. </li></ul><ul><li>ex: </li></ul><ul><li>a-b a+b </li></ul><ul><li>a*b a/b </li></ul><ul><li>a%b -a*b </li></ul><ul><li>Here a and b are variables and are known as operands. </li></ul><ul><li>Note : Modular operator can not be used in the floating point operation. </li></ul><ul><li>Arithmetic operator has three types: </li></ul><ul><li>(1) Integer Arithmetic </li></ul><ul><li>(2) Real Arithmetic </li></ul><ul><li>(3) Mixed mode Arithmetic </li></ul>
  37. 37. <ul><li>Integer Arithmeic: </li></ul><ul><li>When both the operands in a arithmetic expressions are integer, the expression is called an integer expression, and the operation is called integer arithmetic. </li></ul><ul><li>If a and b both are integer than for a=14 and b=4 we have following results: </li></ul><ul><li>a – b = 10 </li></ul><ul><li>a + b = 18 </li></ul><ul><li>a * b = 56 </li></ul><ul><li>a / b = 3 (Decimal part truncated) </li></ul><ul><li>a % b = 2 (remainder of division) </li></ul><ul><li>During integer division,if both the operands are of the same sign,the result is truncated towards the zero. If one of them is negative, the direction of truncation is implementation dependent. That is, </li></ul><ul><li>6/7 = 0 and -6/-7 = 0 </li></ul>
  38. 38. <ul><li>Similarly during modular division, the sign of the result is always the sign of the first operand (dividend). That is </li></ul><ul><li>-14%3 = -2 </li></ul><ul><li> -14%-3 = -2 </li></ul><ul><li> 14%-3 = 2 </li></ul><ul><li>Ex : /* Program to convert a given number of days into months and days*/ </li></ul><ul><li>main() </li></ul><ul><li>{ </li></ul><ul><li>int month, day; </li></ul><ul><li>clrscr(); </li></ul><ul><li>scanf(“%d”,&day); </li></ul><ul><li>month = day/30; </li></ul><ul><li>day = day%30; </li></ul><ul><li>printf(“Month = %d Days = %d”, month,day); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul>
  39. 39. <ul><li>Real Arithmetic : </li></ul><ul><li>An arithmetic expression involving only real operands is called real arithmetic. A real operands may assume values either in decimal or exponential notation. </li></ul><ul><li>If x,y,z are floats, then we will have: </li></ul><ul><li>x = 6.0/7.0 = 0.85713 </li></ul><ul><li>y = -2.0/3.0 = -0.666667 </li></ul><ul><li>the operator % can not be used with real operands. </li></ul><ul><li>Mixed Mode Arithmetic: </li></ul><ul><li>When one of the operand is real and the other is integer, the expression is called the mixed mode arithmetic expression. If either operand is real type then only real operation is performed and the result is always a real number. Thus </li></ul><ul><li>15/ 10.0 = 1.5 </li></ul><ul><li>where as </li></ul><ul><li>15/10 = 1 </li></ul>
  40. 40. <ul><li>Relational operators: </li></ul><ul><li>Whenever you want to compare two entities at that time you can use the Relational operator. </li></ul><ul><li>An expression containing a relational operator is known as relational expression. </li></ul><ul><li>C Support six type of the relational operators: </li></ul><ul><li>operator Meaning </li></ul><ul><li>< less than </li></ul><ul><li><= is less than or equal to </li></ul><ul><li>> greater than </li></ul><ul><li>>= is greater than or equal to </li></ul><ul><li>== is equal to </li></ul><ul><li>!= is not equal to </li></ul><ul><li>Relational operators are generally used in the decision making statements like if and while and it returns value true and false </li></ul>
  41. 41. <ul><li>Ex: 4.5 <= 10 true </li></ul><ul><li>a+b == c+d true if equal else false </li></ul><ul><li>10< 7+5 true </li></ul><ul><li>-35 >= 0 false </li></ul><ul><li>Logical Operators: </li></ul><ul><li>Logical operators are generally used when you want to test more than one condition. </li></ul><ul><li>There are three types of the logical operator: </li></ul><ul><li>Logical And && </li></ul><ul><li>Logical Or || </li></ul><ul><li>Logical Not ! </li></ul><ul><li>Ax expression of this kind, which combines two or more relational expressions, is termed as a logical expression or a compound relational expression. </li></ul>
  42. 42. <ul><li>Truth Table: </li></ul><ul><li>Op-1 Op-2 value of the expression </li></ul><ul><li>op-1 && op-2 op1 || op2 </li></ul><ul><li>1 1 1 1 </li></ul><ul><li>1 0 0 1 </li></ul><ul><li>0 1 0 1 </li></ul><ul><li>0 0 0 0 </li></ul><ul><li>Example : </li></ul><ul><li>if(age > 55 && salary < 1000) </li></ul><ul><li>if(number < 0 || number > 100) </li></ul><ul><li>Assignment Operators: </li></ul><ul><li>Assignment operators are used to assign the result of an expression to a variable. We have seen the usual assignment operator ‘=’. In addition c has a ‘shorthand’ assignment operators of the form. </li></ul>
  43. 43. <ul><li>Syntax: </li></ul><ul><li>v op = exp </li></ul><ul><li>Where v is a variable, exp is an expression and op is a c binary arithmetic operator. </li></ul><ul><li>Is equivalent to : </li></ul><ul><li>V = v op (exp) </li></ul><ul><li>Ex: x+=1 is equivalent to x= x+1 </li></ul><ul><li>Statement with simple Statement with shorthand </li></ul><ul><li>Assignment operator operator. </li></ul><ul><li>a=a+1 a+=1 </li></ul><ul><li>a=a-1 a-=1 </li></ul><ul><li>a=a*(n+1) a*=n+1 </li></ul><ul><li>Use of the shorthand assignment operator: </li></ul><ul><li>Left hand side need not to be repeated so easier to write </li></ul><ul><li>Statement is easier to read </li></ul><ul><li>Statement is more efficient. </li></ul>
  44. 44. <ul><li>Increment(++) and Decrement(--) Operator: </li></ul><ul><li>++ operator add 1 to the operand, while –- subtracts 1. Both are unary operator and takes the both form. </li></ul><ul><li>++m or m++ </li></ul><ul><li>--m or m-- </li></ul><ul><li>We generally used this statement in the for and while loop. </li></ul><ul><li>++m and m++ both are different thing when they are used in the expression. </li></ul><ul><li>For m=5; </li></ul><ul><li>Y= ++m In this case value of y and m would be 6 </li></ul><ul><li>Y= m++ In this case vale of y would be 5 and m would be 6. </li></ul><ul><li>Increment and decrement statement is used in the complex statement: </li></ul><ul><li>M= n++ -j +10 but old value of the n is incremented after the evaluation of the equation </li></ul>
  45. 45. <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int n,k,m; </li></ul><ul><li>clrscr(); </li></ul><ul><li>k=1,m=2; </li></ul><ul><li>n= k++ + m; </li></ul><ul><li>printf(&quot;%d&quot;,k); </li></ul><ul><li>printf(&quot;%d&quot;,n); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul><ul><li>Output:k=2, n=3 </li></ul>
  46. 46. <ul><li>Conditional operator: </li></ul><ul><li>Conditional operator is also called the “ternary operator”. </li></ul><ul><li>Syntax: </li></ul><ul><li>exp1 ? exp2 : exp3 </li></ul><ul><li>Ex: </li></ul><ul><li>a = 10; </li></ul><ul><li>b = 15; </li></ul><ul><li>X = (a>b) ? a : b; </li></ul><ul><li>Its same as </li></ul><ul><li>if (a > b) </li></ul><ul><li>x = a; </li></ul><ul><li>else </li></ul><ul><li>x = b; </li></ul>
  47. 47. <ul><li>Bitwise Operator : </li></ul><ul><li>Whenever you want to manipulate your data at that time you can use the Bitwise operator. </li></ul><ul><li>These operators are generally used to testing the Bits, or shifting them left or right. </li></ul><ul><li>Bitwise operator can not be applied to float or double. </li></ul><ul><li>List of the Bitwise operators and their meanings are as follow. </li></ul><ul><li>Operator Meaning </li></ul><ul><li>& bitwise AND </li></ul><ul><li>| bitwise OR </li></ul><ul><li>^ bitwise exclusive OR </li></ul><ul><li><< shift left </li></ul><ul><li>>> shift right </li></ul>
  48. 48. <ul><li>Special Operator : </li></ul><ul><li>C Support some special operator like comma, and sizeof operator. </li></ul><ul><li>Comma Operator : </li></ul><ul><li>Comma Operator can be used to link the related expression together. </li></ul><ul><li>Ex: </li></ul><ul><li>Value = (x=10, y=5, x+y); </li></ul><ul><li>In for loop. </li></ul><ul><li>A list of expressions are evaluated from left to right direction. </li></ul><ul><li>Ex: </li></ul><ul><li>for(n=1,n<=10;n++); </li></ul><ul><li>The sizeof Operator: </li></ul><ul><li>Sizeof operator is a compile time operator. It returns number of bytes occupied by the operand. The operand may be a variable, constant or a data type. </li></ul>
  49. 49. <ul><li>Ex: </li></ul><ul><li>m = sizeof(sum); </li></ul><ul><li>n = sizeof(long int); </li></ul>
  50. 50. <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int a,b,c,d; </li></ul><ul><li>clrscr(); </li></ul><ul><li>a=15, b= 10; </li></ul><ul><li>c= ++a - b; // value of a is increamented before use in expression </li></ul><ul><li>printf(&quot;a=%d b=%d c=%dn&quot;, a,b,c); </li></ul><ul><li>d= b++ + a; // value of b is increament after use in expression </li></ul><ul><li>printf(&quot;na = %d b = %d d = %d&quot;,a,b,d); </li></ul><ul><li>printf(&quot;n a/b = %d&quot;,a/b); </li></ul><ul><li>printf(&quot;n a%b = %d&quot;,a%b); </li></ul><ul><li>printf(&quot;n a*=b = %d&quot;, a*=b); </li></ul><ul><li>printf(&quot;%dn&quot;, (c>d) ? 1:0); </li></ul><ul><li>printf(“%d”,sizeof(int)); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul><ul><li>Output : </li></ul><ul><li>a=16 b=10 c=6 </li></ul><ul><li>a = 16 b = 11 d = 26 </li></ul><ul><li>a/b = 1 </li></ul><ul><li>a%b = 5 </li></ul><ul><li>a*=b = 176 </li></ul><ul><li>0 </li></ul><ul><li>2 </li></ul>
  51. 51. <ul><li>ARITHMETIC EXPRESSION : </li></ul><ul><li>An arithmetic expression is a combination of variables, constants, and operators arranged as per the syntax of the language. </li></ul><ul><li>Example : </li></ul><ul><li>Algebric Exprssion C Expression </li></ul><ul><li>a x b – c a * b – c </li></ul><ul><li>(m+n)(x+y) (m+n) * (x+y) </li></ul><ul><li>ab/c a*b/c </li></ul><ul><li>3x 2 +x +1 3*x*x + x + 1 </li></ul><ul><li>EVALUATION OF EXPRESSION : </li></ul><ul><li>Syntax : variable = expression; </li></ul><ul><li>Ex: x = a * b – c; z = a – b / c – d; </li></ul>
  52. 52. <ul><li>Precedence of Arithmetic operators: </li></ul><ul><li>High Priority : * / % </li></ul><ul><li>Low Priority : + - </li></ul><ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>float a,b,c,x,y,z; </li></ul><ul><li>clrscr(); </li></ul><ul><li>a = 9; b = 12; c = 3; </li></ul><ul><li>x = a -b / 3 + c * 2 -1; </li></ul><ul><li>y = a -b / (3 + c) * (2-1); </li></ul><ul><li>z = a -(b / (3 + c) *2) -1; </li></ul><ul><li>printf(&quot;x = %f y = %f z = %f&quot;, x,y,z); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul><ul><li>Output : x =10.000000 , y = 7. 000000 , z = 4. 000000 </li></ul>
  53. 53. <ul><li>Rules for Evaluation Expression : </li></ul><ul><li>First, parenthesized sub expression from left to right are evaluated </li></ul><ul><li>If parentheses are nested, the evaluation begins with the innermost sub expressions </li></ul><ul><li>The associativity rule is applied when two or more operator of the same precedence level appear in sub expression. </li></ul><ul><li>The precedence rule is applied to determine the order of operator in evaluating expression. </li></ul><ul><li>Arithmetic operators are evaluated left to right using the rules of precedence </li></ul><ul><li>When parentheses are used, the expressions within parentheses assume highest priority. </li></ul>
  54. 54. <ul><li>Type Conversion in Expression : </li></ul><ul><li>There are two types of the type conversions: </li></ul><ul><li>Implicit Type Conversion </li></ul><ul><li>Explicit Type Conversion </li></ul><ul><li>Implicit Type Conversion : </li></ul><ul><li>When data value automatically convert from one type to another type is called the Imlicit type conversion. </li></ul><ul><li>If the operands are of different types, the lower type is automatically converted to the higher type before the operation proceeds. </li></ul><ul><li>Ex: </li></ul><ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int a,b; </li></ul><ul><li>float c; </li></ul><ul><li>a= 12,b=13; </li></ul><ul><li>c= a+b; // automatic type conversion </li></ul><ul><li>printf(&quot;%f&quot;,c); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul><ul><li>/* Output 25.00000 */ </li></ul>
  55. 55. <ul><li>Explicit Type Conversion : </li></ul><ul><li>When a process of the conversion done manually of locally then this process is called the explicit type conversion process or casting operation. </li></ul><ul><li>Syntax : (type_name) expression : </li></ul><ul><li>Ex: </li></ul><ul><li>x = (int) 7.5; a = (int) 21.3 / (int)4.5 </li></ul><ul><li>b= (double) sum /n y = (int) (a+b); </li></ul><ul><li>z = (int) a + b; </li></ul><ul><ul><ul><li>#include<stdio.h> </li></ul></ul></ul><ul><ul><ul><li>#include<conio.h> </li></ul></ul></ul><ul><ul><ul><li>void main() </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><li>float a,b; </li></ul></ul></ul><ul><ul><ul><li>int c; clrscr(); </li></ul></ul></ul><ul><ul><ul><li>a=12,b=13; </li></ul></ul></ul><ul><ul><ul><li>c= (int) (a+b); // Type Conversion float to int </li></ul></ul></ul><ul><ul><ul><li>printf(&quot;%f&quot;,c); </li></ul></ul></ul><ul><ul><ul><li>getch(); </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><li>/* Output 25 */ </li></ul><ul><li>Note : whenever you try to convert higher data type to lower type your result will be loss. </li></ul>
  56. 56. <ul><li>Chapter : 4 Managing Input Output Operation </li></ul><ul><li>Reading a character : </li></ul><ul><li>Using getchar() function you can get a single character from the screen. </li></ul><ul><li>Syntax: </li></ul><ul><li>Variable_name = getchar(); </li></ul><ul><li>Ex: char name; </li></ul><ul><li>name = getchar(); </li></ul><ul><li>Writing a character : </li></ul><ul><li>Using putchar() function you can print the character which is stored in the variable on to the output screen. </li></ul><ul><li>Syntax: putchar(variable_name); </li></ul><ul><li>Ex: char answer; </li></ul><ul><li>answer = ‘y’ </li></ul><ul><li> putchar (answer); </li></ul>
  57. 57. <ul><li>Character Functions: </li></ul><ul><li>C Support many character functions. These character functions contained in the file ctype.h and therefore the statement </li></ul><ul><li>#include<ctype.h> must be include in the program to access this character function. </li></ul><ul><li>Character set functions: </li></ul><ul><li>isalnum(c) - Is c an alphanumeric character </li></ul><ul><li>isalpha( c ) - Is c an alphabetic character </li></ul><ul><li>isdigit( c) - Is c a digit. </li></ul><ul><li>islower( c ) - Is c lower case letter </li></ul><ul><li>isupper( c ) - Is c Upper Case letter </li></ul><ul><li>isspace( c ) - Is c a white space character. </li></ul><ul><li>ispunct( c ) - Is c a punctual mark </li></ul><ul><li>toupper() - convert lowercase argument into uppercase letter and function </li></ul><ul><li>tolower() – convert uppercase letter into the lowercase letter. </li></ul>
  58. 58. <ul><ul><li>Example : </li></ul></ul><ul><ul><li>/* Write the Program To Convert uppercase to lowercase and lowercase letter to uppercase letter */ </li></ul></ul><ul><ul><li>#include<stdio.h> </li></ul></ul><ul><ul><li>#include<conio.h> </li></ul></ul><ul><ul><li>void main() </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>char alpha; </li></ul></ul><ul><ul><li>clrscr(); </li></ul></ul><ul><ul><li>printf(&quot;Enter a alphabet :&quot;); </li></ul></ul><ul><ul><li>alpha = getchar(); </li></ul></ul><ul><ul><li> if(islower(alpha)) </li></ul></ul><ul><ul><li>putchar(toupper(alpha)); </li></ul></ul><ul><ul><li>else </li></ul></ul><ul><ul><li>putchar(tolower(alpha)); </li></ul></ul><ul><ul><li>getch(); </li></ul></ul><ul><ul><li>} </li></ul></ul>
  59. 59. <ul><li>Chapter 5 : Decision Making and Branching </li></ul><ul><li>if statement </li></ul><ul><li>switch statement </li></ul><ul><li>Conditional operator statement </li></ul><ul><li>goto statement </li></ul><ul><li>All the above statements are known as decision – making statement. </li></ul><ul><li>These statements ‘control’ the flow of the execution, they are also known as control </li></ul><ul><li>statements. </li></ul><ul><li>Decision Making with If statement : </li></ul><ul><li>If statement used to control the flow of the execution. </li></ul><ul><li>Syntax: if(test_expression) </li></ul><ul><li> { </li></ul><ul><li> statement-block; </li></ul><ul><li>} </li></ul><ul><li>statement-x; </li></ul><ul><li>If test_expression condition is true execute the statement_block statements. </li></ul>
  60. 60. <ul><li>Ex: </li></ul><ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int a,b,c,d; </li></ul><ul><li>float ratio; </li></ul><ul><li>clrscr(); </li></ul><ul><li>scanf(“%d %d %d %d”,&a,&b,&c,&d); </li></ul><ul><li>if(c-d != 0) </li></ul><ul><li>{ </li></ul><ul><li>ratio = (float) (a+b)/(float) (c-d); </li></ul><ul><li>printf(“Ratio = %fn”,ratio); </li></ul><ul><li>} </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul>
  61. 61. <ul><li>The If….Else Statement : </li></ul><ul><li>The if…else statement is an extension of the if statement. It is generally used whenever you want to execute the condition’s true and false part. </li></ul><ul><li>Syntax : </li></ul><ul><li>If(test_expression) </li></ul><ul><li>{ </li></ul><ul><li>true- block statements; </li></ul><ul><li>} </li></ul><ul><li>else </li></ul><ul><li>{ </li></ul><ul><li>false – block statements; </li></ul><ul><li>} </li></ul><ul><li>statement – x; </li></ul><ul><li>If test_expression condition is true execute the true-block statements else execute the </li></ul><ul><li>false – block statements. </li></ul>
  62. 62. <ul><li>Ex: </li></ul><ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int a,b; </li></ul><ul><li>clrscr(); </li></ul><ul><li>scanf(“%d %d”, &a, &b); </li></ul><ul><li>if (a>b) </li></ul><ul><li>printf(“A is bigger than B : %d”,a); </li></ul><ul><li>else </li></ul><ul><li>printf(“B is bigger tha A : %d”,b); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul>
  63. 63. <ul><li>Nesting of If…Else statements: </li></ul><ul><li>When a series of decisions are involved, we may have to use more than one if…else </li></ul><ul><li>statements in a program in nested form is called the nesting of if…else statements. </li></ul><ul><li>Syntax: </li></ul><ul><li>if(test_condition 1) </li></ul><ul><li>{ </li></ul><ul><li>if(test_condition 2) </li></ul><ul><li>{ </li></ul><ul><li>statement – 1; </li></ul><ul><li>} </li></ul><ul><li>else </li></ul><ul><li>{ </li></ul><ul><li>statement – 2; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>else </li></ul><ul><li>{ </li></ul><ul><li>statement – 3; </li></ul><ul><li>} </li></ul><ul><li>Statement – x; </li></ul>
  64. 64. <ul><li>Ex: </li></ul><ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>float a,b,c; clrscr(); </li></ul><ul><li>scanf(“%f %f %f”,&a,&b,&c); </li></ul><ul><li>if(a>b) </li></ul><ul><li>{ </li></ul><ul><li>if(a>c) </li></ul><ul><li> printf(“%fn”,a); </li></ul><ul><li>else </li></ul><ul><li> printf(“%fn”,c); </li></ul><ul><li>} </li></ul><ul><li>else </li></ul><ul><li>{ </li></ul><ul><li>if(c>b) </li></ul><ul><li> printf(“%fn”,c); </li></ul><ul><li>else </li></ul><ul><li> printf(“%fn”,b); </li></ul><ul><li>} </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul>
  65. 65. <ul><li>Else If Ladder: </li></ul><ul><li>When we want to take multiple decision, putting ifs together. This construction is known as the else..if ladder. </li></ul><ul><li>Syntax: </li></ul><ul><li>if (Condition 1) </li></ul><ul><li>statement – 1; </li></ul><ul><li>else if (condition 2) </li></ul><ul><li>statement – 2; </li></ul><ul><li>else if (condition 3) </li></ul><ul><li> statement – 3; </li></ul><ul><li>…… .. </li></ul><ul><li>…… . </li></ul><ul><li>else </li></ul><ul><li>default statement; </li></ul><ul><li>Statement x; </li></ul><ul><li>The conditions are evaluated from the top to downwards. As soon as a true condition is </li></ul><ul><li>found, the statement associated with it is executed and the statement is transferred to </li></ul><ul><li>the statement x.When all the condition become false default statement will be executed. </li></ul>
  66. 66. <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int s1,s2,total; </li></ul><ul><li>float per; </li></ul><ul><li>clrscr(); </li></ul><ul><li>printf(&quot;Enter Marks for s1 & s2 :&quot;); </li></ul><ul><li>scanf (&quot;%d %d&quot;,&s1,&s2); </li></ul><ul><li>total = s1+s2; </li></ul><ul><li>printf(&quot;nnTotal Mark is : %d&quot;,total); </li></ul><ul><li>per=(float)(total*100/2); </li></ul><ul><li>printf(&quot;nnPercentage is :%f&quot;,per); </li></ul><ul><li>printf(&quot;nnClass of the Student :&quot;); </li></ul><ul><li>if(per>79) </li></ul><ul><li> printf(&quot;Honours:&quot;); </li></ul><ul><li>else if(per>59) </li></ul><ul><li>printf(&quot;First&quot;); </li></ul><ul><li> else if(per>39); </li></ul><ul><li> printf(&quot;Second&quot;); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul>
  67. 67. <ul><li>The Switch Statement : </li></ul><ul><li>When one of the many alternative is to be selected, we can use an if statement </li></ul><ul><li>to control the selection. However the complexity of such program increases when the </li></ul><ul><li>number of the alternative increases. </li></ul><ul><li>C has a built in multiway decision statement known as switch. </li></ul><ul><li>The switch statement tests the value of a given variable against a list of case value and </li></ul><ul><li>when a match is found, a block of statement associated with that case is executed. </li></ul><ul><li>Syntax: </li></ul><ul><li>switch(expression) </li></ul><ul><li>{ </li></ul><ul><li>case value-1: </li></ul><ul><li>block-1 </li></ul><ul><li>break; </li></ul><ul><li>case value-2: </li></ul><ul><li>block-2 </li></ul><ul><li>break; </li></ul><ul><li>……… .. </li></ul><ul><li> default: </li></ul><ul><li>default-block </li></ul><ul><li>break; </li></ul><ul><li>} </li></ul>
  68. 68. <ul><li>The expression is an integer or character expression. Value1, value2 are constants and are known as case label. </li></ul><ul><li>There is no need to put braces around these blocks. </li></ul><ul><li>When switch is executed, the value of the expression is successfully compared against the Values value1, value2…. If the case is found whose value matches with the value of the expression, then the block of statements that follows the case are executed. </li></ul><ul><li>break statement exit the control from the switch case and transfer the control after the switch case statement. </li></ul><ul><li>The default is an optional case, when present, it will be executed if the value of the expression does not match with any of the case values. </li></ul><ul><li>Rules for switch statement: </li></ul><ul><li>Case label must be integer or character. </li></ul><ul><li>Case label must be unique.No two case labels can have the same value. </li></ul><ul><li>Case label must end with colon. </li></ul><ul><li>The break statement is optional. </li></ul><ul><li>The default label is optional. </li></ul><ul><li>There can be at most one default label. </li></ul><ul><li>The default may be placed any where but usually placed at the end </li></ul><ul><li>It is permitted to nest switch statement. </li></ul>
  69. 69. <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int s1,s2,total,index; </li></ul><ul><li>float per; </li></ul><ul><li>clrscr(); </li></ul><ul><li>printf(&quot;Enter Marks for s1 :&quot;); </li></ul><ul><li>scanf (&quot;%d&quot;,&s1); </li></ul><ul><li>printf(&quot;Enter Marks for s2 :&quot;); </li></ul><ul><li>scanf (&quot;%d&quot;,&s2); </li></ul><ul><li> total = s1+s2; </li></ul><ul><li>printf(&quot;nnTotal Mark is : %d&quot;,total); </li></ul><ul><li> </li></ul><ul><li>per=(float)(total*100/2); </li></ul><ul><li> </li></ul><ul><li>printf(&quot;nnPercentage is :%f&quot;,per); </li></ul><ul><li>printf(&quot;nnClass of the Student :&quot;); </li></ul><ul><li>index=(int)(per/10); </li></ul>
  70. 70. <ul><li>switch(index) </li></ul><ul><li>{ </li></ul><ul><li>case 10: </li></ul><ul><li>case 9: </li></ul><ul><li>case 8: </li></ul><ul><li>printf(&quot;Honour&quot;); </li></ul><ul><li>break; </li></ul><ul><li>case 7: </li></ul><ul><li>case 6: </li></ul><ul><li>printf(&quot;First&quot;); </li></ul><ul><li>break; </li></ul><ul><li>case 5: </li></ul><ul><li>case 4: </li></ul><ul><li>printf(&quot;Second&quot;); </li></ul><ul><li>break; </li></ul><ul><li>default: </li></ul><ul><li> printf(&quot;Grade not displayed&quot;); </li></ul><ul><li>} </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul>
  71. 71. <ul><li>Go TO Statement : </li></ul><ul><li>C support go to statement to branch unconditionally from one point to another in the </li></ul><ul><li>program. </li></ul><ul><li>Although it may not be essential to use the goto statement in a highly structured </li></ul><ul><li>language like C. There may be occasions when the use of goto might be desirable </li></ul><ul><li>The goto requires a label to identify the place where the branch is to be made. Label is </li></ul><ul><li>valid variable name and must be followed by a colon. </li></ul><ul><li>Label is placed before the statement where control is to be transferred. </li></ul><ul><li>goto label; label: </li></ul><ul><li>…… .. Statement; </li></ul><ul><li>…… .. ………. </li></ul><ul><li>…… .. ………. </li></ul><ul><li>label: ………. </li></ul><ul><li>Statement; goto label; </li></ul><ul><li>[Forword Jump] [Backward Jump] </li></ul>
  72. 72. <ul><li>Backward Jump. </li></ul><ul><li>If a label is before the statement goto, a loop will be formed and some statements will be </li></ul><ul><li>Executed repeatedly. Such a jump is known as Backward Jump. </li></ul><ul><li>Forward Jump: </li></ul><ul><li>If the label is placed after the goto label, some statements will be skipped and the jump is </li></ul><ul><li>known as a forward jump. </li></ul>
  73. 73. <ul><li>Ex: </li></ul><ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>#include<math.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>float x,y; </li></ul><ul><li>clrscr(); </li></ul><ul><li>read: </li></ul><ul><li>scanf(“%f”,&x); </li></ul><ul><li>if(x<0) </li></ul><ul><li>{ </li></ul><ul><li>printf(“This is a Negative value”); </li></ul><ul><li>goto read; </li></ul><ul><li>} </li></ul><ul><li>else </li></ul><ul><li>{ </li></ul><ul><li>y=sqrt(x) </li></ul><ul><li>printf(“Suare root of %f is %f ”,x,y); </li></ul><ul><li>} </li></ul><ul><li>goto read; </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul><ul><li>Note : Infinite loop will be occurred because of the above case. </li></ul>
  74. 74. <ul><li>Solution of the infinite loop in goto statement : </li></ul><ul><li>Ex: </li></ul><ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>#include<math.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>float x,y; int count; </li></ul><ul><li>clrscr(); </li></ul><ul><li>read: </li></ul><ul><li>scanf(“%f”,&x); </li></ul><ul><li>if(x<0) </li></ul><ul><li>{ </li></ul><ul><li>printf(“This is a Negative value”); </li></ul><ul><li>goto read; </li></ul><ul><li>} </li></ul><ul><li>else </li></ul><ul><li>{ </li></ul><ul><li>y=sqrt(x) </li></ul><ul><li>printf(“Suare root of %f is %f ”,x,y); </li></ul><ul><li>} </li></ul><ul><li>count++; </li></ul><ul><li>if(count <= 5) </li></ul><ul><li>goto read; </li></ul><ul><li>printf(“n End of the computation”); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul>
  75. 75. <ul><li>Chapter : 6 Decision Making and Looping </li></ul><ul><li>What is looping? </li></ul><ul><li>A sequence of statements are executed until some condition for termination of the loop </li></ul><ul><li>are satisfied is called looping. </li></ul><ul><li>There are two types of the looping. </li></ul><ul><li>Entry controlled loop </li></ul><ul><li>Exit controlled loop </li></ul><ul><li>Entry controlled loop : In this looping first condition will be checked and then Body of the statements are executed is called the entry controlled loop. </li></ul><ul><li>Exit controlled loop: In this looping first body of the statements are executed and then condition will be checked is called the exit controlled loop. </li></ul>
  76. 76. Test Condition ? Body of the loop Body of the loop Test condition ? False True False True (a) Entry Controlled loop (b) Exit Controlled loop
  77. 77. <ul><li>The c language provides for three constructs for performing loop operation. </li></ul><ul><li>The while statement </li></ul><ul><li>The do statement </li></ul><ul><li>The for statement. </li></ul><ul><li>The while statement : </li></ul><ul><li>The while is an entry controlled loop statement. </li></ul><ul><li>Syntax: </li></ul><ul><li>while (test condition) </li></ul><ul><li>{ </li></ul><ul><li>Body of the loop; </li></ul><ul><li>} </li></ul><ul><li>Here while is a key word. </li></ul><ul><li>The test condition is evaluated and if the condition is true, the body of the loop is </li></ul><ul><li>executed. After the execution of the body, the test condition is once again evaluated and </li></ul><ul><li>if it is true, the body is executed once again. </li></ul><ul><li>Note : Braces is needed if the body contains more than one statements. </li></ul>
  78. 78. <ul><li>Ex: </li></ul><ul><li>//program:-9 to find the sum of first 100 natural nos: </li></ul><ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int i,sum; </li></ul><ul><li>clrscr(); </li></ul><ul><li>i=0; sum=0; </li></ul><ul><li>while(i<=100) </li></ul><ul><li>{ </li></ul><ul><li>sum = sum + i; </li></ul><ul><li>i++; </li></ul><ul><li>} </li></ul><ul><li>printf(&quot;nSummation of first 100 is : %d&quot;,sum); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul><ul><li>Hw : Write the program to find the summation of first five no’s square. </li></ul>
  79. 79. <ul><li>Do…While statement : </li></ul><ul><li>Do…while statement is exit controlled loop. </li></ul><ul><li>Syntax: </li></ul><ul><li>do </li></ul><ul><li>{ </li></ul><ul><li>Body of the loop; </li></ul><ul><li>}while (test condition); </li></ul><ul><li>It first executes the body of loop then evaluates the test condition. If the condition is true, </li></ul><ul><li>the Body loop is executed again and again until the condition become false. </li></ul><ul><li>After the while statement there is a semicolon in do..while statement. </li></ul><ul><li>If first time condition become false, though it executes the body of the loop once. </li></ul>
  80. 80. <ul><li>/* Find the factorial of any number */ </li></ul><ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int n, fact=1; </li></ul><ul><li>clrscr(); </li></ul><ul><li>printf(&quot;Enter the number :&quot;); </li></ul><ul><li>scanf(&quot;%d&quot;,&n); </li></ul><ul><li>if(n<0) </li></ul><ul><li>printf(&quot;n factorial of negetive number is not possible :&quot;); </li></ul><ul><li>else if(n==0) </li></ul><ul><li>printf(&quot;Factorial of 0 is 1n&quot;); </li></ul><ul><li>else </li></ul><ul><li>{ </li></ul><ul><li>do </li></ul><ul><li>{ </li></ul><ul><li>fact *= n; </li></ul><ul><li>n--; </li></ul><ul><li>}while(n>0); </li></ul><ul><li>printf(&quot;Factorial of the given no. is : %d&quot;, fact); </li></ul><ul><li> } </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul>
  81. 81. <ul><li>For Loop: </li></ul><ul><li>The for loop is another entry control loop that provides a more concise loop control structure. </li></ul><ul><li>Syntax : </li></ul><ul><li>for( initialization; test-condition ; increment or decrement) </li></ul><ul><li>{ </li></ul><ul><li>Body of the loop </li></ul><ul><li>} </li></ul><ul><li>Initialization: In Initialization part you can initialize the variable. Such as i=0. </li></ul><ul><li>Test-Condition: The value of the control variable is tested using the test – condition. Test condition is relational expression, such as i<10 that determines when loop will exit. If the condition is true body of the loop is executed otherwise the loop is terminated. </li></ul><ul><li>Increment or Decrement : When the Body of the loop is executed, the control is transferred back to the for statement after evaluating the last statement in the loop. Now the control variable is incremented or decremented. </li></ul>
  82. 82. <ul><li>Ex: </li></ul><ul><li>/* Program to Display 0 to 9 */ </li></ul><ul><ul><ul><li>#include<stdio.h> </li></ul></ul></ul><ul><ul><ul><li>#include<conio.h> </li></ul></ul></ul><ul><ul><ul><li>void main() </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><li>int x; </li></ul></ul></ul><ul><ul><ul><li>for(x=0; x<=9 ; x++) </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><li>printf(“%d”,x); </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><ul><li>printf(“n”); </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul>
  83. 83. <ul><ul><ul><li>/* Program to Display 9 to 0 */ </li></ul></ul></ul><ul><ul><ul><li>#include<stdio.h> </li></ul></ul></ul><ul><ul><ul><li>#include<conio.h> </li></ul></ul></ul><ul><ul><ul><li>void main() </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><li>int x; </li></ul></ul></ul><ul><ul><ul><li>for(x=9; x>=0 ; x--) </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><li>printf(“%d”,x); </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><ul><li>printf(“n”); </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul>
  84. 84. <ul><li>Additional features of the for loop : </li></ul><ul><li>More than one variable can be initialized at a time in the for statement. </li></ul><ul><li>Ex: p=1; </li></ul><ul><li>for (n=0; n<17; n++) </li></ul><ul><li>is equivalent to </li></ul><ul><li>for(p=1,n=0; n<17; n++) </li></ul><ul><li>Increment or Decrement section also has more than one statement. </li></ul><ul><li>Ex: for(n=1, m=50; n<=m; n++, m--) </li></ul><ul><li>{ </li></ul><ul><li>p=m/n; </li></ul><ul><li>printf(“%d %d %dn”, n,m,p); </li></ul><ul><li>} </li></ul><ul><li>The test condition may have any compound relation. </li></ul><ul><li>Ex: sum=0; </li></ul><ul><li> for(i=1; i<20 && sum < 100; i++) </li></ul><ul><li>{ </li></ul><ul><li>sum = sum +i; </li></ul><ul><li>printf(“%d %dn”, i, sum); </li></ul><ul><li>} </li></ul>
  85. 85. <ul><li>It is also permissible to use expression in the assignment statements of initialization and </li></ul><ul><li>increment sections. </li></ul><ul><li>Ex: for( x=(m+n)/2; x>0; x= x/2) </li></ul><ul><li>Another feature of the for loop is that one or more section can be omitted, if necessary. </li></ul><ul><li>Ex: m=5; </li></ul><ul><li>for( ; m != 100 ;) </li></ul><ul><li>{ </li></ul><ul><li>printf(“%dn”, m); </li></ul><ul><li>m++; </li></ul><ul><li>} </li></ul><ul><li>Note : If test condition is not present, the for statement sets up an “infinite </li></ul><ul><li>loop” such loop can be broken using break and goto statements in the loop. </li></ul><ul><li>We can set up the time delay loops using the null statement as follow: </li></ul><ul><li>for(j=1000; j>0; j--) ; </li></ul><ul><li>This loop is executed 1000 times without producing any output; it simply causes time delay. </li></ul><ul><li>The Body of the loop contains only a semi colon, known as null statement. </li></ul><ul><li>Note : The c compiler does not give any error message if we place semicolon by mistake at the end of a for statement.. </li></ul>
  86. 86. <ul><li>Nesting of for loop: </li></ul><ul><li>Nesting of for loops, that is one for statement within another for statement. </li></ul><ul><li>Ex: </li></ul><ul><li>for ( i=1 ; i< 10; i++) </li></ul><ul><li>{ </li></ul><ul><li>for ( j=1; j != 5; j++ ) </li></ul><ul><li>{ </li></ul><ul><li>……… .. </li></ul><ul><li>………… </li></ul><ul><li>} </li></ul><ul><li>…………… .. </li></ul><ul><li>…………… . </li></ul><ul><li>} </li></ul><ul><li>Note : ANSI C allow up to 15 levels of nesting. </li></ul>Inner loop Outer loop
  87. 87. <ul><ul><ul><li>/* Program to display the output on the screen */ </li></ul></ul></ul><ul><ul><ul><li>#include<stdio.h> </li></ul></ul></ul><ul><ul><ul><li>#include<conio.h> </li></ul></ul></ul><ul><ul><ul><li>void main() </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><li>int i,j,n; </li></ul></ul></ul><ul><ul><ul><li>clrscr(); </li></ul></ul></ul><ul><ul><ul><li>printf(&quot;Enter the value of i :&quot;); </li></ul></ul></ul><ul><ul><ul><li>scanf(&quot;%d&quot;,&n); </li></ul></ul></ul><ul><ul><ul><li>for(i=0;i<n;i++) </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><li>for(j=0;j<=i;j++) </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><li>printf(&quot;*&quot;); </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><ul><li>printf(&quot;n&quot;); </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><ul><li>getch(); </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul>
  88. 88. <ul><li>/* </li></ul><ul><li>Enter the value of n : 4 </li></ul><ul><li>* </li></ul><ul><li>** </li></ul><ul><li>*** </li></ul><ul><li>**** </li></ul><ul><li>*/ </li></ul>
  89. 89. <ul><li>Jumps in Loops : </li></ul><ul><li>There are two statements break and continue are used to performing operation </li></ul><ul><li>jumps in loops. </li></ul><ul><li>break statement : </li></ul><ul><li>When the break statement is encountered inside a loop, the loop is immediately exited </li></ul><ul><li>and the program continues with the statement immediately following the loop. When the </li></ul><ul><li>loops are nested, the break would only exit from the loop containing it. That is break will </li></ul><ul><li>exit only a single loop. </li></ul><ul><li>Syntax : </li></ul><ul><li>break; </li></ul><ul><li>Since a goto statement can transfer the control to any place in a program . Another </li></ul><ul><li>important use of goto is to exit from deeply nested loops when an error occurs. </li></ul>
  90. 90. for (……..) { for (……….) { if (condition) break; } ---------- } while (……..) { ……………… .. ……………… .. if (condition) break; ……………… } do { ……………… .. ……………… .. if (condition) break; ……………… } while(….) ; for (……..) { ……………… .. ……………… .. if (error) break; ……………… } Exit loop Exit loop Exit loop Exit loop
  91. 91. <ul><li>continue statement : </li></ul><ul><li>The use of the continue statement is to skip the following statements and continue with the next iteration. </li></ul><ul><li>Syntax : continue; </li></ul><ul><li>Unlike the break which causes the loop to be terminated, the continue as the name </li></ul><ul><li>implies, causes the loop to be continued with the next iteration after skipping any </li></ul><ul><li>statements in between. </li></ul><ul><li>In while and do loops, continue causes the control to go directly to the test condition and </li></ul><ul><li>then do continue the iteration process. </li></ul><ul><li>In the case of for loop increment section of the loop is executed before the </li></ul><ul><li>test – condition is evaluated. </li></ul>
  92. 92. for (……..) { for (……….) { if (condition) continue; } ---------- } while (……..) { ……………… .. ……………… .. if (condition) continue; ……………… } do { ……………… .. ……………… .. if (condition) continue; ……………… } while(….) ; for (……..) { ……………… .. ……………… .. if (error) continue; ……………… } Exit loop
  93. 93. <ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>#include<math.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int count,negative; </li></ul><ul><li>float no,sqroot; </li></ul><ul><li>printf(&quot;Enter 9999 To Stopn&quot;); </li></ul><ul><li>count=0; </li></ul><ul><li>negative=0; </li></ul><ul><li>while(count<=20) </li></ul><ul><li>{ </li></ul><ul><li>printf(&quot;nEnter a number :&quot;); </li></ul><ul><li>scanf(&quot;%f&quot;,&no); </li></ul><ul><li>if(no==9999) </li></ul><ul><li> break; //Break statement </li></ul>
  94. 94. <ul><li> if( no < 0 ) </li></ul><ul><li>{ </li></ul><ul><li> printf(&quot;nNumber is negative&quot;); </li></ul><ul><li> negative++; </li></ul><ul><li> continue; // continue statement </li></ul><ul><li>} </li></ul><ul><li>sqroot = sqrt(no); </li></ul><ul><li>printf(&quot;Numer = %fn Square root = %fnn&quot;,no,sqroot); </li></ul><ul><li>count++; </li></ul><ul><li>} </li></ul><ul><li>printf(&quot;nNumber of item done = %dn&quot;,count); </li></ul><ul><li>printf(&quot;nNegative item = %dn&quot;,negative); </li></ul><ul><li>} </li></ul>
  95. 95. <ul><li>Chapter 7 : Arrays </li></ul><ul><li>Defination of Array : An array is a sequences collection of the realated data items that share a common name. </li></ul><ul><li>An array is a derived data type. </li></ul><ul><li>There are mainly three types of the array: </li></ul><ul><li>One – Dimentional arrays </li></ul><ul><li>Two – Dimentional arrays </li></ul><ul><li>Multidimentional arrays </li></ul>
  96. 96. <ul><li>One – Dimentional Arrays: </li></ul><ul><li>A list of items can be given one variable name using only one subscript and such </li></ul><ul><li>a variable is called a single subscripted variable or a one dimentional array. </li></ul><ul><li>Syntax: </li></ul><ul><li>type variable-name[size]; </li></ul><ul><li>The type specifies the type of the element that will be contained in the array, such as int, </li></ul><ul><li>float or char. </li></ul><ul><li>Ex: If we want to represent a set of five numbers say, (35,40,20,57,19) by an array variable number, then we may declare the vaiable number as follows. </li></ul><ul><li>int number[5]; </li></ul><ul><li>And the computer reserves five storage location as shown below: </li></ul>Number[0] Number[1] Number[2] Number[3] Number[4]
  97. 97. <ul><li>The value to the array element can be assigned as follow: </li></ul><ul><li>Number[0] = 35 ; </li></ul><ul><li>Number[1] = 40 ; </li></ul><ul><li>Number[2] = 20 ; </li></ul><ul><li>Number[3] = 57 ; </li></ul><ul><li>Number[4] = 19 </li></ul><ul><li>This would case the array number to store the value as shown below: </li></ul><ul><li>Note : C performs no bound checking and, therefore, care should be exercised to ensure that the array indices are within the declared limits. </li></ul>Number[0] Number[1] Number[2] Number[3] Number[4] 35 40 20 57 19
  98. 98. <ul><li>Same way, </li></ul><ul><li>float height[50]; </li></ul><ul><li>Declares the height to be an array containing 50 real elements. Any subscript to </li></ul><ul><li>0 to 49 are valid. </li></ul><ul><li>Any references to the arrays outside the declared limits would not necessarily cause an error. Rather, it might result in unpredictable program results. </li></ul><ul><li>The size should be either integer constant or symbolic constant. </li></ul><ul><li>The C language treats character string simply as array of characters. The size in </li></ul><ul><li>a character string represents the maximum number of characters that the string </li></ul><ul><li>can hold. </li></ul><ul><li>char name[10] ; </li></ul><ul><li>Declare name as a character array variable that can hold a maximum of 10 characters. </li></ul><ul><li>Suppose we read the following string constant into the string variable name. </li></ul><ul><li>“ WELL DONE” </li></ul>
  99. 99. <ul><li>Each character of the string is treated as an element of the array name and is </li></ul><ul><li>stored in the memory as follows: </li></ul><ul><li>Character string terminates with an additional null character. Thus name[10] holds the null character ‘0’. When declaring character array, we must allow one extra space for the null chararcter. </li></ul>‘ W’ ‘ E’ ‘ L’ ‘ L’ ‘ ’ ‘ D’ ‘ O’ ‘ N’ ‘ E’ ‘ 0’
  100. 100. <ul><li>INITIALIZATION OF ONE – DIMENTIONAL ARRAYS: </li></ul><ul><li>After an array is declared, its element must be initialized. Otherwise, they will </li></ul><ul><li>contain “garbage”. An array can be initialized at either of the following stages: </li></ul><ul><li>At compile time </li></ul><ul><li>At run time </li></ul><ul><li>CompileTime Initialization: </li></ul><ul><li>Syntax: </li></ul><ul><li>Type array-name[size] = {list of values}; </li></ul><ul><li>The value in the list are separed by commas. </li></ul><ul><li>Ex: int number[3] = {0,0,0} ; </li></ul><ul><li>Will declare the variable number as an array of size 3 and will assign zero to </li></ul><ul><li>each element. If the number of values in the list is less than the number of elements, </li></ul><ul><li>then only that many elements will be initialized. The remaining elements will be set to </li></ul><ul><li>zero automatically </li></ul>
  101. 101. <ul><li>float total[5] = { 0.0 , 15.75 , -10}; </li></ul><ul><li>Will initialize the first three elements to 0.0, 15.75 and -10.0 and the remaining </li></ul><ul><li>two elements to zero </li></ul><ul><li>The size may be omitted. In such cases, the compiler allocate enough spaces for </li></ul><ul><li>all initialized elements. </li></ul><ul><li>Ex: int counter[] = {1,1,1,1} </li></ul><ul><li>Counter array contain four element with initial value 1. </li></ul><ul><li>Character array may be initialized similar manner. </li></ul><ul><li>char name[] = {‘R’, ‘A’, ‘M’, ‘0’}; </li></ul><ul><li>Is equivalent to : </li></ul><ul><li>char name[] = “RAM”; </li></ul><ul><li>If we have more than the declared size, the compiler will produce an error. That is the </li></ul><ul><li>statement. </li></ul><ul><li>int number[3] = {10, 20, 30, 40} ; will not work. It is illegal in C. </li></ul>
  102. 102. <ul><li>Run Time Initialization : </li></ul><ul><li>An array can be explicitly initialized at run time. This approach is usually applied </li></ul><ul><li>for initializing large arrays. </li></ul><ul><li>Ex: </li></ul><ul><li>for(i=0; i<100 ; i++) </li></ul><ul><li>{ </li></ul><ul><li>if (i < 50 ) </li></ul><ul><li> sum[i] = 0.0; </li></ul><ul><li>else </li></ul><ul><li>sum[i] = 1.0; </li></ul><ul><li>} </li></ul><ul><li>In the above case the first 50 elements are initialized to zero while the </li></ul><ul><li>remaining elements are initialized to 1.0 are run time. </li></ul><ul><li>We can also use a read function such as scanf to initialize an array. </li></ul><ul><li>Ex: int x[3]; </li></ul><ul><li>scanf (“%d %d %d”, &x[0], &x[1], &x[2] ) ; </li></ul><ul><li>Will initialize array elements with the values entered through the kwyboard. </li></ul>
  103. 103. <ul><li>/* program:-22 write a C proragm to arrange the accepted numbers in ascending order and descending order: */ </li></ul><ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int i,j,ans,n,temp=0, no[20]; </li></ul><ul><li>clrscr(); </li></ul><ul><li>printf(&quot;Enter the requirement:=&quot;); </li></ul><ul><li>scanf(&quot;%d&quot;,&n); </li></ul><ul><li>for(i=0;i<n;i++) </li></ul><ul><li>{ </li></ul><ul><li>printf(&quot;Enter the no:-&quot;); </li></ul><ul><li>scanf(&quot;%d&quot;,&no[i]); </li></ul><ul><li>} </li></ul><ul><li>printf(&quot;Enter the your choice ascending=1&descending=2:=&quot;); </li></ul><ul><li>scanf(&quot;%d&quot;,&ans); </li></ul>
  104. 104. <ul><li> for(i=0;i<n;i++) </li></ul><ul><li>{ </li></ul><ul><li>for(j=i;j<n;j++) </li></ul><ul><li>{ </li></ul><ul><li> if(ans==1) </li></ul><ul><li> { </li></ul><ul><li> if(no[i]>no[j]) </li></ul><ul><li> { </li></ul><ul><li> temp=no[i]; </li></ul><ul><li> no[i]=no[j]; </li></ul><ul><li> no[j]=temp; </li></ul><ul><li> } </li></ul><ul><li> } </li></ul><ul><li> else </li></ul><ul><li> if(no[i]<no[j]) </li></ul><ul><li> { </li></ul><ul><li> temp=no[i]; </li></ul><ul><li> no[i]=no[j]; </li></ul><ul><li> no[j]=temp; </li></ul><ul><li> } </li></ul><ul><li>} </li></ul><ul><li>printf(&quot;%d&quot;,no[i]); </li></ul><ul><li>printf(&quot;n&quot;); </li></ul><ul><li>} </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul>
  105. 105. <ul><li>TWO – DIMENTIONAL ARRAYS: </li></ul><ul><li>If you want to store data into table or matrix form at that time you can use the </li></ul><ul><li>two dimentional array. </li></ul><ul><li>Ex: </li></ul><ul><li>ITEM 1 ITEM2 ITEM 3 </li></ul><ul><li>SALES MAN 1 310 275 365 </li></ul><ul><li>SALES MAN 2 210 190 325 </li></ul><ul><li>SALES MAN 3 405 235 240 </li></ul><ul><li>SALES MAN 4 260 300 380 </li></ul><ul><li>The table discussed above can be define as : </li></ul><ul><li>v[4][3] where 4 Rows and 3 columns . </li></ul><ul><li>Declaration of two dimentional array: </li></ul><ul><li>Syntax : data_type array-name[rowsize][colsize]; </li></ul>
  106. 106. <ul><li>Memory representation of the two dimentional array for v[4][3] . </li></ul><ul><li>column 0 column 1 column2 </li></ul><ul><li>[0][0] [0][1] [0][2] </li></ul><ul><li>Row 0 </li></ul><ul><li>[1][0] [1][1] [1][2] </li></ul><ul><li>Row 1 </li></ul><ul><li>[2][0] [2][1] [2][2] </li></ul><ul><li>Row 2 </li></ul><ul><li>[3][0] [3][1] [3][2] </li></ul><ul><li>Row 3 </li></ul>310 275 365 325 2400 365 275 235 190 10 405 310
  107. 107. <ul><li>INITIALIZING TWO DIMENTIONAL ARRAY: </li></ul><ul><li>Ex: int table[2][3] = {0,0,0,1,1,1}; </li></ul><ul><li>Initialize elements of the first row to 0 and second row to 1. The initialization is </li></ul><ul><li>done row by row. </li></ul><ul><li>The above statement can be equivalent written as : </li></ul><ul><li>int table[2][3] = {{0,0,0},{1,1,1}}; </li></ul><ul><li>We can also initialize a two dimensional array in the form of a matrix as shown below: </li></ul><ul><li>int table[2][3] = { </li></ul><ul><li>{0,0,0}, </li></ul><ul><li>{1,1,1} </li></ul><ul><li>} ; </li></ul><ul><li>When the array is completely initialized with all values, we need not specify the size of the </li></ul><ul><li>first dimension. That is, the statement is permitted. </li></ul><ul><li>int table[][3] = { </li></ul><ul><li>{0,0,0} , </li></ul><ul><li>{1,1,1) </li></ul><ul><li>}; </li></ul>
  108. 108. <ul><li>If the value is missing in an initializes, they are automatically set to zero. </li></ul><ul><li>Ex: int table[2][3] ={ </li></ul><ul><li>{1,1} , </li></ul><ul><li>{2} </li></ul><ul><li> } ; </li></ul><ul><li>Will initialize the first two elements of the first row to one, the first element of the second </li></ul><ul><li>row to two and all other elements to zero. </li></ul><ul><li>When all the elements are initialized to zero, the following short cut method may be used. </li></ul><ul><li>int m[3][5] = { 0, 0 }; </li></ul><ul><li>or </li></ul><ul><li>int m[3][5] = { {0} , { 0} }; </li></ul>
  109. 109. <ul><li>//program30 : addition of two matrix </li></ul><ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int i,j,n,m,a[10][10],b[10][10],m1,n1; </li></ul><ul><li>clrscr(); </li></ul><ul><li>printf(&quot;Enter No of Rows for first matrix:&quot;); </li></ul><ul><li>scanf(&quot;%d&quot;,&n); </li></ul><ul><li>printf(&quot;Enter No of columns for first Matrix:&quot;); </li></ul><ul><li>scanf(&quot;%d&quot;,&m); </li></ul><ul><li>printf(&quot;Enter No of Rows for second matrix:&quot;); </li></ul><ul><li>scanf(&quot;%d&quot;,&n1); </li></ul><ul><li>printf(&quot;Enter No of columns for second Matrix:&quot;); </li></ul><ul><li>scanf(&quot;%d&quot;,&m1); </li></ul>
  110. 110. <ul><li>if(m!=m1 || n!=n1) </li></ul><ul><li> printf(&quot;Sum not Possible.&quot;); </li></ul><ul><li>else </li></ul><ul><li>{ </li></ul><ul><li>for(i=0;i<n;i++) </li></ul><ul><li> { </li></ul><ul><li> for(j=0;j<m;j++) </li></ul><ul><li> { </li></ul><ul><li> printf(&quot;Give value of a[%d][%d]:&quot;,i+1,j+1); </li></ul><ul><li> scanf(&quot;%d&quot;,&a[i][j]); </li></ul><ul><li> } </li></ul><ul><li> } </li></ul><ul><li>for(i=0;i<n1;i++) </li></ul><ul><li> { </li></ul><ul><li> for(j=0;j<m1;j++) </li></ul><ul><li> { </li></ul><ul><li> printf(&quot;Give value of b[%d][%d]:&quot;,i+1,j+1); </li></ul><ul><li> scanf(&quot;%d&quot;,&b[i][j]); </li></ul><ul><li> } </li></ul><ul><li> } </li></ul>
  111. 111. <ul><li>printf(&quot;First Matrix:n&quot;); </li></ul><ul><li> for(i=0;i<n;i++) </li></ul><ul><li> { </li></ul><ul><li> for(j=0;j<m;j++) </li></ul><ul><li> { </li></ul><ul><li> printf(&quot;%d &quot;,a[i][j]); </li></ul><ul><li> } </li></ul><ul><li> printf(&quot;n&quot;); </li></ul><ul><li> } </li></ul><ul><li>printf(&quot;Second Matrixn&quot;); </li></ul><ul><li>for(i=0;i<n1;i++) </li></ul><ul><li> { </li></ul><ul><li> for(j=0;j<m1;j++) </li></ul><ul><li> { </li></ul><ul><li> printf(&quot;%d &quot;,b[i][j]); </li></ul><ul><li> } </li></ul><ul><li> printf(&quot;n&quot;); </li></ul><ul><li> } </li></ul><ul><li> printf(&quot;After Sum New Matrixn&quot;); </li></ul><ul><li>for(i=0;i<n1;i++) </li></ul><ul><li> { </li></ul><ul><li> for(j=0;j<m1;j++) </li></ul><ul><li> { </li></ul><ul><li> printf(&quot;%d &quot;,a[i][j]+b[i][j]); </li></ul><ul><li> } </li></ul><ul><li> printf(&quot;n&quot;); </li></ul><ul><li> } </li></ul><ul><li>} </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul>
  112. 112. <ul><li>MULTIDIMENTIONAL – ARRAY : </li></ul><ul><li>C allows array of three or more dimensions. </li></ul><ul><li>Syntax: </li></ul><ul><li>type array_name [s1] [s2] [s3]……………[sn] ; </li></ul><ul><li>Where s i is the size of the i th dimentions. </li></ul><ul><li>Some ex: int survey [3] [5] [12] ; </li></ul><ul><li>float table [5] [4] [5] [3] ; </li></ul><ul><li>Survey is a three dimensional array while table is four dimensional array. </li></ul><ul><li>Survey contain 180 integer value. Table contain 300 element for floating type. </li></ul><ul><li>Ex: </li></ul><ul><li>int survey[3][5][12]; </li></ul><ul><li>In this first index denotes year, second city and third month. The array survey </li></ul><ul><li>Represent a survey of rainfall during the last three years from January to </li></ul><ul><li>December in five cities. </li></ul>
  113. 113. YEAR 1 YEAR 2 ----------- YEAR 3 Month City 1 2 12 ………………………………… 1 5 ………… Month City 1 2 12 ………………………………… 1 5 …………
  114. 114. <ul><li>Dynamic Array: </li></ul><ul><li>In C it is possible to allocate memory at the run time to the Array is called the </li></ul><ul><li>Dynamic Array. </li></ul><ul><li>Dynamic arrays are created using what are known as pointer variable and </li></ul><ul><li>memory management function like malloc, calloc and realloc. </li></ul><ul><li>The process of allocating memory at compile time is known as static memory </li></ul><ul><li>allocation and the array that receive static memory allocation is called the static </li></ul><ul><li>array. </li></ul>
  115. 115. Chapter : 8 Handling of character String: What is a String : String is a group of character array. Any group of character defined between double quotation mark is a constant string. Ex: printf(“Hello How are you”); output : Hello How are you If you want to included double quotes with string than also it is possible. Ex: printf(“”Hello How are you””); output : “Hello How are you”
  116. 116. <ul><li>Declaring and Initialising String Variable : </li></ul><ul><li>A String Variable is always declared as an array : </li></ul><ul><li>Syntax : char string_name[size]; </li></ul><ul><li>Where size determines the number of character in the string. </li></ul><ul><li>size should be maximum number of the character + 1. </li></ul><ul><li>because When the compiler assigns the character string to character array . It automatically supplies null character (“0”) at the end of the string. </li></ul><ul><li>Ex: char city[10]; </li></ul><ul><li>char name[30]; </li></ul>
  117. 117. <ul><li>Initialization of string variable : </li></ul><ul><li>char array may be initialized when they are declared. </li></ul><ul><li>you can initialized char array with two form: </li></ul><ul><li>char city[9] = “New York”; </li></ul><ul><li>char city[9] = {‘N’, ‘e’, ‘w’, ‘ ’, ‘Y’, ‘o’, ‘r’, </li></ul><ul><li>‘ k’,’0’ }; </li></ul>
  118. 118. <ul><li>C also permit us to initialise character array without specifying the number of elements. </li></ul><ul><li>In such cases, the size of the array will be determined automatically,based on the number of the elements initialised. </li></ul><ul><li>Ex: char string[] = {‘G’,’O’,’O’,’D’,’0’}; </li></ul><ul><li>It defines the string as five elements array. </li></ul>
  119. 119. <ul><li>Reading String from terminal : </li></ul><ul><li>You can read the string using scanf function with %s format specification. </li></ul><ul><li>Ex: char address[15]; </li></ul><ul><li>scanf(“%s”,address); </li></ul><ul><li>(& sign is not included before address) </li></ul><ul><li>The problem with the scanf function is that it terminates its input on the first white space it finds. </li></ul><ul><li>Ex: If input string is New York in address that address array included only New. After New, string will be terminated. </li></ul>
  120. 120. <ul><li>If we want to to read the entire line </li></ul><ul><li>“ New York” then we may use two character array of appropriate sizes. That is, </li></ul><ul><li>scanf(“%s %s”,adr1,adr2); </li></ul><ul><li>Where adr1 assign “New” and adr2 assigns “York”. </li></ul>
  121. 121. <ul><li>Reading a line of Text: </li></ul><ul><li>Program to read a line of text from terminal : </li></ul><ul><li>#include<stdio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>char line[81],character; int c=0; </li></ul><ul><li>do </li></ul><ul><li>{ </li></ul><ul><li>character = getchar(); </li></ul><ul><li>line[c] = character; </li></ul><ul><li>c++; </li></ul><ul><li>} while(character != ‘n’); </li></ul><ul><li>c=c-1; line[c] = ‘0’ ; </li></ul><ul><li>printf(“n %s n”,line) </li></ul><ul><li>} </li></ul><ul><li>Output : programming in C is interesting. </li></ul><ul><li>Programming in C is interesting. </li></ul>
  122. 122. <ul><li>Copying one string to another string </li></ul><ul><li>We can copy one string to another string using character by character basis. </li></ul><ul><li>We can’t assign one string to another string directly. </li></ul><ul><li>Ex: string = “ABC”; </li></ul><ul><li>string1 = string2; </li></ul><ul><li>is not possible in C. </li></ul>
  123. 123. <ul><li>program to copying one string to another string : </li></ul><ul><li>#include<stdio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>char str1[80],str2[80]; int i; </li></ul><ul><li>printf(“Enter the Stringn”); </li></ul><ul><li>scanf(“%s”,str2); </li></ul><ul><li>for(i=0; str2[i] != ‘0’; i++) </li></ul><ul><li>str1[i] = str2[i]; </li></ul><ul><li>str1[i] = ‘0’; </li></ul><ul><li>printf(“%sn”,str1); </li></ul><ul><li>printf(“No.of characters=%dn”,i); </li></ul><ul><li>} </li></ul>
  124. 124. <ul><li>Writing string to screen : </li></ul><ul><li>We have used printf function with %s format to print the string to the screen. </li></ul><ul><li>Ex: printf(“%s”,name); </li></ul><ul><li>Used to display the entire contents of the array name. </li></ul><ul><li>Features of the %s format specification : </li></ul><ul><li>(1) When the field width is less than the length of the string than the entire string is printed. </li></ul><ul><li>(2) The integer value on the right side of the decimal point specifies the number of character to be printed. </li></ul><ul><li>Ex: %10.4s specifies 4 character printed . </li></ul><ul><li>(3) When the number of character to be printed is specified by zero,nothing is printed. </li></ul><ul><li>Ex: %10.0s </li></ul><ul><li>(4) The minus sign in the specification causes the string to be printed left side. Ex: %-10.4s 4 character printed left side. </li></ul>
  125. 125. <ul><li>Another nice features for printing output is: </li></ul><ul><li>printf(“%*.*sn”,w,d,string); </li></ul><ul><li>prints the first d characters of the string in the field width of w. </li></ul><ul><li>Write the program for writing string using %s format : </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>char country[15] = “United Kingdom” </li></ul><ul><li>printf(“%15s n”,country); </li></ul><ul><li>printf(“%5s n”,country); </li></ul><ul><li>printf(“%15.6s n”,country); </li></ul><ul><li>printf(“%-15.6s n”,country); </li></ul><ul><li>printf(“%15.0s n”,country); </li></ul><ul><li>printf(“%0.3s n”,country); </li></ul><ul><li>} </li></ul>
  126. 126. <ul><li>Output : </li></ul><ul><li>United Kingdom </li></ul><ul><li>United Kingdom </li></ul><ul><li>United </li></ul><ul><li>United </li></ul><ul><li>Uni </li></ul>
  127. 127. <ul><li>Write the program to print the output: </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int c , d; </li></ul><ul><li>char string[] = “Cprogramming”; </li></ul><ul><li>for(c=0;c<=11;c++) </li></ul><ul><li>{ </li></ul><ul><li>d=c+1; </li></ul><ul><li>printf(“%-12.*s n”,d,string); </li></ul><ul><li>} </li></ul><ul><li>printf(“nn”); </li></ul><ul><li>for(c=11;c>=0;c--) </li></ul><ul><li>{ </li></ul><ul><li>d=c+1; </li></ul><ul><li>printf(“%-12.*s n”,d,string); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  128. 128. <ul><li>OUT PUT : </li></ul><ul><li>C </li></ul><ul><li>Cp </li></ul><ul><li>Cpr </li></ul><ul><li>Cpro </li></ul><ul><li>Cprog </li></ul><ul><li>……………… .. </li></ul><ul><li>Cprogramming </li></ul><ul><li>Cprogramming </li></ul><ul><li>Cprogrammin </li></ul><ul><li>Cprogrammi </li></ul><ul><li>Cprogramm </li></ul><ul><li>Cprogram </li></ul><ul><li>Cprogra </li></ul><ul><li>Cprogr </li></ul><ul><li>…… .. </li></ul><ul><li>… . </li></ul><ul><li>C </li></ul>
  129. 129. <ul><li>Arithmetic operation on characters : </li></ul><ul><li>To write a character in integer representation , we may write it as an integer. </li></ul><ul><li>Ex: int x; </li></ul><ul><li>x = ‘a’; </li></ul><ul><li>printf(“%dn”,x); it will display number 97 on the screen. </li></ul><ul><li>It is also possible to perform arithmetic operation on the character constants and variable. </li></ul><ul><li>Ex: x = ‘z’ – 1 </li></ul><ul><li>Ascii value of z is 122 so x will be store 121. </li></ul>
  130. 130. <ul><li>The C library provides a function which converts string of digits into their integer value. </li></ul><ul><li>Ex: number = “1988”; </li></ul><ul><li>year = atoi(number); </li></ul><ul><li>String Handling function : </li></ul><ul><li>There are mainly four types of string handling function: </li></ul><ul><li>1. strcat() – Concatenation of two string </li></ul><ul><li>2. strcmp() – Compares two string </li></ul><ul><li>3. strcpy() – copies one string over another string </li></ul><ul><li>4. strlen() – finds the length of the string. </li></ul>
  131. 131. <ul><li>Strcat() : - Using this function you can concat two string: </li></ul><ul><li>syntax : strcat(string1,string2); </li></ul><ul><li>where string1 and string2 both are character array. </li></ul><ul><li>string2 is appended to string1 by removing null character at the end of string1. </li></ul><ul><li>Ex: str1 = “Very 0” </li></ul><ul><li>str2 = “Good0” </li></ul><ul><li>str3 = “Bad0” </li></ul><ul><li>Strcat(str1,str2); - output : Very Good0 </li></ul>
  132. 132. <ul><li>It also allows to concat three string together </li></ul><ul><li>Ex: strcat(strcat(str1,str2),str3); </li></ul><ul><li>output : Very GoodBad </li></ul><ul><li>strcmp() : This function compares two string If both are equal then it return 0 value and if they are not equal then it will return numeric difference between the nonmatching character. </li></ul><ul><li>Ex: strcmp(“their”,”there”); will return numeric difference between ASCII “i”and”r”. </li></ul>
  133. 133. <ul><li>strcpy () : using this function we can copy contents of one string to contents of another string. </li></ul><ul><li>syntax : strcpy(str1,str2); </li></ul><ul><li>strlen() : This function is used to find the length of the string. </li></ul><ul><li>n = strlen(string); </li></ul><ul><li>where n is an integer variable. </li></ul>
  134. 134. <ul><li>Examples of string handling function : </li></ul><ul><li>#include<stdio.h> </li></ul><ul><li>#include<string.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>char s1[20],s2[20],s3[20]; </li></ul><ul><li>int x,l1,l2,l3; </li></ul><ul><li>printf(“Enter two string constantsn”); </li></ul><ul><li>scanf(“%s %s”,s1,s2); </li></ul><ul><li>x=strcmp(s1,s2); </li></ul><ul><li>if(x != 0) </li></ul><ul><li>{ </li></ul><ul><li>printf(“Strings not equal”); strcat(s1,s2); </li></ul><ul><li>} </li></ul><ul><li>else </li></ul><ul><li>printf(“strings are equal”) ; strcpy(s3,s1); </li></ul><ul><li>l1 = strlen(s1); l2=strlen(s2) ; l3 = strlen(s3); </li></ul><ul><li>printf(“s1 = %s length=%d charactern”,s1,l1); </li></ul><ul><li>} </li></ul>
  135. 135. <ul><li>Table of string : </li></ul><ul><li>When you want to store a set of strings in table form than it is possible using two dimensional character array. </li></ul><ul><li>Ex: student[30][15] </li></ul><ul><li>A character array student[30][15] may be used to store a list of 30 names,each of the length not more than 15 characters. </li></ul>
  136. 136. <ul><li>Write a program that would sort a list of names in alphabetical order. </li></ul><ul><li>#define ITEMS 5 </li></ul><ul><li>#define MAXCHAR 20 </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>char string[ITEMS][MAXCHAR],dummy[MAXCHAR]; </li></ul><ul><li>int i=0,j=0; </li></ul><ul><li>printf(“Enter names:”); </li></ul><ul><li>while(i<ITEMS) </li></ul><ul><li>scanf(“%s”,string[i++]); </li></ul><ul><li>for(i=1;i<ITEMS;i++) </li></ul><ul><li>{ </li></ul><ul><li>for(j=1;j<=ITEMS-i;j++) </li></ul><ul><li>{ </li></ul><ul><li>if(strcmp(string[j-1],string[j])>0) </li></ul><ul><li>{ </li></ul><ul><li>strcpy(dummy,string[j-1]); </li></ul><ul><li>strcpy(string[j-1],string[j]); </li></ul><ul><li>strcpy(string[j],dummy); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>Printf(“n Alphabetical order”); </li></ul><ul><li>for(i=0;i<ITEMS;i++) </li></ul><ul><li>printf(“%s”,string[i]); </li></ul><ul><li>} </li></ul>
  137. 137. <ul><li>Write the program to count the number of character,word and spaces in line : </li></ul><ul><li>#include<stdio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>char line[81],ctr; </li></ul><ul><li>int i,c,end=0,character=0,words=0,line=0; </li></ul><ul><li> while(end==0) </li></ul><ul><li>{ </li></ul><ul><li>c=0; </li></ul><ul><li> while((ctr=getchar()) != ‘n’) </li></ul><ul><li>line[c++] = ctr; line[c] = ‘0’; </li></ul><ul><li>if(line[0]=‘ ’) </li></ul><ul><li>break; </li></ul><ul><li>else </li></ul><ul><li>{ </li></ul><ul><li> words++; </li></ul><ul><li> for(i=0;line[i] != ‘0’;i++) </li></ul><ul><li>continue : </li></ul>
  138. 138. <ul><li>if(line[i] == ‘ ’ || line[i] == ‘t’) </li></ul><ul><li>words++; </li></ul><ul><li>} </li></ul><ul><li>/* counting lines and characters */ </li></ul><ul><li>lines = lines +1; </li></ul><ul><li>characters = characters + strlen(line); </li></ul><ul><li>} </li></ul><ul><li>printf(“Number of lines = %dn”,lines); </li></ul><ul><li>printf(“Number of words = %dn”,words); </li></ul><ul><li>printf(“Number of character = %d”,characters); </li></ul><ul><li>} </li></ul>
  139. 139. <ul><li>Chapter - 9 User Defined Function : </li></ul><ul><li>C Functions can be classified into two category : </li></ul><ul><li>1. Library function. </li></ul><ul><li>2. User defined function. </li></ul><ul><li>Difference between library and user defined function : library function is inbuilt function while user defined function is developed by user. </li></ul><ul><li>Ex : printf and scanf are library function while main() is an user defined function </li></ul>
  140. 140. <ul><li>Need for user defined function : </li></ul><ul><li>main is a user defined function.It is possible to write entire code in main function but it leads a number of problems. </li></ul><ul><li>The program becomes to large and complex, so the task of maintaining,debugging and testing is become difficult. </li></ul><ul><li>If a program is divided into function parts then each parts may be individually coded and finally combined into single units. These subprogram is called function. </li></ul><ul><li>And it is easier to understand, to debug code and test. </li></ul>
  141. 141. <ul><li>Benefits of function: </li></ul><ul><li>1. It facilitates top - down modular programming. </li></ul><ul><li>2. The length of source code is reduced by using function at appropriate place. </li></ul><ul><li>3. It is easy to locate and isolate faulty function for further investigation. </li></ul><ul><li>4. A function can be used by many other programs. </li></ul>
  142. 142. <ul><li>Forms (Syntax) of function : </li></ul><ul><li>return_type function_name(argument list) </li></ul><ul><li>{ </li></ul><ul><li>local variable declaration; </li></ul><ul><li>executable statements; </li></ul><ul><li>………… . </li></ul><ul><li>………… . </li></ul><ul><li>return( expression); </li></ul><ul><li>} </li></ul><ul><li>By default return_type of function is int </li></ul><ul><li>void should not return any value. </li></ul>
  143. 143. <ul><li>Ex:1 </li></ul><ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>void printline(); // prototype </li></ul><ul><li>printline(); // calling function </li></ul><ul><li>printf(&quot;This is a function programn&quot;); </li></ul><ul><li>printline(); </li></ul><ul><li>} </li></ul><ul><li>void printline() //called function </li></ul><ul><li>{ </li></ul><ul><li>int i; </li></ul><ul><li>for(i=1;i<40;i++) </li></ul><ul><li>printf(&quot;-&quot;); </li></ul><ul><li>printf(&quot;n&quot;); </li></ul><ul><li>} </li></ul>
  144. 144. <ul><li>Ex:2 </li></ul><ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void printline() //called function </li></ul><ul><li>{ </li></ul><ul><li>int i; </li></ul><ul><li>for(i=1;i<40;i++) </li></ul><ul><li>printf(&quot;-&quot;); </li></ul><ul><li>printf(&quot;n&quot;); </li></ul><ul><li>} </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>//does not required to declare prototype </li></ul><ul><li>printline(); // calling function </li></ul><ul><li>printf(&quot;This is a function programn&quot;); </li></ul><ul><li>printline(); </li></ul><ul><li>} </li></ul><ul><li>If you declared UDF before the main function than it should not required to declare prototype. </li></ul>
  145. 145. <ul><li>Category of the function : </li></ul><ul><li>There are three categories of the function : </li></ul><ul><li>1. Function with no arguments and no return value </li></ul><ul><li>2. Function with arguments and no return value. </li></ul><ul><li>3. Function with arguments and with return value </li></ul>
  146. 146. <ul><li>Function with no arguments and no return value : </li></ul><ul><li>This function can not contain any arguments and any return value. </li></ul><ul><li>Ex: </li></ul><ul><li>#include<stdio.h> </li></ul><ul><li>void mul() </li></ul><ul><li>{ </li></ul><ul><li>int j,k,mul; </li></ul><ul><li>scanf(“%d”,&j); scanf(“%d”,&k); </li></ul><ul><li>mul = j * k; </li></ul><ul><li>printf(“%d”,mul); </li></ul><ul><li>} </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>mul(); </li></ul><ul><li>} </li></ul>
  147. 147. <ul><li>Function with arguments and no return value : </li></ul><ul><li>This function contain arguments but does not return value. </li></ul><ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>void mul(int,int); </li></ul><ul><li>int j,k; </li></ul><ul><li>clrscr(); </li></ul><ul><li>scanf(&quot;%d %d&quot;,&j,&k); </li></ul><ul><li>mul(j,k); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul><ul><li>void mul(int l,int m) </li></ul><ul><li>{ </li></ul><ul><li>int mul; </li></ul><ul><li>mul = l * m; </li></ul><ul><li>printf(&quot;%d&quot;,mul); </li></ul><ul><li>} </li></ul>
  148. 148. <ul><li>Function with arguments and with return value : </li></ul><ul><li>Ex: </li></ul><ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int mul(int,int); </li></ul><ul><li>int j,k,multipli; </li></ul><ul><li>clrscr(); </li></ul><ul><li>scanf(&quot;%d %d&quot;,&j,&k); </li></ul><ul><li>multipli=mul(j,k); </li></ul><ul><li>printf(&quot;%d&quot;,multipli); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul><ul><li>int mul(int l,int m) </li></ul><ul><li>{ </li></ul><ul><li>int mul; </li></ul><ul><li>mul = l * m; </li></ul><ul><li>return(mul); </li></ul><ul><li>} </li></ul>
  149. 149. <ul><li>Recursion : </li></ul><ul><li>Recursion is a process where a function calls itself. </li></ul><ul><li>Ex: main() </li></ul><ul><li>{ </li></ul><ul><li>printf(“This is an example of recursion “); </li></ul><ul><li>main(); </li></ul><ul><li>} </li></ul><ul><li>output : </li></ul><ul><li>This is an example of recursion </li></ul><ul><li>This is an example of recursion </li></ul><ul><li>…… .. </li></ul><ul><li>Execution will be continue indefinitely </li></ul>
  150. 150. <ul><li>Ex: Write the program to find the factorial of the given no. using the recursion : </li></ul><ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>factorial(int no) </li></ul><ul><li>{ </li></ul><ul><li>int fact; </li></ul><ul><li>if(no==1) </li></ul><ul><li>return(1); </li></ul><ul><li>else </li></ul><ul><li>fact=no*factorial(no-1); </li></ul><ul><li>return(fact); </li></ul><ul><li>} </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>int no; </li></ul><ul><li>clrscr(); </li></ul><ul><li>printf(&quot;Enter the number&quot;); </li></ul><ul><li>scanf(&quot;%d&quot;,&no); </li></ul><ul><li>factorial(no); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul>
  151. 151. <ul><li>Output : </li></ul><ul><li>Enter No : 3 </li></ul><ul><li>fact=3*factorial(2) </li></ul><ul><li>= 3*2*factorial(1) </li></ul><ul><li>= 3*2*1 </li></ul><ul><li>=6 </li></ul><ul><li>Function with arrays : </li></ul><ul><li>Syntax : functionname(arrayname,size) </li></ul><ul><li>Ex : largest(a,n) </li></ul><ul><li>Where largest is a function name, a is an array name and n is a size of an array </li></ul>
  152. 152. <ul><li>Ex: Find the maximum no: </li></ul><ul><li>float largest(float a[],int n) </li></ul><ul><li>{ </li></ul><ul><li>int i ; float max; max = a[0]; </li></ul><ul><li>for(i=1;i<n;i++) </li></ul><ul><li>if(max < a[i]) </li></ul><ul><li>max = a[i]; </li></ul><ul><li>return(max); </li></ul><ul><li>} </li></ul><ul><li>main() </li></ul><ul><li>{ </li></ul><ul><li>float largest[float [],int n]; </li></ul><ul><li>float value[4]={2.5,-4.75,1.2,3.67} </li></ul><ul><li>printf(“%fn”,largest(value,4)); </li></ul><ul><li>} </li></ul>
  153. 153. <ul><li>The scope and lifetime of variables in function : </li></ul><ul><li>In C there are four types of storage classes : </li></ul><ul><li>1. Autoamatic storage classes </li></ul><ul><li>2. External variables </li></ul><ul><li>3. Static variables. </li></ul><ul><li>4. Register variables </li></ul><ul><li>Automatic variables : </li></ul><ul><li>Automatic varibles declared inside a function in which they are utilized. </li></ul><ul><li>so, automatic variables are private to that function. </li></ul>
  154. 154. <ul><li>Automatic variables are referred as a local variable or internal variable. </li></ul><ul><li>So you can declare this variable in more than one function without any confusion. </li></ul><ul><li>Ex: main() </li></ul><ul><li>{ int number ; } </li></ul><ul><li>You can also declared explicitly with auto keyword </li></ul><ul><li>Ex: main() </li></ul><ul><li>{ auto int number ; } </li></ul>
  155. 155. <ul><li>External variable : </li></ul><ul><li>variables that are both active and alive throughout the entire program is known as external variable.It is also called global variable. </li></ul><ul><li>unlike local varibles global variables can be accessed by any function in the program. </li></ul><ul><li>Ex: int number; </li></ul><ul><li>main() </li></ul><ul><li>{ </li></ul><ul><li>……… } </li></ul><ul><li>fun1() </li></ul><ul><li>{ </li></ul><ul><li>…… .. } </li></ul><ul><li>fun2() </li></ul><ul><li>{ </li></ul><ul><li>…… .. } </li></ul>
  156. 156. <ul><li>Static variables: </li></ul><ul><li>static variables declared using static keyword. </li></ul><ul><li>Ex: static int number; </li></ul><ul><li>Static variables are initialised only once during the execution of the program. </li></ul><ul><li>static variables are generally used to retain values between function calls. </li></ul><ul><li>Ex: main() </li></ul><ul><li>{ int i; </li></ul><ul><li>for(i=0;i<=3;i++) </li></ul><ul><li>stat(); </li></ul><ul><li>} </li></ul><ul><li>stat() </li></ul><ul><li>{ </li></ul><ul><li>static int x=0; x++ ; </li></ul><ul><li>printf(“%dn”,x) } </li></ul>
  157. 157. <ul><li>Register Variables : </li></ul><ul><li>Register Variables are generally used to store variable register so you can access that variable very fast than store in the memory. </li></ul><ul><li>you can create your register variable using register keyword. </li></ul><ul><li>Ex: register int x; </li></ul><ul><li>register variables are generally used in looping. </li></ul>
  158. 158. <ul><li>Chapter - 10 Structure and Unions </li></ul><ul><li>Structures : </li></ul><ul><li>Structure is nothing but group of dataitems of different type with a single name. </li><