Вы находитесь на странице: 1из 16

C Language -------------Language:-----------Communication or interaction between user and the system.

1) Machine level language 2) Assembly level language 3) High level language History of C language:--------------------------C Language was developed by Dennies ritche in 1970's at bell lab's in U.S.A . BCPL (Basic combined programming language) was developed by Ken thompson in 1960's Structure of the C program ===================== 1) Pre processor directives 2) Variable declaration 3) Main function 4) Other fuctios. 1) Pre processor directives:=================== 1) #include #include<Header file name> stdio.h conio.h 2) # define #define variable name constant value eg:- #define pi 3.14 3) #if a=10 b=5 #if a>b 2) Variable declaration:=================

variable :- It is a storage space inside the memory. syntax:....; -------variable name rules:============== 1) Must start with an alphabet. 2) Special characters are not allowed. 3) Only one special character is allowed ie., underscore( _ ). 4) Blank spaces are not allowed. 5)Variable name should not be a Reserved word. 6) First letter not a number and remain perhaps. eg:- variable name True / False ------------------------------sum T 1sum F ab_cd T emp@no F emp no F emp_no T int F tot1 T variable declaration:-----------------------ex:- int a=10; int a; int sum; int na me; 3)Main fuction:----------------Body of the program must write in main fuction only. main() { statement -1; statement-2; ................. } Data types:-----------int char float data type variable name1,variable name2,..........

structure of the program:================= #include<stdio.h> #define pi 3.14 #if a>b Global variable declaration main() { local variable declaration; statement-1; ................ } other functions; printf():-------This is used for to print the user defined data or the value in the variable on the o/p console. syntax:------printf("Any user defined text control strings escape sequence characters ", vari able name1,varibale name2...); eg:- printf("Welcome to c language"); printf( THe sum is %d",sum); control strings:========== control string ----------------%d %f %c %lf %s %u meaning ---------For integer values For float values For character Double or long float For strings For unsigned int.

escape sequence characters:=================== character ----------\n \t \r Meaning ----------Next line HorizentalTab To print the beging of the line .

\v

Vertical tab

1)declare the constant value 10 to a integer variable and print it on the monito r. 2) To print the user defined text on the o/p consol e 3) float value declaration and print it. 4) Strings 5) Characters. scanf():==== This function is used for to read the data from the key board. syntax:===== scanf(" control string escape sequence characters", &variable name1, &variable name2,...............); eg:scanf("%d",&sno); eg:--1) W.A.P for to read sno,sname and marks in 3 subjects , find the total and prin t the sno,sname and total. 2)w.a.p. for to read a sales person number, sales person name, no.of items sold and rate per item and calcualte the sales amount and print the sales amount. samt= nois* rpi; 3) C tokens:-----------Reserved words:------------------Operator:----------It is used for to perform the operation between differnt operands. In 'C' Language mainly we have 7 types of operators are there those are given b elow. 1) Arithmatic operators:-----------------------------These operators are used for to perform arithmatic operations. Operator -----------+ Meaning -----------Addition

* / % a=7; a/b b=3; ==> 2

Subtraction Multiplication Modulue( Dividend). Reminder

a%b ==> 1 2) Logical operators:------------------------Operator Meaning ---------------------&& Logical and | | Logical or ! Logical not 3) Asignment operator( = ):----------------------------------This operator is used for to asign the right side value to the left side variable . variable name=value; eg:sum=0; 4) equl to operator( == ) :------------------------------This operator is used for to verify the right side variable valu e with the left side variable value. eg:---n==sum; 5) Increment operator( ++ ):---------------------------------This operator is used for to increment the 1 value to the existed variab le value. syntax:-------variablename++; In this increment operator again devided into two ways. i) Pre increment operator. ii) Post increment operator. i) Pre increment operator:-------------------------------This operator increment the variable after that assign the value. syntax:-------++variable name; eg:int a=10,b; b=++a; b value is 11;

ii) post increment operator:--------------------------------This operator first assigns the value then increment the varibale. syntax:variablename ++; eg:int a=10,b; b=a++; b value is 10 6) Decrement operator ( -- ) :-----------------------------------This operator is used for to decrement 1 value to the existed variable. syntax:variable name--; In this again i) pre decrement and ii) Post decrement. In the case of predecrement first decreases 1 value to the existed varib le value then assign the value to the left side variable. syntax:--------variable name= --variable name; eg:int a=10,b; b=--a; The b value is 9;

Where as in the post decrement first assigns the existed varible value t hen decrement. syntax:variable name= variablename --; eg:- int a=10,b; b=a--; The b value is 10; UPDATABLE INCREMENT OPERATORS:-------------------------------------------------------Operator -----------+= - = *= /= %= a=10; a+=2; ==> a-=2; a*=2; a/=2; a%=2; ==> control structures:---------------------12 ==> ==> ==> 0 8 20 5 Meaning -----------Increment right side value and assigns left. Decrement right side value and assigns left. Multiply the right side value and assigns left. Devide the left side with right and assigns left. Do and take reminder

if condition:-----------1) Simple if:syntax:if(condition) statement; if the condition is true then statement is execute. eg:----> W.A.P for to find the wheather the given number is absolute number or not. 2) If else syntax:-------if( condition-1) statement-1; else statement-2; 1)W.A.P for to find wheather the given number is even or odd 2) W.A.P for to find wheather the given number is +ve number or not 3) nested if:========= if( condition-1) statement-1; else if(condition-2) statement-2; else if(condition-3) statement-3; 1) W.A.P for to read a Student number,sname, marks in 3 subjects and find wheath er student passed in first class or second class or third class or fail. 2)W.A.P for to read a consumer's number,consumer's name, current meter reading, previous meter reading and find the net amount of consumer by using the followin g table. nou=cmr-pmr; if nou>=300 net amount= noi*4;

if nou>=200 and <299 net amount=noi*3;

if nou>=100 and <199 net amount=noi*2; 4) Compound if:--------------------syntax:-------if( condition-1) { statement-1; statement-2; statement-3; .................... .................... } else if( condition-2) { statement-1; statement-2; statement-3; ..................... } else if(condition-3) { statement-1; statement-2; statement-3; ..................... } Eg:----e. Empno, ename,bsal, and find the net salary by using the following tabl hra --8% 6% 4% ta -6% 4% 2% pf --3% 2% 1% it -2% 1% nil

bsal da ------ -->10000 10% >8000 >5000 8% 6%

netsal= gross - bsal; gross= da+hra+ta+pf+it; switch condition:--------------------In this, the existed variable value is match with the value1 th en execute the statement1 and terminate from the from the loop. else ie., does n ot match with value1 and if it matches value2 then statement2 will be execute. None of the value is match then execute the default statement ie., statement-n. The break statement is used for to terminate from the loop. switch(Variable_name) {

case value1:Statement1; break; case value2:Statement2; break; case value3:Statement3; break; ....................................... ...................................... default: statement-n; } Eg:---1) W.A.P for to print the days depending on the given input. 1-sunday,2-monday,3-tuesday,4-wednes day,5-thursday,6-friday,7-saturday. 2) W.A.P for to print the appropriate colours depending on the given character. v-Violet, i-Indigo, b-Blue, g-Green, y-Yellow, o-Orange,, r-Red. Loops:-------Loop is nothing but execution of block of statements. In C language we have mainly 3 kinds of loops are there. Those are given below. 1) While loop:----------------The while loop checks the condition first, if the condition is true then execute the block of statements. syntax:-------while( condition ) { statement-1; statement-2; statement-3; } eg:--1) W.A.P for to print the first 10 natural numbers by using while loop. 2) W.A.P for to print the first 10 natural numbers in the reverse order by using while loop. 3) W.A.P for to print the first ten even numbers by using while loop. 4) W.A.P for to print the first ten even numbers in the reverse order by using while loop. 5) W.A.P for to print the first ten odd numbers by using while loop. 6) W.A.P for to print the first ten odd numbers in the reverse order by using while loop. 7) W.A.P for to print the sum for the first 10 natural numbers by using while loop. 8) W.A.P for to print the sum for the following series. 1^2+2^2+3^2+.............

9) W.A.P for to find the factorial value for the given number by using while loo p. eg:- 4 => 4*3*2*1=>24 10) wap for to find the sum of the each digit for the given number. eg:- 786=>7+8+6=>21. 11) W.A.P for to print the reverse number for the given number eg:- 687=> 786. 12) W.A.P for to find wheather the given number is pallendrome or not. pallendrome:- If the reverse number is equal to the given number then that number is called pallendrome number. eg:- 8998 13) W.A.P for to find wheather the given number is amstorng number or not. Amstrong:-Sum of the cubes of the each digit is equal to the given number then that number is called amstrong number. eg:-153 => 1^3 + 5^3 + 3^3 =>1 + 125 + 27 => 153. 14) W.A.P for to find wheather the given number is strong number or not. Strong number:- Sum of the factorials of the each digit is equl to the gi ven number then that number is called stro ng number. eg:- 145 => 1! + 4! + 5! => 1+24+120 => 145 Do-while loop:----------------In the do-while, execute the statements first after that checks the cond ition.If the condition is true then again execute the block of statements.This p rocess is continue until the condition is false. In Do-while either condition is true or false, at least once execute the block of statements. Where as in the while loop if the condition is true then only execute th e statements or else it is not possible. Syntax:-------Do { Statement-1; Statement-2; Statement-3; ..................... } eg:--1) W.A.P for to print the first 10 natural numbers by using Do-while loop. 2) W.A.P for to print the first 10 natural numbers in the reverse order by using Do-while loop. 3) W.A.P for to print the first ten even numbers by using Do-while loop. 4) W.A.P for to print the first ten even numbers in the reverse order by using Do-while loop. 5) W.A.P for to print the first ten odd numbers by using Do-while loop.

6) W.A.P for to print the first ten odd numbers in the reverse order by using Do-while loop. 7) W.A.P for to print the sum for the first 10 natural numbers by using Do-whil e loop. 8) W.A.P for to print the sum for the following series. 1^2+2^2+3^2+............. 9) W.A.P for to find the factorial value for the given number by using Do-while loop. eg:- 4 => 4*3*2*1=>24 10) wap for to find the sum of the each digit for the given number. eg:- 786=>7+8+6=>21. 11) W.A.P for to print the reverse number for the given number eg:- 687=> 786. 12) W.A.P for to find wheather the given number is pallendrome or not. pallendrome:- If the reverse number is equal to the given number then that number is called pallendrome number. eg:- 8998 13) W.A.P for to find wheather the given number is amstorng number or not. Amstrong:-Sum of the cubes of the each digit is equal to the given number then that number is called amstrong number. eg:-153 => 1^3 + 5^3 + 3^3 =>1 + 125 + 27 => 153. 14) W.A.P for to find wheather the given number is strong number or not. Strong number:- Sum of the factorials of the each digit is equl to the gi ven number then that number is called stro ng number. eg:- 145 => 1! + 4! + 5! => 1+24+120 => 145 For loop:---------This is also used for to execute the block of statements. In this loop t he declared variables are intialized in the intialization section first. After t hat checks the condition.If the condition is true then execute the block of stat ements.Then either increment or decrement the variable.This process is continue until the condition is false. Syntax:-------For(Intialization;Condition;Increment/Decrement) eg:--1) W.A.P for to print the first 10 natural numbers by using For loop. 2) W.A.P for to print the first 10 natural numbers in the reverse order by using For loop. 3) W.A.P for to print the first ten even numbers by using For loop. 4) W.A.P for to print the first ten even numbers in the reverse order by using For loop.

5) W.A.P for to print the first ten odd numbers by using For loop. 6) W.A.P for to print the first ten odd numbers in the reverse order by using For loop. 7) W.A.P for to print the sum for the first 10 natural numbers by using For loop. 8) W.A.P for to print the sum for the following series. 1^2+2^2+3^2+............. 9) W.A.P for to find the factorial value for the given number by using For loop. eg:- 4 => 4*3*2*1=>24 10) wap for to find the sum of the each digit for the given number. eg:- 786=>7+8+6=>21. 11) W.A.P for to print the reverse number for the given number eg:- 687=> 786. 12) W.A.P for to find wheather the given number is pallendrome or not. pallendrome:- If the reverse number is equal to the given number then that number is called pallendrome number. eg:- 8998 13) W.A.P for to find wheather the given number is amstorng number or not. Amstrong:-Sum of the cubes of the each digit is equal to the given number then that number is called amstrong number. eg:-153 => 1^3 + 5^3 + 3^3 =>1 + 125 + 27 => 153. 14) W.A.P for to find wheather the given number is strong number or not. Strong number:- Sum of the factorials of the each digit is equl to the gi ven number then that number is called stro ng number. eg:- 145 => 1! + 4! + 5! => 1+24+120 => 145 Arrays -------Array is nothing but collection of similar data type. In C language we have 3 kinds of Arrays are there. 1) Single dimensional array. 2) Two dimensional array. 3)Multi dimensional array. 1) Single dimensional array:--------------------------------In the single dimensional array the elements are stored in the M emory blocks. Each memory block is reffered as cell. Each cell has numbered. And the numbering starts from zero. Hence the last number would be (n-1). Syntax:-------data type array_name[size]; eg:int a[10]; In the above declaration a is an array of type integer.

In that array we can store ten integer values.The first cell number is zero Henc e the last number would be nine. Hence values are stored in the array like a[0], a[1],a[2],..........a[9]. eg:--1) To create an array with 5 cells, input the values and print that values on th e monitor. 2) To create an array with 10 cells, input the values , print the total no.of ev en numbers and odd numbers in the array. 3) To create an array with 10 cells, input the values, print the no.of positive elements, negitive elements and zero's in the array. 4) W.A.P for sum of all the values in the array. 5)To create an array with 5 cells, input the values, find the each digit sum of each value and store the results in another array and print it. 2) Two dimensional array or Multi dimensional array:----------------------------------------------------------------In this two dimensional array values stored in the form of rows and colu mns. Each row and each column has numbered. And the numbering starts from zero. Hence the last number would be (n-1).The intersection of the row and the column is the value, ie., a[0,0]. Syntax:------Data type array_name[row_size][column_size]; eg:- int a[3][3]; In the above example a is double dimensional array of ty pe integer. In that we can store 3 rows and 3 columns, row and column number sta rts from zero . Hence the last number would be 2. eg:--1) W.A.P for to read the values into 3*3 matrix and print that matrix. 2) W.A.P for to prin the following format. 1 2 3 4 5 6 7 8 9 3) W.A.P for to print the following format 1 0 0 0 1 0 0 0 1 4)W.A.P for to print the following matrix. 1 0 1 0 1 0 1 0 1 5) W.A.P for to print the following matrix. 1 1 1 1 1 1 0 0 0 1 1 0 2 0 1 1 0 0 0 1 1 1 1 1 1 6)W.A.P for to print the following matrix. a b c d e f

i the following matrix. C F I

7 ) W.A.P for to print A B D E G H

8) W.A.P for to read two 3*3 matrices and perform the addition and print it. 9) W.A.P for to read two 3*3 matrices and perform the subtraction and print it. 10)W.A.P for to read two 3*3 matrices and perform the multiplication and print it on the monitor. Strings -------Consider the following declaration. Char ch; In the above declaration ch is a variable of type character whic h can stores just a single character. To store more than a character , you need to specify the size of the variable with in sqare braces. for eg:- char name[10]; Creates a variable called "name " which can accomodate 10 characters. It creates 10 memory blocks reffered as cells. Each and every cell has numbered an d the numbering starts from zero. Hence the last cell number would be (n-1). A string is terminated with a special character called '\0' . Th is is reffered as the null terminating character. It denotes the end of the stri ng.The cell which stroes the last character. We can access a specific character from string using the cell number. eg:--1) W.A.P for to read a string and find its length. 2) W.A.P for to read a string and count the no.of vowels. 3) W.A.P for to read a string in lower case and print it to upper case . 4) ) W.A.P for to read a string in upper case and print it to lower case. 5) W.A.P for to read a string, print the reverse string for the given string. getch():-------This function allows you to read only one character from the key board.T he character which you read is not displayed on the monitor. Syntax:getch(); getche():--------This function allows you to read a character from the key board.The char acter which you read is displayed on the screen. syntax:getche(); eg:--1)W.A.P for to read a character from the character and print that character on t he monitor by using getche(). 2)W.A.P for to read a/c holder's no, a/c holder's name, opening balance, transac tion code (d-deposit,w-with draw), transaction amount of the a/c holder.

3) W.A.P for read a string and a character and count the no.of occurences of th e character in the string. 4) Read two strings and concatenate them. Functions -----------A function can be defined as a sub program or a subroutine.These functio ns are used to minimize the program and increase the readability. In C language we have mainly two kinds of functions are there. 1) Predefined functions or Library functions. 2) User defined functions. 1)Pre defined functions or Library functions:------------------------------------------------------Pre defined functions or Library functions are mainly classified into 3 s categories. i) Mathematical functions:------------------------------These functions are used to perform the mathematical operations. These functions are available in the header file " MATH.H" #include<math.h>:---------------------1)

Вам также может понравиться