P. 1
C Language

C Language

|Views: 21|Likes:
Published by Hari Krishna

More info:

Published by: Hari Krishna on Jul 26, 2011
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

12/10/2012

pdf

text

original

-1

-

C Language Introduction
Languages :
A Set Of Statements Is Called A Language.There Are Four Types Of Languages According To Their Time.

I generation languages: these languages are machine languages. To write programs in these languages the
system technology must be required. The data is Non-portable. That means a program written in a system does not work in another systems.

Ii Generation Languages :These Are Assembly Languages. These Are Also system oriented that means to write
any program in a system that system’s technology must be required and data is non-portable. But they used MNEMONIC words in programs. That means they used a single word instead of more words.

III Generation Languages :In these languages programs are witten in

general english language.There is no need to know the system technology and the data can be transfered anywhere.

IV Generation languages :These languages are defined in any one of the
above languages.These are also called as packages. Here I & II Generation languages are called Low Level Languages and III & IV generation languages are called High Level Languages. For high level languages we have to use translaters to translate the source code written in general english language into machine language. These translaters are two types. 1) Interpreters, 2) Compilers. 1) Interpreters :These translaters translate the source code step by step into machine language until any error. If there is any error it stops and shows some message. After correction it can continue. Ex: BASIC, DBase III+, ....

2) Compilers :These translaters translate the entire source code into machine language when it is error-free
and creates an object file in machine language. If there is any error it shows the list of error. After debugging it creates the object file.

Ex: COBOL, C, C++, ...

-2-

C LANGUAGE
The language ‘C’ was designed by Dennis Ritchie at AT & T Bell Laboratories. The standardised C was released in 1979.

The ‘C’ language is used to develop
i) ii) iii) iv) v) Scientific applications, Business applications, Graphical applications (Ex: WINDOWS ), System programs, Operating Systems (Ex: UNIX) , ...

Character Set :
alphabets constants, statements,

digits ==> variables, ==> special symbols keywords

==> Programs instructions

Constants : The unchangeable quantities are called Constants.The constants are generally two types. 1) Character constants :

a) Characters Ex: ‘a’, ‘5’, ‘+’, ‘ ‘, ... b) Strings Ex: “abc”, “435”, ‘rama”, ...
2) Numeric Constants : a) integers Ex: 435, -657, 65535, -32768,... b) Real numbers i) Fractional form Ex: 435.67, 345.00054, ... ii) Exponential form Ex: 0.02e3, 1.17e-38, ... Variables : The quantities which can be changed during the execution of program are called Variables. A variable can be considered as the name of the cell which can hold the constants. To use any variable it must be declared with its data type before the first executable statement and they can be initialised. Naming the variable is very important. 1) The variable name must be start with either alphabets or an underscore and may contain alphabets, digits, hyphen or underscore. 2) The maximum length of a variable is 8 characters. But some compilers can accept upto 32 characters. 3) There must not be any blank spaces or special symbols in a variable name. 4) A variable name must not be a keyword.

Ex:

valid eno empname emp-name

invlid emp name emp(name 45abc

Keywords : These are predefined words. There are 32 keywords in C language. These keywords can not be used as user-defined variables.

Operators : There are 42 operators in C language.

-31) Arithmetic Operators : Ex: 100 + 40 ==> 140 100 - 40 ==> 60 100 * 40 ==> 4000 100 / 40 ==> 2 100 % 40 ==> 20 40 % 100 ==> 40 + - * / %

2) Assigning Operators : = (variable) = (constant) / (variable) / (expression) ; Ex: a = 5 b=a c = a + b -2 3) Multiple operators :
Ex: a = a + 3 ==> a += 3 a = a - 3 ==> a -= 3 a = a * 3 ==> a *= 3 a = a / 3 ==> a /= 3 a = a % 3 ==> a %= 3 4) Unary Operators : Ex : ++ --

+=

-= *= /= %=

a = a + 1 ==> a += 1 ==> a ++ ==> ++ a a = a - 1 ==> a -= 1 ==> a -- ==> -- a 5) Relational Operators : 6) Logical Operators :
7)

== && || !

>

< >= <= !=

, . : ; < > # { [ ( ) ] } ......

Structure of a ‘C’ program : preprocessor commands global declarations main()

{
local declarations ; statements ;

.h> # include “conio. C has case sensitivity.-4- } function(arguments) { local declarations . } ♦ ♦ ♦ ♦ ♦ The ‘C’ program has a free formated structure. All the keywords are defined in lower case. The variables which declared in a block are available in that block only. To use the variable in the entire program with same effect it must be declared as global. where the execution starts.h” # define PI 3. There may be a lot of functions but at least one function must be there that is main(). So better to write entire program in lower case. A program is a collection of functions. Preprocessor commands : The commands which start with a hash(#) symbol are called Preprocessor commands.14159 Global declarations : To use any variable it must be declared with its data type before the first executable statement. Every statement must be terminated with a semicolon . Ex : # include <stdio. statements .

printf(“The marks are %d.H ( CONIO ==> Console Input Output ) Syntax : clrscr().14e-38 to 3.17e4932 Functions :The functions are two types. Note : The function printf() returns an integer value that is the number of arguments given to the statement. To use the function the header file must be included as preprocessor statement. The derived functions are defined by the ‘C’ authors. printf(“ %d %c”. m2. %d. This function’s prototype has defined in the header file CONIO. m1. %f %e %lf %Lf 1) derived functions. This function’s prototype has defined in the header file STDIO. 2) printf() :- This function is used to display the text and the values of variables. . variables) . %d”.17e-4932 to1.H To display the variable’s value the format string must be used. Ex : printf(“ Hello \t World “). j). They defined them in the header files. 1) clrscr() :This function is used to clear the screen. ( STDIO ==> Standard Input Output ) Syntax : printf(“ format string “.17e308 long double 1. 2) user-defined functions.-5Data Types : Type signed char unsigned char Range -128 to 127 0 to 255 occupied bytes 1 1 2 2 4 4 4 8 10 format string %c %c %i %d %o %u %ld %lu %x shortsigned int -32768 to 32767 short unsigned int 0 to 65535 long signed int -2^31 to 2^31 -1 long unsigned int 0 to 2^32 -1 float 3.14e38 double 1.17e-308 to 1. m3 ). k.

printf(“Welcome “) .OBJ FIRST.BAK FIRST.h> # include <conio.C FIRST.-6Remarks :To write any remarks or comments to the statements they must be enclosed with the symbols Ex : /* */ /* sdfjkshadjfsdjkafkjsadjkfhkasdj sdafhasdfhgasdhfgasdgfgasdfhasdfj sdafjksadfjasdkhfjasdhkfjhksda */ Ex Programs : 1) /* My First ‘C’ Program */ # include <stdio. printf(“Bhanodaya “) .C After compilation(Alt-F9) it creates an object file. printf(“Hello” ).h> main() { clrscr() . FIRST. } /* Save this program (F2) as FIRST.EXE Output : Hello Bhanodaya Welcome */ . By saving a modified file it creates a backup file. and an executable file which can be executed at MS-DOS prompt.

The status bar shows online help and the keys information. Then it shows the list of errors or warnings. a status bar at the bottom and a main window to write the programming statements and a sub window which shows the messages. 3) To compile the program select ‘Compile to OBJ’ command from “compile” menu or press Alt + F9.OBJ) and an executable file (. 1) To write a new program select ‘New’ command from “File” menu. Then it shows the submenu which contains some bars and they can be selected using arrow keys. 6) To close the editor select ‘Quit’ command from “File” menu or press Alt + X.-7- TURBO C editor : It is a compiler of C program and it can be also used as an general editor. 5) To seee the output of the execution select ‘User Screen’ command from “Run” menu or press Alt + F5. 2) To save the working program select ‘Save’ command from “File” menu or press F2 and enter a name. If the program is error-free then the compiler creates an object file (. .EXE). 4) To execute the program select ‘Run’ command from “Run” menu or press Ctrl + F9. To enter into editor first change into the directory which contains the software and enter the command TC at the command prompt. The menu bar contains some menu pads and they can be selected by pressing ALT and the highlighted character in the required menu pad. C:\> CD TC C:\TC> tc Then it opens the editor which contains a menu bar at the top.

printf(“Welcome “) .h> # include <conio. printf(“Hello \t “) . } /* Output : Hello Bhanu Welcome */ .h> main() { clrscr() .h> main() { clrscr() . printf(“Hello \t Bhanu \n Welcome “) . printf(“Udaya \n”) .-8Escape Sequences : \0 \t \l \r \n \a \’ \” \\ ==> ==> ==> ==> ==> ==> ==> ==> ==> Null character Tab ( 8 spaces) Line feed Carriage Return New line character ( \n = \l + \r ) Alert (beep sound) Single quotes Double quotes back slash Ex Programs : 2) /* Using Escape Seque nces */ # include <stdio.h> # include <conio. } /* Output : Hello Udaya Welcome */ 3) # include <stdio.

k ) . k. j. b. printf(“\n %5d \t %d”. b = 23456. c.h> # include <conio. # include <stdio. e. } /* Output : 00006 23456 678 */ 6) /* Arithmetic Operati ons */ 6 23456 678 c = 678 . a = 6 . a. b. clrscr() . d. j. c . printf(“\n %05d \t %d”.-9- 4) /* Using Variables # include <stdio.h> main() { int a. k. k. char j = ‘*’ . } /* Output : The value of k is 65 65 A 101 42 The value of j is 42 5) /* 42 * 52 2a */ Formatting the out put */ # include <stdio. a ) . b ) .h> # include <conio. j ) . printf(“\n The value of j is %i %d %c %o %x”. */ printf(“\n The value of k is %i %d %c %o %x”. clrscr() . c ) . f . k. . printf(“\n %05d \t %d”. c. b.h> main() { int k = 65 . j. clrscr() .h> # include <conio. j.h> main() { int a.

c = a + b . m1 = 65. b = 40 . f = a / b .. printf(“The given valu es are %d. given values are 100. /* avg = tot / 3. tot. tot = m1 + m2 + m3 . } /* Output : The The The The The The */ Notes : The Arithmetic operations are three types depend on the types of the operands in the expression. f) . } /* Output : The three subjects marks are 65. m2. d = a .0 . printf(“\n The reminde r is %d”. float avg .h> # include <conio.10 a = 100 . */ printf(“The three subjects marks are %d. %d”. printf(“\n The divisio n %d”. b ) . m2 = 66. m3 ) . clrscr() . a. printf(“\n The additio n is %d”. m3 = 68. avg ) . 66. printf(“\n The product is %d”. printf(“\n The subtrac tion %d”. m2. m3. d) . e = a * b . m1.b . printf(“\n The total %d \t Average %f”.h> main() { int m1. e) . 40 addition is 140 subtraction 60 product is 4000 division 2 reminder is 20 operand1 integer integer real Ex Programs : 7) /* Type casting operand2 integer real real */ result integer real real # include <stdio. avg = (float) tot / 3 . %d. %d”. 68 . a%b) . tot. c) .

printf(“\n %d”. da. printf(“\n %d”. printf(“The Basic Salary %f”. k) .. printf(“\n Net Salary %10. net) . da = bas * 20 / 100 .h> # include <conio. clrscr() .2f”. k --) . k) . k) .33 */ */ Formatting the output of floating point values # include <stdio. pf = bas * 5 / 100 .00 */ q) /* Program to demonstrate the */ # include <stdio. printf(“\n Da %. hra. da. net = bas + da + hra . bas) . hra. k ++ .h> Increment / Decrement operators main() { int k = 5 . pf.h> # include <conio. printf(“\n %d”.1f \t Hra %010.h> main() { float bas.000 Net Salary 7250. clrscr() .. } /* Output : Pf 00250 The Basic Salary 5000.0 Hra 001500. printf(“\n %d”. hra = bas * 30 / 100 . k -.11 The total 8) /* 199 Average 66.0f”.000000 Da 1000. pf) . k = ++k + ++k + ++k .3f \t Pf %5. printf(“\n %d”. 7 7 8 7 5 Output 5 . bas = 5000. --k) . net . k ++). printf(“\n %d”. + +k) .pf . 24 printf(“\n %d”.

k = k++ + ++k + ++k + k++ + k++ . Ex : char c. m2.H Note : To see the entered character the function getche() can be Used. &m2. Syntax: (variable) = getch() . Syntax: scanf(“formatstring” .. &m3). } 38 Notes : scanf() : This function is used to accept the values for the variables while executing the program from keyboard. scanf(“%d%d%d”. . This function’s prototype has defined in the header file STDIO. k) . c = getch(). m3.12 k=5. &(variables) ). But this function does not display the entered character. &a). &m1. scanf(“%d”. This function’s prototype has defined in the header file CONIO. &ch). Ex : i) int a. getch() : This function is used to accept a single character for the variable while executing the program. Note : To accept two or more values with a single scanf() they can be seperated by space or tab or enter key.H The function printf() returns an integer value that is the number of arguments given to the statement. printf(“\n %d”. scanf(“%c”. iii) char ch. ii) int m1. getch() .

} /* Output : Enter any character abcdef You entered the chracter a Enter any character g You entered the chracter g Enter any character You entered the chracter d */ */ */ 10) /* Write a program to calculate the total. getch(). k) . printf(“Enter three subjects marks \n”) . k) .13 - Ex Programs : 9) /* Program to demonstrate the difference between the functions scanf(). clrscr() . k) .h> main() { int m1. &k) . &m3 ) . /* avg = tot / 3. &m2. k = getche().h> # include <conio. clrscr().. printf(“\n\n Enter any character “) . k = getch() . printf(“You entered the character %c”. m2. float avg . printf(“Enter any character “) . . scanf(“%c”. tot. getche(). &m1. tot = m1 + m2 + m3 . printf(“\n You entered the character %c”. m3.h> main() { char k . average of a student’s three subjects marks # include <stdio.0 . printf(“\n\n Enter any character “) . getch() # include <stdio. */ avg = (float) tot / 3 . scanf(“%d%d%d”.h> # include <conio. printf(“\n You entered the character %c”.

k<1 0) . net salary and print all */ Notes : Conditional Statements : In C language the conditional statement returns zero when the condition is false.h> main() { int k = 5 . k+( k==5) ). k>1 0) . m3 ) . m1.h> # include <conio. 68 The total 199 Average 66. %d”. printf(“\n The total %d \t Average %f”. Ex Program : 12) # include <stdio. clrscr() . %d”. calculate da. hra. } %d”.14 printf(“The three subjects marks are %d. k=1 0) .. 5 1 0 6 10 output . pf. %d. %d”.33 */ 11) /* Write a program to accept an employee’s basic salary. %d”. printf(“\n printf(“\n printf(“\n printf(“\n printf(“\n getch() . m2. avg ) . } /* Output : Enter three subjects marks 65 66 68 The three subjects marks are 65. k ). Otherwise it returns a non-zero(1) value when the condition is true. tot. 66. %d”.

} Ex Program : 13)/* Write a program to check whether the given number is zero or not */ # include <stdio. else printf(“The number is not zero “) .h> main() { int k. 1) if. } 14) /* Write a program to check the given number is positive or negative # include <stdio. if(k==0) printf(“The number is zero “) .. 3) conditional operators 1) if . scanf(“%d”. &k) ..15 - Notes : There are three types of conditional statements in ‘C’.h> # include <conio. clrscr() . } else if (condition) { (statements).h> # include <conio. scanf(“%d”.. printf(“Enter any number “) .h> main() { int k . clrscr() . printf(“Enter any number “) . 2) switch. &k) . getch() . */ . else : Syntax : if (condition) { (statements). or } { (statements) .

&a. if(a==b && a==c) printf(“Given all are equal “) . &b. &a.h> # include <conio. else */ . b . else if(k>0) printf(“The number is Positive “). clrscr() . } 15) /* Write a program to find the big number in the given two numbers # include <stdio.. if(a>b) printf(“%d”. clrscr() . else { printf(“\n The big is “) . } 16) /* Write a program to find the biggest number in the given three numbers */ # include <stdio. scanf(“%d%d”.16 if(k==0) printf(“The number is zero “) . getch() . printf(“Enter any three numbers \n”) . } getch() . a) . b) . else printf(“%d”. c . if(a==b) printf(“\n Given both are equl “) . &b) .h> # include <conio.h> main() { int a.h> main() { int a. else printf(“The number is Negative “) . printf(“Enter any two numbers \n”) . b. &c) . scanf(“%d%d%d”.

e. d. a) . else { t=a. clrscr() . &a. &c.h> main() { int a. */ . b. b). if(t>c) t = c .. if(a==b && a==c && a==d && a==e) printf(“\n Given all are equal “) .h> # include <conio. scanf(“%d%d%d%d%d”. e. &b. else if(b>c) printf(“%d”. else printf(“%d”. printf(“\n The biggest is %d”. b. } 17) /* Write a program to find the smallest number in the given five numbers # include <stdio. t) .h> main() { int a. y . } getch( ).17 { printf(“\n The biggest is “) . } getch() . if(t>b) t = b. c. } 18)/* Write a program to find the biggest and smallest numbers in the given five numbers */ # include <stdio.h> # include <conio. d. c. x. if(a>b && a>c) printf(“%d”. t . if(t>e) t = e . &d. &e ) . if(t>d) t = d . c). printf(“Enter any five numbers \n”) .

else y = b. if(x<e) x = e . printf(“\n The biggest is %d”. printf(“\n The smallest is %d”. scanf(“%d%d%d”. m3 ) : printf(“\n The Total %d \t Average %03. printf(“The three subjects marks %d. else if(y>e) y = e .h> main() { int m1. %d”. float avg . scanf(“%d%d%d%d%d”. } getch( ). tot = m1 + m2 + m3 . printf(“Enter any five numbers \n”) . } else . m2.h> # include <conio. printf(“Enter three subjects marks \n”) . m3. if(x<b) x = b. average find the result. &d. &m1. calculate total. tot . } 19) /* Write a program to accept three subjects marks of a student. avg = (float) tot / 3 . &b. else { x = a . tot. m2. else if(y>d) y = d . &a. if(x<c) x = c . else if(y>c) y = c . y = a . if(a==b && a==c && a==d && a==e) printf(“\n Given all are equal “) . &m2. &e ) . &c. %d. &m3 ) .2f”.. clrscr() . division and print all details */ # include <stdio. if(x<e) x = e . if(m1<35 || m2<35 || m3<35) { printf(“\n Result is Fail “) . m1. y) .18 clrscr() . x) . avg ) . printf(“\t division is NIl “) .

case . pf. case : { case (value) : case (value) : (statements) . net salary using the following conditions and print all details if bas >= 10000 10000 > bas >= 5000 5000 > bas >= 2000 bas < 2000 ==> ==> ==> ==> da da da da = = = = 40%. 25%.. if(avg>=60) printf(“Division is I class “) . hra hra hra hra */ = = = = 50%. 35%. else if(avg>=50) printf(“Division is II class”) . 35%... else printf(“Division is III class”) . default : (statements) .h> main() { int k . Ex Programs : 21) # include <stdio. 30%. } break : This keyword stops the execution in the given block and come out. } 20) /* Write a program to accept an employee’s basic salary. statements and looping Statements. 45%.h> # include <conio.19 { printf(“\n Result is Pass \t”) . 40%. Generally this is used in switch. } getch() . pf pf pf pf = = = = 25% 20% 15% 10% net = bas + da + hra .pf Notes : 2) Syntax: switch(variable) switch. calculate da.. hra. (statements) .

} 22) # include <stdio. break . break . scanf(“%d”. clrscr() . case 1 : case 2 : case 3 : case 4 : printf(“\n Number is less than five “) . default : printf(“\n Number is greater than five “) . &k) .. printf(“Enter any number “ ). switch(k) { case 0 : printf(“\n Number is zero “ ). case ‘b’ : case ‘B’ : printf(“\n B for Brave “) . case ‘c’ : . break .h> main() { char k.h> # include <conio. k = getche() . } getch() . case 5 : printf(“\n Number is five “ ). break . printf(“Enter any one of the alphabets “ ). switch(k) { case ‘a’ : case ‘A’ : printf(“\n A for Active “) .20 clrscr() .

scanf(“%d”.21 case ‘C’ : printf(“\n C for Courage “). break . } getch() . Addition “) . &b) . k . &a. printf(“\n\n Select your choice “) . Division “) . a/b). break . case ‘d’ : case ‘D’ : printf(“\n D for Dare “) . scanf(“%d%d”.. printf(“\n”) . } getch() . printf(“\n\n 1.h> # include <conio. } Notes : 3) Conditional Expressions : ( ? : . break . case 2 : printf(“ The subtraction %d”.h> main() { int a. printf(“\n 4. switch(k) { case 1 : printf(“ The addition %d”. ) . break . } 23) # include <stdio. a+b) . clrscr() . default : printf(“ Invalid choice “). a-b) . b. Multiplication “) . default : printf(“\n You are timid “) . printf(“\n 2. &k). Subtraction “) . break . printf(“Enter two numbers \n”) . a*b). case 4 : printf(“ The division %d”. case 3 : printf(“ The multiplication %d”. printf(“\n 3. break .

22 Syntax : (condition) ? (statement1) : (statement2) . &k).. pritnf(“Enter any number “) . getch() . scanf(“%d”. (k==0) ? printf(“Number is zero “) : pritnf(“Number is not zero “) . Ex Programs : 24)/* Write a program to check whether the given number is zero or not # include <stdio. } */ .h> # include <conio.h> main() { int k. clrscr() .

3) . &a. gotoxy(35. printf(“The biggest is %d”. printf(“Welcome “).h> # include <conio.h> main() { clrscr() .H Syntax: gotoxy(column. } .20). gotoxy(70. getch() . getch() . 5).12). printf(“To smile “). t . &b. t = (a>b) ? a : b . c. (t>c)?t:c ). gotoxy(20. gotoxy(50. scanf(“%d%d%d”. This function’s prototype has defined in the header file CONIO.h> # include <conio. &c).h> main() { int a.23 - 25)/* Write a program to find the biggest number in the given three numbers # include <stdio. Generally in MS-DOS mode the screen contains 80 columns and 25 rows. clrscr() . b.. row) . printf(“Enter any three numbers \n”) . Ex Programs : 26) # include <stdio. printf(“Bhanodaya “) . printf(“Hello “). } Notes : gotoxy() : */ This function locates the cursor position to the given place on the screen.

abc : printf(“Welcome “) .. } /* Output : Hello World Welcome To smile Note : Looping Statements : Repeating a block of statements number of times is called Looping. 2) while.. printf(“World “) .24 Notes : goto : This command changes the execution control to the given statement. printf(“Hello “) . 1) do. 3) for.h> # include <conio. Ex Programs : 27) # include <stdio. goto abc . (label) : (statements) . There are three types of looping statements defined in C language. Ex Programs : 28) # include <stdio. Note : The keyword ‘goto’ cn be also used to repeat a block of statements number of times.while. goto end . end : getch() .h> main() { clrscr() . goto xyz . printf(“Go out “) . Syntax: goto (label) .h> */ . xyz : printf(“To smile “) .

} 29) /* Program to display the first 10 natural numbers # include <stdio. k++ .. clrscr() . k=1. .. while() : Syntax : do { (statements). getch() . } */ Notes : 1) do .h> main() { int k .25 main() { abc : printf(“Welcome “) . goto abc . k) .. abc : printf(“%d “.h> # include <conio. } while(condition) . if(k<=10) goto abc .

}while(k<=10) . clrscr() .26 Ex Programs : 30) /* Write a program to display the first 10 natural numbers */ # include <stdio. do { printf(“%d “. }while (k<=10) . printf(“\n The sum is %d”. do { printf(“%d “. n.h> main() { int k . s) . } Notes : . } 31) /* Write a program to display the even numbers upto the given number and find the sum of them */ # include <stdio. getch() . k) . k = 2 . getch() .h> # include <conio. k=1. &n) . k) . scanf(“%d”. k++ . clrscr() .. s += k . s .h> main() { int k. printf(“Enter any number “) . s = 0. k += 2 .h> # include <conio.

k++ . n. while(k<=10) { printf(“%d “. s = 0.h> # include <conio. } getch() . 2) while() : Syntax: while(condition) { (statements). while (k<=10) { printf(“%d “. It executes the conditional statement after executing the statement. k) . } Ex Programs : 32) /* Write a program to display the first 10 natural numbers # include <stdio.27 There is a draw-back in do. s . k = 2 ..h> # include <conio. &n) . clrscr() . clrscr() . s += k . scanf(“%d”. } 33) /* Write a program to display the even numbers upto the given number and find the sum of them */ # include <stdio. while() staement. k) ..h> main() { int k. k=1. printf(“Enter any number “) . k += 2 .h> main() { int k . */ .

/* I method */ */ # include <stdio. s = 0. } if(s==2) printf(“\n Number is Prime “ ). clrscr() . s. scanf(“%d”.h> # include <conio.28 } printf(“\n The sum is %d”. s) . scanf(“%d”.h> main() { int n. else printf(“\n Number is not a Prime “) getch( ). } /* II method # include <stdio. printf(“Enter any number “) . while(k<=n/2) { if(n%k==0) { s++. k. } 34) /* Write a program to check whether the given number is Prime or not Prime Number : A number which is divisible by 1 and itselft. k++ . break . */ . } k++ . getch() . s. &n) . while(k<=n) { if(n%k==0) s++.h> # include <conio. printf(“Enter any number “) ..h> main() { int n. k. &n) . clrscr() . s = 0. k = 2. k = 1.

371. else printf(“\n Number is not a Prime “) getch( ). a /= 10 . int n. 407 # include <stdio. b . r = a % 10 . b = (b*10) + r . } 35) /* Write a program to find the number of digits. Ex: 1. getch( ).h> main() { */ */ . } printf(“\n The number of digits %d”. b = 0. } 36) /* Write a program to check whether the given number is Armstrong or not Armstrong Number : A number which is equal to the sum of the cubes of the digits is called Armstrong Number. printf(“\n The sum of digits %d”.h> # include <math. clrscr( ). 153. s = 0. r . printf(“Enter any big number “ ). n) . while(a>0) { n ++ . scanf(“%ld”.h> # include <conio. sum of digits. n = 0. s += r . printf(“\n The Reverse number %lu”. s) . s.. &a) . b) .29 } if(s==0) printf(“\n Number is Prime “ ). and reverse order of the given number # include <stdio. 370.h> # include <conio.h> main() { long int a.

else printf(“\n The number is not an Armstrong “) . a>=n . b = a . for ( . /* n = 1 . s = 0. iteration ) /* s += r * r * r . printf(“Enter any number “ ). condition .30 int a. getch() . while(a>0) { r = a % 10 . b. clrscr() . scanf(“%d”. &a ). } Ex Programs : 37) /* Write a program to display the odd numbers upto the given number # include <stdio. a /= 10. s = pow(r. r. a . &a) . */ { (statements) . clrscr() . 3). } Notes : 3) for() : Syntax: for ( initialisation .h> main() { int n. printf(“Enter any number “) .. ) { */ . } if(b==s) printf(“\n The number is an Armstrong “) . scanf(“%d”.h> # include <conio. s .

scanf(“%d”. getch() . n) . printf(“Enter any number “) . } 38) /* Write a program to display the even numbers upto the given number and find the sum of them */ # include <stdio. n). s=0 . n<=a . ) { printf(“%d “. printf(“\n The sum is %d”. s . } */ /* s=0. s+=n. } */ for(n=2.31 printf(“%d “. n+=2) printf(“\n %d “ . clrscr() . s += n. n<=a . for( . n+=2) { printf(“%d “ . n ). */ } for(n=1. for(n=2. n += 2 . n+=2 ) printf(“%d “. &a) . n) . . n<=a. n += 2 .h> main() { int a.h> # include <conio. /* n=2. s) . n<=a . getch() .. n) .s=0. s += n . n.

clrscr() .h> # include <conio. s+=n. printf(“\n The sum of natural numbers %d”. a -.. &a) . else odd += n. for(n=1. even=0 . even ) . clrscr() . getch() . even numbers. printf(“\n The sum of even numbers %d”. a>1 .h> # include <conio.32 } 39) /* Write a program to find the sum of natural numbers. odd numbers upto the given number */ # include <stdio. scanf(“%d”. f . /* f = 1 .h> main() { int a. odd=0. &a) . printf(“Enter any number “) . } */ . s.h> main() { long int a. scanf(“%ld”. n<=a . n++) if(n%2==0) even += n. n. } 40) /* Write a program to find the factorial value of the given number n! = n * (n-1) ! */ # include <stdio.) { f *= a . for( . printf(“Enter any number “) . even . s=0. printf(“\n The sum of odd numbers %d” .. odd. odd ) . s) .

k.33 - for(f=1. n. &n) . while( k<= 10 ) { printf(“\n %d x %2d = %3d “. k=1.. n. a>1. getch() . scanf(“%d”. n.h> main() { int k . do { printf(“\n %d x %2d = %3d”. } 42) /* Write a program to display the ASCII chart ASCII ==> American Standard Code for Information Interchange # include <stdio. /* k = 1 .h> # include <conio. clrscr() . k. k<=10. n*k ). */ } for(k=1. f*=a. } while (k<=10) . k. printf(“Enter any number “ ). */ */ /* . k ++ . getch() . k ++ . */ # include <stdio. n*k). } 41) /* Write a program to display the multiplication table of the given number using all types of loopings. k. f) .h> # include <conio. printf(“\n The factorial is %ld” . a--) . n*k ).h> main() { int n. k++) printf(“\n %d x %2d = %3d”.

&n) . /* k=0. scanf(“%d”. k ++ . k. for(k=1. k++) { printf(“\t %d %c”. k<=n. k. */ /* k = 0 . if(k%50==0) getch() . k. k++) { */ */ . } getch() . do { printf(“\t %d %c”. k) . printf(“Enter any number “) .. if(k%50==0) getch() . j .h> # include <conio.h> main() { int n. while(k<=255) { printf(“\t %d %c”. } 43) /* Write a program to display the multiplication table of the given number using all types of Loopings. } for(k=0. k. Ex Programs : 44) # include <stdio. if(k%50==0) getch() . k) . k ++ . k) . } while(k<=255 ) . clrscr() . Notes : Nested Loops : Looping in a loop is called Nesting of Loops. k<=255.34 clrscr() .

k<=n. clrscr() . } getch() . &n) . } /* output : Enter any number 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 45) # include <stdio. j<=k. j .h> # include <conio..h> # include <conio. k++) { printf(“\n “) . } /* output : Enter any number 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 46) # include <stdio. printf(“Enter any number “) . j<=k. scanf(“%d”. for(j=1.35 printf(“\n “) . for(k=1. k. k) . } getch() . j++) printf(“%d “.h> main() { int n. j) . j++) printf(“%d “.h> 5 5 */ */ . for(j=1.

k--) { printf(“\n “) . for(k=n. &n) .j. } /* output : Enter 1 2 3 1 2 1 1 */ any number 4 5 3 4 2 3 1 2 5 47) # include <stdio.&a). j) .h> # include <conio. k.36 - main() { int n.k. . clrscr(). j<=k. for(j=1. j++) printf(“ “) . printf(“Enter any number “) . clrscr() . j . scanf(“%d”.a. k>=1.. for(n=a. printf(“Enter the any number”). } getch() .n>0.h> main() { int n. j++) printf(“%d “.n--) { printf(“\n”). scanf(“%d”. for(j=1. j<n-k.

k--) printf(“%c”. for(n=1. n. printf(“Enter any number “) . } getch().. n.64+k).k++) printf(“%c”. k.h> main() { int a.k>=1. clrscr() . getch() . n++) { for(k=1. scanf(“%d”. for(k=1. &a) . clrscr() .h> # include <stdio.h> # include <conio. for (.64+k).k<=n.37 for(k=1. k++ ) printf(“\n %d x %2d = %3d “. n<=a. } } 49) /* Write a program to display the list of Prime numbers upto the given number */ # include <conio.k++) printf(“ “).k<=2*(a-n)-1. n*k). } /* Output : Enter any number 5 ABCDEDCBA ABCD DCBA ABC AB A CBA BA A */ 48) /* Write a program to display the multiplication tables upto the given number */ # include <stdio.h> . k . k<=10. k=(a==n)?n-1:n.

s=0. The arrays are two types. length and width using arrays */ */ /* Output : Enter box length 10 Enter box width 5 Enter any character * * * * * * Notes : Arrays : An array is a collection of similar data type elements... n++) { for(k=2. k.. The size must be a constant. 13. k<=n/2. 8. printf(“Enter any number “) . 3. 55. k++) if(n%k==0) { s++. * * * * * * * * * * * * * * * * * * * * * */ . } getch() . 21. 2) Multi Dimensional Arrays. In this series every element is the sum of its previous two numbers 52)/* Write a program to display a box with the given character. 1) Single Dimensional Arrays : Syntax : (type) (variable) [size] . . s . &a ) . 5. n<=a. 1. clrscr() . scanf(“%d”. 2. n) . 1. 34. 1) Single Dimensional arrays. n. } if(s==0) printf(“%d “. The indexing is start from 0. To use any array it must be declared with its size and they may be initialized.38 main() { int a. } 50) /* Write a program to display the list of Armstrong Numbers upto the given number */ 51) /* Write a program to display the Fibonacci Series upto the given number Fibonacci Series : 0. break . for(n=2.. It stores the elements in contiguous locations.

for(k=0. printf(“Enter any five numbers \n”) . 67. float bas[] = { 56. 45}.h> main() { int k. k<5.h> # include <stdio. } /* Output: 7 7 7 7 7 */ */ */ 54) /* Write a program to accept 5 numbers and print all in reverse order # include <conio. char ena[] = { ‘a’.45. store 7 in cells and print all # include <conio. k++) printf(“%d “. 45.67. getch() . k<=4. a[5] .. 900. &a[k]) . clrscr() . printf(“Enter any five numbers \n”) . printf(“\n The array elements in reverse order \n”) .h> # include <stdio. a[5] . printf(“\n The array elements are \n”) . getch() . clrscr() . ‘b’. ‘d’ } . for(k=0. a[k]) . a[k]) . } 55) . k++) a[k] = 7 . 567 } .39 Ex: int eno[5] = { 56. 89. for(k=0. k<=4.h> main() { int k. k++) scanf(“%d”. ‘c’. k>=0. Ex Programs : 53) /* Write a program to create a 5 cells single dimensional array. k--) printf(“%d “. for(k=4.

That means a double dimensional array is a collection of single dimensional arrays. “ramakrishna”.a[9]+=a[k].h> # include <stdio.h> main() { int k. &a[k]) . char na[3][20] = { “abcdefgh”. printf(“Enter any nine numbers \n”) .. for(k=0. for(k=0. b[4][5][6][7]. k++) scanf(“%d”. a[k]) .s+=a[k]. .. clrscr() . s. accept 9 cells values. printf(“\n The array elements are \n”) . &a[k]) . a[10] . getch() . k++) printf(“%d “. for(k=0. a[5] . } */ Notes : 2) Multi Dimensional Arrays : Ex: int a[5][3].. } 56) /* Write a program to create 10 cells single dimensional array.a[9]=0. s) . a[k]) . for(k=0. k<10. k<=8. getch() . k++) scanf(“%d”.h> # include <stdio. and find the sum of them # include <conio.h> main() { int k. k++) printf(“%d “. clrscr() .. k<5. k<=4. s=0.40 /* Write a program to accept 5 numbers print all. . printf(“Enter any five numbers \n”) . “Bhanodaya” }. printf(“\n The array elements are \n”) . printf(“\n The sum of elements is %d”.. A multi dimensional array is a collection of another arrays. assign the sum of them to the last cell and print all */ # include <conio.

h> # include <conio. j++) a[k][j] = 7. j++) a[k][j] = (k==j || k+j==4) ? 7 : 0. j<=4. a[k][j] ). a[5][5] . j++) printf(“%d “.41 Ex: The array a[5][3] is a collection of 5 single dimensional arrays with size 3.. k<5. for(k=0. for(k=0. } getch() . k<5. k++) { printf(“\n”). store 7 in all cells and print them as a matrix */ # include <stdio. (same as above) } /* Output : 70007 07070 00700 07070 70007 output: 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 */ . j. j<5. clrscr() .h> main() { int k. k++) for(j=0. k<=4. k++) for(j=0. } 58) main() { (same as above) for(k=0. for(j=0. j<5. Ex Programs : 57) /* Write a program to create a 5x5 double dimensional array.

k<w. j++) a[k][j] = (k==0 || j==0 || k==w-1 || j==len-1) ? ch : 32 . j++) */ . for(j=0. for(k=0.42 59) main() { (same as above) for(k=0. &w ). k++) for(j=0. for(k=0. k<w. printf(“Enter the box width “) . k<5. len. j<5. printf(“Enter a character “). printf(“Enter the box length “).h> # include <conio. k++) { printf(“\n”). w .. a[25][80] . j<len. length and width using arrays # include <stdio. scanf(“%d”. int k. scanf(“%d”. (same as above) } /* Output : 77777 70007 70007 70007 77777 */ 60) /* Write a program to display a box with the given character. j. ch = getche() . j++) a[k][j] = (k==0 || k==4 || j==0 || j==4) ? 7 : 0. clrscr() . j<len. &len) . printf(“\n\n”) .h> main() { char ch. k++) for(j=0.

. j<3. &a[k][j] ). k. b[3][3]. j . j<3. for(k=0. clrscr(). k++) for(j=0. } getch() . j++) c[k][j] = a[k][j] + b[k][j] . c[k][j] ). j<3. k++) for(j=0. j++) scanf(“%d”. */ . for(k=0.h> main() { int a[3][3]. j<3. for(k=0. a[k][j] ). printf(“Enter 9 numbers for firs array \n”) . c[3][3].h> # include <conio. k++) { printf(“\n ³ “). } /* Output : Enter box length 10 Enter box width 5 Enter any character * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 61)/* Write a program to find the addition matrix of two 3x3 matrices # include <stdio. j++) scanf(“%d”. printf(“³”). for(k=0.43 printf(“%c “. printf(“\n The Addition matrix is \n”). printf(“\n Enter 9 numbers for second array \n”) . k<3. for(j=0. &b[k][j] ). k<3. j++) printf(“%3d “. k++) for(j=0. k<3. k<3.

j++) printf(“%3d “. for(k=0. k++) for(j=0. } getch(). t . j++) scanf(“%d”. j<3. j++) for(t=0.44 } getch(). k<3. k++) for(j=0. k++) for(j=0. k<3. j<3. t++) c[k][j] += a[k][t] * b[t][j] . clrscr(). printf(“\n Enter 9 numbers for second array \n”) .h> main() { int a[3][3]. b[3][3]. k++) { printf(“\n ³ “).h> # include <conio. j++) scanf(“%d”. printf(“³”). t<3. for(k=0. j<3. for(k=0. j.. printf(“\n The multiplication matrix is \n”). c[3][3]. c[k][j] ). &a[k][j] ). for(k=0. k. c[k][j]=0 . printf(“Enter 9 numbers for firs array \n”) . k<3. } 62) /* Write a program to find the multiplication matrix of two 3x3 matrices # include <stdio. k<3. j<3. &b[k][j] ). for(j=0. } */ .

printf(“\n You entered the string %s”. Ex : gets(str) . str) . ‘e’. str2[40] .45 Notes: STRINGS : A string is an array of characters. This function’s prototype has defined in the header file STDIO. char na2[6] = “abcde” . “abcd” } . gets() : This function is used to accept the value for a string variable.h> main() { char str[80]. But it does not allow spaces in the string. getch() . Ex programs : 63) # include <stdio. puts() : */ . ‘b’. clrscr(). ‘\0’ } . Syntax : gets(varaible) . scanf(“%s”. printf(“Enter any string “).’c’. Note : The format string for a string variable is %s . } /* Output : Enter any string udaya bhanu You entered the string udaya Notes : scanf() function can accept the string values. “krishna”.H. ( ‘\0’ ==> Null character ) Ex : char na1[6] = { ‘a’.. str) . char names[][] = { “rama”. To avoid this problem gets() can be used. ‘d’. It ends with a null character. char str1[20].h> # include <conio.

- 46 This function is used to display the string value of the variable. This function’s prototype has defined in the header file STDIO.H Syntax : puts(string) ; Ex : puts(“The string is “); puts(str) ; Ex Programs : 64) # include <stdio.h> # include <conio.h> main() { char str[80] ; clrscr() ; printf(“Enter any string “) ; gets(str) ; printf(“ You entered “); puts(str); getch() ; } /* Output : Enter any string udaya bhanu You entered the string udaya bhanu # include <stdio.h> # include <conio.h> main() { char str[80]; int k; clrscr() ; printf(“Enter any string “); gets(str) ; /* k = 0; while(str[k]!=’\0’) k++ ; */ for(k=0; str[k] !=’\0’; k++) ; printf(“\n The length is %d”, k); getch() ; */ 65) /* Write a program to find the length of a string */

- 47 } 66)/* Write a program to change the given string into upper case */ # include <stdio.h> # include <conio.h> main() { char str[80] ; clrscr() ; int k ;

printf(“Enter any string \n”) ; gets(str) ; for(k=0; str[k]!=’\0’; k++) if(str[k]>=97 && str[k]<=122) str[k] -= 32 ; /* if( str[k] >=’a’ && str[k] <=’z’ ) str[k] -= ‘a’ - ‘A’; */ printf(“\n In upper case %s”, str) ; getch() ; } /* Output : Enter any string Udaya Bhanu In upper case UDAYA BHANU */

67)/* Write a program to change the given string into lower case */ 68)/* Write a program to change the given string into Sentence case that means the first character into upper case and the remaining into lower case */ # include <stdio.h> # include <conio.h> main() { char str[80] ; int k ; clrscr() ; printf(“Enter any string “) ; gets(str) ; if(str[0]>=’a’ && str[0]<=’z’ ) str[0] -= ‘a’ - ‘A’ ; for(k=1; str[k]!=’\0’; k++) if(str[k]>=65 && str[k]<=90) str[k] += 32 ;

- 48 printf(“\n In sentence case %s”, str) ; getch() ; } /* Output : Enter any string UDAYA BHANU In upper case Udaya bhanu 69) /* */ Title Case */

Write a program to change the given string into

/* Output : Enter any string UDAYA BHANU In upper case Udaya Bhanu 70) /*

*/ */

Write a program to copy a string to another

# include <stdio.h> # include <conio.h> main() { char s[80], t[80] ; int k; clrscr() ; printf(“Enter the source string to copy “) ; gets(s) ; k=0; while(s[k]!=’\0’) { t[k] = s[k] ; k++ ; } t[k] = ‘\0’ ; printf(“\n The new string is %s”, t) ; getch() ; } 71)/* Write a program to concatenate two strings */ # include <stdio.h> # include <conio.h> main() { char a[80], b[80] ; int k, j ; clrscr() ; printf(“Enter two strings \n”) ;

puts(“Enter your na me “). k++) .h> # include <conio.49 gets(a) . for(j=0.h> # include <conio.. gets(b). b[j]!=’\0’. b) . printf(“Enter any string “) .h> main() { char a[80]. getch() . r. j++) a[k] = b[j] . j=0. } 72)/* Write a program to reverse the given string */ # include <stdio. int k. j++) b[j] = a[k]. while(!kbhit()) { .a[k]!=’\0’. for(k--. a[k]!=’\0’. c. getch(). printf(“\n The concatenated string is %s”. DL. printf(“\n In reverse order %s”. DL = 10000. k--. for(k=0. for(k=0. clrscr() . a) . k++. gets(a) .h> # include <string. k>=0. clrscr(). } /* Output : Enter any string bhanodaya In upper case 73)/* ayadonahb */ */ Program to move the given name around the screen # include <stdio.h> main() { int k. a[k] = ‘\0’ . gets(str). char str[80]. k++) . b[j] = ‘\0’ . b[80] . j . k = strlen(str).

n++) { for(c=0. c. c-=3) { gotoxy(c. clrscr(). r--) { gotoxy(80-k.1). c<=80-k. r<=15. clrscr(). c<k. delay(DL). clrscr(). gets(na). } } } 74)/* Program to fall and replace the given name character by character # include <stdio. delay(DL).r). n<5. DL. } for(c=80-k.. clrscr(). k. str).c>0. printf(“%s”.printf(“ “). clrscr(). } for(r=23. */ . int r. printf(“%s”.h> # include <string. k = strlen(na).r>0. r++) { gotoxy(1.r). delay(DL). DL = 2000. c+=3) { gotoxy(c. } for(c=1.50 for(r=1. n. r++) { gotoxy(c+20. r<=23. printf(“%s”. printf(“Enter your name “).h> main() { char na[80].r). str). c++) { for(r=1.h> # include <conio.23). delay(DL). str).h> # include <dos. str). for(n=1. printf(“%s”. clrscr().

k<n. } } } 75)/* Program to display the given name as a box # include <stdio. n++) . int k. r--) { gotoxy(c+20. } } for(c=0. else if(k==n-1) a[k][j] = str[n-j-1] .r+1). delay(DL). printf(“Enter Your name “) . for(j=0. k++) for(j=0. j++) if(k==0) a[k][j] = str[j] . j<n. else a[k][j] = 32 . printf(“ “). k<n. r>1. na[c]).51 gotoxy(c+20. r-1). k++) { printf(“\n”).h> main() { char str[20]. for(n=0.h> # include <conio. delay(DL). gets(str) . j. j++) printf(“%c “. c++) { for(r=16. a[k][j] ) . for(k=0. j<n. gotoxy(c+20. clrscr( ). r).printf(“%c”. n . a[25][20]. else if(j==n-1) a[k][j] = str[n-k-1] . c<k. str[n]!=’\0’. for(k=0. na[c]). } */ } . else if(j==0) a[k][j] = str[k]. printf(“%c”..

} /* Output : Enter Your name Bhanodaya B h a n o d a y a h a n • o d a y a y a d o n a h B y a d n a h */ .52 getch() ..

second) . length means the number of characters. Syntax : strupr(string) . Syntax : strrev(string) . Syntax : strcat(destination. second) . Syntax : strcpy(target. 2) strupr() : This function changes the given string into uppercase characters. 5) strcat() : This function adds two strings. source) . 3) strlwr() : This function changes the given string into lowercase characters.53 Notes : STRING.h> # include <string.H 1) strlen(): This function returns an integer value that is the length of the string. Syntax : strlwr(string) . 6) strrev() : This function change the given string into reverse order. Syntax : int strcmp(first. Syntax: int strlen(string) . Ex Programs : 77) /* Program to demonstrate the library functions of STRING.H # include <stdio. 7) strcmp() : This function compares two strings and returns zero when both are same..h> main() { */ . 4) strcpy() : This function copies the string to another string.H functions : To manipulate the strings the C authors designed some functions in the header file STRING.h> # include <conio.

printf(“\n The length of string is %d”. printf(“\n In Lower case %s”. clrscr() . strlwr(a) ). printf(“\n In reverse order %s”. gets(a) .54 char a[80].. b[80] . b) . a). strupr(a)) . printf(“\n In Upper case %s”.b) ). b) . printf(“Enter a string “) . printf(“\n The given string is %s”. strcmp(a. strcpy(b. strlen(a) ) . printf(“\n The difference is %d”. } /* Output : Enter a string Bhanodaya The given string is Bhanodaya The length of string is 9 In Upper case BHANODAYA In Lower case bhanodaya The new string is bhanodaya In reverse order ayadonahb The difference is 1 */ . getch(). strrev(b). a) . printf(“\n The new string is %s”.

The functions are two types. or 1) Function definition. 1) Derived functions. In the parenthesis there may be arguments with their data types. and it should be followed by parenthesis.h> void first(void) . 2) User-defined functions. } void fourth(void) { . 1) Functions declarations. void third(void) { printf(“\n This is in third function “) . /* Function /* Function (functionname) (arguments) } These functions are three types. calling */ (datatype) definition */ { (statements) . The derived functions are provided by the C writers and they defined them in header files.h> # include <conio. 3) Arguments with return value functions. If the function does not return any value it must be declared as void. 1) No arguments with no return value functions.55 FUNCTIONS : The function is a piece of code. 2) Arguments with no return value functions. In the function declaration and/or in the definition the name must be follow the return data type. These functions are used to reduce the repetition of coding. 3) Function definition.. To use these functions the header file must be included at the top of the program. The default return type is int. 2) Functions calling. We can create our own functions. 2) Function calling. Syntax: (datatype) (functionname) (argumentstype) . To write any function there are three steps. /* Function declaration */ functionname(arguments) . 1) No arguments with no return value functions : 78) # include <stdio. void second(void) .

printf(“\n This is in main again “) .h> void replicate(void) { int k. } void first(void) { printf(“\n This is in first function “) . } void main() { clrscr() . replicate() . for(k=1. printf(“This is in Main “) ..56 printf(“\n This is in fourth function “) . k++) printf(“*”) . } void main() { clrscr() . first() . second() . k<=50. third() . } /* Output : This is in Main This is in first function This is in second function This is in third function This is in fourth function This is in mainagain 79) # include <stdio. getch() . fourth() . printf(“\n Hello \n”) .h> # include <conio. } void second(void) { printf(“\n This is in second function “) . */ .

printf(“\n Hello \n”) . replicate(60.h> void replicate(int n. replicate() . getch().’@’) . getch(). replicate(40.. } /* Output : Hello Bhanodaya */ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Welcome . k++) printf(“%c”. replicate(30.’*’) . printf(“\n Welcome \n”) . printf(“\n Bhanodaya \n”) . for(k=1.’#’) .h> # include <conio. int ch) { int k. k<=n.’%’) . printf(“\n World \n”) .57 replicate() . } /* Output : Hello World Welcome */ /* 2) Arguments with No Return value functions 80) # include <stdio. printf(“\n Welcome \n”) . replicate() . ch) . } void main() { clrscr() . replicate(50.

} void add(int m. } void subtr(p. &b ) . subtr(a. k=m-n. } void div(int k. int b) { printf(“\n The multiplication %d”. } . a*b) . printf(“\n The subtraction %d” . void subtr(int.h> # include <conio. b) . b) . getch() . { int r . k) . scanf(“%d%d”. (float)k / j ) . int) . b) . q . int n) { int k.h> void add(int.58 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ */ 81) # include <stdio. printf(“\n The addition is %d”. clrscr() . &a. r) . div(a. int j) { printf(“\n The division %f”. b .. q) int p. printf(“Enter any two numbers \n”) . r=p-q. } void main() { int a. mult(a. add(a. b) . void mult(int a. int) .

int y) { int z . c ).h> # include <conio. return p. getch() .. } 3. &a. int q) { return p-q . c = add(a. int ) .b) ).b) ) . mult(a. printf(“Enter any two numbers \n” ). int main() { int a. } int mult(int. c = mult(a. return z . c . printf(“\n The subtraction %d”. int n) { int p = m / n.h> int add(int x. int b) { return a*b. return 0. subtr(a. printf(“\n The division %d”. div(a. scanf(“%d%d”. int div(int. } int div(int m.b) . &b) . printf(“\n The addition is %d”. z = x + y. b) . int) . b. clrscr() .59 /* 82) # include <stdio. Arguments with Return value functions */ . } int subtr(int p. printf(“\n The multiplication %d”.b) ). } int mult(int a.

. &k) . } void looping() { char ch = ‘y’ . return 0. } } */ */ */ void condition() { int a. /* Arguments with Return value functions int subtr(int. void condition(void) . switch(k) 5) 6) 7) 8) . . scanf(“%d”.60 - 83) Important : /* Program to demonstrate all types of functions # include <stdio. printf(“1. Addition “ ). scanf(“%d%d”.h> # include <conio. int) . 15). /* Arguments with No return value functions */ void add(int. . int div(int.h> /* No arguments with No return value functions void looping(void) . &b) . int) . gotoxy(30. printf(“ Do you want to cotinue (y/n) “) . printf(“3.. 22) . Subtraction “) . gotoxy(30. gotoxy(30. . printf(“2. Division “) . gotoxy(30. condition() . void mult(int. k. 10 ). &a. int) . printf(“4. gotoxy(30. gotoxy(45. int main() { looping() . gotoxy(10. ch = getche (). int) . while(ch==’y’ || ch==’Y’) { clrscr() . printf(“\n Enter any two numbers \n”) . Multiplication “) . printf(“Enter Your choice “) . b.

default : printf(“ Invalid choice “) . Ex : # define PI 3. break . int b) { printf(“\n The addition %d”. r) . } void mult(int p.y. case 2 : printf(“ The subtraction %d”. } int subtr(int x. subtr(a. c=a/b. a+b) . int q) { int r .h” # define A 1. case 3 : mult(a.h> # include “conio. } } void add(int a. break . break .7 . int b) { int c . r=p*q.b) ) .. break. printf(“The multiplication %d”. return z . case 4 : printf(“ The division %d”. return c.61 { case 1 : add(a. div(a. } Notes : PREPROCESSOR COMMANDS : The commands which start with hash (#) are called Preprocessor Commands. } int div(int a. int y) { int z = x .14159 # include <stdio.b) ).b ). b) .

pf(msg) . sf(“%f”. There are two types to include the files. area®) . 1) # include < name > This type of command includes the file which is located in the specified directory. pf(“\n The perameter of circle %f”. cls . Ex Programs : 84) # include <stdio. 2) # include “ name “ This type of command includes the file which is located in the current directory and/or in the specified directory. .h> # define MN main() # define pf printf # define cls clrscr() # define wait getch() # define sf scanf # define PI 3. wait .h> # include <conio.. pf(“\n The area of circle is %f”. &r) . } Notes : include : This command is used to include the files which contains the definition of functions.62 - define : This command is used to define our own constants and macros. per® ) .14159 # define msg “Enter the radius of circle “ # define area(g) PI*g*g # define per(g) 2*PI*g MN { float r . These specifications are set by selecting the Directories command from Options menu.

c . printf(“Enter any two numbers \n” ). int n) { int p = m / n. printf(“\n The multiplication %d”. b) . printf(“\n The division %d”. int div(int.b) ).b) ). &a. mult(a. return p.63 Ex Programs : 85) /* Save this progam as SUB. } int subtr(int p. int b) { return a*b.C # include <stdio. } int div(int m.b) ) . int main() { int a. clrscr() . return 0.C # include “sub. c = add(a. c = mult(a.b) . c ). z = x + y. int) . } */ */ . int y) { int z . b. printf(“\n The addition is %d”. } int mult(int a.h> # include <conio. div(a. return z . } /* Save and execute this program as MAIN. printf(“\n The subtraction %d”. &b) . scanf(“%d%d”. subtr(a. int q) { return p-q .c” int mult(int.. int ) .h> int add(int x. getch() .

The default type is auto. k ). 3) static. k ).h> # include <conio. But by using the storage clauses they can be stored either in memory devices or in CPU registers.. } 87) # include <stdio.h> main() { int k . 1) auto. clrscr() . 4) extern 1) auto : This keyword is an option. { int k = 5 .h> main() { auto int k. 456 20 5 output : } printf(“\n %d”. These storage clauses are 4 types. Ex Programs : 86) # include <stdio. The variable which declared in a block that is available in that block only. . getch() . It stores the variable in memory device. The variables initialisation depends on this type.64 - Notes : STORAGE CLAUSES : The declared variables are generally stored in memory devices. } printf(“\n %d”. { auto int k = 20 .h> # include <conio. It assigns a junk(garbage) value to the variable. 2) register.

k) . k<=5. } /* Output : 4567 20 20 20 20 20 Notes : 2) register : This keyword stores the variable in CPU registers.. k<=100.. k += 3 . k) . printf(“%d \n”. Ex Programs 88) # include <stdio. In CPU registers it can’t store more and big values like floats. for(k=1. It can store Only chars and integers. clrscr() . doubles. getch() .. printf(“ %d “ . k). k ).h> main() { register int k .h> # include <conio. for(k=1.65 clrscr() . k++) display() . } */ Notes : . k++) printf(“%d “. } display() { auto int k = 20 . These variables are also local that means the variables which declared in a block are available in that block only. Generally these variables are used to generate the looping statements.etc. getch() . It assigns a junk(garbage) value to the variable. printf(“%d \n”.

The variable’s value can be changed in the functions. It takes the previous value when the controller entered into that block again.h> main() { static int k . k). k<=5. k += 3 . It initialises the variable as 0.. But it does not destroy the variable’s value when end that block. */ .66 3) static : This keyword stores the variable in memory device. clrscr() . k++) display() . It initialises the variable as 0. The scope of variable is local that means the variables which declared in a block are available in that block only. k) .h> # include <conio. This variable is declared before the main() . printf(“ %d “ . printf(“%d \n”. getch() . for(k=1. } /* Output : 0 20 23 26 29 32 Notes : 4) extern : This keyword stores the variable in memory device. The scope of variable is global. } display() { static int k = 20 . Ex Programs : 89) # include <stdio.

disp3() .h> # include <conio. k) . } disp1() { printf(“\n %d”. k ). k += 3 . printf(“\n %d”. } /* Output 0 5 20 8 8 */ .h> int k . main() { clrscr() . k) . } disp2() { int k = 20 . getch() . k=5.. printf(“\n %d”. disp2() .67 Ex Programs : 90) # include <stdio. k ). printf(“\n %d”. } disp3() { printf(“\n %d”. k ). disp1() .

5600 } . struct employee emp = { 5. char ename[80]. }. . This is created with the keyword struct. char ename[80]. } emp = { 5. float bas. 5600 } .(element) Ex : 1) struct employee { int eno. } emp = { 5. 5600 } .. 3) struct { int eno. } .68 Notes : STRUCTURES : A structure is a collection of variety of data type elements. struct (name) { (elements declaration). float bas. float bas. “rama”. “rama”. char ename[80]. “rama”. (variable). 2) struct employee { int eno. struct (name) (variable) .

char title[40] .h> # include <conio. float price .h> main() { struct book { int pages. printf(“\n The title of book %s” . scanf(“%d” . printf(“\n The price of book %. struct book bk . bk. struct book bk = { 500. } 92) # include <stdio. printf(“Enter the number of pages of book “) . float price . printf(“\n The number of pages %d” .price ) . bk. } . printf(“\n The price of book %.h> main() { struct book { int pages. printf(“\n The number of pages %d” . getch( ). bk. char title[40] .title ) . scanf(“%f”.price ) . } .2f” . clrscr() . “Let us C”.h> # include <conio.price).. &bk.pages ) . scanf(“%s”. bk. getch( ). printf(“Enter the book title “) .pages ) . bk. bk.pages ) . &bk. } . clrscr() . 175 } .title ). printf(“\n The title of book %s” .69 Ex Programs : 91) # include <stdio. bk. printf(“Enter the cost of book “) .2f” .title ).

struct book bk[3] = { { 500. bk.h> # include <conio.price ). main() { struct book bk = { 500.title. k++) { printf(“\n\n The title of book %s” . “Let us C”. } . printf(“\n The price of book %.price ) . bk[k]. for(k=0. . } } 94) /* Structures to Functions */ # include <stdio. float price . bk. char title[40] . bk[k]. “Let us C”. char title[40] . display(bk. } . { 800. bk[k]. dispstrct(bk) .2f” . 235 } . 350 } } . 175 } . clrscr() . float price . getch( ).pages ) . int k . 175 }. clrscr() .title ).h> main() { struct book { int pages.70 - 93) /* Array of Structures */ # include <stdio. “Datastructures through C and C++ “. { 350. getch( ). “Graphics under C”. k<3.pages. printf(“\n The number of pages %d” .h> # include <conio.h> struct book { int pages..

b. printf(“\n The shop address %s “. printf(“\n The shop name %s” . sh. printf(“\n\n The book title %s” . { 1200. char na[].pn.pages).bk. printf(“\n The cost of each pen %. printf(“\n The number of pages in the book %d”. float price .name ) .2f” . struct pens { int qty. na). sh. pr ). “Reynolds”.price ). printf(“\n The number of pages %d”. 12 } } .title). “Test your skills in C”. printf(“\n\n The name of pen %s” .pn. float pr) { printf(“\n The title of book %s”.bk. printf(“\n The quantity of pens %d” . 175 } .street). struct shop { char name[40]..pages ) . } dispstrct(struct book b) { printf(“\n The title of book %s”. sh.71 } display(int pg. “Chintal “. getch() . main() { struct shop sh = { “Udaya Book world “. sh. float price . printf(“\n The cost of each book %. clrscr() . b. printf(“\n The price of book %f”. } .qty ) . } . } 95) /* Structures in structure # include <stdio. sh.bk. sh. } */ . struct book bk .price ) . street[40] .price ) . } .sh. char name[40]. b.h> struct book { int pages.2f” . struct pens pn .h> # include <conio.pn. { 50.name ) .name ) . sh. printf(“\n The price of book %f”. printf(“\n The number of pages %d”. char name[40]. pg).

72 96) /* Write a program to interchange the values of two variables */ ..

that is an integer. a ==> 5 &a ==> 65500 *(&a) ==> 5 ==> ==> Address of Value at address of These address value can be stored in another variable. Ex Programs : 97) # include <stdio. p = &k. &q. printf(“\n The value at address %u is %d”. q) . getch() .h> main() { int k. p) . printf(“\n The value at address of %u is %u”. clrscr() . & * Ex : int a = 5. *(&k) ) . **q . printf(“\n The value at address %u is %u”. *(&q) ) . p. q. &k). printf(“\n The value at address %u is %d”. printf(“\n The address of k is %u”. q = &p.To access with pointers we have to use two new operators. Every cell has a unique address in memory. *p ). printf(“The value of k is %d”. &p. printf(“\n The value of P is %u”. This address is a number. printf(“\n The value at address %u is %u”. But that must be a pointer variable of the same type to the variable. To use any variable it must be declared with its data type before the first executable statement. By declaring a variable the compiler reserves the required space in memory between 64 kb and 128 kb. k = 7.h> # include <conio. The variables stored in the memory can be accessed with their addresses using pointers.73 - Notes : POINTERS This topic is the most important in C.. } /* Output : The value of k is 7 The address of k is 65524 The value at address 65524 is 7 . printf(“\n The value of q is %u”. &k. printf(“\n The integer value is %d”. *q). *(*q) ) . k) . *p. *(&p) ) .

Ex Programs : 98) /* Write a program to interchange the values of two variables using functions A) CALL BY VALUE : # include <stdio.h> # include <conio. */ */ . sizeof©. float b. That means the address of the variable can be send to change the value. clrscr() .74 The value of P is 65524 The value at address 65522 is 65524 The value at address 65524 is 7 The value of q is 65522 The value at address of 65520 is 65522 The value at address 65522 is 65524 The integer value is 7 98) # include <stdio. clrscr() . char c. *bp . If the value of these variables changed in the functions that does not effected to the function calling. *ap . sizeof(ap) ) . b=30 . printf(“\n The size of char is %d and Pointer is %d”..h> main() { int a=20. sizeof(b).h> main() { int a. sizeof(a). printf(“\n The size of int is %d and pointer is %d”.h> # include <conio. sizeof(cp) ) . } Notes : Pointers in Functions : The functions can be send some arguments to the definition and may take a return value.sizeof(bp) ) . *cp . printf(“\n The size of float is %d and Pointer is %d”. To change the value after calling the function the values can be send by reference. getch() .

swap(&a. printf(“\n\n After swapping \n”). b) . } swap(int *x. x. printf(“ a = %d \t b = %d”.h> main() { int a=20. } swap(int x. *x. printf(“\n\n After swapping in main \n”) . a. printf(“a = %d \t b = %d”. printf(“ x = %d.. *x = *y . y = t .75 printf(“The initial values are \n”) . int y) { int t . int *y) { int t . clrscr() . t = x. y = %d”. printf(“ x = %d. b) . a. } /* Output : a = 20 x = 30 a = 20 b = 30 y = 20 b = 30 */ B) CALL BY REFERENCE : # include <stdio. a. t = *x. getch() . swap(a.h> # include <conio. x = y . getch() . printf(“\n\n After swapping in main \n”) . printf(“a = %d \t b = %d”. b=30 .&b) . printf(“\n\n After swapping \n”). *y). y). printf(“The initial values are \n”) . a. *y = t . b) . b) .b) . y = %d”. } /* Output : a = 20 x = 30 b = 30 b = 30 . printf(“ a = %d \t b = %d”.

} getch () . k<5. clrscr() . a+k ) . n . n) .h> main() { int *a.h> # include <conio. for(k=0. getch() . a[5] = { 10. n) . 50 } . 30. printf(“Enter the number of elements you want “) . display(a. scanf(“%d”. } accept(int *p. The array elements can be accessed using the address of array and the index value. &n) . printf(“the value is %d %d %d %d”. k++) { printf(“\n the addres of %d element %u %u”. k[a] ) . 20.h> # include <conio. int n) { . Ex Programs : q) # include <stdio. } q) # include <stdio. The address of first element is considered the address of the array. clrscr() . accept(a.76 a = 30 b = 20 */ Notes : Arrays with Pointers : An array is a collection of similar data type elements mentioning with a single variable. *(k+a). *(a+k). a[k].h> main() { int k. 40. &a[k]. k..

} Notes : malloc() : .h> # define M 5 main() { int a[M] . k<n. p+k) . M) .h> # include <conio. accept(a.. display(a. printf(“\n The elemetns are \n”) . n) . printf(“\n The array elements are \n”) . for(k=0. int k) { int n . getch() . for(k=0. } accept(int *p. k) . d[n] ) . p+n) . n<k. int n) { int k. } q) # include <stdio. for(n=0. printf(“Enter %d numbers \n “. printf(“Enter any %d numbers \n”. } display(int d[]. n<k. int k) { int n . k++) printf(“%d “. k++) scanf(“%d”. for(n=0. k<n. } display(int *p.77 int k. n++) scanf(“%d”. *(p+k) ) . M) . n++) printf(“%d “. clrscr() .

exit(0) . number ) . Note : If the compiler failed to allocate the required space this function returns a NULL value. } printf(“Enter %d numbers \n”. for(k=0. p+k ). getch() .h> # include <conio. k++) scanf(“%d”. Ex Programs : q) # include <stdio. printf(“\n The elements are \n”) .h> # include <stdlib. k++) printf(“%d “. k<n.s+=p[k].h> # include <alloc.h> # include <alloc. } q) /* Write a program to sort the given numbers using Linear Sort technique # include <stdio.H There is an another function to allocate memory. k. s=0.h> main() { int *p. This function’s prototype was defined in the header file ALLOC.. (pointer) = (pointertype) calloc( size . for(k=0.h> */ . n. k<n. s) . The malloc() assigns junk values the allocated space. s.h> # include <conio. &n) . n) . scanf(“%d”. But calloc() assigns 0 to the allocated space. clrscr() . Syntax: (pointer) = (pointertype) malloc( size * number) . printf(“\n The sum is %d”. p[k] ).78 This function is used to allocate the required space in the memory while executing the program. p = (int *) malloc(sizeof(int) * n) . printf(“Enter the number of elements “) . if(p==NULL) { printf(“Unable to allocate the required space “) . that is calloc().

n) . for(k=0. &n) . int n) { int k . j++) if(a[k]<a[j]) { t = a[k] . for(k=0. /* bubble(p. printf(“Enter the number of elements “) . k<n. */ display(p. k++) printf(“%d “. n) . } accept(int *p. exit(0). } display(int *p. n) .79 - main() { int *p.. } } . j<n. printf(“Enter %d numbers \n”. for(k=0. int n) { int k. getch() . k<n. a[j] = t . a[k] = a[j] . p+k) . t . n. *(p+k) ) . int n) { int k . p = (int *) malloc(2*n) . scanf(“%d”. } accept(p. linear(p. k++) scanf(“%d”. if(p==NULL) { printf(“\n Unable to allocate the required space “) . j. } /* linear(int a[]. n) . clrscr() . k<n. n) . printf(“\n The elements are “ ) . k++) for(j=k.

k<n-1. *(a+j) = *(a+j+1) . j<n. } } /* bubble(int *a. j++ ) if(*(a+j) < *(a+j+ 1) ) { t = *(a+j) . *(a+j) = t . for(k=0. k<n. t . for(k=0. k. j<n-1. k++) for(j=0. } } bubble(int a[]. j. int n) { int k. int n) { int t. a[j] = a[j+1] . j++) if(*(a+k) < *(a+j) ) { t = *(a+k) . *(a+j+1) = t . j . *(a+k) = *(a+j) . int n) { int t.. for(k=0. k++) for(j=k. k++) for(j=0. a[j+1] = t . j . j<n-1. j++) if(a[j] < a[j+1] ) { t = a[j] . } } */ Notes : String Pointers to Functions : Ex Programs : . k.80 */ linear(int *a. k<n-1.

} */ substr(char str[]) { char dst[80]. *(dst+j+1) = ‘\0’. j++. } *(dst+j) = c. k=1. j++. c = *str. int k.h> <string.81 q) /* # # # # Moving the given string as a Banner include include include include <stdio. . ch . } dst[j++] = ch . gotoxy(20. k++. 12).dst). dst[j] = ‘\0’ . clrscr(). delay(100).. } getch(). printf(“%s”. } /* substr(char *str) { char *dst. while(str[k]!=’\0’) { dst[j] = str[k] . int i=1. j . strcpy(str.h> <conio.h> <dos.str). while(*(str+i)!=’\0’) { *(dst+j) = *(str+i). while(!kbhit()) { substr(str). ch = str[0] .h> */ void main() { char *str = “ Bhan odaya is a super hero “. c. i++. j=0. j=0.

main() { struct book bk = { 500. } Notes : Structures with Pointers : The elements of a structure variable can be accessed using a period operator between the variable and the element. } q) /* Structure Pointers to Functions # include <conio.price ) ..00 } . disppntr(&bk) . dst) . (variablepoitner) -> (element) Ex Programs : q) # include <conio. } .pages ) . b->title) . b->pages ) . char title[40]. */ . bk. struct book *b . printf(“\n The book title %s” . printf(“\n The size of structure pointer %d”. printf(“\n\n The size of structure variable %d”. 175. To access the elements using the address of the variable the ‘ -> ‘ operator must be used. b = &bk .00 } .h> struct book { int pages. dispvar(bk) . printf(“\n The number of pages %d”.h> # include <stdio. 175.82 strcpy(str. char title[40]. printf(“\n\n The book title %s” . bk. float price . clrscr() . clrscr() .h> # include <stdio.title ) . } . getch() . float price . main() { struct book bk = { 500.2f “ . printf(“\n The cost of book %. printf(“\n The number of pages %d” . “Pointers in C”.h> struct book { int pages.2f “. printf(“\n The cost of book %.sizeof(bk) ). b->price ) . bk. sizeof(b) ) . “Pointers in C”.

2f “ .. } Notes : FILE HANDLING IN C Using C programs the data files and text files can be manipulated.. . To close the file the function fclose() must be used. printf(“\n The size of structure variable %d” . printf(“\n The cost of book %. fp = fopen(“data. printf(“\n The number of pages %d”. (filepointer) = fopen(“filename”. b->pages ) . fclose() : The file. } dispvar(struct book bk) { printf(“\n The book title %s” . “mode” ). b->title) . b->price ) . bk. bk.txt”.“w” ==> To create the file and store the data “a” ==> To add the data to the file “r” ==> To read the data from the file Ex : FILE *fp.83 getch() . In C programs the file pointer where the file stored can be found. modes : . printf(“\n The size of structure pointer %d”. printf(“\n The number of pages %d” . which opened in memory must be closed to avoid the data corruption. bk. Syntax : FILE * (filepointer) .pages ) . “w”).2f “. printf(“\n The cost of book %.. } disppntr(struct book *b) { printf(“\n\n The book title %s” . To use any file it must be loaded into memory. sizeof(bk) ). fopen(): This function is used to open the required file in the required mode and returns the file pointer where the file stored.title ) .price ) . sizeof(b) ) . If it is unable to open in the given mode it returns a constant value NULL.

} fclose(fp) . */ . exit(0) . if(fp==NULL) { printf(“\n Unable to open the given file “) . fp) . Syntax : char variable = fgetc(filepointer) . fgetc() : This function is used to read a character to the variable from the file pointer. char ch . } while(1) { ch = getchar(). fputc(): This function is used to store the characters in the data file opened in the filepointer. Ex : fclose(fp) .h> main() { FILE *fp . Syntax : fputc(char. fp = fopen(“DATA”. fp) . # include <stdio. fputc(ch. Ex : fputc(ch. Ex Programs : q) /* Write a program to create a text file DATA and store some data.h> # include <conio.84 - Syntax : fclose(filepointer). filepointer) . Ex : ch = fgetc(fp) . “w”) . if(ch==EOF) break .h> # include <stdlib..

h> # include <conio. fp = fopen(“DATA”.h> main() { FILE *fp .h> main() { FILE *fp . if(fp==NULL) { printf(“\n File not found “) .h> # include <conio. putchar(ch) . if(ch==EOF) break . “r”) . } fclose(fp) . */ . clrscr() . clrscr() .h> # include <stdlib.85 } q) /* Write a program to read the text from the file DATA # include <stdio. getch() . } */ q)/* Write a program to read the text from the file DATA display in Upper case characters # include <stdio.h> # include <stdlib. fp = fopen(“data”. if(fp==NULL) { printf(“\n File not found “) . } while(1) { ch = fgetc(fp). char ch . exit(0) . exit(0) . “r”) . char ch ..

h> main() { FILE *fs. *ft . fs = fopen(src. } while(1) { ch = fgetc(fs). } q)/* Write a program to read the text from the file DATA copy to another in file in upper case characters # include <stdio. } fclose(fp) . src) . clrscr() . char ch. printf(“Enter the source file to copy “) . exit(0) . if(ft==NULL) { printf(“\n Unable to open the target file “) . */ . if(fs==NULL) { printf(“\n Source File not found “) .. exit(1) .86 } while(1) { ch = fgetc(fp). *trg .h> # include <stdlib. if(ch>=97 && ch<=122) ch -= 32 . trg) . if(ch>=97 && ch<=122) ch -= 32 . scanf(“%s”. scanf(“%s”. “r”) .h> # include <conio. *src. “w”) . if(ch==EOF) break . ft = fopen(trg. } printf(“\n Enter the target file to copy “) . putchar(ch) . if(ch==EOF) break . getch() .

Syntax : fscanf(filepointer. “w”) .h> # include <stdlib. eno. variables). } while(ans==’y’ || ans==’Y’) .. char ans=’y’ . int eno.DAT”. &eno. } Notes : fprintf() : This function stores the data in the file. Ex : fprintf(fp.87 putchar(ch) . ena.DAT. float bas . if(fp==NULL) { printf(“\n Unable to open in the required mode “) . “%d%s%f”. } fclose(fs) . &bas). ft) . fscanf() : This function reads the data from the file. “format string”. char ena[40]. Ex : fscanf(fp. Syntax : fprintf(filepointer. ena. basic salary and store all in the data file*/ # include <conio. clrscr() . bas). variables). “%d %s %f”. fp = fopen(“EMP. Ex Programs : q)/* Write a program to create a data file EMP.h> main() { FILE *fp . fputc(ch. name. getch() . accept employee number. fclose(ft) . exit(0) .h> # include <stdio. “format string”.

net) . printf(“\n Basic Salary %. scanf(“%d”. “%d%s%f”. ena) . printf(“Do you want to continue “) .h> # include <stdio.88 { printf(“\n Enter Employee Number “) . ena. ena. &bas) > 0) { da = bas * 20 / 100 . hra. } while( fscanf(fp.DAT. eno) . pf ) . if(fp==NULL) { printf(“\n File not found “) . net salary and print all # include <conio. float bas. printf(“\n\n Employee Number %d”. } getch(). printf(“ Enter Employee Name “) . eno.DAT”. net = bas + da + hra .. ena) . } q)/* Write a program to read the records from the data file EMP.2f”.2f”. exit(0) . scanf(“%s”. fp = fopen(“EMP.2f”. fprintf(fp. pf. printf(“\n Net Salary %. hra. hra = bas * 30 / 100 . scanf(“%f”. &bas) .pf . “r”) . bas) .2f \t Pf %. printf(“\n Da %.2f \t Hra %. pf = bas * 10 / 100 . &eno) . clrscr() . char ena[40]. calculate da. */ . ans = getche() . net . int eno. pf. hra. da.h> main() { FILE *fp . bas) . printf(“\n Employee Name %s”. printf(“ Enter Basic Salary “) . da. “\n %d %s %f”. &eno.h> # include <stdlib.

(numberofvariables). } . fp). { int eno. Syntax: fwrite ( (pointer to variable). clrscr() . exit(0) . } do { . “wb”) . fp = fopen(“empbn. float bas . sizeof(emp). (size of variable). char ena[20]. if(fp==NULL) { printf(“Unable to open the data file “) .89 getch() . 1.. FILE *fp . (numberofvariables).h> struct employee fread(&emp. (size of variable). Ex: fwrite( &emp.h> # include <conio. Ex: Ex Programs : q) # include <stdio.dat”. } } Notes : fwrite() : This function is used to store the structures in the file in binary mode. char ans . fread() : This function is used to read the structures from the file in binary level. Syntax: fread ( (pointer to variable).(filepointer) ). fp).(filepointer) ). main() { struct employee emp . 1. sizeof(emp).

emp. float bas . scanf(“%f”.bas) . “rb”) . printf(“Do you want to add more “) . char ena[20].. scanf(“%s”. } } q) # include <stdio. fp = fopen(“empbn.h> # include <conio. fp) > 0) { printf(“\n\n Employee Number %d”. 1. getch() .h> . main() { struct employee emp .ena) . } while( fread(&emp. sizeof(emp). &emp. printf(“\n Employee Name %s”. printf(“\n Basic Salary %. exit(0) .dat”. fwrite(&emp. 1. FILE *fp . ans = getche() . emp.90 printf(“\n\n Enter employee Number “) . &emp. } while(ans==’y’ || ans==’Y’) . } .bas) .eno) . printf(“Enter Basic Salary “) . } q) # include <stdio. fp) .h> # include <conio. clrscr() . scanf(“%d”. printf(“Enter Employee Name “) . emp.eno) . sizeof(emp). if(fp==NULL) { printf(“Unable to open the data file “) .2f”. emp.h> struct employee { int eno.ena) .

Here argv is the an integer value and it is the number of arguments passed and *args[] is an array of strings contain the arguments Syntax: main(int argv.. emp. emp.2f”.The main() is also a function and some arguments can be passed to it from the MS-DOS prompt. emp.bas) . fs) > 0) { printf(“\n\n Employee Number %d”. float bas . emp. fprintf(ft.bas).ena) . 1. FILE *fs. printf(“\n Basic Salary %. exit(1).dat”. printf(“\n Employee Name %s”. } } Notes : COMMAND LINE ARGUMENTS : The arguments can be passed to any function when they are calling. getch() .91 - struct employee { int eno. clrscr() . if(fs==NULL) { printf(“Unable to open the data file “) . char ena[20]. } while( fread(&emp. emp.ena. “rb”) . fs = fopen(“empbn.   Here we have to use the words argv. sizeof(emp). if(ft==NULL) { printf(“\n Unable to open the target file “) .eno) . “w”) . *ft . emp.dat”.eno. char *args[]) Ex Programs : . } ft = fopen(“emp. } . exit(0) . “\n %d %s %f”. main() { struct employee emp . args[] .

exit(0) . if(ch==EOF) break . } while(1) { ch = getchar() . if(argv!=2) { printf(“\n Invalid number of arguments “) . } fp = fopen(args[1]. k<argv. char *args[]) { int k .h> void main(int argv.h> # include <stdlib. } /* Save this program as ARG. FILE *fp .EXE. for(k=0.OBJ and an executable file ARG. argv) . exit(1) . printf(“\n The arguments are \n”) . args[k] ) . if(fp==NULL) { printf(“\n Unable to create the given file “) .. .h> void main(int argv. “w”) . char *args[]) { char ch. which can be executed at MS-DOS prompt [prompt] ARG abc lkj xyz mnb q)/* */ */ Program to create a file which can create text file # include <stdio. k++) printf(“\n %s”.C After compilation it creates an object file ARG. printf(“\n The number of arguments given %d”.92 q) /* This program demonstrates about the arguments passed to the function main() */ # include <stdio.

[prompt] q) /* CREATE (filename) */ */ Program to create a file which can read a text file # include <stdio. } printf(“\n 1. putchar(ch) . exit(0) .C After compilation it creates an executable file CREATE. char *args[]) { char ch. } } . file created \n”) . } fp = fopen(args[1]. if(ch==EOF) break . FILE *fp . } while(1) { ch = fgetc(fp) . exit(1) .%s”.h> # include <stdlib.. if(fp==NULL) { printf(“\n File not found . which can be executed at MS-DOS prompt. } /* Save this program as CREATE. if(argv!=2) { printf(“\n Invalid number of arguments “) .EXE. fp) . “r”) .h> void main(int argv.93 fputc(ch. args[1] ) .

- 94 -

/* Save this program as SHOW.C After compilation it creates an executable SHOW.EXE [prompt] SHOW (filename) q)/* */

Program to create a file which can copy the text from a file to another */

# include <stdio.h> # include <stdlib.h> void main(int argv, char *args[]) { FILE *fs, *ft ; char ch ; if(argv!=3) { printf(“Invalid number of arguments “); exit(0) ; } fs = fopen(args[1], “r”) ; if(fs==NULL) { printf(“\n Unable to open the source file “) ; exit(1) ; } ft = fopen(args[2],”w”) ; if(ft==NULL) { printf(“\n This target file %s not found “, args[2] ) ; exit(2) ; } while( (ch=fgetc(fs)) != EOF) fputc(ch, ft) ; fclose(fs) ; fclose(ft) ; printf(“\n 1. File copied “) ; }

/*

Save this file as

FLCP.C */

[prompt] FLCP (source) (target)

- 95 -

Notes : random() : This function returns the value which is between 0 and 1 less than the given number. This function’s prototype has defined in the header file STDLIB.H Syntax : random(n); This function returns any number from 0 to n-1 . textattr() & textbackground() : This function changes the text color to the given color.

- 96 -

GRAPHICS Generally the screen is in textmode. It can be changed by the display adopters. There are a number of adopters like VGA, CGA,EGA, ... The normal text mode has 25 rows and 80 columns. It can be changed using the MODE command in MS-DOS.Using C programs we can design some graphics by changing the screen from text mode to graphics mode. initgraph() : This function changes the screen from text mode to graphics mode. But here we have to mention the display driver and mode. To use this command the file EGAVGA.BGI must be in the current directory. This function’s prototype has defined in the header file GRAPHICS.H Syntax: initgraph( (address of driver), (address of mode), (path to file) ); Ex : int gdr = DETECT, gmd; initgraph(&gdr, &gmd, “C:\TC” ); detectgraph() : This function detects the using graphic driver and modes. It assign the values to the variables. Syntax : detectgraph( (address of graphic driver), (address of graphic mode) ); Ex : int gdr, gmd; detectgraph(&gdr, &gmd); closegraph() : This function closes the graphics mode and changes to text mode. Syntax: closegraph(); setcolor() : This function changes the displaying color of screen. In VGAHI we can use 16 colors. The color can be mentioned as integer. Syntax: setcolor(integer) ; Ex : setcolor(RED) ; setcolor(4) ; setbkcolor() : This function changes the background color of screen .

Syntax : line(x1. Ex : line(200. x2. 360. y2) .300. line() : This function displays a line between the given coordinates. setbkcolor(15). 100 ). color) . y1. arc() : . st-angle. y2 ). Ex : lineto(400. 5) . 240. y-coordinate. y. Syntax : circle(x. lineto() : This function displays a line to the given coordinates from the current coordinates. 200. 370 ). 240. Ex : ellipse( 320. 240.. Syntax : putpixel(x-coordinate. end-angle.x-radius. circle() : This function displays a circle with the given center coordinates and the radius. 600) . radius) . Ex : circle(320. 240. 100) . y-radius ).97 Syntax : setbkcolor(integer) . Syntax : lineto(x2. Ex : putpixel(320. Ex: setbkcolor(YELLOW) . 0. ellipse() : Syntax : ellipse(x. y. putpixel() : This function highlights the pixel(picture element) at the given co-ordinates to the given color .

45. y. 5 ). 440. 240. . Syntax : settextstyle( font. 200 ) . direction. end-angle. x2. st-angle. settextstyle() : This function sets the displaying text style . Syntax : outtextxy(x. Ex : rectangle( 200. 320 ). outtextxy() : This functions displays the given text at the given coordinates with the setted style. radius ).. size ). text) . y. 135. Ex : arc(320. rectangle() : Syntax : rectangle( x1.98 Syntax : arc(x. fonts : direction : HORIZ_DIR. VERT_DIR size : Ex : settextstyle( 3. 0. 150. y1. y2 ).

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->