You are on page 1of 13

C Programming:

Introduction: C is a structured programming Language. It is a high level language. C programming is used to create operating system.(Windows XP,7,8) and Software. It was originally developed in the 1970 at BELL laboratories Inc. It is a case sensitive language. Character set: The following are the valid alphabets, numbers and special symbols permitted in C Numerals: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Alphabets: a, b, .z A, B, ...Z Arithmetic Operations: +, -, *, /, %(Mod) Special Characters: ( = \ ) ! blank { $ & } ? | _ [ . ^ / ] , ~ * < : ` % > ; # @

Variables or Identifiers: Variable names are names given to locations in the memory of computer. These locations can contain integer or character. An integer variable can hold only an integer and a character variable can hold only a character constant. Rules: 1. Maximum 31 characters will be allowed. 2. No special symbol other than an underscore (_) can be used in a variable name. Ex.: si_int 3. An identifier consists of alphabets, digits and underscore (_). 4. It must be unique with in a program. 5. Variable name always starts with character. 6. Keywords like if,else,etc are not allowed. Keywords or Reserved words: All keywords must be written in lowercase. All keyword have fixed meaning and this meaning cannot be changed. Every C statement must end with a semicolon (;). The following are the keywords: auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while

Constants: They are fixed values that do not change during program execution. The various constants in C are 1. Numeric constant: a) INTEGER

A number without decimal point. Eg. 12545, -45555, 54 b) REAL A number with a decimal point Eg. 15.21, 554.5645,-0.758 2. Character constant: a) SINGLE CHARACTER: They contain a single character enclosed in single quotes.. eg. A, 7,+ b) STRING They consists of group of characters enclosed in double quotes. Eg. hellow , 1996 Data Types: C has a concept of 'data types' which are used to define a variable before its use. The definition of a variable will assign storage for the variable and define the type of data that will be held in the location. The value of a variable can be changed any time. C has the following basic built-in data types. 1. Int: int is used to define integer numbers. { int Count; Count = 5; } 2. float: float is used to define floating point numbers. { float Miles; Miles = 5.6; }

3. double:

Double is used to define BIG floating point numbers. It reserves twice the storage for the number. On PCs this is likely to be 8 bytes. { double Atoms; Atoms = 2500000000; }

char defines characters. { char name; name = raj; } Escape Sequences: Backslash followed by a particular character is called escape sequence. Length is one character. Example; Escape Sequence \a \b \f \n \r \t Character Bell or Sound Alert (speaker beeps) Backspace Form feed/clear screen New line Carriage Return Tab

4. char:

\v \\ \? \' \" Operators in C: 1. Relational operators:

Vertical tab Backslash Question mark Single quote Double quote

Relational operators compare between two operands and return in terms of true or false i.e. 1 or 0. In C and many other languages a true value is denoted by the integer 1 and a false value is denoted by the integer 0. The following are the various relational operators < <= > >= == != Less than Less than or equal to Greater than Greater than or equal to Equal to Not equal to

Example: Suppose we have three integer variables a, b and c with values 1,2 and 3 respectively. Expression A<b (a+b)>=c (b+c)>(a+5) (c!=3) B==2 Returns 1 1 0 0 1 Meaning True True False False True

2. Assignment operators: Assignment operators are used to assign the result of an expression to a variable. The following is the list of assignment operator. Operators = +=

Operators -+ *= /= %= 3. Logical operators: A logical operator is used to compare or evaluate logical and relational expressions. There are three logical operators available in the C language. Operator && || ! Operator + * / % i) addition subtraction multiplication division remainder after division( modulo division) Meaning of Operator

Logical AND Logical OR Logical NOT


Meaning of Operator

4. Arithmetic or Binary Operators;

ii)

iii)

Integer Arithmetic: In a calculation if both the numbers are integer then the calculation is called as integer arithmetic. Integer arithmetic always yields an integer value. Eg: x=10 y=20 x*y=200 Real Arithmetic: In a calculation if both the numbers are real then the calculation is called as real arithmetic. Real arithmetic always yields an real value. Eg: x=10.256 y=20.41 x*y=209.32496 Mixed Mode Arithmetic: When one of the number is real and the other is integer ,the expression is called as mixed mode arithmetic expression, then only the real operation is performed and the result is always a real number. Eg: x=15 y=10 x/y=1 (fractional part truncated)

5. Unary operators or Increment and decrement operator: In C programming, ++ and -- are called increment and decrement operators respectively. Both of these operators are unary operators. Variable Declaration: In C program all variable must be declared before they are used. A declaration determines the type of data, and contains the list of variables having the same data type. A variable can be initialized with a value at the time of declaration. int no; char name; char name[5]; float a; Remark or Comment Line: Eg:

The statement begins with /* and ending with */ is called as remark or command line. Command line is ignored by C compiler therefore this line will not have any effect on execution of C program. Build-In Functions in C Programming: Statement beginning with #(hash) is called pre processor statement. They should be before main(). These statements are processed before the main program. Include statements: stdio.h Full form is standard input output header file. If you use various input output function such as scanf(), printf(),etc. Whenever you use any of this files you should write #include stdio.h before main(). conio.h Full form is console input output header file. If you use various input output function such as clrscr(),etc. Whenever you use any of this files you should write #include conio.h before main(). math.h This header file contains various mathematical functions such as pow(), sqrt(), etc. Whenever you use any of this files you should write #include math.h before main(). main() Every program starts with main(). Every c program must have main(). Library Functions: clrscr() It is used to clear the screen. sqrt() It is used to find square root of a given positive umber. Eg: sqrt(25)=5 pow() It is used to find power. Eg. 25 , pow(2,5)=32 Input/output Functions: 1) Single character input Function: getchar() It is used to input single character only after the enter key is pressed. This is defined in the header file stdio.h getche() It is used to input single character without waiting for the enter key to be pressed. This is defined in the header file conio.h getch() It is used to input single character without waiting for the enter key to be pressed. This is defined in the header file conio.h. It will not display an entered character on the screen. 2) Single character output Function: putchar() It is used to display single character. This is defined in the header file stdio.h putch() It is used to display single character. This is defined in the header file conio.h The gets and puts function: 1) gets() It is used to input a string. It can read multiword strings. This is defined in the header file stdio.h. 2) puts()

It is used to input a string. It automatically moves the cursor to the next line after displaying the output. Hence no need to use /n. This is defined in the header file stdio.h The scanf() function: It is used to input any type of data. Ie. Intgeger, real single character or string but string should be without blank spaces. This is defined in the header file stdio.h. In scanf() function each variable must be preceded by &(ampersand) expect string variable. Syntax: scanf(control string, argument1, argument2, ., argument); c data in a single character. d- data item in integer. s- data item in string. f- data item in floating point. The printf() function: It is used to display any type of data. This is defined in header file stdio.h. here the arguments are not preceded by the & symbol. Syntax: printf(control string, argument1, argument2, ., argument); 1. Write a C program to input two integers and find and display their sum and average. #include <stdio.h> #include <conio.h> void main() { int a,b; float tot,avg; clrscr(); printf(Enter two integer numbers); scanf(%d %d,&a,&b); sum=a+b; avg=sum/2; printf(sum=%f and average=%f, sum, avg); getch(); } 2. Write a C program to find da,hra,totalpay,tax. #include <stdio.h> #include <conio.h> void main() { float basic, da,hra,tpay,tax; clrscr(); printf(Enter basic amount); scanf(%f,&basic); da=basic*50/100; hra=basic*20/100; tpay=basic+da+hra; tax=tpay*20/100; printf(basic =%.2f\n,basic); printf(hra=%.2f\n,hra); printf(tpay=%.2f\n,tpay);

printf(tax=%.2f\n,tax); getch(); } Control statement: There are two types of control statement 1. IF statement: It is used to check a condition and execute a set of statements when the condition is satisfied Syntax: if (condition) Statement 1 else Statement 2 Example1:Write a C program to find the result. Mark >35 <35 #include <stdio.h> #include <conio.h> void main() { int m; clrscr(); printf(Enter the mark); scanf(%d,&m); if(m<35) printf(fail\n); else printf(pass\n); getch(); } Example2:Write a C program to find the result. Total Marks >=360 <360 but >=270 <270 but >=210 <210 Grade First Class Second Class Pass Class Fail Grade Pass Fail

#include <stdio.h> #include <conio.h> void main() { int m; clrscr() printf(Enter the total mark);

scanf(%d,&m); if(m>=360) printf(grade is first class\n); elseif (m>=270) printf(grade is second class \n); elseif (m>=210) printf(grade is third class \n); else printf(grade is fail\n); getch(); }

Example 3: Write a program in C to input name and income and print name and income tax. INCOME INCOME TAX FIRST 200000 NEXT 600000 EXCESS
#include <stdio.h> #include <conio.h> void main() { float itax, income char name[20]; clrscr(); printf(Enter salesman name); scanf(%s,name); printf(Enter income tax ); scanf(%s,&income); if(income<=200000) itax=0; else if (income<=80000) itax=(income-200000)*.20; else itax=600000*.20+(income-800000)*.30; printf(salesman name %s, name); printf(income tax Rs: %f, itax);

NIL 20% 30%

getch(); }

Loops in C Programming: A loop refers to a group of statements or even a single statement which in the program which will be repeatedly executed again and again. Whenever a group of statements are to be used again and again we write this statement in a loop. There are three looping statements. 1. while() In the while loop the condition is checked first. If the condition is satisfied, the statement with in the loop is executed. This continues till the condition is not satisfied, at which point the loop is terminated. Syntax: while (condition) { Statement 1; Statement 2; } Example 1: Write a C program to print the integers from 1 to 5. #include <stdio.h> #include <conio.h> void main() { int i; while (i<=5) { printf(%d,i); i++; } getch(); } Example 2: Write a C program to print all odd integers from 21 to 36. #include <stdio.h> #include <conio.h> void main() { Int i; while (i<=36) {printf(%d\n,i); I=i+2; } getch(); } 2. for() The for() loop is the most popular loop statement in C. Syntax: For (initial value; condition; increment/decrement) { Statements; } Example1: Write a program in C to calculate and sum of 1+2+3+..+100 #include <stdio.h>

#include <conio.h> void main() { int i, s=0; for (i=1;i<=100;i++) { s+=i; } printf(sum=%d\n,s); getch(); } Example2: Write a program in C to calculate and display sum of 2+5+8+..+302

#include <stdio.h> #include <conio.h> void main() { int i, s=0; for (i=2;i<=302;i+=3) { s+=i; } printf(sum=%d\n,s); getch(); }

Example 3. : Write a program in C Language to find and display the sum of 1/122+3/142+5/162 ++19/302 #include <stdio.h> #include <conio.h> void main() { float s=0; int x,y; for(x=1,y=12,x<=19,y<=30,x+=2,Y+=2) s=s+x/(y*y); printf(sum=%.2f\n,s); getch(); }
3. do while() In this loop the statement within the loop are executed first and then the condition is checked, as the condition is written at the end of the loop. If the condition is satisfied the loop is repeated, if the condition id not satisfied the loop terminated. The do loop is simply a transposition of the while loop. Syntax:

Do Statement While (expression) Example: Write a program to print APPLE one below the other 5 times using do loop. #include <stdio.h> #include <conio.h> void main() { int i=1; do { printf(APPLE\n); i++; } While (i<=5); getch(); } Arrays: An array is a data structure that consists of a list of values of the same type. Each value in the array has a distinct subscript which is used to identify the position in the list. Array is used to store many values of the same type in the memory of the computer. Eg; int x[10]; declares an array having the following 10 elements. X[0],x[1],x[2],x[3],x[4], X[5],x[6],x[7],x[8],x[9] Uses; These arrays are used in data sorting.

Fill in the blanks; 1. C is not a case sensitive language. 2. There are 32 keyword in C programs. 3. Every C statement must end with a semicolon. 4. The subscript of the first element of an array is 0. 5. A function can be defined inside another function. 6. Functions cannot return more than one value at a time. 7. A while () loop may not be executed even one. 8. The getchar() function is used to receive a single character. 9. All elements in a multidimensional array can have similar data types. 10. The switch statement is simplification of if statements. 11. The command line will not have any effect on execution of a C program. 12. All keyword must be written in lower case 13. The function getch() is defined in the header file.
i) studio.h iii) math.h ii) conio.h iv)cono.h

14. To read any type of data typed from the keyboard the function is used i) putchar() ii) getch() iii) printf() iv) scanf() 15. Break statement takes the control _________ loop. i) out ii) within iii) starting iv) none of these 16. The format character used for representing a real constant is _ i) %s ii) %c iii) %d iv)%f 17. The format character used for representing a group of character is i) %s ii) %c iii) %d iv)%f 18. There are _______ storage clause in C. i) 4 ii) 3 iii) 5 iv) 10

Assignment :

1. Write a program in C to find and print the sum of the numbers. +2/3+3/4+ .+100/101. 2. Salesmans commission is computed on the basis of total sales. Commission is 7% of sales. If sales exceed Rs. 50000, a bonus of Rs. 1000 is added to the commission. Write a program in C to input name and sales and then calculate and print name and commission. 3. Write a C program to input a number and then print whether it is divisible by 3 or 7 or not. 4. Write a program in C Language to find and display the sum of 2+5+8++299 5. Write a program in C to input name and income and print name and income tax. INCOME FIRST 200000 NEXT 300000 NEXT 500000 EXCESS INCOME TAX NIL 10% 20% 30%

6. Write a program in C Language to find and display the sum of 5*22+7*52+9*82++23*292 7. Write a program in C Language to find and display the sum of 1/122+3/142+5/162 ++19/302 8.
9.

Write a program in C to calculate and sum of 1+2+3+..+100 Write a C program to input total marks and then display total marks as well as grade.
Total Marks >=360 <360 but >=270 <270 but >=210 <210 Grade First Class Second Class Pass Class Fail

10.There Write a program in C Language to find and display the sum of 1/2*3+3/4*5+5/6*7 ++41/42*43 11.Write a program in C Language to find and display the sum of 2+5+8++299

You might also like