C Language

May 11, 2017 | Author: Sandeep Sandy | Category: N/A
Share Embed Donate


Short Description

Download C Language...

Description

-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.

-2-

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++, ...

-3-

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) Scientific applications, ii) Business applications, iii) Graphical applications (Ex: WINDOWS ), iv) System programs, v) Operating Systems (Ex: UNIX) , ... Character Set : alphabets constants, digits ==> variables, ==> special symbols keywords

statements, ==> 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.

-4-

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. 1) 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

+=

-= *= /= %=

-5-

4) Unary Operators :

++

--

Ex : a = a + 1 ==> a += 1 ==> a ++ ==> ++ a a = a - 1 ==> a -= 1 ==> a -- ==> -- a 5) Relational Operators :

==

6) Logical Operators :

&&

> < >= # { [ ( ) ] } ......

Structure of a ‘C’ program : preprocessor commands global declarations main() { local declarations ; statements ; } function(arguments) { local declarations ; statements ; } ♦ The ‘C’ program has a free formated structure. ♦ Every statement must be terminated with a semicolon ; ♦ A program is a collection of functions. There may be a lot of functions but at least one function must be there that is main(), where the execution starts. ♦ C has case sensitivity. All the keywords are defined in lower case. ♦ So better to write entire program in lower case.

-6-

Preprocessor commands : The commands which start with a hash(#) symbol are called Preprocessor commands. Ex : # include # include “conio.h” # define PI 3.14159 Global declarations : To use any variable it must be declared with its data type before the first executable statement. 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.

-7-

Data Types : Type signed char unsigned char

Range occupied bytes format string -128 to 127 1 %c 0 to 255 1 %c

shortsigned int -32768 to 32767 %o %x 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.14e-38 to 3.14e38 double 1.17e-308 to 1.17e308 long double 1.17e-4932 to1.17e4932

2

%i

2 4 4 4

8 10

%d

%u %ld %lu %f

%e %lf %Lf

Functions :The functions are two types. 1) derived functions, 2) user-defined functions. The derived functions are defined by the ‘C’ authors. They defined them in the header files. To use the function the header file must be included as preprocessor statement. 1) clrscr() :This function is used to clear the screen. This function’s prototype has defined in the header file CONIO.H ( CONIO ==> Console Input Output ) Syntax : clrscr(); 2) printf() :This function is used to display the text and the values of variables. This function’s prototype has defined in the header file STDIO.H To display the variable’s value the format string must be used. ( STDIO ==> Standard Input Output ) Syntax : printf(“ format string “, variables) ; Ex : printf(“ Hello \t World “); printf(“ %d %c”, k, j);

-8-

printf(“The marks are %d, %d, %d”, m1, m2, m3 ); Note : The function printf() returns an integer value that is the number of arguments given to the statement.

-9-

Remarks :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 # include main() { clrscr() ; printf(“Hello” ); printf(“Bhanodaya “) ; printf(“Welcome “) ; } /* Save this program (F2) as FIRST.C After compilation(Alt-F9) it creates an object file, and an executable file which can be executed at MS-DOS prompt. By saving a modified file it creates a backup file. FIRST.C FIRST.BAK FIRST.OBJ FIRST.EXE Output : Hello Bhanodaya Welcome

- 10 -

*/

- 11 -

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

- 12 -

Escape 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 Sequences */ # include # include main() { clrscr() ; printf(“Hello \t “) ; printf(“Udaya \n”) ; printf(“Welcome “) ; } /*

Output :

Hello Udaya Welcome */ 3) # include # include main()

- 13 -

{ clrscr() ; printf(“Hello \t Bhanu \n Welcome “) ; } /*

Output :

Hello Bhanu Welcome */

4) /* Using Variables */ # include # include main() { int k = 65 ; char j = ‘*’ ; clrscr() ; printf(“\n The value of k is %i %d %c %o %x”, k, k, k, k, k ) ; printf(“\n The value of j is %i %d %c %o %x”, j, j, j, j, j ) ; } /* Output : The value of k is 65 65 A 101 42 The value of j is 42 42 5) /*

*

Formatting the output # include # include

52 2a */ */

- 14 -

main() { int a, b, c ; clrscr() ; a = 6 ; b = 23456; printf(“\n %05d \t %d”, a, a ) ; printf(“\n %05d \t %d”, b, b ) ; printf(“\n %5d \t %d”, c, c ) ; }

c = 678 ;

/* Output : 00006 6 23456 23456 678 678 */ 6)

/*

Arithmetic Operations */

# include # include main() { int a, b, c, d, e, f ; clrscr() ; a = 100 ; b = 40 ; c = a + b ; d = a - b ; e = a * b ; f = a / b ; printf(“The given values are %d, %d”, a, b ) ; printf(“\n The addition is %d”, c) ; printf(“\n The subtraction %d”, d) ; printf(“\n The product is %d”, e) ; printf(“\n The division %d”, f) ; printf(“\n The reminder is %d”, a%b) ; }

- 15 -

/*

Output :

The The The The The The

given values are 100, 40 addition is 140 subtraction 60 product is 4000 division 2 reminder is 20

*/ Notes : The Arithmetic operations are three types depend on the types of the operands in the expression. operand1 operand2 result integer integer integer integer real real real real real Ex Programs : 7) /* Type casting

*/

# include # include main() { int m1, m2, m3, tot; float avg ; clrscr() ; m1 = 65; m2 = 66; m3 = 68; tot = m1 + m2 + m3 ; /* avg = tot / 3.0 ; avg = (float) tot / 3 ;

*/

printf(“The three subjects marks are %d, %d, %d”, m1, m2, m3 ) ; printf(“\n The total %d \t Average %f”, tot, avg ) ; }

- 16 -

/* Output : The three subjects marks are 65, 66, 68 The total 199 Average 66.33 8)

/* */

*/

Formatting the output of floating point values

# include # include main() { float bas, da, hra, pf, net ; clrscr() ; bas = 5000; da = bas * 20 / 100 ; hra = bas * 30 / 100 ; pf = bas * 5 / 100 ; net = bas + da + hra - pf ; printf(“The Basic Salary %f”, bas) ; printf(“\n Da %.1f \t Hra %010.3f \t Pf %5.0f”, da, hra, pf) ; printf(“\n Net Salary %10.2f”, net) ; } /*

Output :

The Basic Salary 5000.000000 Da 1000.0 Hra 001500.000 Net Salary 7250.00 */

Pf 00250

- 17 -

q) /* Program to demonstrate the Decrement operators */

Increment /

# include # include main() { int k = 5 ; clrscr() ; Output 5 printf(“\n %d”, k) ; k ++ ; 7

printf(“\n %d”, ++k) ;

7

printf(“\n %d”, k++);

8 printf(“\n %d”, k) ; k -- ; 7

printf(“\n %d”, k--) ;

5 printf(“\n %d”, --k) ; k = ++k + ++k + ++k ;

24 printf(“\n %d”, k) ;

k=5; k = k++ + ++k + ++k + k++ + k++ ; printf(“\n %d”, k) ; getch() ; }

Notes : scanf() :

38

- 18 -

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

- 19 -

Ex Programs : 9) /* Program to demonstrate the difference between the functions scanf(), getche(), getch()

*/

# include # include main() { char k ; clrscr(); printf(“Enter any character “) ; scanf(“%c”, &k) ; printf(“You entered the character %c”, k) ; printf(“\n\n Enter any character “) ; k = getche(); printf(“\n You entered the character %c”, k) ; printf(“\n\n Enter any character “) ; k = getch() ; printf(“\n You entered the character %c”, k) ; 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, average of a student’s three subjects marks */

- 20 -

# include # include main() { int m1, m2, m3, tot; float avg ; clrscr() ; printf(“Enter three subjects marks \n”) ; scanf(“%d%d%d”, &m1, &m2, &m3 ) ; tot = m1 + m2 + m3 ; /* avg = tot / 3.0 ; */ avg = (float) tot / 3 ; printf(“The three subjects marks are %d, %d, %d”, m1, m2, m3 ) ; printf(“\n The total %d \t Average %f”, tot, avg ) ; } /* Output : Enter three subjects marks 65 66 68 The three subjects marks are 65, 66, 68 The total 199 Average 66.33

*/

11) /* Write a program to accept an employee’s basic salary, calculate da, hra, pf, net salary and print all */ Notes : Conditional Statements : In C language the conditional statement returns zero when the condition is false. Otherwise it returns a non-zero(1) value when

- 21 -

the condition is true. Ex Program : 12) # include # include main() {

output

int k = 5 ; clrscr() ; printf(“\n printf(“\n printf(“\n printf(“\n printf(“\n getch() ; }

%d”, %d”, %d”, %d”, %d”,

k ); k10) ; k+(k==5) ); k=10) ;

1 0 10

5 6

- 22 -

Notes : There are three types of conditional statements in ‘C’. 1) if, 2) switch, 3) conditional operators 1) if ... else : Syntax : if (condition) { (statements); }

if (condition)

{ or

(statements);

}

else { (statements) ; } Ex Program : 13)/* Write a program to check whether the given number is zero or not */ # include # include main() { int k; clrscr() ; printf(“Enter any number “) ; scanf(“%d”, &k) ; if(k==0) printf(“The number is zero “) ; else printf(“The number is not zero “) ; }

getch() ;

- 23 -

14)

/* Write a program to check the given number is positive or negative */

# include # include main() { int k ; clrscr() ; printf(“Enter any number “) ; scanf(“%d”, &k) ; if(k==0) printf(“The number is zero “) ; else if(k>0) printf(“The number is Positive “); else printf(“The number is Negative “) ; getch() ; } 15)

/* Write a program to find the big number in the given two numbers */

# include # include main() { int a, b ; clrscr() ; printf(“Enter any two numbers \n”) ; scanf(“%d%d”, &a, &b) ; if(a==b) printf(“\n Given both are equl “) ;

- 24 -

else { printf(“\n The big is “) ; if(a>b) printf(“%d”, a) ; else printf(“%d”, b) ; } getch() ; } 16) /* Write a program to find the biggest number in the given three numbers*/ # include # include main() { int a, b, c ; clrscr() ; printf(“Enter any three numbers \n”) ; scanf(“%d%d%d”, &a, &b, &c) ; if(a==b && a==c) printf(“Given all are equal “) ; else { printf(“\n The biggest is “) ; if(a>b && a>c) printf(“%d”, a) ; else if(b>c) printf(“%d”, b); else } getch() ;

printf(“%d”, c);

- 25 -

} 17)

/* Write a program to find the smallest number in the given five numbers */

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

- 26 -

{ int a, b, c, d, e, x, y ; clrscr() ; printf(“Enter any five numbers \n”) ; scanf(“%d%d%d%d%d”, &a, &b, &c, &d, &e ) ; if(a==b && a==c && a==d && a==e) printf(“\n Given all are equal “) ; else { x = a ; y = a ; if(x bas 15% bas < 10%

>= 10000

==> da = 40%,

hra = 50%,

bas >= 5000

==> da = 35%,

hra = 45%,

>= 2000

==> da = 30%,

hra = 40%,

pf =

2000

==> da = 25%,

hra = 35%,

pf =

- 28 -

net = bas + da + hra - pf

*/

Notes : 2)

switch... case :

Syntax: switch(variable) { case (value) : case (value) :

(statements) ; (statements) ;

default : (statements) ; } break : This keyword stops the execution in the given block and come out. is used in switch..case statements and looping Statements. Ex Programs : 21) # include # include main() { int k ; clrscr() ; printf(“Enter any number “ ); scanf(“%d”, &k) ;

Generally this

- 29 -

switch(k) { case 0 : printf(“\n Number is zero “ ); case 1 : case 2 : case 3 : case 4 : printf(“\n Number is less than five “) ; break ; case 5 : printf(“\n Number is five “ ); break ; default : printf(“\n Number is greater than five “) ; } getch() ; } 22) # include # include main() { char k; clrscr() ; printf(“Enter any one of the alphabets “ ); k = getche() ; switch(k) { case ‘a’ : case ‘A’ : printf(“\n A for Active “) ;

- 30 -

break ; case ‘b’ : case ‘B’ : printf(“\n B for Brave “) ; break ; case ‘c’ : case ‘C’ : printf(“\n C for Courage “); break ; case ‘d’ : case ‘D’ : printf(“\n D for Dare “) ; break ; default : printf(“\n You are timid “) ; } getch() ; } 23) # include # include main() { int a, b, k ; clrscr() ; printf(“Enter two numbers \n”) ; scanf(“%d%d”, &a, &b) ; printf(“\n\n 1. Addition “) ; printf(“\n 2. Subtraction “) ; printf(“\n 3. Multiplication “) ; printf(“\n 4. Division “) ; printf(“\n\n Select your choice “) ; scanf(“%d”, &k);

- 31 -

printf(“\n”) ; switch(k) { case 1 : printf(“ The addition %d”, a+b) ; break ; case 2 : printf(“ The subtraction %d”, a-b) ; break ; case 3 : printf(“ The multiplication %d”, a*b); break ; case 4 : printf(“ The division %d”, a/b); break ; default : printf(“ Invalid choice “); } getch() ; } Notes : 3) Conditional Expressions : ( ? : ; ) Syntax : (condition) ? (statement1) : (statement2) ; Ex Programs : 24)/* Write a program to check whether the given number is zero or not */ # include # include main() { int k; clrscr() ; pritnf(“Enter any number “) ; scanf(“%d”, &k); (k==0) ? printf(“Number is zero “) : pritnf(“Number is not zero “) ; getch() ; }

- 32 -

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

- 33 -

clrscr() ; gotoxy(20, 3) ; printf(“Hello “); gotoxy(70, 5); printf(“Bhanodaya “) ; gotoxy(35,12); printf(“Welcome “); gotoxy(50,20); printf(“To smile “); getch() ; }

Notes : goto : This command changes the execution control to the given statement. Syntax: goto (label) ; (label) : (statements) ; Ex Programs : 27) # include # include main() { clrscr() ; printf(“Hello “) ; printf(“World “) ; goto abc ; printf(“Go out “) ; xyz : printf(“To smile “) ;

- 34 -

goto end ; abc : printf(“Welcome “) ; goto xyz ; end : getch() ; } /* Output : Hello World Welcome To smile

*/

Note : Looping Statements : Repeating a block of statements number of times is called Looping. There are three types of looping statements defined in C language. 1) do..while, 2) while, 3) for. Note : The keyword ‘goto’ cn be also used to repeat a block of statements number of times. Ex Programs : 28) # include main() { abc : printf(“Welcome “) ; goto abc ; } 29) /* Program to display the first 10 natural numbers # include # include

*/

- 35 -

main() { int k ; clrscr() ; k=1; abc : printf(“%d “, k) ; k++ ; if(k
View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF