You are on page 1of 64

**

'C' => Programming Language => Middle Level Language => Case Sensitive Language (Recognisation of upper and lower case alphabets will be different) ASCII Values (American Standard Code for Information Interchange) 0 to 255 Software: "Set of related Programs" 1. Operating Systems "Acts as an interface media between the user and the system" 2. Packages "Readymade set of programs" Ex: MS office 3. Languages " No readymade programs, instead we have to create our own programs" Ex: 'C' Language: "Communication media" Binary / Machine Language( 0's & 1's) => Programming Languages: These are used as media by the programmer to interact / instruct the sys tem. 1. Low Level Languages => Easy only for machines Ex: Assembly Language 2. Middle Level Languages => Easy for both -> interface between low & High Ex: 'C' Language 3. High Level Languages => Easy only for human beings Ex: BASIC, COBOL, PASCAL, C++, JAVA => 'C' Character Set: => Alphabets

1. Upper Case (A to Z) 2. Lower Case (a to z) => Numerals / Digits 0 to 9 => Special Characters 1. Simple Keys + - * / | _ " ; : ' ? \ > < , . # % ^ & ! ~ ( ) { } [ ] = ..... 2. Compound/Meta Keys >= <= == != && || += -= *= ++ -- >> ....

Words: "Grouping up of characters, to convey some meaning" 1. Reserved Words(Keywords)

2. User Defined Words(Identifiers) Software | Set of related program | Set of ordered instructions/Statements | Grouping up of Expressions | Combination of Constants, Operators, Operands, Keywords, Function Calls, etc. (Collection of Tokens) Tokens (Building Blocks) It is a small individual unit used in a program 1. Keywords 2. Identifiers 3. Literals 4. Operators 5. Punctuators 1. Keywords (Reserved Words): These are the pre-defined words of the language assigned with unique me anings or operations. 'C' consists of totally 32 keywords (ANSI-C -> 36 keyword s). int char float double long short return while for do struct union signed unsigned static include define typedef FILE EOF volatile void const sizeof Variable: It is an entity (memory container) that holds th e data stored Ex: 2. Identifiers: These are the user-defined words / labels defined for giving identity to different entities used in a program, such as variable names, function names, f ile names, fieldnames, typenames, etc. => Conventions for framing identifiers: 1.Should begin with an alphabet followed by numerals may also be used. 2. No special characters except under score ( _ ) is allowed 3. Should be short, easy and meaningful. 4. Should be unique i.e. non-similar 5. Keywords should not be used as Identifiers 6. Recognisation of Upper case and Lower case alphabets will be different 7. It can be of maximum 40 characters long in length enum extern register auto if else switch case default break continue goto

3. Literals: These are the constants that remains unchanged throughout the program ex ecution. 'C' classifies the literals into the following five types: i. Character Literals => Enclosure of single key within single quotations Ex: 'A' , '6', '#', '\n' ii. String Literals => Enclosure of group of characters within double quotations Ex: "Satyam", "123" iii. Integral Literals=> Rep. a numerical value without decimal fraction Ex: 250 , -32768 iv. Floating Literals => Rep. a numerical value with decimal fraction Ex: 3.14159 , -124.3657 v. Logical / Boolean Literals => Rep boolean values i.e. in the form of true (1) / false (0) Ex: 10>20 => 0 4. Operators: These are the special characters(symbols) which are assigned with unique operations to be executed when used along with operands. C has rich set of built-in operators, which are categorised into two cla ssifications: 1. Unary Operators: Used along with single operand Ex: ++ -- & - ! ~ 2. Binary Operators: Used along with two operands Ex: + - * / % < > = ... In C, operators are divided into different types, depending on the type of operations they perform: 1. Arithmetical Operators: Used for mathematical calculations + (plus) => Addition a + b - (minus) => Substraction x - y * (Asterisk)=> Multiplication r * m / (Slash) => Division a / b % (Percent) => Modular Value(Remainder) x % z 2. Relational Operators: Used for making comparision between two similar type of data items > => Greater than < => Less than >= => Greater than or Equals to <= => Less than or Equals to == => Equals to != => Not Equals to 3. Logical Operators: Used for joining two conditions as single And ( && ) => all must be satisfied Or ( || ) => Either one should be satisfied Not ( ! ) => Neither should be satisfied 4. Bitwise Operators: Used to manipulate the data in the form of bits ~ (Tilda) << >> & (Ambersent) | (Pipe) => => => => One's Compliment Shift Left Shift Right Bitwise AND => Bitwise OR

^ (Carrot)

=> Bitwise Exclusive OR (XOR)

5. Assignment Operators: Used for assigning RHS into LHS operands = => Assigns RHS into LHS Ex: c=a+b; += => Adds RHS into LHS Operand Ex: x=x+10; -> x+=10; -= => Substract RHS from LHS Ex: r=r-5; -> r-=5; *= => Multiplies RHS with LHS Ex: a=a*10; -> a*=10; /= => Divides LHS by RHS Ex: m=m/2; -> m/=2; %= => Modularizes LHS mod RHS Ex: g=g%2; -> g%=2; 5. Punctuators: These are the special characters used for grouping or ungrouping the ele ments of a program. i. { } => Braces -> Used for grouping set of statements as a single block -> -> ii. ( ) => Paranthesis -> Used for giving or representing the data for the functions ->Used for representing higher priority in evaluation of => Brackets

an expression iii. [ ] -> -> iv. ; v. ,

=> Semi Colon ->

-> Used as statement terminator

=> Comma -> Used as separator between the elements of a program ->

vi. . => Decimal Period -> vii. # => Hash -> Used for including the required header files -> Also used for defining constant identifiers Data Types Classification of the data into different types depending on the type of keys used in framing the data, for exact memory allocation and faster memory a ccess. 1. Primitive Data Types: These are pre-defined data types of C language 1. int : => It is used for storing numerical value without decimal fraction => It only allows numerals from 0 to 9 => It also allows two special characters + and => It is one word in length => It occupies two bytes (16 bits) of memory

2.

3.

4.

5.

=> It's value ranges between -32768 to 32767 Ex: 256, -4576, +226 char : => It is used for storing any single key / character => It is one word in length => It holds the ASCII value of the given character => It occupies one byte (8 bits) of memory => It's value ranges between -128 to 127 Ex: A -> 65, 8 -> 56, # float : => It is used for storing numerical value with decimal fraction => It allows numerals from 0 to 9 => It also allows three special characters i.e. +, - and decimal ( . ) => It is two words in length => By default it takes 6 decimal digits => It occupies 4 bytes (32 bits) of memory => It's value ranges between 3.4E-38 to 3.4E+38 Ex: 123.5774, -2458.33 double: => It is used for storing numerical value with decimal fraction => It allows numerals from 0 to 9 => It also allows three special characters i.e. +, - and decimal ( . ) => It is two words in length => By default it takes 8 decimal digits => It occupies 8 bytes (64 bits) of memory => It's value ranges between 1.7E-308 to 1.7E+308 void: => It is blank i.e. no data so no memory allocation => It is used in function as return data type.

Expressions: It is combination of constants, operators, operands, punctuators, functi on calls, keywords, etc., which when executed returns any value/data. In 'C', expressions are classified into five types depending on the type of data it returns. 1. Character Expressions (< ExpC >): Ex: toupper('a') 2. String Expressions (< ExpS >): Ex: "abc" + "def" 3. Integral Expressions (< ExpN >): Ex: a+20, 25/10=>2 4. Floating Expressions (< ExpN >): Ex: 3.14159*r*r 5. Logical / Boolean Expression (< ExpL >): Ex: a>b => 1/0 Statements: "Grouping up of expressions according to the symantic rules of 'C' langu age, which when executed performs an act or an operation. In 'C', statements are of the following categories: 1. 2. 3. 4. Comment-entry statements Input Statements Output Statements Declaration Statements i. Variable Declarations a. Local Declarations

5. 6.

7. 8.

b. Global Declarations ii. Structure Declarations iii. Type definitions Pre-processor directive Statements Control Structures i. Branching Statements a. goto b. continue c. return d. break ii. Conditional Statements a. if b. if else c. switch statements iii. Loop / Iterative Controls a. while statement b. for statement c. do while statement d. True / Continuous Statement Null Statements Definition Statements, etc.

=> Evolution of Programming Languages: Binary Language ( 0's & 1's) | Assembly Language (Low-Level Language) | High Level Languages || Procedure Oriented Programming (POP) (Emphasis is on task) || Object Oriented Programming (OOP) (Emphasis is on data) =>Structure of 'C' Program: [ Descriptive / Comment-Entry Statements (Documentation Section) ] Pre-Processor Directive Statement ( Linkage Section ) [ Global Declarations / Definitions ] Body [1. Local Declaration Statements ] 2. Executable Statements Syntax: main ( ) { [Declaration Statements;] Executable Statements; } [Function1 ( ) { Declaration Statements; Executable Statements; } .. .. ..] =>Descriptive / Comment-Entry Statements: These are non-executable and optional statements (Compulsory to be used)

that are used for giving description about the current program, such as purpose , environment used, date and time of program creation, name of the programmer an d so on. These statements can appear any where in the program for any number of t imes. Syntax: 1. /* Description */ => Single Line Comment 2. /* Description Description Description */ => Multi Line Comment 1. /* Sample C Program */ 2. /* Program to find sum of two numbers Written by Dennis Ritchie */ => Pre-processor Directive Statements: Library Files (Header Files) <Stdio.h> <Math.h> <String.h> <Graphics.h> <Ctype.h> <Io.h> <Process.h> <Stdlib.h> .... These are the statements that are used for including i.e. linking the n ecessary header file /s within the current program using "#include" directive, d epending on the type of functions we use in the program. Syntax: # include < Header Filename > Ex: # include<Stdio.h> # include<Conio.h> # include<Math.h> These statements are also used for defining constant type of variables w ith the required expression, by using "#define" directive Syntax: # define <Identifier> <Initial Value / Data> Ex: # define pi 3.14159 # define size 100 # define Hi "Good Evening" =>Global Declarations / Definitions: (optional statements) Statements used for declaration of global version of variables, constant s, functions or also used for defining global version of type definitions, which can be accessed throughout the program and gets released as soon as the program ends. -> Declaration of global version of variables: Syntax: <typename> <Identifier/s>; Ex: int x;

Ex:

char ch; float p; double d; int a,b,c; => Body: As 'C' is a modular programming language, where a single program can be split into n number of modules known as functions. Body is the main function of the C program that gets executed first by C compiler as soon as the programmer runs the program. To differentiate the body from the other functions, it is given heading as "main ( )". Beginning and End ing of a block are represented by " { " and " } " respectively. Every statement in C should b e terminated by " ; ". Body consists of two types of statements: 1. Declaration Statements 2. Executable Statements Syntax: main ( ) { [Declaration Statements ;] Executable Statements ; } Ex: main() { printf (" Hi, Welcome to the World of Satyam ") ; } 1. Declaration Statements: These are used for declaration of local version of variables that can be accessed only within the main function. Syntax: <typename> <Identifier/s>; 2. Executable Statements: These are the statements that when executed performs an act or an operat ion, such as takes input, gives output, checks condition and so on. => Functions: (Self Contained block or program) It is a module or block of a program, that contains set of statements re lated to perform a particular sub task, having a unique identity known as functi on name. /* Sample C Program */ #include<Stdio.h> main() { int a,b,c; a=10; b=20; c=a+b; printf(" Sum = %d" , c); } => Different C Compilers: | These are the translator programmes that are used for converting the source code into binary code and vice versa.

=>

TC -> Turbo C -> Turbo Corporation QC -> Quick C -> Micro Soft Corporation UNIX & C -> A T & T Bell Laboratories BC -> Borland C -> Borland Corporation ANSI - C -> American National Standard Institute

Getting Started with Turbo C programming: 1. Start => Run & type C:\Tc\Tc.exe => Click on OK button 2. Start => Run & type Command => Click on OK button => CD\ C:\> => Type CD TC <enter> C:\TC> => Type TC and press enter key C:\TC> TC <enter> 3. Double click on Turbo C Short icon on the Desktop (if exist) 4. Select Start => Programs => Turbo C => Press Alt+Enter Notes: 1. To Save the Program : Alt+F(File) => Save (Default extension for C files will be ".C") OR Press F2 (Sample . C) 2. To compile the program: Alt+C(Compile)=>Compile OR Alt+F9 (Automatically creates object file with ".OBJ") (Sample.OBJ) 3. To zoom / unzoom the current window: Press F5 4. To toggle between the message window and edit window: Press F6 5. To run/execute the program: Alt+R(Run)=> Run OR Press Ctrl+F 9 (Automatically creates output file with ".EXE") (Sample.Exe) Source Code =>Compiler => Object Code => Linker => Output Code (.C) (.OBJ) (.EXE) 6. To toggle between the output window and the editor Alt+F5 7. To write a new program: Alt+F(File)=> New 8. To opening an existing program: Alt+F(File)=> Load OR F3 9. To quit the editor: Alt+F(File)=>Quit OR /* Sample C Program */ #include<stdio.h> main( ) { printf(" Hi, Good Morning ") ; } => Output Statements: These are the statements that gives output to the user by the program. => printf ( ): print Formatted It is the built-in C formatted output function that prints the output of the given expressions on the monitor in the specified format. It is defined in <Stdio.h> file. Syntax: printf ( [ " Format Specifiers " , ] <Exp/s> ) ; Where Expression => a Constant, an Operand, an operator, Press Alt+X

Press

a formula, a function call, a keyword, etc. Format Specifiers: These are the codes that are used for representing the type of data to be taken as input or the type of output to b e given by the program, to C compiler. "%d" => int "%c" => char "%f" => float "%s" => string "%l" => long "%u" => unsigned "%lf" => double "%ld" => long int "%ud" => unsigned int Ex: 1. printf(" Good Evening "); 2. int x =10 , y = 20; printf(" %d & %d " => 10 & 20 3. printf ( " x = %d and => x =10 and y = 4. c = x + y ; printf ( " Sum = %d " OR printf ( " Sum = , x , y ); y = %d " , x , y ) ; 20 , c ) ; %d " , x + y ) ; => Sum = 30

5. int x =10; char ch = 'A' ; float p =123.57888 ; printf ( " x = %d ch = %c p = %f " , x , ch , p ) ; => x = 10 ch = A p = 123.578878 /* To find sum of two nos */ #include<STDIO.H> main() { int a=40 , b=20; printf(" Sum = %d " , a+b); } => WAP to find sum, difference, product and division of two nos where x=50 and y=5 /* To find the area of a circle */ #include<stdio.h> #define pi 3.14159 main() { int r=5; printf(" Area of a Circle : %f ",pi*r*r); } /* To find sum, difference, product and division of two nos */ #include<stdio.h> main() { int x=50 , y=5; printf("\n Sum = %d " , x+y);

printf("\n Difference = %d " , x-y); printf("\n Product = %d\n",x*y); printf(" Division = %d " , x/y); } -> printf( "\n Sum = %d\n Difference = %d\n Product = %d\n Division = %d ", x+y , x-y , x*y , x/y); => Input Statements: These are the statements that are used for taking the necessary input from the user through keyboard. => scanf ( ): scan format It is a built-in C formatted input function that scans or gets the required specified type of data as input from the user and stores them into the given identifiers (variables) address. It is defined in <Stdio.h> file. Syntax: scanf( " format specifiers " , & <Identifier/s>); Where Identifier => Variable name & (Ambersent ) => Reference / Address Operator format specifiers => codes used for rep. the data type Ex: 1. int a,b; scanf( "%d%d" , &a , &b ); 2. int n; scanf("%d" , &n); 3. int x; char ch; float p; scanf( "%d%c%f" , &x , &ch , &p ) ; /* To find sum, difference, product and division of two given nos */ #include<Stdio.h> main() { int a,b; printf("\n Enter two values :"); scanf("%d%d",&a,&b); printf("\n Sum = %d ",a+b); printf("\n Difference = %d\n",a-b); printf(" Product = %d \n",a*b); printf(" Division = %d ", a/b); } /* To find the area of a circle */ #include<Stdio.h> main() { int r; printf(" Enter radius of a Circle :"); scanf("%d" , &r); printf(" Area of Circle = %f " , 3.14159*r*r ) ; } => wap to find the area and perimeter of a rectangle /* To find the area and perimeter of a rectangle */ #include<stdio.h>

main() { int l,b; clrscr(); printf(" Enter length and breadth of a rectangle :"); scanf("%d%d", &l, &b); printf(" Area of a Rectangle = %d ", l*b); printf("\n Perimeter of a Rectangle = %d ", 2*(l+b)); } /* To swap the two given numbers */ #include<stdio.h> main ( ) { int a , b , c ; => int a , b; printf(" Enter two numbers :"); scanf( "%d%d" , &a , &b ); printf(" Before swapping values are a=%d and b=%d" , a , b ); c=a; => a=a+b; a=b; => b=a-b; b=c; => a=a-b; printf("\n After swapping values are a=%d and b=%d " , a , b ); } => clrscr ( ): It is a built-in C console function that clears the output window's scre en. It is defined in <Conio.h> file Syntax: clrscr ( ); 'C' Language Programming Language Middle Level Language Case Sensitive Language Modular / Block Programming (Large programs can be split into blocks) 1. Easy readability and easy understandability 2. Easy debugging of errors 3. Easy sharing of work => Procedure Oriented Programming (POP) => Emphasis is on task => Large programs are split into functions => General Purpose programming => Structural programming -> Syntax & Symantic rules -> Memory resident programming feature Definition: 'C' is a general purpose structured modularized procedure oriented case sensitive middle level programming language, that acts as an interface between low level and high level languages. => => => => History of C Language: High-Level Languages (ALGOL) - early 1960's | BCPL - Martin Ritchard - early 1964's (Basic Combined Programming Language) | "B" (BASIC) Language - Ken Thompson - 1967 | "C" Language - Dennis M Ritchie - 1972

at A T & T Bell Laboratories, Murray Hills, New Jersy (USA) | "C with Classes" - Bjarne Stroustrup - 1982 | Renamed it as "C++" - 1983 /* To read and print a character */ #include<stdio.h> main ( ) { char x; clrscr( ); printf(" Enter an alphabet :"); scanf("%c", &x); printf(" Given alphabet = %c" , x ) ; } => getchar ( ): It is a built-in C Character input function that gets or reads a single character / key as input and assigns it into the left hand side character variab le. It is defined in <Stdio.h> file Syntax: <Char Var>=getchar ( ); Ex: char m; m=getchar ( ); => putchar ( ): It is a built-in C Character output function that puts or prints the giv en character expression on the monitor. It is defined in <Stdio.h> file. Syntax: putchar(<ExpC>); Ex: 1. putchar(m); 2. putchar( 'S' ); => S /* To read and print a character using getchar ( ) and putchar ( )*/ #include<stdio.h> main ( ) { char x; clrscr( ); printf(" Enter an alphabet :"); x=getchar( ); printf(" Given alphabet = "); putchar(x); } #include<stdio.h> main() { int a , b; clrscr(); printf( " Enter two numbers :"); scanf("%d%d" , &a, &b); if(a>b) { printf(" Biggest = %d " , a); printf("\n Smallest = %d " , b); }

else printf(" Biggest = %d \n Smallest = %d ", b , a); getch(); } /* To convert the given upper case alphabet into lower case */ #include<stdio.h> main ( ) { char ch; clrscr(); printf(" Enter an upper case alphabet :"); ch=getchar( ); ch=ch+32; printf(" Lower case alphabet = %c" ,ch); } => To convert the given lower case alphabet into upper case => Conditional / Selection Statements: Condition: => Making comparision between two similar type of data items 1. Simple Conditions: Ex: a>b 2. Compound/ Complex Conditions: Ex: a>b && a>c These are the statements that are used for decision making within a prog ram by imposing a condition and performing either executions, i.e. these statements are used for altering the sequential execution of the program. i. Simple if ii. if else iii. Nested if else / if else if ladder iv. switch Statement i. Simple if: In this notation we only specify what to be executed when the condition is true i.e. no alternative execution is specified. Syntax: if (<Condition>) { Statement 1; Statement 2; ................... Statement n; } Ex: if ( a % 2 == 0 ) { printf(" Given number is Even "); } if( a % 2 != 0 ) { printf(" Given number is Odd "); }

ii. if

else: In this notation we specify either executions to be executed depending on the evaluation of the condition. Syntax: if (<Condition>) { Statement 1; Statement 2; --> True Block ................... Statement n; } else { Statement 1; Statement 2; --> False Block .................... Statement n; } Ex: if ( a % 2 == 0) { printf(" Given number is Even "); } else { printf(" Given number is Odd "); } /* To find the biggest among the two given numbers */ #include<stdio.h> main() { int x , y; clrscr(); printf(" Enter two numbers :"); scanf("%d%d" , &x ,&y); if( x>y ) printf(" Biggest = %d " , x); if( y>=x ) => else printf(" Biggest = %d " , y); getch(); } => To find biggest and smallest among two given numbers => To find whether the given number is even or odd /* To find whether the given alphabet is an upper case or lower case alphabet */ #include<stdio.h> main() { char x ; clrscr(); printf(" Enter an alphabet :"); x=getchar( ); if( x>=65 && x<=90 ) -> if ( x>= 'A' && x <= 'Z' ) printf(" Given alphabet is of Upper case "); else printf(" Given alphabet is of Lower Case "); getch( ); }

#include<stdio.h> main() { int a,b,c; clrscr(); printf(" Enter three numbers :"); scanf("%d%d%d", &a,&b,&c); /* Compound Conditions */ if(a>b && a>c) printf(" Biggest = %d ",a); else if(b>c) printf(" Biggest = %d ",b); else printf(" Biggest = %d ",c); if(a<b && a<c) printf("\n Smallest = %d ",a); else if(b<c) printf("\n Smallest = %d ",b); else printf("\n Smallest = %d ", c); getch(); } => Character Functions: These are the functions that are used for manipulation of the character type of data. These functions are defined in <Ctype.h> file. 1. toupper ( ): ->Returns the converted given character expression from lower case to upper case. Syntax: ( x - 32 ) toupper(<ExpC>); Ex: 1. x = toupper( x ) ; 2. toupper( 'b' ); => B 2. tolower( ): -> Returns the converted character of the given character expression fro m upper case to lower case. Syntax: ( x + 32 ) tolower(<ExpC>) Ex: 1. ch = tolower( ch ); 2. tolower( 'G' ) ; -> g 3. isupper( ): -> Used for checking whether the given character expression is represent ing an upper case alphabet or not. If yes return 1 (true) otherwise returns 0 (fals e). Syntax: ( x >= 65 && x <= 90 ) isupper (<ExpC>) Ex: 1. if ( isupper(x) )

printf(" Given alphabet is of upper case "); else printf(" Given alphabet is of lower case"); 2. isupper( 'j' ) -> 0 4. islower( ): -> Used for checking whether the given character expression is represent ing an lower case alphabet or not. If yes return 1 (true) otherwise returns 0 (fals e). Syntax: (x >= 97 && x <= 122 ) islower (<ExpC>) Ex: 1. if ( islower(x) ) printf(" Given alphabet is of Lower case "); else printf(" Given alphabet is of Upper case"); 2. islower( 'j' ) -> 1 5. isalpha( ): -> Used for checking whether the given character expression is represent ing an alphabet or not. If yes return 1 (true) otherwise returns 0 (false). Syntax: ( (x >= 65 && x <= 90) | | (x >=97 && x <= 122) ) isalpha (<ExpC>) Ex: 1. if( isalpha(x) ) printf(" Given Character is an alphabet"); else printf(" Given character is not an alphabet"); 2. isalpha( '8' ) -> 0 6. isdigit( ): -> Used for checking whether the given character expression is represent ing a digit or not. If yes return 1 (true) otherwise returns 0 (false). Syntax: (x >= '0' && x < = '9' ) isdigit (<ExpC>) Ex: 1. if( isdigit(x) ) printf(" Given character is a digit "); else printf(" Given character is not a digit"); 2. isdigit( 'j' ) -> 0 7. isspace( ): -> Used for checking whether the given character expression is represent ing a space or not. If yes return 1 (true) otherwise returns 0 (false). Syntax: (x==' ') isspace (<ExpC>) Ex: 1. if( isspace (x) ) printf(" Given character is a space "); else printf(" Given character is not a space"); 2. isspace( ' ' ) -> 1

=> Write a program to find whether the given character is an alphabet or not 3. Nested Conditional Statements: -> Grouping or using one or more conditional statements within an another conditional statement, is referred as Nested Conditional Statements. Syntax: if(<Condition1>) if(<Condition2>) { Statements; -> 1-T & 2-T } else { Statements; -> 1-T & 2-F } else if(<Condition3>) { Statements; -> 1-F & 3-T } else { Statements; -> 1-F & 3-F } /* To find biggest among three given numbers */ /* Simple Conditions */ /* Nested Conditional Statements */ #include<stdio.h> main( ) { int a , b , c ; clrscr( ); printf(" Enter three values :"); scanf( "%d%d%d" , &a , &b , &c ) ; if(a>b) if(a>c) printf(" Biggest = %d",a); else printf(" Biggest = %d",c); else if(b>c) printf(" Biggest = %d",b); else printf(" Biggest = %d",c); getch( ); } => To find biggest and smallest among three given nos => if else if Ladder: Grouping up of if statements after else part of the conditional statemen ts is referred as if else if ladder. Syntax: if(<Condition1>) { Statements; -> 1-T } else

if(<Condition2>) { Statements; } else if(<Condition3>) { Statements; } ..... else { Statements; } /* To find biggest among three given numbers */ /* Compound Conditions */ /* if else if Ladder */ #include<stdio.h> main( ) { int a,b,c; clrscr( ); printf(" Enter three values :"); scanf("%d%d%d" , &a , &b , &c ) ; if(a>b && a>c) printf(" Biggest = %d",a); else if(b>c) printf(" Biggest = %d",b); else printf(" Biggest = %d",c); getch( ); }

-> 1-F & 2-T

-> 1-F , 2-F & 3-T

-> No condition is true

=> Wap to find whether the given number is positive, negative or zero /* To check whether the given character is an alphabet, a digit, a space or a sp ecial character*/ #include<stdio.h> #include<Ctype.h> main( ) { char x; clrscr( ); printf(" Enter a character :"); x=getchar( ); if( isalpha(x) ) printf(" Given character is an alphabet "); else if( isdigit(x) ) printf(" Given character is a digit "); else if( isspace(x) ) printf(" Given character is a space "); else printf(" Given character is a special character "); getch( ); }

=> Ternary / Conditional Operator ( ?: ): It is a special built-in 'C' operator that is used for evaluation of the given condition and perform either action or returns either value. Syntax: (<Condition>) ? <T_Action> : <F_Action> Ex: (a>b) ?a :b OR printf(" Biggest = %d" , (a>b) ?a :b ); 2. (a>b) ?printf(" Biggest = %d",a); : printf(" Biggest =%d " ,b); 3. (a>b) ? (a>c) ? a : c :(b>c) ? b : c erators -> Simple Conditions 4. (a>b && a>c) ? a : (b>c) ? b : c -> Compound Conditions /* To find biggest and smallest among three given numbers using Ternary Operator */ #include<stdio.h> main( ) { int x , y , z ; clrscr( ); printf(" Enter three numbers :"); scanf("%d%d%d" , &x , &y , &z ); printf(" Biggest = %d" , (x>y && x>z) ?x :(y>z) ?y :z ); printf("\n Smallest = %d", (x<y && x<z) ?x : (y<z) ?y : z ); getch(); } => To find the smallest among two given numbers using Ternary Operator /* To print the given digit in words */ #include<stdio.h> main() { int n; clrscr(); printf(" Enter a single digit :"); scanf("%d" , &n); if(n==0) printf(" Zero"); else if(n==1) printf(" One"); else if(n==2) printf(" Two"); else if(n==3) printf(" Three "); else if(n==4) printf(" Four"); else if(n==5) printf(" Five "); else if(n==6) printf(" Six"); else -> Nesting of Ternary Op

if(n==7) printf(" Seven"); else if(n==8) printf(" Eight"); else if(n==9) printf(" Nine"); else printf(" Invalid Digit, Try again"); getch(); } 4. switch Statement: It is a selection statement that is used for performing one among the gi ven n number of alternative executions depending on the output yielded by the given expression. Syntax: switch(<Expression>) { case <Output1> : Statements; break; case <Output2>: Statements; break; case <Output3>: Statements; break; ..... ..... case <Output n>: Statements; break; default: Statements; } /* To print the given digit in characters using switch statement */ #include<stdio.h> main( ) { int n; clrscr( ); printf(" Enter a digit :"); scanf("%d",&n); switch(n) { case 0: printf(" Zero"); break; case 1: printf(" One "); break; case 2: printf(" Two"); break; case 3: printf(" Three"); break; case 4: printf(" Four"); break; case 5: printf(" Five"); break; case 6: printf(" Six"); break; case 7: printf(" Seven"); break; case 8: printf(" Eight"); break; case 9: printf(" Nine"); break; default: printf(" Invalid digit, try again"); } getch(); } /* To check whether the given alphabet is a vowel or a consonent */ #include<stdio.h>

main( ) { char m; clrscr( ); printf(" Enter an alphabet :"); m=getchar( ); switch(m) { case 'A' : case 'a' : case 'E' : case 'e' : case 'I' : case 'i' : case 'O' : case 'o' : case 'U' : case 'u' : printf(" Given alphabet is a Vowel "); break; default: printf(" Given alphabet is a Consonent "); } getch( ); } => Escape Sequence Characters: These are the codes that are used for formatting the output of a program by executing the printable characters and printing the executable characters. Thes e codes should begin with Backward slash ( \ ) followed by a character and should be specified within double quotations. "\a" => Audible Bell (Beep sound) "\b" => Backspace key "\r" => Cariage Return "\t" => horizontal Tab ( 8 spaces) "\v" => Vertical Tab "\n" => New line character "\\" => Prints \ character "\"" => Prints " character "\' " => Prints ' character "\?" => Prints ? character '\0' => Null Character Ex: printf("\"Hi, Good evening\" "); => " Hi, Good Evening"

/* To display the address of Satyam institute */ #include<stdio.h> main() { clrscr(); printf("\n\n\n\n\n\n\n\n\n\n\t\t\t\t\"SATYAM COMPUTER EDUCATION"); printf("\n\t\t\t #17-7-77, Laxmi Linga Rao Arcade,"); printf("\n\t\t\t 4th Floor, Above Hero Honda Showroom,"); printf("\n\t\t\t\tMankamma Thota, KARIMNAGAR\""); getch(); } => gotoxy( ): It is a built-in 'C' function that is used to move the cursor to the spe cified position on the screen, where the position is specified in terms of column and r

ow, where column can be in between 0 to 79 and row can be in between 0 to 23. Syntax: gotoxy(<Column>,<Row>); Ex: gotoxy(25,12); /* To display the address of Satyam institute */ #include<stdio.h> main() { clrscr(); gotoxy(24,10); printf("\"SATYAM COMPUTER EDUCATION"); gotoxy(20,11); printf("#17-7-77, Laxmi Linga Rao Arcade,"); gotoxy(20,12); printf("4th Floor, Above Hero Honda Showroom,"); gotoxy(24,13); printf("Mankamma Thota, KARIMNAGAR\""); gotoxy(28,9); printf(" N. Kamalakar Rao"); getch(); } Loop /Iterative Control Statements These are the statements that are used for repeatation of the given set of statement's execution for required n number of times. 'C' provides four different types of loop statements, which can also be referred as Iterative control statements: 1. while Statement 2. for Statement 3. do while Statement 4. True / Continuous Statement 1. while Statement: It is a loop / iterative control statement that repeats the execution of the given set of statements for repeated n number of times, until the given conditio n is met or satisfied. Syntax: Initialization of Loop Variable; while(<Condition>) -> Loop Exit Test { Statement 1; Statement 2; . . . . . . . . . . . Statement n; Modification of Loop Variable; } Ex: /* To generate whole numbers from 0 to 20 */ #include<stdio.h> main( ) { int n=2; clrscr( ); while(n<=100) { printf("\n %d " , n); n=n+2; } getch( ); } /* To generate natural numbers from 50 to 1 */ #include<stdio.h>

main( ) { int n=50; clrscr( ); while(n>=1) { printf("\n %d " , n); n=n-1; } getch( ); } => To generate even numbers from 2 to 100 => To generate odd numbers from 99 to 1 and find their total /* To generate all ASCII codes and characters in page format */ #include<stdio.h> main( ) { int n=0; clrscr( ); while(n<=255) { printf("\n %d = %c " , n , n); n=n+1; if(n%20==0) getch( ); } getch( ); } /* To generate multiplication table for the given number */ #include<stdio.h> main( ) { int m , n=1 ; clrscr( ); printf(" Enter the required multiplication table number :"); scanf("%d" , &m ) ; while(n<=20) { printf("\n %d * %d = %d " , m , n , m*n ) ; n=n+1; } getch( ); } => To generate addition table for the given number /* To generate fibonnacci series below 100 */ #include<stdio.h> main() { int a=0 ,b=1; clrscr(); while(b<100) { printf("\n %d \n %d" , a , b ) ; a+=b; b+=a; } getch(); }

=> Increment & Decrement Operators: C has two special built-in operators by name Increment and Decrement ope rators, which are denoted as "++" and "--" respectively. ++ => Increment Operator -- => Decrement Operator Ex: n=n+1; x=x-1; erands value by one. Decrement Operator (--) is used for decreasing i.e. reducing the given o perands value by one. Both the above operators are classified into two types: 1. Pre 2. Post In Pre-Incrementing / Decrementing, the value of the given operand is fi rst raised / reduced and later the modified value is used in the given execution. In Post-Incrementing / Decrementing, the value of the given operand is f irst used in the given execution (unmodified) and later it is raised / reduced. Pre-Increment Syntax: ++<Var>; Ex: int x=3,y; y=++x; -> x=4 y=4 Pre-Decrement Syntax: --<Var>; Ex: int m,n=7; m=--n; -> n=6 m=6 Ex: int m,n=7; m=n--; m=7 n=6 Syntax: <Var>--; Ex: int x=3,y; y=x++; ->y=3 x=4 Post-Decrement Syntax: <Var>++; Post-Increment --> n++; / ++n; --> x-- ; / --x;

Increment Operator (++) is used for increasing i.e. raising the given op

/* To generate whole numbers from 20 to 0 */ #include<stdio.h> main( ) { int n=20; clrscr( ); while(n>=0) printf("\n %d", n-- ); getch( ); } /* To extract the individual digits of the given value */ #include<stdio.h> main() { int n , r; clrscr();

printf(" Enter a value :"); scanf("%d" , &n ) ; while(n>0) { r = n % 10 ; printf( " \n %d " , r ) ; n = n / 10; } getch(); } /* To find the reverse value for the given number */ #include<stdio.h> main() { int n , r , rev=0 ; clrscr(); printf(" Enter the required value :"); scanf("%d" , &n); while(n>0) { r=n%10; rev=rev*10+r ; n=n/10; } printf("\n Reverse Value = %d " , rev ); getch(); } => Nested while Statements: Grouping or using one or more while statements with in an another while statement, is referred as Nested while statements. Syntax: while(<Condition1>) { Statements; while(<Condition2>) { m <Statements; ->n => mxn ................. } Statements; } /* Sample program for nested while statements - output */ #include<stdio.h> main() { int x=1 , y ; clrscr(); while( x <= 3 ) { y=1; while( y <= 3 ) { printf("\n %d \t %d " , x , y ) ; y++; } x++; } getch(); }

=> Write programs for the following 1. 3 3 2. 1 2 3 2 1 3 3 3 2 1 2 2 2 1 2 3 3 1 1 2 1 2 1 1 3 3. 4. 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 .......... 1 2 3 4 5 6 7 ....n

outputs 1 2 3 1 2 3 1 2 3

1 1 1 1

2 3 4 5 2 3 4 2 3 2 1

3. /* output3 */ #include<stdio.h> main() { int x=1 , y , n ; clrscr(); printf(" Enter value for n:"); scanf("%d" , &n ) ; while( x<=n ) { y=1; while(y<=x) { printf(" %d" , y ) ; y++; } printf( " \n " ); x++; } getch( ); }

-> printf ( " *");

/* To generate multiplication tables from 1 to 10 */ #include<stdio.h> main() { int x=1,y; clrscr(); while(x<=10) { y=1; while(y<=20) { printf("\n %d * %d = %d " , x , y , x*y ) ; y++; } x++; getch(); }

} /* To find whether the given value is Armstrong or not */ #include<stdio.h> main() { int n , r , n1 , s=0 ; clrscr( ); printf(" Enter a value :"); scanf("%d" , &n ) ; n1=n; while( n>0) { r = n%10; s=s + r * r * r ; n= n / 10; } if(s==n1) printf(" Given number is an Armstrong "); else printf(" Given number is not an Armstrong"); getch(); } /* To generate all Armstrong nos */ #include<stdio.h> main() { int n=100 , r , n1 , s ; clrscr( ); while(n<=999) { n1=n; s=0; while(n1>0) { r=n1%10; s=s+r*r*r; n1=n1/10; } if(s==n) printf(" \n %d ",n); n++; } getch(); } /* To find the Hail Stone Value for the given number */ #include<stdio.h> main() { int n ,count ; clrscr(); printf(" Enter a number :"); scanf("%d" , &n); for(count=0 ; n>1 ; count++) { if ( n % 2 == 0 ) n=n / 2; else n= n * 3 + 1 ;

} printf(" Hail Stone Value = %d",count); getch(); } 2. for Statements: It is also an iterative control / loop statement that repeats the execut ion of the given set of statements for repeated n number of times, until the given conditio n is met or satisfied. This statement is same as while statement with only difference in the pl ace of specifying the initializations, condition and modification, which are specified as parameters with for statement. Syntax: for( Initializations ; <Condition> ; Modifications ) { Statement 1; ..................... Statement n; } /* To generate natural numbers from 1 to 20 */ #include<stdio.h> main() { int n; clrscr(); for(n=1 ; n<=20 ; n++) printf("\n %d" , n); getch(); } /* To generate fibonnacci series below 100 using for statement */ #include<stdio.h> main() { int a , b ; clrscr(); int a=0 , b=1; clrscr() ; for(a=0 , b=1 ; b<100 ; a+=b , b+=a ) for( ; b<100 ; ) printf("\n %d \n %d",a,b); { getch(); printf("\n %d \n %d",a,b ); } a+=b ; b+=a; } /* To find the factorial value for the given number */ #include<stdio.h> main( ) { long int n , f ; clrscr( ); printf(" Enter a number :"); scanf("%ld" ,&n ); for(f=1 ; n>=1 ; n--) f=f*n; printf(" \n Factorial Value = %ld" , f); getch( ); }

/* To find whether the given number is prime or not */ #include<stdio.h> main( ) { int n , f , d ; clrscr(); printf(" Enter the required number :"); scanf("%d" , &n ) ; for(f=0 , d=1 ; d<=n ; d++) if(n%d==0) f++; if(f==2) printf(" Given number is a Prime Number "); else printf(" Given number is not a Prime Number "); getch(); } => To find whether the given value is pallindrome number or not 2. /* To generate an output using nested for statements */ #include<stdio.h> main() { int x,y,sp=20,k; clrscr(); for(x=1 ; x<=9 ; x+=2) { for(k=1 ; k<=sp ; k++) printf(" "); for(y=1 ; y<=x ; y++) printf(" %d",y); printf("\n"); sp-=2; } sp+=4; for(x=7 ; x>=1 ; x-=2) { for(k=1; k<=sp ; k++) printf(" "); for(y=1 ; y<=x ; y++) printf(" %d",y); printf("\n"); sp+=2; } getch(); } => Nested for Statements: Grouping or using one or more for statements within an another for state ment is referred as Nested for statements. Syntax: for( Initializations1 ; <Condition1> ; Modifications1 ) { Statement 1; for( Initializations2 ; <Condition2> ; Modifications2 ) { Statement 1; m <= ..................... => n --> mxn

Statement n; } Statement n; } /* To generate an output using nested for statements */ #include<stdio.h> main() { int x , y ; clrscr(); for( x=1 ; x<=3 ; x++ ) { for ( y=1 ; y<=3 ; y++ ) printf( "\n %d \t %d " , x , y ) ; } getch(); } /* To generate an output using nested for statements */ #include<stdio.h> main() { int x,y; clrscr(); for(x=1 ; x<=5 ; x++) { for(y=1 ; y<=x ; y++) printf(" %d ",y); => printf(" * "); printf("\n"); } getch(); } => 1 * 1 2 * * 1 2 3 * * * 1 2 3 4 * * * * 1 2 3 4 5 * * * * * /* To generate an output using nested for statements */ #include<stdio.h> main() { int x , y , sp=20 , k ; clrscr(); for(x=1 ; x<=9 ; x+=2 ) { for(k=1 ; k<=sp ; k++) printf(" "); for(y=1 ; y<=x ; y++ ) printf(" %d" , y ) ; printf("\n"); sp-=2; } getch( ); } => 1 1 2 3 1 2 3 4 5 1 2 3 4 5 6 7

1 2 3 4 5 6 7 8 9 /* To generate all prime numbers below the given number */ #include<stdio.h> main( ) { int x, n , f , d ; clrscr( ); printf(" Enter the maximum prime required number :"); scanf("%d" , &x); for(n=1 ; n<=x ; n++ ) { for(d=1,f=0 ; d<=n ; d++) if(n%d==0) f++; if(f==2) printf("\n %d",n); } getch(); } => Write program to generate the following outputs: 1. * * * * * * * * * * * * * * * * * * * * * * * * * /* To generate the following output */ #include<stdio.h> main() { int x,y,sp=10,k; clrscr(); for(x=9 ; x>=1 ; x-=2) { for(k=1 ; k<=sp ; k++) printf(" "); for(y=1; y<=x ; y++) printf(" *"); printf("\n"); sp++; } getch(); } 2. 1 1 2 1 2 3 1 2 3 4 1 2 3 1 3. 1 1 2 1 1 2 3 2 1 1 2 3 4 3 2 1 1 2 3 3 4 5 4 5 6 5 6 7 4 5 6 2 3 4 1 2 3 1 7 8 9 7 5

1 2 3 4 5 4 3 2 1 /* To generate the following output */ #include<stdio.h> main() { int x,y,sp=10,k; clrscr(); for(x=1 ; x<=9 ; x+=2) { for(k=1 ; k<=sp ; k++) printf(" "); for(y=1; y<=x/2+1 ; y++) printf(" %d",y); for(y-=2; y>=1; y--) printf(" %d",y); printf("\n"); sp--; } getch(); } => Wap to generate multiplication tables from the given starting number till the given ending number 3. do while Statement: It is also a loop / iterative control statement that repeats the executi on of the given set of statements for repeated n number of times until the given condition is met or satisfied. This statement is similarly to while statement, with only difference in the place of condition evaluation i.e. in while the condition is evaluated at th e beginning of the loop statement where as in do while the condition is evaluate d at the end of the loop statement, so in do while the loop statements gets exec uted at least for once. Syntax: do { Statement 1; Statement 2; ..................... Statement n; }while(<Condition>) ; Ex: int n=0; while(n>=20) { printf("\n %d",n); n++; } => No Output int n=0; do { printf("\n %d",n); n++; }while (n>=20); => 0

=> Write a program to generate the following menu GENERATE MENU 1. 2. 3. 4. EVEN NUMBERS ODD NUMBERS ASCII CODES & CHARS FIBONNACCI SERIES

5. ARMSTRONG NOS 6. PRIME NOS 7. EXIT ENTER U'R CHOICE: /* To generate a menu */ #include<stdio.h> main() { int choice,n,n1,r,s; do { clrscr(); gotoxy(35,7); printf(" GENERATE MENU"); gotoxy(30,9); printf(" 1. EVEN NUMBERS"); gotoxy(30,10); printf(" 2. ODD NUMBERS"); gotoxy(30,11); printf(" 3. ASCII CHARS & CODES"); gotoxy(30,12); printf(" 4. FIBONNACCI SERIES"); gotoxy(30,13); printf(" 5. ARMSTRONG NUMBERS"); gotoxy(30,14); printf(" 6. PRIME NUMBERS"); gotoxy(30,15); printf(" 7. EXIT"); gotoxy(30,17); printf(" ENTER UR CHOICE:"); scanf("%d",&choice); switch(choice) { case 1: for(n=2 ; n<=100 ; n+=2) printf("\t %d",n); break; case 2: n=1; while(n<=99) { printf("\t %d",n); n+=2; } break; case 3: for(n=0 ; n<=255; n++) { printf("\n %d = %c",n,n); if(n%20==0) getch(); } break; case 4: n=0; n1=1; while(n1<100) { printf("\t %d \t %d",n,n1); n=n+n1; n1=n+n1; } break; case 5: for(n=100 ; n<=999 ; n++) { for(n1=n,s=0; n1>0; n1/=10) { r=n1%10; s=s+r*r*r; } if(s==n) printf("\n %d",n); } break; case 6: for(n=1 ; n<=100 ; n++) { for(r=1,s=0; r<=n; r++)

if(n%r==0) s++; if(s==2) printf("\t %d",n); } break; case 7: printf(" Thank Q for using Menu "); break; default : printf(" Invalid Choice, Try again"); } getch(); }while(choice!=7); } 4. True / Continuous Statement: It is also a loop / iterative control statement that repeats the executi ons of the given set of statements continuously for infinity number of times unl ess we manually break it using Ctrl+Pause key or by using "break" branching stat ement. Syntax: while( 1 ) { Statement 1; Statement 2; .................... Statement n; } /* Sample program for true statement */ #include<stdio.h> main() { int n=0; clrscr(); while(1) { printf("\n %d",n); n++; if(n>20) break; } getch(); } => To find sum of the given 10 numbers into an array #include<stdio.h> main() { int n[10] , id , s ; clrscr(); printf(" Enter ten values :"); for( id=0; id<=9 ; id++) scanf("%d" , &n[id]); printf(" Given numbers are "); for( id=0, s=0 ; id<=9 ; id++) { printf(" \n %d",n[id]); s=s+n[id]; } printf("\n Sum of the given numbers = %d " , s); getch(); }

ARRAYS These are of non-primitive derived data type, which is collection of ele ments allocated adjacently used for storing group of same type of data items tog ether, which can be referred by a single common identity known as Array name. In 'C', arrays are classified into three types: 1. Single Dimensional Arrays 2. Double Dimensional Arrays 3. Multi Dimensional Arrays 1. Single Dimensional Arrays: These are the array variables where the memory for the elements is alloc ated in one dimensional format i.e. in vertical format, so while declaring these variables we need to specify single subscript ( size) i.e. number of elements r equired. Syntax: <typename> <Arrayname>[Subscript]; Where typename => Any primary data type name Arrayname => Common identity for the array Subscript => size i.e. no. of elements Ex: int n[5]; char name[20]; float p[4]; double d[10]; int a[10], b[5]; When needed we can represent the entire array including all the elements together by a single common identity known as Arrayname, or if necessary we can also derepresent the individual elements of an array using Arrayname followed b y index numbering, which is automatically assigned by the Compiler starting from 0 (zero) and ending at Subscript-1. /* To read and print 5 values into an array */ #include<stdio.h> main() { int x[5] , id ; clrscr(); printf(" Enter five values :"); for(id=0 ; id<=4 ; id++) scanf("%d", &x[id] ); printf(" Given Values are :"); given values"); for(id=0 ; id<=4 ; id++) ) printf("\n %d" , x[id] ); getch( ); } printf(" Reverse order of --> for ( id=4 ; id>=0 ; id - -

=> To find sum of the given 10 numbers into an array /* To count total number of even and total number of odd occured within the given array of 10 values */ #include<stdio.h> main() {

int x[10] , id , e , d ; clrscr(); printf(" Enter ten values :"); for(id=0 , d=0 , e=0 ; id<=9 ; id++) { scanf("%d" , & x[id] ) ; if( x[id] % 2 == 0 ) e++; else d++; } printf(" \n Total number of Even are = %d " , e ) ; printf("\n Total number of Odd are = %d " , d ) ; getch(); } => To count total number of positive, negative and zero's occured within the giv en array of 10 numbers

/* To search for the given element within the given array of n numbers */ #include<stdio.h> main() { int x[50] , n , i , ele ; clrscr( ); printf(" How many values to be accepted :"); scanf("%d" , &n ) ; printf(" Enter %d values " , n ) ; for(i=0 ; i<=n-1 ; i++) scanf("%d" , & x[i] ) ; printf(" Enter the element to be found :"); scanf("%d" , & ele ) ; for( i=0 ; i<=n-1 ; i++) if( x[i]==ele ) break; if( i==n ) printf(" Given element is not found "); else printf(" Given element is found at %d position" , i+1 ) ; getch(); } /* To sort the given array of n numbers in ascending order */ #include<stdio.h> main() { int x[50] , n , i , j , temp ; clrscr(); printf(" How many values to be accepted :"); scanf("%d" , &n ) ; printf(" Enter %d values " , n ) ; for(i=0 ; i<=n-1 ; i++ ) scanf("%d" , &x[i] ) ; for(i=0 ; i<=n-2 ; i++) for( j=i+1 ; j<=n-1 ; j++)

if( x[i] > x[ j ] ) { temp=x[i]; x[i]=x[ j ]; x[ j ]=temp; } printf(" Ascending Order of Given Values are :"); for(i=0 ; i<=n-1 ; i++) printf("\n %d" , x[i] ) ; getch(); } => To sort the given n number of elements in an array in descending order => To store the first 10 even numbers into an array and print them /* To store the first 10 even numbers into an array and print them*/ #include<stdio.h> main() { int n[10] , i , e ; clrscr(); for(i=0 , e=2 ; i<=9 ; i++, e+=2) n[i]=e; printf(" Elements of the array are "); for(i=0 ; i<=9 ; i++) printf("\n n[%d] = %d " , i , n[i] ) ; getch(); } 2. Double Dimensional Arrays: These are the matrix type of variables where the memory for the elements is allocated in two dimensional format i.e. in the form of rows and columns, so while declaring these variables we need to specify two subscripts (sizes) i.e. row subscript and column subscript Syntax: Row Column <typename> <Arrayname>[Subscript][Subscript]; Ex: int x[3] [3]; char str[10] [20] ; float p[3] [4]; int a[3] [2] , b[4] [3] ; When needed we can represent the entire array including all the elements together by a single common identity known as Arrayname, or if necessary we can also derepresent the individual elements of an array using Arrayname followed b y row index numbering and column index numbering, which automatically starts fro m 0 (zero) and ending at Subscript - 1. /* to read elements into 3x3 array and print them in martrix format */ #include<stdio.h> main() { int x[3][3] , r , c ; clrscr(); printf(" Enter elements into 3x3 matrix\n "); for(r=0 ; r<=2 ; r++) for(c=0 ; c<=2 ; c++) scanf( "%d" , &x[r][c] ) ; printf(" Given Matrix is \n");

for( r=0 ; r<=2 ; r++) { for(c=0 ; c<=2 ; c++) printf(" %d" , x[r][c] ) ; printf( "\n" ); } getch( ); } => To find the sum of the given elements into 3x4 matrix => Write programs to generate the following matrices of 4x4 sizes 1. Null Matrix 2. Identity Matrix 3. Upper Triangular Matrix 0 0 0 0 1 0 0 0 0 5 5 5 0 0 0 0 0 1 0 0 0 0 5 5 0 0 0 0 0 0 1 0 0 0 0 5 0 0 0 0 0 0 0 1 0 0 0 0 4.Lower Triangular Matrix 5. 0 0 0 0 1 5 5 5 7 0 0 0 7 1 5 5 7 7 0 0 7 7 1 5 7 7 7 0 7 7 7 1 /* To generate identity matrix of 4x4 size */ #include<stdio.h> main() { int n[4][4] , r , c ; clrscr(); for( r=0 ; r<=3 ; r++) for( c=0 ; c<=3 ; c++) if( r==c ) n[r][c] = 1; else n[r][c] = 0; printf(" Identity Matrix is \n"); for(r=0 ; r<=3 ; r++) { for(c=0 ; c<=3 ; c++) printf(" %d" ,n[r][c] ) ; printf( "\n" ); } getch(); } /* To generate the transpose matrix for the given 3x4 matrix */ #include<stdio.h> main() { int n[3][4] , r , c ; clrscr(); printf(" Enter elements into 3x4 matrix \n"); for(r=0 ; r<=2 ; r++) for(c=0 ; c<=3 ; c++) scanf("%d" , & n[r][c] ) ; printf(" Transpose Matrix is \n"); for(r=0 ; r<=3 ; r++) { for(c=0 ; c<=2 ; c++) printf(" %d" , n[c][r] ); printf( "\n" ); } getch(); } => To copy all the elements from the given 3x5 matrix into another matrix

=> To find the addition matrix for the given two mxn and pxq size matrices /* To find the product matrix for the given two matrices of mxn and pxq sizes respectively */ #include<stdio.h> main() { int A[10][10],B[10][10],S[10][10]; int r,c,m,n,p,q,k; clrscr(); printf(" Enter the order of first matrix in terms of rows and columns"); scanf("%d%d",&m,&n); printf(" Enter the order of second matrix in terms of rows and columns"); scanf("%d%d",&p,&q); if(n==p) { printf(" Enter elements into first %d x %d matrix \n",m,n); for(r=0; r<=m-1; r++) for(c=0; c<=n-1; c++) scanf("%d",&A[r][c]); printf(" Enter elements into second %d x %d matrix\n",p,q); for(r=0; r<=p-1; r++) for(c=0; c<=q-1; c++) scanf("%d",&B[r][c]); for(r=0 ; r<=m-1; r++) for(c=0; c<=q-1; c++) { S[r][c]=0; for(k=0; k<=n-1; k++) S[r][c]=S[r][c]+A[r][k]*B[k][c]; } printf(" Product Matrix is \n"); for(r=0; r<=m-1; r++) { for(c=0; c<=q-1; c++) printf(" %d",S[r][c]); printf("\n"); } } else printf(" Invalid matrices orders so product matrix is not possible"); getch(); } 3. Multi-Dimensional Arrays: These are the array variables where the memory for the elements is alloc ated in three or more dimensional formats, so while declaring these variables we need to specify three or more subscripts. Syntax: <typename> <Arrayname>[Subscript1][Subscript2].....; Ex: int n[3][2][3]; => Initialization of Arrays: Initializing an array is nothing but assigning the required initial valu es for the elements of an array while declaring it, within the declaration state ment, may be single dimensional or double dimensional arrays. -- Single Dimensional Array Initialization -Syntax:

<typename> <Arrayname>[Subscript]= { Data1, Data2, ..... , Datan } ; Ex: int n[5] = { 10, 46, 7 , 84 , 22 } ; int x[ ]={ 10 , 20 , 30 , 40 , 50 , 60 } ; char str [ ] = { 'C' , 'O' , 'M' , 'P' , 'U' , 'T' , 'E' , 'R' } ; -- Double Dimensional Array Initialization -Syntax: Row Column <typename> <Arrayname>[Subscript][Subscript] ={ Data1, Data2, .... , Datan } ; Ex: int n[3][3]={ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } ; or int n[ ] [ ]={ { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; String Manipulations A string is group of characters represented as a single notation. As 'C ' doesnot provides any built-in data type by name string, we are adopting Arrays concept and creating a character array for storing and manipulating strings, ma y be of single dimensional format or double dimensional format. In 'C', we are using "%s" format specifier to represent a string. The end of the string is rep resented by Null character ('\0') /* To read and print a string */ #include<stdio.h> main( ) { char name[20]; clrscr(); printf(" Pls enter your name :"); scanf("%s",name); printf(" Hi %s, Glad 2 C U ", name); getch( ); } => gets( ): get string Built-in 'C' string input function that gets or reads a string (collecti on of characters including white spaces) as input from the user through keyboard and stores it within the given character array. Defined in <Stdio.h> file Syntax: gets(<Char Array>); Ex: char x[20]; gets(x); => puts( ): put string Built-in 'C' string output function that puts or prints the given string expression on the monitor. Defined in <Stdio.h> file Syntax: puts(<String expression>); Ex: 1. puts(name); 2. puts(" Hi, Good Evening "); /* To read and print a string using string functions*/ #include<stdio.h> main( ) { char name[20]; clrscr(); puts(" Pls enter your name :"); gets(name);

puts( name); getch( ); } => strlen( ): string length => Returns the length i.e. number of characters available in the given s tring expression. Defined in <String.h> file Syntax: strlen(<String Expression>); Ex: 1. strlen(name); 2. strlen(" Satyam Computers"); => 17 /* To find the length of the given name */ #include<stdio.h> #include<string.h> main() { char name[20]; int len ; clrscr(); puts(" Pls Enter UR Name :"); gets(name); len = strlen(name); printf("Hi %s , Length of Your Name is %d " , name , len) ; getch(); } => strcpy( ): String Copy Built in 'C' string function that assigns/copies the given source string expression into the given character array. Defined in <String.h> file Syntax: strcpy (<Char Array> , <String Expression> ) ; Ex: char x[20] , y[20] ; gets(x); strcpy( y , x ) ; 2. char result[5] ; strcpy(result , "pass" ) ; /* To copy the given string from first array into second array */ #include<stdio.h> #include<string.h> main() { char x[20] , y[20]; clrscr(); puts(" Pls Enter The Req. String :"); gets(x); strcpy( y , x ) ; printf(" Given string is %s " , x ) ; printf("\n Copied string is %s " , y ) ; getch(); } => strcmp( ): String Comparision Built-in 'C' string function that compares the two given string expressi ons and returns positive value when first string is greater than second, negativ e value when second string is greater than first or zero when both the strings a re same. Defined in <String.h> file Syntax:

strcmp(<Str Exp1>,<Str Exp2>); Ex: 1.char x[20],y[10]; gets(x); gets(y); strcmp(x,y); 2. strcmp("ABC","abc"); => -32 /* To compare the two given strings */ #include<stdio.h> #include<string.h> main() { char x[20],y[20]; int check; clrscr(); puts(" Pls Enter The First String :"); gets(x); puts(" Pls Enter The Second String :"); gets(y); check=strcmp(x,y); if(check>0) printf(" %s string is greater than %s",x,y); else if(check<0) printf(" %s string is greater than %s",y,x); else printf(" %s string is same as %s",x,y); getch(); } => strcat( ): String Concatenation Built-in 'C' string function that is used for contenation i.e. joining o f the two given strings as a single string. Defined in <String.h> file Syntax: strcat(<Target CharArray>,<Str Exp>); Ex: char x[20]; strcpy(x,"Satyam "); strcat(x," Computers"); /* To join the two given strins as single */ #include<stdio.h> #include<string.h> main() { char x[20],y[10]; clrscr(); puts(" Pls Enter The First Part of the String :"); gets(x); puts(" Pls Enter The Last Part of the String :"); gets(y); strcat(x,y); printf(" Concatenated string is %s",x); printf("\n Last Part of the String is %s",y); getch(); } FUNCTIONS (Self contained block or program used for a particular sub task) As 'C' is a modular programming language, function is a block or module of a program consisting of set of ordered statements related to perform a partic

ular sub task, having a unique identity known as function name. Features: => Easy readability and understandability => Easy Debugging of the errors => Provides Modular programming feature => Reusability of the code => Sharing of the work => Reduces the code and saves time and money, etc. In 'C', functions are classified into three types: 1. Library Functions 2. Macros / Defined Functions 3. User Defined Functions 1. Library Functions: These are the pre-defined functions of the 'C' language that are availab le within the compiler in the form of library files known as Header files. Ex: printf( ), scanf( ), getchar( ), clrscr( ), strlen( ), etc. 2. Macros / Defined Functions: These are the defined functions consisting of single executable expressi on, which are to be defined using "#define" directive, as pre-processor directiv e statement. Syntax: #define <Functionname>([<Parameters list>]) <Executable Exp> Ex: #define Sum(a,b) (a+b) #define Area(r) (3.14159*r*r) #define Big(x,y) (x>y)?x:y /* To find area of a circle by defining a Macro */ #include<stdio.h> #define Area(x) (3.14159*x*x) main( ) { int r; clrscr(); printf(" Enter radius of a Circle :"); scanf("%d" , &r); printf(" Area of Circle = %f", Area(r)); getch(); } /* To find biggest and smallest among two given numbers by defining Macros using Ternary Operators */ #include<stdio.h> #define Max(a,b) (a>b)?a:b #define Min(a,b) (a<b)?a:b main( ) { int x,y; clrscr(); printf(" Enter two numbers :"); scanf("%d%d",&x,&y); printf(" Biggest = %d",Max(x,y)); printf("\n Smallest = %d" , Min(x,y)); getch(); } => Write a program to find biggest and smallest among three given numbers by def

ining macros using ternary operators 3. User-Defined Functions: These are the functions that are defined by the user for his own need or requirement, in the form of separate module / block by grouping the necessary s et of statements related to perform a particualr sub task. Each function is assigned with a unique identity known as functionname. Functions may or may not takes parameters and similarly they may or may not ret urns value / data. To manipulate user defined functions in 'C', follow the following three steps: 1. Function Declaration (Prototype Statements) 2. Function Definition 3. Function Call / Invoke 1. Function Declaration: The first step to manipulate user defined functions is to declare the fu nctions, to give the necessary specification to 'C' compiler about the functionn ame, number and type of parameters and type of return value. The statements used for declaration of functions are known as function p rototype statements. Syntax: Formal [<rt-type>] <Functionname>( [<Parameters List>] ); Where rt-type => return data type name (default it takes "int" type) Functionname => Function identity Parameters list => The type of data items passed from the calling point Ex: 1. int Sum(int,int); OR Sum(int , int); 2. float Area( int ); 3. int Big(int , int , int); 4. int Reverse( int ); 2. Function Definition: The second step after function prototype statements is to define the fun ctions, which is nothing but associating the necessary code(statements) to the f unctions in the form of separate blocks/modules, may be before the main() or aft er the main() block. Function definition consists of two parts: 1. Function Header Row 2. Function Body Syntax: Formal [<rt-type>] <Functionname>( [<Parameters List>] ) { Statement 1; .................... Statement n; } Ex: int Sum(int x,int y) { return(x+y); } OR Sum(x , y) int x , y; -> Fn Header Row -> Fn Body

{ return(x+y); } 3. Function Call / Invoke : The last step in manipulation of user defined functions is to call or in voke the functions defined within the main() block and pass the necessary data i n the form of parameters, for their execution. Syntax: Ex: c=Sum(10,20); OR printf(" Sum = %d ", Sum(a,b)); => Parameters : These are the arguments that represents the data, to be transferred betw een two blocks of a program. In 'C', parameters are classified into two types: 1. Actual Parameters 2. Formal Parameters Actual Parameters are the arguments that are used for passing the necess ary data to the function called from its calling point, which may a constant, an operand, an operator, a formula, a sub function call. Formal Parameters are the arguments that are used for receiving the data passed from the calling point, which are used within the function header row (f n definition). As the formal parameters are used for the first time within the function, they needed to be declared there and then. The number and type of for mal parameters should be equal and same as that of actual parameters passed from the calling point. /* To find the area of a circle by defining a UDF */ #include<stdio.h> float Area(int); /* Fn Prototype Statement */ main( ) { int r; clrscr(); printf(" Enter radius of a Circle :"); scanf("%d",&r); printf(" Area of a Circle = %f" , Area(r) ); /* Fn Ca ll */ getch(); } float Area(int x) /* Fn Definition */ { return ( 3.14159 * x * x ); } /* To find biggest and smallest among two given nos by defining UDFs */ #include<stdio.h> main() { int m,n; clrscr(); printf(" Enter two numbers :"); scanf("%d%d",&m,&n); printf(" Biggest = %d ", Big(m,n) ); printf("\n Smallest = %d ", Small(m,n) ); getch(); } Actual <Functionname>( [<Parameters List>] );

Big(int x,int y) { if(x>y) return(x); else return(y); } Small(int m,int n) { if(m<n) return(m); else return(n); } => To find the area and perimeter of a rectangle by defining UDFs => To find the smallest among three given numbers by defining UDF Type of User Defined Functions: Depending on the parameters and return value of the functions, they are divided into four types: 1. 2. 3. 4. Functions Functions Functions Functions with with with with parameters and with return value. parameters and no return value. no parameters but returns value. no parameters and no return value.

/* To swap the two given numbers by defining a UDF */ /* Fn with parameters but no return value */ #include<stdio.h> swap(int x,int y) { int z; z=x; x=y; y=z; printf("\n After swapping values are %d and %d ",x,y); } main() { int a,b; clrscr( ); printf(" Enter two values :"); scanf("%d%d" , &a, &b); printf(" Before swapping values are %d and %d ",a,b); swap(a,b); getch(); } /* To find the factorial value for the given number by defining UDF */ #include<stdio.h> int fact(int x) { int f; for(f=1;x>=1; x--) f=f*x; return(f);

} main() { int n; clrscr(); printf(" Enter the required number :"); scanf("%d" , &n); printf(" Factorial Value = %d ", fact(n) ); getch(); } /* To generate fibonnacci series below n by defining UDF */ #include<stdio.h> fibo(int n) { int a,b; for(a=0 , b=1 ; b<=n ; a+=b , b+=a ) printf("\n %d \n %d",a,b); } main( ) { int n; clrscr(); printf(" Enter the maximum number :"); scanf("%d",&n); fibo(n); getch(); } /* To check whether the given character is an alphabet, a digit, a space or a special character by defining UDFs */ #include<stdio.h> int is_alpha(char x) { if((x>=65 && x<=90) || (x>=97 && x<=122)) return(1); else return(0); } int is_digit(char x) { if(x>=48 && x<=57) return(1); else return(0); } int is_space(char x) { if(x==' ') return(1); else return(0); } main( ) {

char m; clrscr(); printf(" Enter a Character :"); m=getchar(); if(is_alpha(m)) printf(" Given character is an alphabet "); else if(is_digit(m)) printf(" Given character is a digit "); else if(is_space(m)) printf(" Given character is a blank space "); else printf(" Given character is a special character "); getch(); } => Passing of Arrays as Parameters to Functions: In 'C', when we are in need to pass group of same type of data items as parameters to a single function, rather than passing them individually one by on e, we can pass them together as a single parameter in the form of an array, whic h may be single or double dimensional format. To pass Single dimensional array as parameter to a function, use only th e arrayname as actual parameter at the function calling point and declare same t ype of dummy array without subscript as formal parameter within the function hea der row. Here the dummy array is not a copy of the actual array, instead it is the reference for the actual array i.e. directly the original elements are manip ulated by the dummy array (Call by Reference). /* To read and print 5 values into an array by defining UDFs */ #include<stdio.h> read_array(int x[]) { int i; for(i=0 ; i<=4 ; i++) scanf("%d" , &x[i]); } main() { int n[5]; clrscr(); printf(" Enter 5 values :\n"); read_array(n); printf(" Given values are "); display_array(n); getch(); } display_array(int x[]) { int i; for(i=0 ; i<=4 ; i++) printf("\n %d",x[i]); } => Program to read 10 numbers into an array and find their sum by defining UDFs /* To find biggest among n given numbers into an array by defining UDFs */ #include<stdio.h> read_array(int m[ ],int n)

{ int i; for(i=0 ; i<=n-1 ; i++) scanf("%d" , &m[i]); } main() { int x[50],n; clrscr(); printf(" Enter how many numbers to be accepted :"); scanf("%d", &n); printf(" Enter %d values :\n",n); read_array(x,n); printf(" Biggest = %d",big_array(x,n)); getch(); } int big_array(int m[ ],int n) { int i,big; big=m[0]; for(i=1 ; i<=n-1 ; i++) if(m[i]>big) big=m[i]; return(big); } /* To sort the n given numbers into an array in ascending order by defining UDFs */ #include<stdio.h> read_array(int m[],int n) { int i; for(i=0 ; i<=n-1 ; i++) scanf("%d" , &m[i]); } main() { int x[50],n; clrscr(); printf(" Enter how many numbers to be accepted :"); scanf("%d", &n); printf(" Enter %d values :\n",n); read_array(x,n); sort_array(x,n); printf(" Ascending order of given numbers are "); display_array(x,n); getch(); } sort_array(int m[],int n) { int i,j,temp; for(i=0 ; i<=n-2 ; i++) for(j=i+1 ; j<=n-1 ; j++)

if(m[i]>m[j]) { temp=m[i]; m[i]=m[j]; m[j]=temp; } } display_array(int m[],int n) { int i; for(i=0 ; i<=n-1 ; i++) printf("\n %d",m[i]); } => Passing of double dimensional arrays as parameters to functions: When we are in need to pass double dimensional array as parameter to a f unction, use only the array name as actual parameter at the function calling poi nt and same type of double dimensional dummy array as formal parameter without s pecifying the row subscript but giving the column subscript. (Call by reference) /* To read and print elements into 3x4 matrix by defining UDFs */ #include<stdio.h> read_matrix(int m[][4]) { int r,c; for(r=0 ; r<=2 ; r++) for(c=0 ; c<=3 ; c++) scanf("%d",&m[r][c]); } main() { int x[3][4]; clrscr(); printf(" Enter elements into 3x4 matrix :\n"); read_matrix(x); printf(" Given Matrix is \n"); display_matrix(x); getch(); } display_matrix(int m[][4]) { int r,c; for(r=0 ; r<=2 ; r++) { for(c=0 ; c<=3 ; c++) printf("%3d",m[r][c]); printf("\n"); } } => Scopes of the Variables: A variable is an entity i.e. memory container that holds the data stored for future usage. In 'C', every variable has its accessing limitations known a s scope. The variable when it is declared it is referred as the variable is bor n, its accessing limitations are referred as its living(life) and when it is rel eased it is referred as its dead.

Depending on the accessing limitations of the variables, they are classi fied into three scopes: 1. Local Variables (auto variables) 2. Global Variables (extern variables) 3. Static Variables 1. Local Variables: These are the variables that are localized i.e. their accessing is limit ed only to the block in which they are declared and gets released (dead) as soon as the block's execution ends. /* Sample Program for Local Variables */ #include<stdio.h> main( ) { int a , b; clrscr( ); printf(" Enter two numbers :"); scanf("%d%d" , &a , &b); printf(" Sum = %d ", Sum(a,b) ); getch( ); } int Sum(int x, int y) { return(x+y); } --> Where a & b => Local to main ( ) x & y => Local to Sum ( ) 2. Global Variables: These are the variables that are globalized i.e. their accessing is limi ted to the entire program that is in all the blocks / modules and gets released (dead) as soon as the program's execution is ended. These variables needed to b e declared outside the blocks at the beginning of the program as Global Declarat ion statements. /* Sample program for global variables */ #include<stdio.h> int a , b; main( ) { clrscr( ); printf(" Enter two numbers :"); scanf(" %d%d ", &a , &b); printf(" Smallest = %d ", Small( ) ); getch( ); } int Small( ) { if(a<b) return(a); else return(b); } ---> Where

a & b => Global Variables for the entire program 3. Static Variables: These are the variables whose memory remains unchangable through out the program execution for all the other remaining declaration of the variables with the same name. To declare static variables, preceed the variables declaration with "sta tic" keyword. Syntax: static <typename> <Identifier/s>; ex: static float p; static int a,b; /* Sample program for static variables */ #include<stdio.h> main() { static int a,b; clrscr(); printf(" Enter two numbers :"); scanf("%d%d",&a,&b); printf(" Product of a and b = %d",product(a,b)); getch(); } int { } ---> Where a & b => static variables /* Sample program for static variables */ #include<stdio.h> main() { static int x=50; clrscr(); printf("\n x = %d",x); sample(); getch(); } sample( ) { int x; x=x+5; printf("\n %d ", x); } => Recursions: It is the concept of calling a function within it self for repeated n nu mber of times, to reduce the code and to increase the execution speed of the pro gram. In recursive function, we need to specify a condition to stop the recursi ve execution otherwise the program execution will enter into infinity loop. /* To find the factorial value for the given number */ product(int a, int b) return(a*b);

/* By using Non-recursive concept */ #include<stdio.h> main() { int n; clrscr(); printf(" Enter a number :"); scanf("%d", &n); printf( " Factorial Value = %d",fact(n)); getch(); } int fact(int x) { int f; for(f=1 ; x>=1 ; x--) f=f*x; return(f); } /* To find the factorial value for the given number */ /* By using recursive concept */ #include<stdio.h> main() { int n; clrscr(); printf(" Enter a number :"); scanf("%d", &n); printf( " Factorial Value = %d",fact(n)); getch(); } int fact(int x) { if(x==1) return(1); else return(x * fact(x-1)); } /* To swap the two given numbers by using global variables */ /* Functions with no parameters and no return value */ #include<stdio.h> int x , y; swap( ) { int temp; temp=x; x=y; y=temp; } main() { clrscr(); printf(" Enter two numbers :"); scanf("%d%d",&x,&y); printf(" Before swapping x = %d and y = %d" , x , y );

swap( ); printf("\n After swapping x = %d and y = %d " , x , y ); getch( ); } POINTERS These are of non-primitive derived data type concept. Pointers are special variables that holds the address of the data stored , rather than directly holding the data (Unlike the ordinary variables). Pointe rs are mainly used for the following features: 1. It increases the program execution speed, as it is directly pointing the address 2. It is provides memory resident programming feature, using which we ca n directly manipulate or use the system's internal memory. 3. It is also used for managing dynamic type of memory, which is allocat ed at the time of execution of the program and which is non-static in size. 4. It is also used for increasing the size of an array at the time of pr ogram execution. 5. It saves the time and also saves the memory 6. It is also used for creation of new variables at run time, etc. Follow the following format for declaration of pointer variables: Syntax <typename> Where typename => any primitive data type name * (Asterisk) => Pointer operator / Value at Address operator / Indirect operator Ex: int *p; float *f,*d; char *st; /* Sample program for pointers */ #include<stdio.h> main() { int n,*ptr; clrscr(); ptr=&n; printf(" Enter a value :"); scanf("%d",ptr); printf("\n Value at n = %d",n); printf("\n Address of n = %ld",&n); printf("\n Value at ptr = %ld",ptr); printf("\n Value at Address = %d", *(&n)); printf("\n Value at pointer = %d",*ptr); getch(); } /* To read and print a #include<stdio.h> main() { char ch,*p; p=&ch; printf(" Enter *p=getchar(); printf(" Given getch(); } character using pointer */ *<Pointername /s>;

clrscr(); a character :"); character = %c",*p);

/* To read and print 5 values into an array using pointers */ #include<stdio.h> main() { int n[5],*p,i; clrscr(); p=n; /* p=&n[0]; */ printf(" Enter 5 values :"); for(i=1 ; i<=5 ; i++) { scanf("%d",p); p++; } printf(" Given values are :"); for(p=n , i=1 ; i<=5 ; i++ , p++) => for(n-- , i=1 ; i<=5 ; i++ , n--) printf(" \n %d",*p); getch(); } => To find sum of the given 10 numbers into an array using pointers /* To find biggest among the given 10 numbers into an array using pointers */ #include<stdio.h> main() { int n[10],*p,i,big; clrscr(); p=n; /* p=&n[0]; */ printf(" Enter 10 values :"); for(i=1 ; i<=10 ; i++) scanf("%d",p++); big=n[0]; for(p=&n[1] , i=2 ; i<=10 ; i++ , p++) if(*p>big) big=*p; printf(" \n Biggest = %d",big); getch(); } => Function Calls: In 'C', the user defined functions can be called or invoked in two diffe rent methods, they are: 1. Call by Value (Pass by Value) 2. Call by Reference (Pass by Reference) In Call by Value method, we create dummy variables as formal parameters within the function header row and receive the copy of the actual parameters pas sed from the calling point, manipulating which within the function does not affe cts the original actual parameters data/value. /* To swap the two given numbers by defining a UDF */ /* Using Call by Value method */ #include<stdio.h> swap(int x,int y) {

int z; z=x; x=y; y=z; printf("\n After swapping values are %d and %d ",x,y); } main() { int a,b; clrscr( ); printf(" Enter two values :"); scanf("%d%d" , &a, &b); printf(" Before swapping values are %d and %d ",a,b); swap(a,b); getch(); } In Call by Reference method, we create pointer type of variables as form al parameters within the function header row and receives the reference (address ) of the actual parameters from the calling point, manipulating which within the function directly affects the original actual parameters data/value. /* To swap the two given numbers by defining a UDF */ /* Using Call by Reference method */ #include<stdio.h> swap(int *x, int { int z; z=*x; *x=*y; *y=z; } main() { int a,b; clrscr( ); printf(" Enter two values :"); scanf("%d%d" , &a, &b); printf(" Before swapping values are a = %d and b = %d ",a,b); swap(&a,&b); printf("\n After swapping values are a = %d and b = %d ", a, b); getch(); } STRUCTURES These are of non-primitive user defined data type concept, which is enca psulating (grouping) collection of different type of data items together for sto ring related information, which can be referred by a single common identity know n as Structure name or Tag name. In general, a structure can also be defined as "Collection of different type of data items grouped together for storing related information". The diffe rent data items grouped in a structure can be referred as members. To manipulate structure in 'C', we have to follow the following three st eps: 1. Structure Definition 2. Declaration of Structure Variables *y)

3. Accessing the individual members 1. Defining a Structure: As structures are of user-defined data type concept, first we have to de fine the structure to give necessary specifications to the 'C' compiler, about t he structure name and scopes of the members (member names and their types). Syntax: struct <Structurename> { <typename> <Identifier/s>; <typename> <Identifier/s>; ......................... <typename> <Identifier/s>; }; Ex: struct Sample { int x; char y; float z; }; (Allocates 7 Bytes for Sample Structure Variable) The members of a structure can not only be of primitive data type, inste ad if required they can also be of non-primitive user defined data type, may be of pointers, arrays, sub-structures. Ex: 1. struct Student { int rno,s1,s2,s3,total; char name[20],result[5]; float avg; }; (Allocates 39 bytes for student structure variable) 2. struct Emp { int eno; char *name; float basic; }; 3. struct date { int dd,mm,yy; }; struct Stu { int rno; char name[20]; struct date dob; --> Nesting of structures };

2. Declaration of Structure Variables: After defining a structure, we have to declare the variabels pertaining to the structure defined, for storing the data. The variables of a particular s tructure type are referred as structure variables (Objects). Syntax: struct <Structurename> <Identifier/s>; Ex:

struct Sample s; struct Student x; struct Emp e1,e2,e3; struct Stu x,y; When we are in need of three or more variables of same structure type, r ather than declaring them individually one by one, we can declare them as array type may be of single or double dimensional format. Syntax: struct <Structurename> <Arrayname>[Subscript]...; Ex: struct Student a[5]; struct Emp e[3][2]; 3. Accessing the Individual Members of a Structure Variable: The last step in manipulation of structure is to access the individual m embers of a structure variable, by using structure variable name followed by me mber accessing operator ( . ) and member name. Syntax: <Structure Variablename> . <Membername> Ex: s.x x.rno e1.eno s.y x.name e1.name s.z x.s1 ..... x.result .......x.avg e1.basic x.rno x.name x.dob.dd x.dob.yy a[i].name e[r][c].name x.dob.mm ..... a[i].avg e[r][c].basic

a[i].rno e[r][c].eno

/* To read and print a single student record using structures */ #include<stdio.h> #include<String.h> main() { struct Student { int rno,s1,s2,s3,total; char name[20], result[5]; float avg; }; struct Student x; clrscr(); printf(" Enter Rollno :"); scanf("%d",&x.rno); printf(" Enter Name :"); scanf("%s",x.name); printf(" Enter three sub marks :"); scanf("%d%d%d",&x.s1,&x.s2,&x.s3); x.total=x.s1+x.s2+x.s3; x.avg=x.total/3.0; if(x.s1>34 && x.s2>34 && x.s3>34) strcpy(x.result,"pass"); else strcpy(x.result,"fail"); clrscr(); printf(" Rollno : %d",x.rno); printf("\n Name : %s",x.name); printf("\n Subject 1 marks: %d",x.s1); printf("\n Subject 2 marks: %d",x.s2);

printf("\n printf("\n printf("\n printf("\n getch(); }

Subject 3 marks: Total Marks : Average Marks : Result :

%d",x.s3); %d",x.total); %.2f",x.avg); %s",x.result);

=> To read and print an employee particulars using structures Eno,name,desig,dept,basic calculate hra = 30 % of basic da = 15 % of basic pf = 5 % of basic net = basic + hra + da -pf /* To read and print n number of student records using structures */ #include<stdio.h> #include<String.h> struct Student { int rno,s1,s2,s3,total; char name[20], result[5]; float avg; }; main() { struct Student x[20]; int n,i; clrscr(); for(i=0 ; i<=20 ; i++) { printf("Enter Rollno or 0 to stop:"); scanf("%d",&x[i].rno); if(x[i].rno==0) break; printf(" Enter Name :"); scanf("%s",x[i].name); printf(" Enter three sub marks :"); scanf("%d%d%d",&x[i].s1,&x[i].s2,&x[i].s3); x[i].total=x[i].s1+x[i].s2+x[i].s3; x[i].avg=x[i].total/3.0; if(x[i].s1>34 && x[i].s2>34 && x[i].s3>34) strcpy(x[i].result,"pass"); else strcpy(x[i].result,"fail"); } n=i; clrscr(); printf(" Rollno\t Name\t\t Sub1 Sub2 Sub3 Total AVerage Result"); for(i=0; i<=n-1 ; i++) printf("\n %5d %-20s %5d %5d %5d %7d %8.2f %-8s",x[i].rno, x[i].name,x[i].s1,x[i].s2,x[i].s3,x[i].total,x[i].avg,x[i].result); getch(); } => Passing structure variables as parameters to functions: In 'C', when necessary we can also pass structure variables as parameter s to functions, which can be done in two different methods:

1. Call by Value method 2. Call by reference method In call ructure type as y of the actual tructure member by value method, we create dummy variables of the same passed st formal parameter within the function header row and receives cop structure variable passed from the calling point. Here we use s accessing operator as dot (. )

In call by reference method, we create pointer variable of passed struct ure type as formal parameter and receives the reference i.e. address of the pass ed structure variable. Here we use -> (Arrow operator) as member accessing oper ator. /* To read and print an emplyee record using structures and UDFs */ #include<stdio.h> struct Emp { int no,basic; char name[20],desig[10],dept[10]; float hra,da,pf,net; }; main() { struct Emp x; clrscr(); printf(" Enter an employee record :"); read_record(&x); printf(" Given Employee Record is :\n"); display_record(x); getch(); } read_record(struct Emp *m) { printf("\n Enter employee Number :"); scanf("%d",&m->no); printf("\n Enter employee Name :"); scanf("%s",m->name); printf("\n Enter Designation :"); scanf("%s",m->desig); printf("\n Enter Department :"); scanf("%s",m->dept); printf("\n Enter Basic Salary :"); scanf("%d",&m->basic); m->hra=m->basic*0.30; m->da=m->basic*0.15; m->pf=m->basic*0.05; m->net=m->basic+m->hra+m->da-m->pf; } display_record(struct Emp m) { printf("\n Employee Number : printf("\n Employee Name : printf("\n Employee Designation: printf("\n Employee Department : printf("\n Employee Basic : printf("\n H R A : printf("\n D A :

%d",m.no); %s",m.name); %s",m.desig); %s",m.dept); %d",m.basic); %.2f",m.hra); %.2f",m.da);

printf("\n P F printf("\n Net Salary } FILES

: %.2f",m.pf); : %.2f",m.net);

These are of non-primitive user defined data type concept, which is a me an used for storing large amount of information permanently within the system's memory for later use, having a unique identity known as File name. This concept in 'C' allows us to store the given input or output yielded permanently within the system. Depending on the storage device, files are classified into two types: 1. Primary Files (Internal / Temporary Files): Automatically created and manipul ated by the 'C' compiler to store the input and output, within the RAM memory 2. Secondary Files (External / Permanent Files): Created and manipulated by the programmer, within the auxillary storage devices such as Harddisk, Floppy, Pen d rive, etc. Depending on the accessing mode of files, files are classified into two types: 1. Sequential Access Files: Data can be accessed only in an sequential manner, a dopted while created. These type of files are easy to create and difficult to a ccess. 2. Random Access Files: Data can be accessed randomly without any sequence. The se type of files are difficult to create and easy to access. Depending on the format adopted for storing the data, files are classifi ed into two types: 1. Data / Text Files: These are sequential files where the data is stored in co ntinuous stream format. 2. Binary Files: These can be sequential or random files where the data is store d in record by record i.e. row by row format. Steps for File Manipulations: 1. Create / Open a File 2. Perform Input and Output Operations 3. Close a File 1. Create / Open a File: As files are managed by the operating system not by 'C' language, first we have to establish link between the current program and the current operating system, by creating a pointer variable of FILE type, which is defined in <Stdio .h> file. Syntax: FILE *<Pointername>; Ex: FILE *fp; FILE *f, *p; fopen ( ): File Open It is a built-in 'C' file function that is used for opening a file with the given name in the given mode and assigns its address to the FILE pointer. Defined in <Stdio.h> file. Syntax: <File Pointer> = fopen ( "Filename" , " mode "); Where File Pointer => A pointer of FILE type Filename => Name to the file, according to the DOS conventions mode => Status of file opening "w" => Write mode "r" => Read mode

"a" "w+" "r+" Ex:

=> Append mode => Write and Read mode => Read and Write mode

1. FILE *fp; fp=fopen(" Sample.Txt" , "w"); 2. FILE *ptr; ptr=fopen(" Student.Dat " , "r"); 3. Closing the File: The last step in manipulation of files is to close the file i.e. to brea k the link established between the current program and the current operating sys tem, which is done by using fclose ( ) function, which is defined in <Stdio.h> file Syntax: fclose(<Filepointer>); Ex: 1. fclose(fp); 2. fclose(ptr); 2. Performing Input and Output Operations: To perform input and output operations i.e. to store or read data from a file, 'C' provides the following functions, which are defined in <Stdio.h> fil e. i. fputc( ): file put character Used for putting i.e. writing the given single character expression into the file. Syntax: fputc(<Filepointer> , <ExpC>); Ex: fputc(fp , ch); ii. fgetc( ): file get character Used for getting i.e. reading a single character from the file and assig ns it in to the left hand side given character variable. Syntax: <Char variable> = fgetc(<Filepointer>); Ex: ch = fgetc(ptr); iii. EOF : End Of File It is a keyword that returns true when the cursor is at the end of the f ile otherwise returns false. /* To store collection of character into Sample.Txt file */ #include<Stdio.h> main() { FILE *fp; char ch; clrscr(); fp=fopen("Sample.Txt", "w"); printf(" Enter the required Text :\n"); while((ch=getchar())!=EOF) fputc(ch,fp); fclose(fp); }

/* To read total content from Sample.Txt file */ #include<Stdio.h> main() { FILE *fp; char ch; clrscr(); fp=fopen("Sample.Txt", "r"); printf(" File Content is :\n"); whil

You might also like