C material


Published on

this is the ppt of C upto data structure.

Published in: Education, Technology
1 Comment
  • converting postfix to infix expressions
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

C material

  1. 1. C_Review AppsConnect Technologies Pvt Ltd
  2. 2. CONTENTS <ul><li>Introduction to ‘C’ </li></ul><ul><li>Importance of C </li></ul><ul><li>The ‘C’ Compilation Model </li></ul><ul><li>Basic structure of C program </li></ul><ul><li>‘ C’ Identifiers, Constants, Data Types </li></ul><ul><li>‘ C’ Operators </li></ul><ul><li>Console input/output Functions </li></ul>
  3. 3. <ul><li>Decision Making and Branching </li></ul><ul><li>If statement </li></ul><ul><li>The if-else statement </li></ul><ul><li>Nesting of if-else statement </li></ul><ul><li>The Switch statement </li></ul><ul><li>The goto statement </li></ul><ul><li>Decision Making and Looping </li></ul><ul><li>The while statement </li></ul><ul><li>The do statement </li></ul><ul><li>The for statement </li></ul>
  4. 4. <ul><li>Functions </li></ul><ul><li>Introduction </li></ul><ul><li>Elements of function </li></ul><ul><li>Function definition </li></ul><ul><li>Function call </li></ul><ul><li>Function declaration </li></ul><ul><li>Category of function of function </li></ul><ul><li>Function with no arguments and no return values </li></ul><ul><li>Function with arguments and no returns </li></ul><ul><li>Function with arguments and one return value </li></ul><ul><li>Function with no arguments but return a value </li></ul><ul><li>Recursion </li></ul>
  5. 5. <ul><li>Passing array to the function </li></ul><ul><li>Passing string to function </li></ul><ul><li>The scope,visibility, and lifetime of variables </li></ul><ul><li>Automatic variable </li></ul><ul><li>External variable </li></ul><ul><li>Static variable </li></ul><ul><li>Register variable </li></ul><ul><li>Arrays </li></ul><ul><li>Introduction </li></ul><ul><li>Types of array </li></ul><ul><li>One-Dimensional array </li></ul><ul><li>Two-Dimensional array </li></ul>
  6. 6. <ul><li>Pointers </li></ul><ul><li>Introduction </li></ul><ul><li>Accessing the address of a variable </li></ul><ul><li>Declaring & initialization pointer variables </li></ul><ul><li>Accessing a variable through its pointer </li></ul><ul><li>Pointers and Arrays </li></ul><ul><li>Array of pointers </li></ul><ul><li>Pointers as a function arguments </li></ul><ul><li>Functions returning pointers </li></ul><ul><li>Pointer to functions </li></ul><ul><li>Pointer and structures </li></ul>
  7. 7. <ul><li>Character Arrays & Strings </li></ul><ul><li>Introduction </li></ul><ul><li>Declaring and initializing string variables </li></ul><ul><li>Reading a line of text using getchar and gets function </li></ul><ul><li>Writing strings to screen using puts and putchar </li></ul><ul><li>String-Handling functions </li></ul><ul><li>Strcat(), strcmp(), strcpy(), strlen() etc.. </li></ul>
  8. 8. <ul><li>Structure </li></ul><ul><li>Introduction </li></ul><ul><li>Defining and declaring a structure variables </li></ul><ul><li>Accessing structure members </li></ul><ul><li>Structure initialization </li></ul><ul><li>Array of structure </li></ul><ul><li>Array within structure </li></ul><ul><li>Structure within structure </li></ul><ul><li>Structures and functions </li></ul>
  9. 9. <ul><li>File Management in C </li></ul><ul><li>Introduction </li></ul><ul><li>Defining and opening a file </li></ul><ul><li>Closing a file </li></ul><ul><li>Input/output Operations on File </li></ul><ul><li>Random access to file </li></ul><ul><li>The preprocessor </li></ul><ul><li>Introduction </li></ul><ul><li>Macro substitution </li></ul><ul><li>File Inclusion </li></ul>
  10. 10. <ul><li>Linked Data Structures </li></ul><ul><li>Dynamic Data structures </li></ul><ul><li>Single linked list </li></ul><ul><li>The operations that can be performed on singly linked lists- </li></ul><ul><li>Creation of a list </li></ul><ul><li>Insertion of nodes </li></ul><ul><li>Traversal of the list </li></ul><ul><li>Search of a specific node </li></ul><ul><li>Modification of a node </li></ul><ul><li>Deletion of a node </li></ul><ul><li>Double linked list </li></ul>
  11. 11. <ul><li>Introduction :- C was developed by Dennis Ritchie at Bell Laboratories in 1972. Most of its principles and ideas were taken from the earlier language B, BCPL and CPL. CPL was developed jointly between the Mathematical Laboratory at the University of Cambridge and the University of London computer unit in1960s. In 1972, a co-worker of Ken Thompson, Dennis Ritchie developed C Language by taking some of the generality found in BCPL to the B language . In 1973, C language had become powerful enough that most of the Unix kernel was rewritten in C. This was one of the first operating system kernels implemented in a language other than assembly. The committee approved a version of C in December 1989which is now known as ANSI C. </li></ul>
  12. 12. <ul><li>Year Language Developed </li></ul><ul><li> 1960 ALGOL International Committee </li></ul><ul><li> 1967 BCPL Martin Richards </li></ul><ul><li> 1970 B Ken Thompson </li></ul><ul><li> 1972 Traditional C Dennis Ritchie </li></ul><ul><li> 1989 ANSI C ANSI Committee </li></ul><ul><li> 1990 ANSI/ISO ISO Committee </li></ul><ul><li> 1990 C99 Standardization Committee </li></ul>History of ANSI C
  13. 13. <ul><li>Introduction of C :- It is a robust language whose rich set of built-in-functions and operators can be used to write any complex program. The C compiler combines the capabilities of an assembly language with the feature of high level language and therefore it is well suitable for writing both system software and business package. Programs written in C are efficient and fast. this is due to variety of data types and powerful operators. It is suited for structured programming. It offers portability. It is ability to extend itself. </li></ul><ul><li>C compiling process :- </li></ul>
  14. 14. <ul><li> Source code (editor) </li></ul><ul><li> Preprocessor </li></ul><ul><li> Compile (Compiler) </li></ul><ul><li> Link (Linker) </li></ul><ul><li> Execute (Loader) </li></ul>Source File x.c Object file x.o Other object modules The steps involved in C compilation process
  15. 15. <ul><li>Basic structure of C program :- </li></ul><ul><li>Documentation Section </li></ul><ul><li>Link Section </li></ul><ul><li>Definition Section </li></ul><ul><li>Global Declaration Section </li></ul><ul><li>Main() Function Section </li></ul><ul><li>{ </li></ul><ul><li>} </li></ul><ul><li>Subprogram section </li></ul>(User defined function) Declaration part Executable part Function1 Function2 - Function n
  16. 16. <ul><li>Filename : hello.c </li></ul><ul><li>Author : Brian Kernighan & Dennis Ritchie </li></ul><ul><li>Description : This program prints the greeting “Hello, World!” */ </li></ul><ul><li>#include <stdio.h> </li></ul><ul><li>int main ( ) </li></ul><ul><li>{ </li></ul><ul><li>printf (“Hello, World!n”) ; </li></ul><ul><li>return 0 ; </li></ul><ul><li>} </li></ul>
  17. 17. <ul><li>Keyword :- Keywords are the words whose meaning has already been explained to the ‘C’ compiler. The keywords are also called as reserved words. There are 32 keywords available in ‘C’. </li></ul>
  18. 18. <ul><li>Identifiers :- Identifiers refers to the names of variables, functions, and arrays. These are user-defined names and consists of a sequence of letters and digits, with a letter as a first character. Both uppercase and lowercase letters are permitted. The underscore character also permitted in identifiers. </li></ul><ul><li>Rules for identifiers:- </li></ul><ul><li>First character must be a alphabet or underscore. </li></ul><ul><li>Must consist of only letters, digits or underscore. </li></ul><ul><li>Cannot use a keyword. </li></ul><ul><li>Must not contain a white space. </li></ul>
  19. 19. <ul><li>Constants :- A constant is a quantity that doesn’t change this quantity can be stored at a location in the memory of the computer. </li></ul><ul><li>There are mainly two types of constant. </li></ul><ul><li>Numeric Constant </li></ul><ul><li>Integer Constant </li></ul><ul><li>Floating point Constants </li></ul><ul><li>Character Constants. </li></ul><ul><li>1. Single Character Constant </li></ul><ul><li>String Constant </li></ul><ul><li>Variable :- A variable is a data type that may be used to store a data value . </li></ul>
  20. 20. <ul><li>Data Types :- C language is rich in its data type. There are mainly three classes of data types. </li></ul><ul><li>Primary Data types </li></ul><ul><li>Derived Data types </li></ul><ul><li>User defined data types </li></ul><ul><li>Primary data type :- There are mainly five primary data types. </li></ul><ul><li>Int </li></ul><ul><li>Char </li></ul><ul><li>Float </li></ul><ul><li>Void </li></ul>
  21. 21. <ul><li>Integer type:- </li></ul><ul><li>Data Type Range Byte Format </li></ul><ul><li>Signed char -128 to +127 1 %c </li></ul><ul><li>Unsigned char 0 to 255 1 %c </li></ul><ul><li>Short signed int -32768 to +32767 2 %d </li></ul><ul><li>Short unsigned int 0 to 65535 2 %u </li></ul><ul><li>Long signed int -2147483648 to 4 %ld +2147483647 </li></ul><ul><li>Long unsigned int 0 to +4294967295 4 %lu </li></ul>
  22. 22. <ul><li>Floating Point:- Floating point numbers are defined in C by the keyword “float”. </li></ul><ul><li>Depending upon the accuracy, it is divided into three types. </li></ul><ul><li>Float </li></ul><ul><li>Double </li></ul><ul><li>Long double. </li></ul><ul><li>Data Type Range Byte Format </li></ul><ul><li>Float -3.4e38 to +3.4e38 4 %f </li></ul><ul><li>Double -1.7e308 to +1.7e308 8 %lf </li></ul><ul><li>long double -1.7e4932 to +1.7e4932 10 %Lf </li></ul>
  23. 23. <ul><li>Character Types – A single character can be defined as char type. Character are usually stored in 1 byte of internal storage. The qualifier signed or unsigned may be explicitly applied to char. </li></ul><ul><li>Unsigned char 0-255, </li></ul><ul><li>Signed char -128to 127. </li></ul><ul><li>Void types:- The void type has no value. The type of a function said to be void when it does not return any value to the calling function. </li></ul>
  24. 24. <ul><li>Derived data type:- There are four derived data types. </li></ul><ul><li>Array </li></ul><ul><li>Function </li></ul><ul><li>Structure </li></ul><ul><li>Pointer </li></ul><ul><li>User Defined data type :- </li></ul><ul><li>typedef type identifier </li></ul><ul><li>enum identifier{value1,value2,…valuen}; </li></ul><ul><li>Typedef :- C supports a feature known as type definition that allow to define a identifier that would represent an existing data type . </li></ul><ul><li>its general form : typedef type identifier; </li></ul><ul><li> example: typedef int units; </li></ul>
  25. 25. <ul><li>2. Enumerated data type:- It is defined as follows. </li></ul><ul><li>enum identifier {value1,value2,..valuen}; </li></ul><ul><li>The enumerated variables v1,v2,…vn can only have one of the values value1,value2,…valuen. </li></ul><ul><li>Example:- 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> </li></ul>
  26. 26. C Operators <ul><li>Operator:- A operator is a symbol that tells the computer to perform certain mathematical or logical manipulation. Operators are used in programs to manipulate data and variables. </li></ul><ul><li>C operator are classified into number of categories. They are listed below. </li></ul><ul><li>Arithmetic operators </li></ul><ul><li>Relational operators </li></ul><ul><li>Logical operators </li></ul><ul><li>Assignment operators </li></ul><ul><li>Increment and decrement operators </li></ul><ul><li>Conditional operators </li></ul><ul><li>Bitwise operators </li></ul><ul><li>Special operators </li></ul>
  27. 27. <ul><li>Arithmetic Operator:- C provides all the basic operators. They are listed below. </li></ul><ul><li>Operator Meaning </li></ul><ul><li>+ Addition or unary plus </li></ul><ul><li>_ Subtraction or unary minus </li></ul><ul><li>* Multiplication </li></ul><ul><li>/ Division </li></ul><ul><li>% Modulo Division </li></ul><ul><li>Program:- </li></ul><ul><li>#include<stdio.h> </li></ul><ul><li> main() </li></ul><ul><li> { </li></ul><ul><li>int months,days; </li></ul><ul><li>Printf(“enter the days:n”); </li></ul><ul><li>Scanf(“%d”,&days); </li></ul><ul><li>Month=days/30; </li></ul><ul><li>Days=days%30; </li></ul><ul><li>Printf(“months=%d days=%d, month,days”); </li></ul><ul><li> } </li></ul>
  28. 28. <ul><li>Relational Operator:- If we need any comparison or relation in an expression, then this can be achieved with the help of relational operator. They are listed below. </li></ul><ul><li>Operator Meaning </li></ul><ul><li>< is less then </li></ul><ul><li><= is less then or equal to </li></ul><ul><li>> is greater then </li></ul><ul><li>>= is greater then or equal to </li></ul><ul><li>== is equal to </li></ul><ul><li>!= is not equal to. </li></ul><ul><li>Its expression- ae-1< relational operator> ae-2. </li></ul>
  29. 29. <ul><li>Logical operator:- C has three logical operators. They are given below. </li></ul><ul><li>&& meaning logical AND </li></ul><ul><li>|| meaning logical OR </li></ul><ul><li>! meaning logical NOT. </li></ul><ul><li>Example:- (a>b)&& x==10 </li></ul><ul><li>Logical Operator:- Assignment Operators are used to assign the result of an expression to a variable. The assignment operator is “=”. </li></ul><ul><li>Its expression is:- v op= exp ; where v= variable. </li></ul><ul><li> exp=expression </li></ul><ul><li>example:- x=x+3; </li></ul>
  30. 30. <ul><li>Increment & Decrement Operator :- C allows two very useful operators not generally found in other languages. These are increment(++) & decrement operator(--). </li></ul><ul><li>Example:- m++ or ++m; </li></ul><ul><li> m-- or ; --m; </li></ul><ul><li>Conditional Operator:- A ternary operator pair” ? : “ is available in C to make the conditional expression of the form </li></ul><ul><li>exp1 ? Exp2 : exp3 ; </li></ul><ul><li>Where exp1,exp2,exp3 are the expression. </li></ul><ul><li>Example:- a=10; </li></ul><ul><li> b=15; </li></ul><ul><li> x=(a>b)?a:b; </li></ul>
  31. 31. <ul><li>Bitwise Operator:- C has a distinction of supporting special operators known as bitwise operators for manipulation of data at bit level. These operators are used for testing the bits, or shifting them right or left. It may not be applied for float or double. </li></ul><ul><li>The list of bitwise operator is given below. </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><ul><li>Special Operator:- C supports some special operators. They are comma operator, size of operator, pointer operator and member selection operator. </li></ul>
  32. 32. <ul><li>Comma operator:- it is used to link the related expressions together. </li></ul><ul><li>Example:- value=(x=10,y=5,x+y); </li></ul><ul><li>The size of operator:- The size of is the compile time operator and when with an operand, it returns the number by bytes the operand. </li></ul><ul><li>Example:- m=sizeof (sum); </li></ul><ul><li>n=sizeof (long int); </li></ul><ul><li>Arithmetic expression:- An arithmetic expression is the combination of variables, constants, and operators arranged as per the syntax of the C language. </li></ul><ul><li>Example:- variable=expression; </li></ul><ul><li>x=a*b-c; </li></ul>
  33. 33. <ul><li>Console input and output function:- The screen and keyword together are called a console. The console input and output function are classified into two types. </li></ul><ul><li>Formatted console I/O function. </li></ul><ul><li>The list of console of i/o are given below. </li></ul><ul><li>The formatted i/o are given below. </li></ul><ul><li>Printf(), scanf(), escape sequences, sprintf(), sscanf(). </li></ul><ul><li>Unformatted console I/O function. </li></ul><ul><li>The list of unformatted function are listed below. </li></ul><ul><li>getch(), getche(), getchar(), fgetchar(), gets(), puts(). </li></ul>
  34. 34. <ul><li>Decision Making and Branching :- The C offers such decision-making capabilities by supporting the following statements. They are </li></ul><ul><li>1. if statement </li></ul><ul><li>2. Switch statement </li></ul><ul><li>3. Conditional Operator statement </li></ul><ul><li>4. goto statement </li></ul><ul><li>If statement :- The if statement is a powerful decision making statement and is used to control the flow of execution of statements. Its takes the following form. </li></ul><ul><li>If(test expression) </li></ul><ul><li>{ statement-block; </li></ul><ul><li>} </li></ul><ul><li>statement-x; </li></ul>
  35. 35. <ul><li>The if-else statement :- the if…else statement is an extension of the simple if statement. The general form is: </li></ul>If(test expression) { true-block statement(s); } else { false-block statement(s); } statement-x
  36. 36. <ul><li>Nesting of if…else statements :- When a series of decision are involved, then we may use if-else statement in nested form. Its general form is: </li></ul>If (test condition-1) { if (test condition-2); { statements-1; } else { stateement-2; } } else { statement-3; } Statement-x;
  37. 37. <ul><li>Switch Statements :- C has a built-in multiway decision statements known as a switch. The switch statement tests the variable of a given variable against a list of case value and when a match is found, a block of statements associated with that case is executed. General form is: </li></ul>Switch (expression) { case value-1: block-1 break; case value-2: block-2 break; ……… ……… Default: default-block Break; } Statement-x;
  38. 38. <ul><li>The goto statement :- A statement in a computer program that provides for the direct transfer of control to another statement with the identifier that is the argument of the GOTO statement. Its general form is: </li></ul>goto label; -------------- -------------- -------------- label: statement;
  39. 39. <ul><li>Decision Making and Looping :- A program loop consists of two segments, one known as the body of the loop and the other is known as the control statement. So a looping process, in general consists of four steps. </li></ul><ul><li>1. Setting and initialization of a condition variable. </li></ul><ul><li>2. Execution of the statements in the loop. </li></ul><ul><li>3. Test for a specified value of the condition variable for execution of the loop. </li></ul><ul><li>4. Incrementing or updating the condition variable. </li></ul><ul><li>C language provides three constructs for performing the loop operation. They are list below. </li></ul><ul><li>The while statement. </li></ul><ul><li>The do statement. </li></ul><ul><li>The for statement. </li></ul>
  40. 40. <ul><li>While statement :- The simplest of all the looping structure in c is the while statement. The basic format of while statements is: </li></ul><ul><li> while (test condition) </li></ul><ul><li> { </li></ul><ul><li> body of loop </li></ul><ul><li> } </li></ul><ul><li>This is a entry controlled loop statement. the test-condition is evaluated and if the condition is true, then the body of the loop is executed. It will continue till condition becomes false. </li></ul><ul><li>Do statement:- As we saw in the while statement if the condition is false it can’t execute statements. But there might be necessary to execute the body of the loop before the test is performed. </li></ul>
  41. 41. <ul><li>General form of do-while:- </li></ul><ul><li>do </li></ul><ul><li> { </li></ul><ul><li> body of the loop </li></ul><ul><li> } </li></ul><ul><li> while (test-condition); </li></ul><ul><li>For statement:- The for loop is another entry- controlled loop that provides a more concise loop control structure. The general form of the loop is:- </li></ul><ul><li>for (initialization ; test-condition ; Increment) </li></ul><ul><li> { </li></ul><ul><li> body of loop </li></ul><ul><li> } </li></ul>
  42. 42. <ul><li>Function :- A function is a self contained block of statements that perform a coherent task of some kind. </li></ul><ul><li>There are three elements that are related to the user defined function. </li></ul><ul><li>Function definition </li></ul><ul><li>Function Call </li></ul><ul><li>Function declaration. </li></ul><ul><li>FUNCTION DEFINITION :- The Function definition is an independent program module i.e, specially written to implement the requirements of the function. </li></ul><ul><li> It is also known as function implementation shall include the following elements. </li></ul>
  43. 43. <ul><li>Function name; </li></ul><ul><li>Function type; </li></ul><ul><li>List of parameters; </li></ul><ul><li>Local variable declaration; </li></ul><ul><li>Function statements; </li></ul><ul><li>A return statements. </li></ul><ul><li>A general format of a function definition is given below: </li></ul><ul><li>function_type function_name(parameter list) </li></ul><ul><li> { </li></ul><ul><li>Local variable declaration; </li></ul><ul><li>Executable statement1; </li></ul><ul><li>Executable statement2; </li></ul><ul><li>………… </li></ul><ul><li>………… </li></ul><ul><li>Return statement; </li></ul><ul><li> } </li></ul><ul><li>The first line is known as function header. and the statements within the opening and closing braces is known as the function body . </li></ul>
  44. 44. <ul><li>Function header :- The function header consists of three parts; the function types, the function name, the formal parameter list. </li></ul><ul><li>Name and type :- The function type specifies the type of value that the function is expected to return to the program calling the function. By default it is integer type. </li></ul><ul><li>Formal parameter list :- The parameter list declares the variables that will receive the data sent by the calling program. </li></ul><ul><li>Function body :- The function body contains the declaration and statements necessary for the performing the required task . It is enclosed with open and closed braces, contains three parts </li></ul><ul><li>1. local declaration </li></ul><ul><li>2. function statements </li></ul><ul><li>3. a return type statements </li></ul>
  45. 45. <ul><li>Function Call :- A function can be called by simply using the function name followed by a list of actual parameters or arguments. </li></ul><ul><li>example:- </li></ul><ul><li>main() </li></ul><ul><li> { </li></ul><ul><li> int y; </li></ul><ul><li> y = mul(10,5); </li></ul><ul><li> Printf(“%dn”, y); </li></ul><ul><li> } </li></ul><ul><li>When the compiler encounters a function call, the control is transferred to the function mull(). This function is then executed line by line as described and a value is returned when a return statement is encountered. </li></ul><ul><li>Function declaration :- All the function in C program must be declared, before they are involved. A function declaration (function prototype) consists of four parts. </li></ul>
  46. 46. <ul><li>Function type. </li></ul><ul><li>Function name. </li></ul><ul><li>Parameter list </li></ul><ul><li>Terminating semicolon. </li></ul><ul><li>They are coded in the following format: </li></ul><ul><li>Function-type function-name (parameter list); </li></ul><ul><li>There are five category of functions. They are listed below. </li></ul><ul><li>Function with no arguments and no return values. </li></ul><ul><li>Function with arguments and no return values. </li></ul><ul><li>Function with arguments and return value. </li></ul><ul><li>Function with no arguments but return a value. </li></ul><ul><li>Function that return multiple values. </li></ul>
  47. 47. <ul><li>Recursion :- When a called function in turn calls another function a process of chaining occurs. Recursion is a process where a function calls itself. </li></ul><ul><li>example:- main() </li></ul><ul><li> { </li></ul><ul><li> printf(“recursion typen”); </li></ul><ul><li>main(); </li></ul><ul><li> } </li></ul><ul><li>The output of this program is like this: </li></ul><ul><li>“ recursion type “ </li></ul><ul><li>“ recursion type “ </li></ul><ul><li>“ recursion type “ </li></ul><ul><li>Example:- A best example of recursion is factorial of a given number. </li></ul>
  48. 48. <ul><li>/*example of recursion*/ </li></ul><ul><li>#include<stdio.h> </li></ul><ul><li> int rec ( int); </li></ul><ul><li> int main() </li></ul><ul><li> { </li></ul><ul><li> int a, fact; </li></ul><ul><li> printf(“enter any number ”); </li></ul><ul><li> Scanf(“%d”,&a); </li></ul><ul><li> fact=rec(a); </li></ul><ul><li> printf(“factorial value=%dn”,fact); </li></ul><ul><li> Return 0; </li></ul><ul><li> } </li></ul><ul><li>Int rec( int x ) </li></ul><ul><li>{ </li></ul><ul><li>int f; </li></ul><ul><li> if(x==1) </li></ul><ul><li> return(1); </li></ul><ul><li> else </li></ul><ul><li> f=x*rec(x-1); </li></ul><ul><li>return (f); </li></ul><ul><li>} </li></ul>
  49. 49. <ul><li>Passing array to function :- Like the other variable, it is possible to pass the value of an array to the function. It may may be either single-dimension array or multi-dimension array. For the single dimension array we can passed without any subscripts and the size of the array as arguments. </li></ul><ul><li>Example:- largest(a,n) </li></ul><ul><li> and the largest function header looks like: </li></ul><ul><li> float largest(float array[], int size) </li></ul><ul><li>The multi-dimensional array can be passed by using the following simple rules. </li></ul><ul><li>Function must be called by passing the array name. </li></ul><ul><li>In function definition, must indicate array has two dimensions by indicating two sets of brackets. </li></ul><ul><li>The size of the second dimension must be specified. </li></ul><ul><li>The prototype declaration should be similar to the function header. </li></ul>
  50. 50. <ul><li>Passing string to function :- The strings are treated as character arrays in C. hence rules for the passing strings to the functions are very similar to the passing array to the function. Basic rules are </li></ul><ul><li>The function prototype must show that the argument in a string. It is written as </li></ul><ul><li>void display(char str[]); </li></ul><ul><li>A call to the function must have a string array name without subscripts as its actual arguments. </li></ul><ul><li>Example:- display (name); </li></ul><ul><li>The string to be passed must declared a formal argument of the function when it is defined. </li></ul><ul><li>example:- void display(char item_name[]) </li></ul><ul><li>{ ………… </li></ul><ul><li> ………… </li></ul><ul><li>} </li></ul>
  51. 51. <ul><li>Scope,visibility and lifetime of variable:- In c not only do all all variables have a data type, they also have a storage class. These variable storage classes are most relevant to functions. There are four types storage class in C. </li></ul><ul><li>Automatic Variables. </li></ul><ul><li>External variables. </li></ul><ul><li>Static Variables. </li></ul><ul><li>Register variables </li></ul><ul><li>AUTOMATIC VARIABLES :- Automatic variables are declared inside a function in which they are to be utilized. They are created when the function is called and destroyed automatically when the function is excited, hence the name automatic. Automatic variables are local to the function. </li></ul>
  52. 52. <ul><li>Example:- A program to illustrate the automatic variables work. </li></ul><ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>void function1(void); </li></ul><ul><li>void function2(void); </li></ul><ul><li> main() </li></ul><ul><li> { </li></ul><ul><li>int m=1000; </li></ul><ul><li>Function2(); </li></ul><ul><li>Printf(“%dn”,m); </li></ul><ul><li> } </li></ul><ul><li> Void function(void) </li></ul><ul><li> { </li></ul><ul><li> Int m=10; </li></ul><ul><li> Printf(“%dn”,m); </li></ul><ul><li> } </li></ul><ul><li> Void function2(void) </li></ul><ul><li> { </li></ul><ul><li> Int m=100; </li></ul><ul><li> Function1(void); </li></ul><ul><li> Printf(“%dn”,m); </li></ul><ul><li> } </li></ul>
  53. 53. <ul><li>Extern variable :- The variable that are both alive and active throughout the entire program are known as external variables. It is also known as the global variables. It can be accessed by any function in the program. It is declared outside the main function. </li></ul><ul><li>Example:- int number; </li></ul><ul><li>float length=7.5; </li></ul><ul><li>main() </li></ul><ul><li>{ </li></ul><ul><li> ------ </li></ul><ul><li> ------ </li></ul><ul><li>} </li></ul><ul><li>function1() </li></ul><ul><li>{ </li></ul><ul><li> ----- </li></ul><ul><li> ----- </li></ul><ul><li>} </li></ul><ul><li>function2 () </li></ul><ul><li>{ </li></ul><ul><li> ------- </li></ul><ul><li> ------- </li></ul><ul><li> } </li></ul>
  54. 54. <ul><li>Static variables :- The value of static variables persists until the end of the program. A variable can be declared static using keyword static. It may be either an internal type or external type. </li></ul><ul><li>example: - static int x; </li></ul><ul><li>Register variables :- A register declaration advises the compiler that the variable in question will be heavily used. The idea is that register variables are to be placed in machine registers, which may result in smaller and faster programs. </li></ul><ul><li>The register declaration looks like </li></ul><ul><li>register int x; </li></ul><ul><li>register char c; </li></ul><ul><li>The register declaration can only be applied to automatic variables and to the formal parameters of a function. In this later case, it looks like </li></ul><ul><li>f(register unsigned m, register long n) </li></ul><ul><li> { register int i; </li></ul><ul><li> ... } </li></ul>
  55. 55. <ul><li>Array:- Array is the collection of the similar data types. </li></ul><ul><li> there are three types of array. </li></ul><ul><li>One-dimension arrays </li></ul><ul><li>Two dimension arrays </li></ul><ul><li>Multi-dimension arrays </li></ul><ul><li>One-dimension array :- A list items can be given one variable name using only one subscript and such a variable is called single dimension array . </li></ul><ul><li>example:- x[1],x[2],x[3]… </li></ul><ul><li>Declaration:- type variable-name [size]; </li></ul><ul><li>initialization:- type array-name[size]={list of value} </li></ul><ul><li>Two-dimension arrays :- it has two subscript. </li></ul><ul><li>example:- x[5][3] </li></ul><ul><li>declaration:- type array_name[row_size][column_size]. </li></ul><ul><li>initialization:- int table[2][3]={1,1,2,2,3,3} </li></ul><ul><li> int table[2][3]={ </li></ul><ul><li> {1,2,3}, </li></ul><ul><li> {4,5,6}, </li></ul><ul><li> } </li></ul>
  56. 56. <ul><li>Pointer :- A pointer is a derived data type. It is built in from one of the fundamental data type available in c. Pointers contains memory addresses as there values. Pointers are used for the following benefits to the programmers. </li></ul><ul><li>They are </li></ul><ul><li>Pointers are more efficient in handling arraya and data tables. </li></ul><ul><li>Pointers can be used to return multiple values from a function via function arguments. </li></ul><ul><li>Pointer reduce length and complexity of program. </li></ul><ul><li>They increase the execution speed. </li></ul><ul><li>Pointers allow C to support dynamic memory management. </li></ul><ul><li>It is widely used in data structure. </li></ul><ul><li>The use of pointer arrays to character strings results in saving of data storage space in memory. </li></ul>
  57. 57. <ul><li>Accessing the address of a variable :- The operator ‘&’ is used to access the address of the variable associated with it. </li></ul><ul><li>p=&quantity; </li></ul><ul><li>Declaring pointer variable :- The declaration of the pointer variables takes the following form: </li></ul><ul><li>data_types *pt_name; </li></ul><ul><li>example:- int*p; </li></ul><ul><li>Initialization of pointer variable :- A process of assigning the address of a variable to the pointer variable is known as initialization. </li></ul><ul><li>Example:- int quantity; </li></ul><ul><li>Int *p; </li></ul><ul><li>P=&quantity; </li></ul>
  58. 58. <ul><li>Accessing a variable through its pointer :- </li></ul><ul><li>int quantity, *p, n; </li></ul><ul><li>quantity=175; </li></ul><ul><li>P=&quantity; </li></ul><ul><li>n=*p; </li></ul><ul><li>Pointer and arrays :- When an array is declared, the compiler allocates a base address and sufficient amount of storage to contain all the elements of an array in continuous memory locations. </li></ul><ul><li>Suppose we declare an array x as follows: </li></ul><ul><li>int x[5]={ 1,2,3,4,5} </li></ul>1 2 3 4 5 Elements value Address
  59. 59. <ul><li>Pointer and the character string :- It is declared and initialized as follows: char str[5]=“good”; </li></ul><ul><li>The compilers automatically insearts null character ‘0’ at the end of the string. It we can print the string using either printf or puts functions. </li></ul><ul><li>Array of pointers :- It is declared and initialized as follows: </li></ul><ul><li>char *name[3]={ “siwan”, </li></ul><ul><li> “ patna” </li></ul><ul><li>“ jamshedpur” </li></ul><ul><li> }; </li></ul><ul><li>Pointers as function arguments :- When we passed address to the function, the parameters receiving the addresses should be pointers. </li></ul>
  60. 60. <ul><li>The process of calling a function using pointers to pass the addresses of a variables is known as the “call by reference”. And the process of passing the actual value of a variables is known as “call by value”. </li></ul><ul><li>WAP to exchange the values stored in two locations in memory using pointer: </li></ul><ul><li>#include<stdio.h> </li></ul><ul><li>void exchange(int *,int *); </li></ul><ul><li>main() </li></ul><ul><li> { </li></ul><ul><li> int x,y: </li></ul><ul><li> x=100; </li></ul><ul><li> y=200; </li></ul><ul><li> printf(“before exchange: x=%d y=%dn”,x,y); </li></ul><ul><li> exchange(&x,&y); </li></ul><ul><li> printf(“after exchange : x=%d y=%dn”,x,y); } </li></ul><ul><li> exchange (int *a,int *b) </li></ul><ul><li>{ int t; </li></ul><ul><li> t=*a; </li></ul><ul><li> *a=*b; </li></ul><ul><li> *b=t; } </li></ul>
  61. 61. <ul><li>Function returning pointers:- </li></ul><ul><li>int *larger(int *,int *); </li></ul><ul><li>main() </li></ul><ul><li>{ </li></ul><ul><li> int a=10; </li></ul><ul><li> int b=20; </li></ul><ul><li> int =*p; </li></ul><ul><li> p=larger(&a,&b); </li></ul><ul><li> printf(“%d”,*p); </li></ul><ul><li>} </li></ul><ul><li>int *larger(int *x, int *y) </li></ul><ul><li>{ </li></ul><ul><li> if(*x>*y) </li></ul><ul><li> return(x); /*address of a*/ </li></ul><ul><li> else </li></ul><ul><li> return(y); /”address of b”/ </li></ul><ul><li>} </li></ul>
  62. 62. <ul><li>Pointer to functions :- A function is like a variable, has a type and address location in the memory. Therefore we can declare a pointer to function. </li></ul><ul><li> it is declared as follows:- type (*fptr) (); </li></ul><ul><li>Here , this tells to the compiler that fptr is a pointer to function, which returns type value. </li></ul><ul><li>Example:- double mul(int, int); </li></ul><ul><li> double (*p1)(); </li></ul><ul><li> P1=mul; </li></ul><ul><li> here p1 as a pointer to function and mul is a punction. P1 is pointing to mul() function. To call the mul, we have to use the list like that </li></ul><ul><li>(*p1)(x,y) /*function call*/ </li></ul>
  63. 63. <ul><li>Pointers and structures :- it is declared as follows: suppose product is an array variable of struct type. The name product represents the address of its 0 th elements. </li></ul><ul><li>struct details </li></ul><ul><li> { </li></ul><ul><li> char name[20]; </li></ul><ul><li> int number; </li></ul><ul><li> float price; </li></ul><ul><li> } product[2], *ptr; </li></ul><ul><li>Here, statement declares product as a array of two elements, each of type struct details and ptr as a pointer to data objects of the type struct details . </li></ul><ul><li>ptr=product; here, it is assign the address of the 0 th elements of product to ptr. The pointer ptr will point to product[0]. It is accessed like following notation. </li></ul><ul><li>ptr->name; </li></ul><ul><li>ptr->number; </li></ul><ul><li>Ptr->price ; </li></ul>
  64. 64. <ul><li>WAP to to illustrate the use of structure pointers. </li></ul><ul><li>#include<stdio.h> </li></ul><ul><li>struct invent </li></ul><ul><li> { </li></ul><ul><li>char *name[20]; </li></ul><ul><li>int number; </li></ul><ul><li>float price; </li></ul><ul><li> }; </li></ul><ul><li> main() </li></ul><ul><li> { </li></ul><ul><li> struct invent product[3],*ptr; </li></ul><ul><li> printf(“inputnn”); </li></ul><ul><li> for(ptr=product;ptr<product+3;ptr++) </li></ul><ul><li> scanf(“%s %d %f”, ptr->name, &ptr->number, &ptr->price); </li></ul><ul><li> printf(“n outputnn”); </li></ul><ul><li> ptr=product; </li></ul><ul><li> while(ptr<product+3) </li></ul><ul><li> { </li></ul><ul><li> printf(“%-20s %5d %10.2fn”, </li></ul><ul><li> ptr->name, </li></ul><ul><li> ptr->number, </li></ul><ul><li> ptr->price); </li></ul><ul><li> ptr++; </li></ul><ul><li> } </li></ul><ul><li> } </li></ul>
  65. 65. <ul><li>Character arrays and strings :- A string is a sequence of characters that is treated as a single data items. </li></ul><ul><li>Declaration :- char string_name[size]; </li></ul><ul><li>Example :- char city[10]; </li></ul><ul><li>char name[20]; </li></ul><ul><li>For reading and writing the string, there are many function. </li></ul><ul><li>there are many function to do this. They are printf();scanf();gets();puts();getchar();putchar(); etc. </li></ul><ul><li>There is a C library supports a large number of string-handling functions that can be used to manipulate the string. </li></ul><ul><li>In C many string handling function to do some specific task. They are listed below. </li></ul>
  66. 66. <ul><li>strcat():- strcat(string1,string2); </li></ul><ul><li>strcmp():- strcmp(string1,string2); </li></ul><ul><li>strcpy():- strcpy(string1,string2); </li></ul><ul><li>strlen():- strlen(string1,string2); </li></ul><ul><li>These are some string handling function in C. </li></ul><ul><li>Structure :- Structure is a user defined data types. It is a mechanism for packing together data of different types. </li></ul><ul><li>Defining a structure :- struct book_name </li></ul><ul><li> { </li></ul><ul><li> char title[20]; </li></ul><ul><li> char auther[15]; </li></ul><ul><li> int pages; </li></ul><ul><li> float price; </li></ul><ul><li> }; </li></ul>
  67. 67. <ul><li>Declaration of a structure variables :- A structure variable declaration is similar to the declaration of variables of any other data types. </li></ul><ul><li>example :- struct book_bank </li></ul><ul><li>{ </li></ul><ul><li> Char title[20]; </li></ul><ul><li> Char auther[15]; </li></ul><ul><li> Int pages; </li></ul><ul><li> Float price; </li></ul><ul><li> }; Struct book_bank book1, book2, book3; </li></ul><ul><li>Accessing structure members :- To access the member of a structure. Make link between a member and a variable by using the member operator ‘ . ’ which is also known as dot operator or period operator. </li></ul>
  68. 68. <ul><li>For example:- book1.price </li></ul><ul><li> strcpy(book.title, “basic”); </li></ul><ul><li> strcpy(book1.auther, “ansi”); </li></ul><ul><li> book1.pages=250; </li></ul><ul><li> book1.price=120.50; </li></ul><ul><li>Structure initialization :- </li></ul><ul><li>main() </li></ul><ul><li> { </li></ul><ul><li> Struct </li></ul><ul><li> { </li></ul><ul><li> Int weight; </li></ul><ul><li> Float height; </li></ul><ul><li> } </li></ul><ul><li> Student={60,180.75}; </li></ul><ul><li> …… </li></ul><ul><li> …… </li></ul><ul><li> } </li></ul>
  69. 69. <ul><li>Array of structure :- The array of structure , each elements of an array representing a structure variable. </li></ul><ul><li>example :- struct marks </li></ul><ul><li>{ </li></ul><ul><li> int subject1; </li></ul><ul><li> int subject2; </li></ul><ul><li> int subject3; </li></ul><ul><li> }; </li></ul><ul><li> main() </li></ul><ul><li>{ </li></ul><ul><li> struct marks student[3]={{45,56,58},{65,58,98},{89,69,36}}; </li></ul><ul><li>This declares the student as an array of three elements student[0],student[1],student[2] and initializes their members as follow: </li></ul><ul><li>student[0].subject=45; </li></ul><ul><li>student[0].subject2=56; </li></ul><ul><li>……… </li></ul><ul><li>……… </li></ul><ul><li>Student[2].subject3=36; </li></ul>
  70. 70. <ul><li>Write a program to calculate the subject-wise and student-wise totals and store them as a part of the structure. </li></ul><ul><li>#include<stdio.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>struct marks </li></ul><ul><li>{ </li></ul><ul><li> int sub1; </li></ul><ul><li> int sub2; </li></ul><ul><li> int sub3; </li></ul><ul><li> int total; </li></ul><ul><li> }; </li></ul><ul><li> main() </li></ul><ul><li> { </li></ul><ul><li> int I; </li></ul><ul><li> struct marks student[3]={{56,36,58,0},{47,58,96,0},{98,56,87,0}}; </li></ul><ul><li> struct marks total; </li></ul><ul><li> for(i=0;i<=2;i++) </li></ul><ul><li> { student[i].total=student[i].sub1+student[i].sub2+student[i].sub3; </li></ul><ul><li> total.sub1=total.sub1+student[i].sub1; </li></ul><ul><li> total.sub2=total.sub2+student[i].sub2; </li></ul><ul><li> total.sub3=total.sub3+student[i].sub3; } </li></ul><ul><li>printf(“student totalnn”); </li></ul><ul><li>for(i=0;i<=2;i++) </li></ul><ul><li>printf(“student[%d] %dn”,i+1,student[i].total); </li></ul><ul><li>printf(“n subject totalnn”); </li></ul><ul><li>printf(“%s %dn%s %dn%s %dn”,”subject1 ”,total.sub1,”subject2 ”,total.sub2,”subject3 ”, total.sub3); </li></ul><ul><li>printf(“n grand total = %dn”, total.total); </li></ul><ul><li>}” </li></ul>