C Prog
C Prog
Keyword: They have some built in function in the compiler and hence cannot
be used as variable names.
Types of C instructions:
1. 2. 3. 4. Declaration Input/Output Arithmetic Control (i) Sequential (ii) Decision (iii) Loop (iv) Case
Shortcut keys:
F1 Help F2 Save F3 Open a file Alt+F3 Close current file Shift + Del - Cut Ctrl + Ins Copy Shift + Ins Paste Ctrl+ Y Delete a line Alt + Backspace Undo F5 Maximise window Alt + X Close
Arithmetic Instructions:
1. Arithmetic operation between one integer and another integer gives the output as integer. 2. Arithmetic operation between one integer and float gives the output as float. 3. Arithmetic operation between float and float gives the output as float. WAP to print Hello World WAP to read in an integer, float and character Rameshs basic salary is input through the keyboard. His DA is 40% of basic and HRA is 20% of basic. WAP to calculate his gross salary
If the marks obtained by a student in 5 subjects are taken as input, WAP to find the total, percentage and class obtained. Display all 3 parameters. WAP to swap the contents of 2 variables(with and without a 3rd variable). WAP to ask a user to enter a 4 digit number and print its reverse.
Data Types:
Data type Signed char Unsigned char Short signed int Short unsigned int Long signed int Long unsigned int Float Double Long double Range -128 to 127 0 to 255 -32768 to +32767 0 to 65535 -2147483148 to +2147483147 0 to 4294967295 -3.4e38 to +3.4e38 -1.7e308 to +1.7e308 -1.7e4932 to 1.7e4932 Bytes 1 1 2 2 4 4 4 8 10 Format %c %c %d %u %ld %lu %f %lf %Lf
Operators in C
Assignment ++ -+= -= %= /= *= = Examples: Suppose a=8 b=6 c=a&&b c=1 (because it checks if both no.s are >0, so result is 1) c=a&b; a=1000 b=0110 _______ c=0000 Arithmetic + * / % Conditional > >= < <= == != Logical && || ! Bitwise & | ^
Suppose a=0; b=10 b=++a; b=a++; b=--a; b=a--; b=11;a=11; b=10;a=11; b=9;a=9; b=10;a=9;
Precedence of operators:
Sl. No. 1. Category Highest Operator () [] -> :: . !,~,+,-,+ +,--,&,*,sizeof, new, delete *,/,% .* ->* +,<<,>> <,<=,>,>= ==,!= & ^ | && || ?: =,*=,/=,%=,+=, -=, &=,^=,| =,<<=,>>= , What it is? Function Call Array subscript C++ indirect component selector C++ scope resolution C++ direct component selector
Evaluate
if( )
if(condition) { . . }
2.
ifelse
3.
ifelse ifelse if
{ . . }
4.
Nested ifelse
Ternary operator:
Syntax: (condition)?true:false;
?:
(condition)?((condition)?true : false): ((condition)?true : false); WAP to accept a number and display whether it is greater, less than or equal to 100 WAP to accept 3 numbers and arrange them in ascending order. A company insures its driver in the following cases (i) if the driver is married (ii) if the driver is unmarried, male and greater than 30yrs (iii) if the driver is unmarried, female and greater than 25yrs WAP to display whether the driver is insured or not.
WAP to accept marks of 6 subjects from a user. Check if the person has passed in all subjects. If so, display the total and percentage. If not, display FAIL. An insurance company follows the following rules to calculate the premium (i) persons health is excellent, his age is b/w 25 & 35, lives in a city, is a male then the premium is Rs. 4000 and the policy amount should not exceed Rs. 2,00,000. (ii) If the person satisfies all above conditions except that the person is female then the premium is Rs. 3000 and policy amount is Rs. 1,00,000 (iii) If the persons health is poor, lives in a village, age b/w 25 & 35, is male then the premium is 6000 and policy amount cant exceed Rs. 5,00,000. WAP to display premium and policy amount. A certain graded steel is graded according to the following condition 1) Hardness >50 2) Carbon content <0.7 3) Tensile strength >500 If all the conditions are met then grade 1 If 1&2 are met then grade 2 If 1&3 are met then grade 3 If 2&3 are met then grade 4 If any of the condition is true then grade 5 Else grade 6
:
for(initialization; condition; counter) { . . }
for(i=0;i<10;i++) { . . if(i==3) break/continue; printf(%d\n,i); } . . } Break 0 1 2 3 (loop terminated) Continue 0 1 2(skips next iteration) 4 5 6 7 8 9
WAP to display 15 to 1 in reverse order(using while) WAP to display all even numbers from 1 to 50 (using do-while) WAP to check if a no. entered by the user is prime or not. (for loop) WAP to take in 2 numbers and an operator. Depending on the input, perform the desired operation and print the result. WAP to generate Fibonacci series WAP to find factorial of a number. Arrays: Collection of variables of same data type. Eg: int marks[5]; int marks[]={10,20,30,40,50}; int marks[]; ----------- invalid because compiler does not know how much space is to be allocated. marks[0] marks[1] marks[2] marks[3] marks[4]
int marks[5][5]; int marks[][]; ----------- invalid [0] [1] [2] [3] [4] [0] [1] [2] [3] [4] Initialising a single dimensional array: for(i=0;i<5;i++) scanf(%d,&marks[i]); Initialising a two dimensional array: for(i=0;i<5;i++) for(j=0;j<5;j++) scanf(%d,&marks[i][j]); WAP to perform matrix multiplication WAP to arrange the elements of a matrix in ascending/descending order
str[0]
str[1]
str[2]
str[3]
str[4]
\0 null character
< 0 if str1 is less than str2 ==0 if str1 is equal to str2 > 0 if str1 is greater than str2 Thus we write Strings are equal in the else part. WAP to find a name in a given database WAP to swap two databases.
Functions:
Short module of a program that can be called as many times as needed. Execution always starts from main. Functions without argument list: func( ) { printf(Into func); } void main( ) { printf(Beginning of main); func( ); printf(End of main); getch( ); } Output: Beginning of main Into func End of main Functions with argument list void sum(int a, int b) { int c; c=a+b; printf(%d + %d = %d ,a,b,c); } void main( ) { int x,y; // formal parameters // called function
//calling function
//actual parameters
x=5;y=10; sum(x,y); getch(); } Functions that return a value: int sum(int a, int b) { int c; c=a+b; return(c); } void main( ) { int x,y; x=5;y=10; z =sum(x,y); printf(Sum = %d ,z); getch(); } // formal parameters
//actual parameters
Call by value: void swap(int a, int b) { int c; c=b; b=a; a=c; printf(Swap in func %d %d \n,a,b); } void main() { int x,y; clrscr(); printf(Enter 2 no.s); scanf(%d %d,&a,&b); swap(x,y); printf(Swap in main %d %d\n,x,y); getch(); } Output: Enter 2 no.s : 5 10 Swap in func 10 5 Swap in main 5 10 Call by reference: void swap(int *a, int *b) { int c; c=*b; *b=*a; *a=c; printf(Swap in func %d %d \n,*a,*b); } void main() { int x,y; clrscr(); printf(Enter 2 no.s); scanf(%d %d,&a,&b); swap(&x,&y);
printf(Swap in main %d %d\n,x,y); getch(); } Output: Enter 2 no.s : 5 10 Swap in func 10 5 Swap in main 10 5
Call by pointer: void swap(int *a, int *b) { int *c; c=b; b=a; a=c; printf(Swap in func %d %d \n,*a,*b); } void main() { int x,y; clrscr(); printf(Enter 2 no.s); scanf(%d %d,&a,&b); swap(&x,&y); printf(Swap in main %d %d\n,x,y); getch(); } Output: Enter 2 no.s : 5 10 Swap in func 10 5 Swap in main 5 10
Hand written program Text editor C Source Code Pre processor directives Expanded source code Compiler Object Code Linker Executable Code
Pre- processor directives: #define PI 3.1475 #define AND && # define ARRANGE (a>45 AND a<50) #define AREA (3.14*r*r) These are also known as macros. File inclusion: It is used in the following cases if we have a large program then the code is divided into several different files each containing a set of related functions Concerned functions or macro definitions are required in almost every program. // constants // operators // Comparisons //Functions
(i) Compile the program having a particular function(say factorial). (ii) Save the current file with a .h extension in your directory and make the following changes comment the pre-processor directives comment the main( ) (iii) Make a new file which consists of your main program and save it with a .C extension in your directory This file should have: pre-processor directives #include pathname\.h main program (iv) Compile the current program to obtain the desired result. Conditional Compilation: It is done to to make the same program work for 2 or more conditions to comment out absolute line of code, if the client wants some changes in execution at the last minute. Eg: #ifdef, #if, #elif, #else, #endif #define PRINT main() { #ifdef PRINT printf(This line is executed\n); #else printf(These lines are printed\n); #endif printf(Common Code\n); getch(); } Explanation: Since PRINT is defined, #ifdef holds true so that part of the code is executed, #else statements are not compiled and then the common code is executed. Hence this is known as conditional compilation. Every #ifdef or #if must be terminated with a #endif. However the #else part is not compulsory.
If we check any conditions we have to use #if instead of #ifdef as shown in the part of code below.
#define PRINT 11 main() { #if PRINT == 11 printf(This line is executed\n); #elif PRINT ==20 printf(These lines are printed\n); #else printf(Sorry\n); #endif printf(Common Code\n); getch(); }