New C Language
New C Language
New C Language
1/56
Explain the importance of C language. 1. C is portable means program written for one computer may run successfully on other computers. 21. C is fast means i.e the executable program obtained after compiling and linking run very fast. 32. C is compact i.e. the statements in C-language are generally short but are very powerful; several operators can be combined together in just one statement. 34. C language is the middle-level language. Thats why ,The C language has both the simplicity of high-level language and low-level language. What is the structure of c program? i) Any C program is the combination of functions. main() is one such function. ii) The set of statements belonging to a function are enclosed within a pair of braces. iii) Any variable used in the program must be declared before using it. iv) Any c statement always ends with a ; (semicolon) The structure of c program is as follows: # Preprocessor directives .................................... .................................... void main () { body of the main function; ........................; ........................; } What are the relational operators available in C? The various relational operators available in C are: Operator Description > Greater thenthan < Less thenthan >= Greater then than and equal to <= Less then than and equal to == Equal to != Not Equal to What is the purpose of adding comments in a program? Can comments span more then a line. Comments are non-executable statements. With the help of comments we can give description about the program or any particular statement. Comments are enclosed with in /* and */. Any number of comments can be given at any place in the program. Comments can't not be nested. Single line comment is represented as follows: // this is a comment Yes, Aa comment can be split over more than one line, that is known as multi-line comments, as in, /* this is comment */ What is the syntax of PRINTF function of 'C'?
Q2. Ans.
Q3. Ans.
Q4. Ans.
Q5.
Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
format string could be , %f for printing real values %d for printing integer values %c for printing character values Q6. What is the syntax of nested if statement? Ans. It is perfectly alright if we write an entire if-else construct within either the body of the if statement or the body of an else statement. This is called of 'nesting' of if statement. The syntax is as follow: if (condition) { if (condition) do this; else { do this; and this; } } else do this; Q7. What is the syntax of switch statement? Ans. The control statement, which allows us to make a decision from the number of choices, is called switch. Or more correctly switch-case-default, since these three keywords go together to make up the control statement. They most often appear as follows: switch (integer expression) { case constant 1: do this; break; case constant 2: do this; break; case constant 3: do this; break; default: do this; break; } Q8. What is the syntax of FOR statement? Ans. The FOR loop allows us to specify three things about a loop in a single line: a) Setting a loop counter to an initial value. b) Testing the loop counter to determine whether its value has reached the number of repetitions desired. Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
Q12. Ans.
Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
Q16. Ans.
Structures (structure.member)
Q18. Ans.
Q19. Ans.
Q20. Ans.
Q21.
What do you mean by local and global variables? Local variables: Local variables are defined with in the pair of braces or between the body of the function. The scope of the variable is with in the function when function finished they will obsolete. Global variables: These variables defined out side of all the functions. And can be used to any function with in the file because the scope of these variable are global to the file. Explain various programming techniques? Programming techniques are used to make the program simple, efficient, understandable and easy to maintain. The three programming techniques commonly used are:Top down design: - Using this design program is divided into the smaller blocks, which can be linked and called whenever needed. The programmers design the program in different levels and can be called any level at any time. Bottom up design: - It is also designed in the same manner and divide the whole program into the smaller blocks, but when it is called it begins from the smaller block to the bigger till the program end. In this design, program is divided into blocks and the control flows from bottom to top unlike top down design where the control flows from top to bottom. Modular design: - A level consists of one or more modules. The first level is a complete main program and the module of the successive level contains sub modules Execution is controlled by the main program What is sizeof operator? The sizeof operator returns the size, in bytes, of given operand. Syntax is: sizeof (exp) Example: sizeof (float) Result: returns value 4 Differentiate between expression and statement. An expression refers to variables, constants and operators. It represents a single data item, such as a numbers or characters. One or more operators may interconnect these variables or constants. In C, very complex expressions can be solved easily. An algebraic expression in C is represented as follows: Algebraic expression: a x b c x d Its relative C expression will be written as: a * b c* d. What is the use of #define directives statement?
Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
Q24. Ans.
Q25. Ans.
Q26. Ans.
Q27. Ans.
Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
e=a^b; f=~a; printf("\n c=%d",c); printf("\n d=%d",d); printf("\n e=%d",e); printf("\n f=%d",f); getch(); } How you can determine the memory size occupied by a variable of a particular data type? The value range is calculated from the number of bits occupied by the data type. For eg., int uses 2 bytes , i.e 16 bits; from this 1 bit is taken for sign(- or +). So the range is expressed as 215 to 215 1. Bring out the difference between exit and break control statement. Exit statement is used to come out of from the program. Whereas break statement is used to break loop or switch statement. What are constants? How many types of constants are available in C programming language? A constant is a quantity that doesntwhose value doesnt change throughout the program. This quantity can be stored at a location in the memory of the computer. Types of c conatsntsconstants:C constant can be divided into two major categories. a) Primary constants Integer constants, real constants, character constants. b) Secondary constants Array, pointers, structures, union, enum.
Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
What is the difference between do-while loop and while loop? Give an example to explain. In both the cases the condition is checked. In case of while, condition is checked before the execution of the statements but in case of do-while, statements are executed at least once whether the condition is true or false i.e. condition is checked at the end of the do-while block. A=11; While(a<=10) { printf(%d,a); a++; } Output: [BLANK SCREEN] A=11; Do { printf(%d,a); a++; } While(a<=10); Output: 11
Q2. Ans.
Q3. Ans.
What are the various types of programming languages? Or Why C language called structured language. Unstructured programming: - In unstructured programming the main program directly operates on global data. This programming technique has tremendous disadvantages once the program gets sufficiently large e.g. the programs written in basic language. Structured programming: - This is also known as procedural programming. Using this programming you are able to combine returning sequences of statements into one single place. A procedure call is used to invoke the procedure after the sequence is processed .The main program is responsible to pass data to the individual calls. The data is processed by the procedure and once the procedure has finished the resulting data is present. E.g. C and Pascal language using functions Modular programming: - With modular programming the statements of a common functionality are grouped together into separate modules .A program therefore no longer consist of only one single part. E.g. C language using header files. Object oriented programming:- Object oriented programming is an approach that provides a way of modularizing programs by creating separate/common memory area for both data and functions that can be used as templates for creating copies of such modules on demand. e.g. c++ , java languages using classes and objects. Discuss the structure of a C program. Explain using an example. A C program basically has the following form: 1. Preprocessor Commands 2. Type definitions 3. Function prototypes -- declare function types and variables passed to function. 4. Variables 5. Functions We must have a main () function. A function has the form: type function_name (parameters) {
Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
Q4. Ans.
Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
Q5. Ans.
Q6. Ans.
Write a C program to read characters and print their ASCII codes. #include<stdio.h> #include<conio.h> int main() { char ch; clrscr(); while(1) { clrscr(); puts("\nEnter a character (Enter to quit): "); fflush(stdin); ch=getchar(); printf("%d",ch); puts("\nWant to continue(y/n): "); ch=getche(); if(ch=='n' || ch=='N') break; } getch(); return 1; } What do you mean by data types? Give examples of data types available in C language.
Q8.
Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
, Basic data types available in C are given below. Integer (Int): Int type of constant is a positive or negative number without a decimal point. e.g. -34,506,-11,-9642 .they are whole numbers in the range -32768 to +32767. To print the value of int type of constant we use conversion specifier %d. it It takes two bytes of memory. Float: These are numbers either positive or negative WITH OR WITHOUT DECIMAL E.G. -13.26, 45.890,89,89.0 ETC. They lie in the range 3.4*10-38 to 3.4* 10+38 accurate up to 7 digits and conversion specifier is %f. and it takes four bytes of memory Character (Char): This type constants consists of a character enclosed in a pair of single quotes such as 'a', B', c even a blank space can be used as a character. This variable need only one byte of memory space. tThe conversion specifies is %c. Long: These type of integers have a range -2147484648 2147438648 to + 214774383647 and we use conversion specifies %ld. Double: The keyword double is used to declare variables of the double type and the value assigned is accurate up to sixteen digits it lies in the range 1.7*10-308 to 1.7*10+308 and conversion specifies is %lf. It takes 8 bytes of memory. Q9. Differentiate between operator and operand. Ans. Operator refers to a symbol that represents a particular operation to be performed on data. There are different types of operator in c. Assignment operator: Assignment operators are used to Assign the values of a variable example of these operators are (+=,-=,/=,*= etc). Arithmetic operator: From the arithmetic calculation on the operands these are +,-,*,/ etc.These operators are used in arithmetic calculations like +,-,* etc Modulus operator: Modulus operators are used to find out the remainder by dividing one number with another. ( %) is the symbol for this operator. Relational operator: Use to create relation between operands. E g. >,<,<=,>= etc. Logical operator: These operators are used to create logic relation with in the operands. tThese are &&(AND), ||(OR), !(NOT). Operand: The data used to calculation on which the operation is going to perform is called operand.It refers to the variable upon which operation has to be performed. E.g. In the following expression 12+30, 12 and 30 both are operands and + is an operator. Write down a hierarchy of operators or the order of precedence of the entire operator using in c. Also explain associativity of operators. Precedence order 1 2 3 4 5 Operators ( ), [] , ++, --, -(unary) !, ~, *, &, sizeof *, /, % +, <<, >> Associativity Left to Right Right to left Left to right Left to right Left to right
Q10. Ans.
Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
Q11. Ans.
Q12. Ans.
Q13. Ans.
Write and compare arithmetic, assignment, logical, bitwise and special operators. Arithmetic assignment is an operator that is used to assign the value of an arithmetic expression to a variable. (=, +=, -=, *=, /=, %=) Logical operator are used to create logic relation with in the operands these are &&(AND), ||(OR), ! (NOT). Bit wise operators allow direct manipulation of individual bits with in a word. They are also used to perform certain numerical computations faster. There are three bit wise operators; 1. Bit wise logical operators a. Bit wise and (&) b. Bit wise or (|) c. Bit wise xor (^) 2. Bit wise shift operators a. Bit wise Left (<<) b. Bit wise right (>>) 3. Ones complement operator (~) Special Operators: C support some Special Operators of interest such as comma (,), sizeof, pointer (*) and member selection (. And ->) operator. What do you mean by control string? What is the purpose of the control string in a scanf function? What type of information does it convey? The control string has placeholders for the variables being used there can also be characters in the control string. For example Scanf (control string, &a1, &a2.&an) Where control string refers to the field format in which data is to be entered and a1, a2 an are the arguments that represent the individual input data items. It tells what type of variable can display there value with the help of conversion specified if we apply this with printf() statement we can also specify the short messages to recognize the values provided by variables. How the memory is allocated? Explain by giving example. The function Malloc is used to allocating allocate memory dynamically. Here is a little function which tries to allocate memory for a string of length n, and which returns zero (false) if it fails and 1 (nonzero, or true) if it succeeds, returning the actual pointer to the allocated memory via a pointer: #include <stdlib.h> int allocstr(int len, char **retptr) { char *p = malloc(len + 1);
/* +1 for \0 */
Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
Q14. Ans.
char *string = "Hello, world!"; char *copystr; if(allocstr(strlen(string), ©str)) strcpy(copystr, string); else fprintf(stderr, "out of memory\n"); Write a program to find an area of a rectangle. void main( ) { float area,len,bre; printf(enter value for length , breadth ); scanf(%f%f,&len,&bre); area=len*bre; printf(area= %f ,area);
Q15. Ans.
} Write a program to find whether a number is Armstrong or not. Void main() x=x/10; { } int n,x,r,c,s=0; if(cs==n) clrscr(); printf("number is Armstrong"); printf("enter the number:"); else scanf("%d",&x); printf("number is not Armstrong"); n=x; getch(); while(x>0) } { r=x%10; c=r*r*r; s=s+c; Write a program for solving a quadratic equation. #include<processstdio.h> void main() { float a,b,c,d, x1,x2,d2,y1,y2; printf ("enter value of a,b,c in a quadratic equation(a*X*X+b*x+c\n"); scanf("%f%f%f",&a,&b,&c); d=b*b-4*a*c; if(d<0) { d=-d; d2=sqrt(d); y1=(-b)/(2*a); y2=d2/(2*a); printf ("value of x1 is (% of x is (%ff-+i%f)/%f) , y1,y2); printf("value of x2 is (%f-i%f) , y1,y2); and (%f+i%f)/%f",b,d2,(a*2),b,d2,(a*2)); exit(0); } else { d2=sqrt(d) ; x1=(-b+d2)/(2*a);
Q16. Ans.
Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
Q18. Ans.
Q19. Ans.
Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
Q20.
Ans.
Q21. Ans.
Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
Q22. Ans.
void main() { { r=num%10; long int num,num1,numrev=0,r; numrev=(numrev*10)+r; clrscr(); num=num/10; printf("Enter a number-->"); } scanf("%d",&num); printf("reverse of %ld is %ld",num1,numrev); num1=num; getch(); while(num>0) } Q23. Write a program to print a Pascal triangle. Ans. #include <stdio.h> # include <conio.h> Void main() { int i,j,k; for (i=1;i<=5;i++) { for (k=20;k>=i;k--)void main() { int i=0,j,a[10][10],s=20,c; clrscr(); for(i=1;i<=5;i++) { for(j=s;j>=i;j--) printf(" "); c=1; a[1] [c]=1; printf(" %d",a[i][c]); if(i>1) { for(c=2;c<i;c++) { Q24. Ans. printf ( ); for (j=1;j<=i;j++) printf (%d,j); for (j=I-1;j>=1;j--) printf (%d,j); printf (\n); } getch(); }a[i] [c]=0; a[i][c]=a[i-1][c-i]+a[i-1][c]; printf(" %d",a[i] [c]); } } if(i>1) { a[i] [c]=1; printf(" %d",a[i] [c]); } s--; printf("\n"); } getch(); } What do you mean by loops? What are different types of loops provided by C language? Loops are used to execute the statements repeatedly according the condition. There are different types of loops in c
While Do while For While loop: This statement is used to execute the loop till the condition is true. Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
Q25. Ans.
Q26. Ans.
Ans. void main() { int n,a=1,b; printf(enter number for table); scanf(%d,& n); while(a<=10) { b=a*n; printf( %d * %d = %d,n,a,b); a++; } getch(); }
Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
Ans. void main() { int a[5],I,j,temp; for(I=0;I<5;I++) { printf(enter array elements); scanf(%d,&a[I]); } for(I=0;I<4;I++) for(j=I+1;j<5;j++) if(a[I]>a[j]) { temp=a[I]; a[I]=a[j]; a[j]=temp; } printf(sorted list); for(I=0;I<5;I++) printf(%d,a[I]); getch(); } } Q31. Ans.
Ans. void main() { int a=0,b,c; clrscr(); for(b=1;b<50;b++) { for(c=2;c<b;c++) if(b%c==0) break; if(b==1) { printf(" \n%d",b); a++; } } if(c==b) { printf("\n %d",b); }a++; } }} getch(); } } Write a program in C to generate Fibonacci series of 20 terms.
Q32. Ans.
void main() c=a+b; { a=b; int a=0,b=1,c=1,i; b=c; clrscr(); } for(i=1;i<=20;i++) getch(); { } printf(" %d",c); What is the difference between break and continue statements? Explain with the help of an example. Break statement is used to exit from the loop. Whenever break statement is encountered the rest of the statements inside the loop are ignored and the control goes to the next statements after the loop. But the case of using continue is different. Continue statement is used to skip the later on statements and go back to the starting of the loop in order to execute it again. Break Example Continue Example Void main() Void main() { { int I; int I; for (I=1;I<=10;I++) for (I=1;I<=10;I++) { { if (I==5) if (I==5) break; continue; printf(%d,I); printf(%d,I); } }} } }}
Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
OutPut: 1,2,3,4,6,7,8,9,10
Write a program to fill the entire screen with a smiling face. The smiling face has an ASCII value 1. for(c=0;c<=79;c++) printf("%c",21);
for(r=0;r<=24;r++)
getch();}
Q.34 Write a program in C language to produce output as given below 1 121 12321 1234321 123454321 12345654321 Ans. #include <stdio.h> printf ( ); # include <conio.h> for (j=1;j<=iI;j++) Void main() printf (%d,j); { for (j=II-1;j>=1;j--) int Ii,j,k; printf (%d,j); for (iI=1;Ii<=85;Ii++) printf (\n); { } for (k=20;k>=1i;k--) getch(); } Q.35 Ans. # include <stdio.h> # include <conio.h> # include <math.h> void main() { int n,s,i,b=1; float p=0; printf (\n Enter number ); scanf (%d,&n); printf (\n Enter how many number for series ); scanf (%d,&s); for (i=1;i<=s;i+=2) { if (b==1) { p=p+pow(n,i)/(float)i; b=0; } else { p=p-pow(n,i)/(float)i; b=1; }} } printf (\n Value of sin (%d) is %f,n,p); } Write a program in c language to find the value of n using expansion series given below: Sin(x)=x-x3/3+x5/5-x7/7.
Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
Q.36
Write a program to sum up the following series: 1 1 + 1 1 + 2 3 4 #include<stdio.h> #include<conio.h> void main() { int n,i; float j=1, sum=0; clrscr(); puts("Enter the upper limit of the series: "); scanf("%d",&n); for(i=1;Ii<=n;Ii++)
., +
1 n
Ans. { if(i%2==0) sum-=(float)(1/ij); else sum+=(float)(1/ji); j++; } printf("\n\nSum of the series is : %.2f",sum); getch(); }
Q37. Ans.
Write a program in C language to find out the sum of first n natural numbers using do.while loop. #include<stdio.h> #include<conio.h> void main() { int no=1,sum=0,final; clrscr() printf ("Enter the value of n final number "); scanf ("%d",&final); do { sum=sum+no; no=no+1; } while (no<=final); printf ("sum is %d ",sum); }
Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
Q2. Ans.
Q3. Ans.
Q4. Ans.
Q5.
Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
Q6. Ans.
Q.7. Ans.
Q8. Ans.
Q.10. Explain the memory management of an integer array. Ans. An array is finite collection of similar elements stored in adjacent memory locations. An array containing n number of elements is referenced using an index that varies from 0 to n-1. In an integer array, each element occupies 2 bytes in memory. For example, consider the following integer array: A[0] a[1] a[2] a[3] a[4] 3268 3270 3272 3274 3276 Here , each element of the array occupies 2 bytes. The starting address is 3268. The next address is 2 bytes ahead of the first. Q.11. How bubble sort method is different from selection sort. Ans. In bubble sort method, the first element is compared with the second one and if it is greater than the second element, they are swapped, this method continues till the list is sorted. Whereas, in selection sort, a minimum function is implemented which finds the minimum element from the list & inserts at the appropriate place. Q12. What condition must be satisfied by all the elements of any given array? Ans. All the elements in any array must belong to same data type. That is if any array is declared as integer array then all the elements in that array must belong to integer type. Q13. Differentiate between sprintf() and sscanf() functions. Ans. The sprintf() function works similar to the printf() function except for small difference . Instead of sending the output to the screen as printf () does, thid this function writes the output to an array of characters. The counterpart of sprintf() is the sscanf() function. It allows us to read characters from a string and to convert and store then them in C variables according to specified formats. The sscanf() function comes in handy for in-memory conversion of characters to values. Q14. What is the difference between x and x? Ans. x represents a string constant and it must be followed by the null character \0, whereas x represents a character constant. Q15. What will be the output of the following program? main() { Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
Q16. Ans.
Q17. Ans.
Q18. Ans.
Q19. Ans.
Q20. Ans.
Q21. Ans.
Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
Q3. Ans.
Write a program in C language to Q2. Write a C program to print 5X5 add two matrices. matrix. Ans. Ans. void main() #include<stdio.h> { #include<conio.h> int a[2][2],b[2][2],c[2][2]; int main() for(int i=0;i<2;i++) { for(int j=0;j<2;j++) int matrix[5][5],i,j; scanf("%d",&a[i][j]); clrscr(); for( i=0;i<2;i++) for(i=0;i<5;i++) for( j=0;j<2;j++) for(j=0;j<5;j++) scanf("%d",&b[i][j]); { for( i=0;i<2;i++) printf ("Enter values of matrix[%d][%d]:" ,i for( j=0;j<2;j++) +1, c[i][j]=a[i][j]+b[i][j]; j+1); for( i=0;i<2;i++) scanf("%d",&matrix[i][j]); { } for( j=0;j<2;j++) printf("\n\nYou entered the matrix as..."); printf("%d ",c[i][j]); printf("\n"); printf("\n"); for(i=0;i<5;i++) } { } for(j=0;j<5;j++) { printf(" %d",matrix[i][j]); } printf("\n"); } getch(); return 1; } Write a program to find sum of two matrices. #include<stdio.h> #include<conio.h> int main() { int matrix1[2][2],matrix2[2][2],matrix3[2][2],i,j; clrscr(); for(i=0;i<2;i++) { for(j=0;j<2;j++) { printf ("Enter values of matrix1[%d][%d]::", i +1, j+1)); scanf("%d",&matrix1[i][j]); }} for(i=0;i<2;i++) { for(j=0;j<2;j++) { printf("Enter values of matrix2[%d][%d]: scanf("%d",&matrix2[i][j]); }} for(i=0;i<2;i++) { for(j=0;j<2;j++) { matrix3[i][j]=matrix1[i][j]+matrix2[i][j]; }} printf("\n\nResultant is...\n"); for(i=0;i<2;i++) { for(j=0;j<2;j++) { printf(" %d",matrix3[i][j]); } printf("\n"); } getch(); return 1;
Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
Ans. #include<stdio.h> #include<conio.h> int main() { int matrix[2][2],i,j; clrscr(); for(i=0;i<2;i++) { for(j=0;j<2;j++) { printf("Enter values of matrix1[%d][%d]: ",i+1,j+1):); scanf("%d",&matrix[i][j]); } } puts("Transpose of the matrix is..."); for(i=0;i<2;i++) { for(j=0;j<2;j++) { printf(" %d",matrix[j][i]); } printf("\n"); } getch(); return 1; }
Q7.
Write a C program to find minimum and maximum values in a given series of data.
Ans. #include<stdio.h> #include<conio.h> int main() { int matrix[43][43],i,j; int sum=0; clrscr(); matrix[0][2]=matrix[1][2]=matrix[2][2]=0; matrix[2][0]=matrix[2][1]=matrix[2][2]=0; for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("Enter value of matrix[%d][%d]: ",i+1,j+1); scanf("%d",&matrix[i][j]); }} for(i=0;i<3;i++) { for(j=0;j<3;j++) { matrix[i][3]+=matrix[i][j]; matrix[3][j]+=matrix[i][j]; }} prpriintf("\n\nYou entered the matrix as..."); printf("\n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf(" %d",matrix[i][j]); }printf("\n");} for(i=0;i<3;i++) { for(j=0;j<3;j++) { sum=sum+matrix[i][j]; }} printf(The sum of elements are: %d,sum); getch(); return 1; } Q8. Write a C program to sort data in two-dimensional array. Ans. void main() { int a[3][3],b[9],i,j,k=0,temp=0; clrscr(); for(i=0;i<3;i++) for(j=0;j<3;j++) { printf("enter array element-->"); scanf("%d",&a[i][j]);
Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
Q9.
Ans. void main() { int a[7],i,f=0,s; printf("enter the numbers:"); for(i=0;i<7;i++) scanf("%d",&a[i]); printf("enter the no. to search:"); scanf("%d",&s); for(i=0;i<7;i++) { if(a[i]==s) { f=1; printf("%d is found at %dth position\n",s,i+1); } } if(f==0) printf("no. not found"); getch(); }
Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
Q11. Ans.
Write a program to transpose a matrix using pointers. void main() /* transposing the matrix */ { for(I=0;I<2;I++) int a[2][2],I,j; { for(I=0;I<2;I++) for(j=0;j<2;j++) { { printf(enter the values for %d row of printf(\t%d,a[j][I]); matrix,I+1); } for(j=0;j<2;j++) printf(\n); { } scanf(%d,&a[I][j]); getch(); } } }
Q12. Ans.
What is the use of following String functions and list the commonly useable function? String library functions are the functions, which are used regularly and stored in library file and whenever these functions are needed , you need to include the required header file in your program. There are different types of functions strlen: This function is used to count the characters in a string. It calculates the length of the string. Syntax: strlen(array variable); strcpy: This function copies the contents of one string into another Syntax strcpy(target,source); strcat: This function is used to concatenate the source string at the end of the target string. Syntax strcat(target,source);
Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
Q14. Ans.
How can you apply arithmetic operations on strings? write the role of a atoi( ) function in string. Arithmetic operations can be applied on strings by converting strings to integers using atoi () function. tThis function converts digits in strings to integers. atoi ( ) function is a c library function which is used to convert a string of digits to the integer value. E.g. char st[10]=12345; int n; n= atoi(st); This will assign the integer value 24175 to the integer variable n. Q15. Write a program that accepts a letter and convert it into uppercase to lowercase or lowercase to uppercase? Q16. Write a program to copy a string. Ans. #include<stdio.h> #include<conio.h> #include<string.h> int main() { char str1[20],str2[20]; clrscr(); printf("Enter text: "); gets(str1); strcpy(str2,str1); puts("\nAfter copying to another string...\n"); puts(str2); getch(); return 1; }
Ans. void main() { char n; clrscr(); printf("Enter any character: "); n=getchar(); if (n>='A' && n<='Z') printf("%c",n+32); else if (n>='a' && n<='z') printf("%c",n-32); else printf("Invalid Character"); getch();
Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
Ans. int largest(int *a, int n) { int max, I=0 ; max=a[I]; for(I=1;I<n; I++) { if(max<a[I]) max=a[I]; } return max; } void main( )
Ans. #include<string.h> void main() { char a[20]; printf("enter string \n"); scanf("%s",a); int i=strlen(a); int k=--i; for(int j=0;j<k;j++) { if(a[i]!=a[j]) { printf(" not a palindrome");
Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
Q22.
Ans. void main() { int a[5],I,j,temp; for(I=0;I<5;I++) { printf(enter array elements); scanf(%d,&a[I]); } for(I=0;I<4;I++) for(j=I+1;j<5;j++) if(a[I]>a[j]) { temp=a[I]; a[I]=a[j]; a[j]=temp; } printf(sorted list); for(I=0;I<5;I++) printf( %d,a[I]); getch(); }
Q23.
Write a program in C to Q24. Write a program to count number of concatenate two strings without vowels in a string. using strcat() function. Ans. Ans. void main() void main() { { char a[20]; char s1[10],s2[10],s3[20];int i; int c=0,I; clrscr(); printf(enter any string); printf("Enter first string: "); gets(s1); scanf(%s,&a); printf("Enter second string: "); gets(s2); for(I=0;a[I]!=NULL;I++) for(i=0;i<strlen(s1)+strlen(s2);i++) if(a[I]==a||a[I]==e||a[I]==i||a[I]==o||a[I]==u|| if(i>=strlen(s1)) a[I]==A|| a[I]==E||a[I]==I||a[I]==O||a[I]==U) s3[i]=s2[i-strlen(s1)]; c=c+1; else printf(No of vowels in a string %d,c); s3[i]=s1[i]; getch(); s3[i]=NULL; } printf("After concatenating:\n %s",s3); Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
Q26. Ans.
Write a c program to calculate number of days between two dates using function. struct date { int year; int day; int mon; }; void main() { struct date d1,d2; void datedif(struct date,struct date); clrscr(); datedif(d1,d2); getch(); } void datedif(struct date d1,struct date d2) { int d,m,y,daydiff; printf("enter first date-->"); printf("\nenter day-->"); Q27. scanf("%d",&d1.day); printf("enter month-->"); scanf("%d",&d1.mon); printf("enter year-->"); scanf("%d",&d1.year); printf("enter second date greater than first-->"); printf("\nenter day-->"); scanf("%d",&d2.day); printf("enter month-->"); scanf("%d",&d2.mon); printf("enter year-->"); scanf("%d",&d2.year); d=d2.day-d1.day; m=d2.mon-d1.mon; y=d2.year-d1.year; daydiff=d+(m*30)+(y*365); printf("difference of days=%d",daydiff); getch(); }
Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
Q29.
Write a function that will calculate and display the roots of a quadratic equation.
Q30.
Write a C function that accepts decimal number and return its equivalent binary number.
Ans. #include<stdio.h> void main() { float a,b,c,d, x1,x2,d2,y1,y2; printf ("enter value of a,b,c in a quadratic equation(a*X*X+b*x+c\n"); scanf("%f%f%f",&a,&b,&c); d=b*b-4*a*c; if(d<0) { d=-d; d2=sqrt(d); y1=(-b)/(2*a); y2=d2/(2*a); printf("value of x1 is (%f+i%f) , y1,y2); printf("value of x2 is (%f-i%f) , y1,y2); exit(0); }
Ans. void main() { int n,a[10],j,i=0; clrscr(); printf("Enter any decimal number: "); scanf("%d",&n); do { a[i]=n%2; n=n/2; i++; } while(n!=0); for(j=i-1;j>=0;j--) printf("%d",a[j]); getch(); }
Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
A)
Ans.
B)
Using recursion.
A) Without recursion void main() { long int num,sum=0,r; clrscr(); printf("Enter a five digit no: "); scanf("%ld",&num); while(num>0) { r=num%10; sum+=r; num/=10; } printf("sum of digits is %ld",sum); getch(); }
B) With recursion int s=0; int sum(int num) { if(num==0) return s; else { s+=(num%10); sum(num/10); } } void main() { clrscr(); printf("sum of digits is %d",sum(565)); getch();
Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
Q32. Ans.
Write a program to find the sum of the rows, column, diagonal elements of a matrix. #include<stdio.h> #include<conio.h> int main() { int matrix[3][3],i,j; int sum=0; clrscr(); for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("Enter value of matrix[%d][%d]: ",i,j); scanf("%d",&matrix[i][j]); }} #include<stdio.h> #include<conio.h> int main() { int matrix[4][4],i,j; clrscr(); matrix[0][2]=matrix[1][2]=matrix[2][2]=0; matrix[2][0]=matrix[2][1]=matrix[2][2]=0; for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("Enter value of matrix[%d][%d]: ",i+1,j+1); scanf("%d",&matrix[i][j]); } } for(i=0;i<3;i++) {for(j=0;j<3;j++) { priintf("\n\nYou entered the matrix as..."); printf("\n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf(" %d",matrix[i][j]); }printf("\n");} for(i=0;i<3;i++) { for(j=0;j<3;j++) { sum=sum+matrix[i][j]; }} printf(The sum of elements are: %d,sum); getch(); return 1; }matrix[i][3]+=matrix[i][j]; matrix[3][j]+=matrix[i][j]; } } printf("\n\nYou entered the matrix as..."); printf("\n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf(" %d",matrix[i][j]); } printf("\n"); } getch(); return 1; }
Q.33 Ans.
Write a program in C which finds the sum of elements of an integer array. Use user defined function to pass array to the function. #include<stdio.h> int add(int []); void main() { int a[5],i,sum; clrscr(); for (i=0;i<=4;i++) { printf ("\n Enter no "); sum=add(a); printf ("\n sum is %d",sum); } add(int a[]) { int i,sum=0; for(i=0;i<=4;i++) sum=sum+a[i]; return sum;
Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
Ans.
What is pointer? Why pointers are used and how we initialize pointer variables? OR What kind of information does a pointer variable represent. Pointer is variable that represents the location of a data item. These variables stored in memory location and are assigned a unique number i.e., the address of that location. This address points to whatever is contained within the location in the same way as each array element has its own index. Thus pointers provide a way of accessing a variable without referring to it directly but by using its address. We can initialize pointer by using command (int *p=50;) Q2. Write a program using pointers to copy a string to another string variable. Q3. Write a C program using pointers to multiply two integers. Ans. void main() { int *x,*y,*k; int a,b; clrscr(); *x=20; *y=3;x=&a; y=&b; printf(\nEnter two numbers: ); scanf(%d %d, &a,&b); *k=*x * *y; printf("Multiply: %d",*k); getch(); }
Ans. void main() { char *a="Ajay", *b;char s[]=Jai mata di; char t[25]; char *ss, *tt; ss=s; while(*tt++=*ss++) { *tt=\0; } clrscr(); b=a; printf("%s",b); getch(); } Q4. Ans.
How structure is represented in memory? Whatever be the elements of a structure, they are always stored in contiguous memory locations. Once the new structure data type has been defined one or more variable can be declared to be of that type. The memory consumption of structure is show in the following program; main() { struct book { char name;float price;int pages; }; struct book b1={'A',130.00,550}; printf ("\n address of name = %u",&b1.name); printf ("\n address of price = %u",&b1.price); printf ("\n address of pages = %u",&b1.pages); } Here is the output of the program: Address of name = 1001 Address of price = 1002
Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
Q6.
Ans.
Write a C program to compute the size of union. union student { int roll_no; char sname[20]; }; void main() { clrscr(); printf ("Size of union is %d bytes",sizeof(union student)); getch(); }
Q8. Ans.
Q9.
Define text & binary files. Text file contain a sequence of characters including carriage returns and linefeeds. In text mode, there is no requirement that individual character remain unaltered as they are written to or read from a file. It is a simple file so we can read easily. Binary file contain a sequence of bytes with a one to one correspondence to the sequence found in the external device (disk, tape) It is a Encrypted file so we cannot read it. What do you mean by data file? What is the primary advantage of using a data file?
Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
Q10. Ans.
What is purpose of library function feof()? The function is used to check whether the file pointer has reached at the end or not. It returns the non-zero value when theif file pointer points the end of the file has been reached otherwise it return zero. This is generally written as: int feof(FILE *fp)
Q11. Ans.
What are the various types of modes used in file handling? Explain with example. Files Modes Operator Rr wW aA rb RB WBwb abAB
Description Open a text file for reading. Create a text file for writing. If the file exists, it is overwritten. Open a text file in append mode. Text is added to the end of the file. Open a binary file for reading.
Create a binary file for writing. If the file exists, it is overwritten. Open a binary file in append mode. Data is added to the end of the file. r+R+ Open a text file for reading and writing. w+W+ Create a text file for reading and writing. If the file exists, it is overwritten. a+A+ Open a text file for reading and writing at the end. R+B rb+or Open binary file for reading and writing. RB+ wb+W+B Create a binary file for reading and writing. If the file exists, it is or WB+ overwritten. ab+A+B or Open a text file for reading and writing at the end. Q12. Ans. Explain the redirection and command line argument. Redirection means performing input from other than the standard input stream, or output to other than the standard output stream. You can redirect the output of the print and printf statements to a file or a system command, using the `>' , `>>' , and `|' operators. You can redirect input to the getline statement using the `<' and `|' operators.
Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
Q13. Ans.
What are the features of structures? 1. The values of a structure variable can be assigned to another structure variable of the same type using the assignment operator. 2. One structure can be nested within another structure. Using this facility complex data types can be created. 3. Like an ordinary variable, a structure variable can also be passed to a function. We may either pass individual structure elements or the entire structure at one go. 4. The way we have a pointer pointing to an int, or a pointer pointing to a char , similarly we can have a pointer pointing to a structure. Such pointers are known as structure pointers.
Q14. Ans.
Q15. Ans.
How structure elements are stored? Whatever be the elements of a structure, they are always stored in contiguous memory locations.Eg. b1.name b1.price b1.pages b 130.00 550 Address of name = 1001 Address of name price = 1002 Address of pagesname = 1006 What do you mean by enumerated data types? How it is useful. The enumerated data type gives you an opportunity to invent your own data type and define what values the variable of this data type can take. This can help in making the program listings more readable, which can be an advantage when a program gets complicated or when more than one programmer would be working on it. Using enumerated data type can also help you reduce programming errors. Its syntax is: enum colour{ red=5,black=2,blue}; then it initializes value 5 to red, 2 to black and 3 to blue If the initialization of elements is not given, it starts from 0 and the next element takes a constant value, one more than the previous element. What is the use of fseek() and rewind() function. The fseek() function lets us move the pointer from one record to another. The rewind() function places the pointer to the beginning of the file, irrespective of where it is present right now. What is the significance of EOF?
Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
Q18. Ans.
Q19. Ans.
Q22. Ans.
Pointer is double-edged weapons explain it. A pointer can store address of other variable as well as value also. Where as a variable can store a value only not the address. What is multiple indirection? We can have a pointer point to another pointer that points to the target value. This situation is called multiple indirection, or pointers to pointers. A variable that is a pointer to a pointer must be declared as placing an additional asterisk in front of the variable name. For eg., the following declaration tells the compiler that newbalance is a pointer to pointer of type float: float **newbalance; What do you mean by streams? The C file system is designed to work with a wide variety of devices , including terminals, disk drives, and tape drives. Each Even though each device is very different, the buffered file system transforms each into a logical device called a stream. All streams behave similarly. Because streams are largely device independent, the same function that we can write to a disk file can also be used to write to another type of device, such as the console. There are two types of streams :text and binary.
Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
Write a C program using pointers to find biggest of given three numbers. void main() printf("X is biggest"); { else int *a,*b,*c,x,y,z; if (*b>*c) x=45; y=78; z=23; printf("Y is biggest"); a=&x; b=&y; c=&z; else clrscr(); printf("Z is biggest"); printf("X:%d, Y:%d, Z:%d\n",x,y,z); getch(); if (*a>*b && *a>*c) } What is the use of pointers? What is a pointer to an array and an array of pointers? A pointer is a variable, which contains the memory address of another variable. We can have a pointer to any variable type. The unary or monadic operator & gives the address of a variable. The indirection or dereference operator * gives the contents of an object pointed to by a pointer. To declare a pointer to a variable do: int *pointer; In C there is a very close connection between pointers and arrays. In fact they are more or less one and the same thing! Suppose Wwhen you declare an array as: int a[10]; you are in fact declaring a pointer a to the first element in the array. That is, a is exactly the same as &a[0]. The only difference between a and a pointer variable is that the array name is a constant pointer - you cannot change the location it points at. When you write an expression such as a[i] this is converted into a pointer expression that gives the value of the appropriate element. To be more precise, a[i] is exactly equivalent to *(a+i) i.e. the value pointed at by a + i . In the same way *(a+ 1) is the same as a[1] and so on. Arrays of Pointers are a data representation that will cope efficiently and conveniently with variable length text lines. This eliminates: complicated storage management. high overheads of moving lines. Q3. Write a program to swap two Q4. Write a C program using pointers Variables by using call by to count the number of characters reference. in a given string. Ans. Ans. void swap(int *a, int *b) void main() {int temp; { temp=*a; char str[20], *ptr; *a= *b; int i,cnt=0; *b = temp; clrscr(); } printf("Enter any string: "); void main( ) gets(str); { int a, b; ptr=&str[0]; printf(\nEnter first numbers); for (i=0;str[i]!=NULL;ii++++,ptr++z) scanf(%d,&a); { printf(\nEnter second numbers); if ((*ptr>='a' && *ptr<='z') || (*ptr>='A' && scanf(%d,&a); *ptr<='Z')) swap(&a,&b); cnt++;} printf(\nAfter swapping values are :%d ptr++;
Q2. Ans.
Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
Ans. void main() { int a[5]={3,1,5,7,8}; int *p,i,j,t; p=a; clrscr(); for(i=0;i<4;i++) for(j=i+1;j<5;j++) if(*(p+i)>*(p+j)) { t=*(p+i); *(p+i)=*(p+j); *(p+j)=t; } for(i=0;i<5;i++) printf("\n %d",a[i]); getch(); }
Ans. void main() { char *a; int i,j,k; printf("enter string \n"); scanf("%s",a); int i=strlen(a); int k=--i; for(int j=0;j<k;j++) { if(a[i]!=a[j]) { printf("Not a Palindrome"); exit(0); } i--; } printf("String is Palindrome"); }
Q7. Ans.
What does function pointer mean? Give an example. The way functions return a data type, similarly, a function can return a pointer. To make this possible you had to declare the function as a pointer called function pointer. int *cube(int n) { { int *k; int r; clrscr(); r=n*n*n; k=cube(3); return &r; printf("Address of cube: %u",k); } printf("\nValue of cube : %d",*k); void main() getch(); } How does pointer arithmetic help a programmer? As you perforqm arithmetic operations using variables, the same can be done with pointers. They increase execution speed. Only two arithmetic operations can be performed on pointer that are increment and decrement. For Example: Void main() { char arr[]=University, *b; b=&arr; printf(B now points to %c,*b); b++; printf(\nNow b is pointing to %c,*b);
Q8. Ans.
Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
Q.10 Ans.
Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
Q12.
Ans. #include<stdio.h> #include<conio.h> #include<string.h> int main() { struct cust { char name[20],tele[15]; } c[50]; int i=0,counter=0; char ch='y',Name[20]; clrscr(); while(ch=='y' || ch=='Y') { puts("Enter name: ");gets(c[i].name); puts("Enter Phone: ");gets(c[i++].tele); puts("Wanna enter more record(y/n): "); fflush(stdin);ch=getch(); } getch(); ch='y'; while(ch=='y' || ch=='Y') { counter=i-1; i=0; clrscr(); puts("Enter customer name: ");gets(Name); while(i<counter) { if(strcmpi(Name,c[i].name)==0) { printf("\n\n%s %s",c[i].name,c[i].tele); break; } i++;
Write a program to accept the record of 5 employees with their names, age and addresses and also display them. Use structures to implement the program.
Ans. struct personal_info { char name[20], address[20]; int age; }; void main() { struct personal_info pi[5]; int I; for(I=0;I<5;I++) { printf(enter the name:); fflush(stdin); gets(pi[I].name); printf(enter the address:); fflush(stdin); gets(pi[I].address); printf(Enter the age:); scanf(%d,&pi[I].age); } for(I=0;I<5;I++) { printf(\n\nName :) puts(pi[I].name); printf(\n\nAddress :); puts(pi[I].address); printf(\n\nAge:); printf(%d,pi[I].age); } }
Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
Ans.
Q15. Ans.
Write a C program to define a structure with data members registeration number, student name and total marks obtained. Declare a structure pointer to read and display the value of the data members using it. #include <stdio.h> #include <conio.h> struct report { int regno,tmarks; char name[20]; }; scanf("%d",&rpt->regno); printf("Enter student name: "); fflush(stdin); gets(rpt->name); printf("Enter total marks: "); scanf("%d",&rpt->tmarks); printf("Output\n"); printf("\nRegistrationNo.:%d",rpt->regno);
Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
Ans. void main() { FILE *f1; Char c; printf(data input\n); F1=fopen(input,w); while((c=getchar())!=EOF) putc(c,f1); fclose(f1); printf(\n\ndata output\n\n); F1=fopen(input,r); While((c=getc(f1))!=EOF) printf(%c,c); fclose(f1);
Ans. void main() { char ch; FILE *fp; fp=fopen("myfile.txt","w"); clrscr(); printf("\ntype the text and press x to end.\n\n"); while((ch=getchar())!='x') putc(ch,fp); fclose(fp); }
}
Q18. Ans. Explain with examples the various file handling functions. Files are permanent storage medium of information so that they can be accessed and altered whenever needed. There are some function examples to handle the files. 1. fopen(): To create, process, read or write a file, the first step we require is to open a file. For opening a files a function fopen() is used. For Example: FILE *fp; Fpfp=fopen(filename,mode); 2. fclose(): We have the function fclose() for closing the file. This function closes the existing opened file. For Example: Fclosefclose(fp); 3. getc(): This functions is used to reads character by character from the file. It return an end-of-file (EOF) marker when the end of the file has been reached ior or if there is any error. For Example: Char c; Cc=fgetc(fp); 4. putc(): It is used to write character by character to the file opened. For example: Char c=$; Putcputc(c,fp); 5. fgets(): This function, reads the character from the file pointer into the character array until a newline character is read, or end-of-file is reached. It then appends the terminating null character after the last character reads and returns string if end-of-file occurs before reading any character, and error occurs during input fgets() returns NULL.
Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
6. fputs(): this function writes to the file pointer except the terminating null character
of string. It returns end-of-file if an error occurs during the output otherwise it returns a non-negative value. For Example: Fputsfputs(const char *s, FILE *fp) 7. fprintf(): this function gives the output on file which the file pointer is pointing to string represents the variable whose values are to be printed. For example: Fprintffprintf(fp, format, variables)
8. fscanf(): This function reads from the file to which the file pointer is pointing. It
returns the values that has been read. For example: Fscanffscanf(fp, format, variables) 9. fread(): This function reads data into an array up to Nitems data items each of size size from the file pointer. If there is an error in reading the file, it return endof-file the number of characters successfully read advances the file position indicator. For example Freadfread(p, size, Nitems, fp) 10. fwrite(): This function appends at the most Nitems item each of the size size in the file of which the file pointer points to, from the array to which the pointer points to. The functions returns the number of items if written successfully, otherwise, returns end-of-file if an error is encountered. For example Fwrite(p, size, Nitems, fp) Q19. Write a C program to create an unformatted master file. Q20. of a Write a program to copy contents
Ans. file to another. #include<stdio.h> Ans. #include<conio.h> #include<stdio.h> #include<process.h> #include<conio.h> int main() #include<process.h> {FILE *fptr; int main() char ch; {FILE *fptr,*fptr1; clrscr(); char ch; fptr=fopen("datafile.txt","w"); clrscr(); if(fptr==NULL) fptr=fopen("datafile.txt","r"); { fptr1=fopen("file.txt","w"); puts ("Error in opening file...\nPress any key if(fptr==NULL) to { break..."); puts ("Error in opening file...\nPress any key getch(); to exit(0); break..."); } getch(); while(ch!='\r') exit(0); Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
Q21. Ans.
What are a buffer and error file handler? Buffer: A storage area for data that is used to compensate for a speed difference, when transferring data from one device to another. Usually refers to an area reserved for I/O operations, into which data is read, or from which data is written. It is a storage device, or area on a storage device, which holds data temporarily, until needed for processing or printing. A buffer can also be used to aid communication between two devices with very different processing speeds (such as two modems, or the CPU and the printer). Error File Handler: It is possible that an error may occur during I/O operations on a file. Typical error situations include. 1. 2. 3. 4. Trying to read beyond the end-of-file mark. Device overflow. Trying to use a file that has been opened. Trying to perform an operation on a file, when the file is opened for another type of operation. 5. Opening a file with an invalid filename. 6. Attempting to write to a write-protected file. If we fail to check such read and write errors, a program may behave abnormally when an error occurs. An unchecked error may result in a premature termination of the program or incorrect output. Fortunately, we have two status-inquiry library functions, feof() and ferror() that can help us detect I/O errors in the files. What are command line arguments? Explain them using appropriate example. It is often useful to give program arguments when it is running. This is done with command line arguments, which are arguments of main(). There are two arguments: an integer, argc, which is the number of items in the command line (including the program name), and an array of strings, *argv[], which are the actual items. The first string, argv[0], is the name of the function being executed. If a number is needed, it has to be obtained using sscanf(), which is the same as scanf() except it takes a string as its first argument. This example prints the square root of a number. main(int argc, char *argv[]) /** program to find sqrt(x) **/ { float x; if (argc == 2) { sscanf(argv[1], "%f", &x); printf("the square root of %f is %f\n", x, sqrt(x)); } else
Q22. Ans.
Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
Q2. Ans.
Q3. Ans.
Q4. Ans.
Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
Q6. Ans.
What is the difference between a compiler and an interpreter? Compiler and interpreter both are used to convert the high-level language program into the object source code. But Interpreter:- Interprets the program in line by line Compiler: - Compiles the whole program at a time. What do you mean by keywords? Give some examples. Keywords are the reserved words with some specified function. We cannot use keywords as variable names. In C there are 32 keywords For example if we declare variable like this. C display errors int float; int if; int printf; Error: Too many types of declaration Error: Declaration terminated incorrectly Error: Call of non-function
Q7. Ans.
Explain the difference between if-else and switch. Advantages of switch over if-else: 1. Switch statement reduces the complexity of the problem Dealt dealt with. 2. User can verify various cases depending upon The the nature of the problem. Cases can include check for Specific specific values, in between values etc.
3. The if-else statement could become more complex while dealing with
larger Number number of conditions. Advantages of if-else over switch: 1. In switch, we can check only for equal values not for in-between values. But it is not in case of if-else. 2. In switch, we cant check for floating values, but in if-else we can check for any type of value. Q8. What is the role of goto? Ans. The goto statement is used to alter the normal sequence of program execution by transferring the control to some of the defined level in the program. Or we can say goto statement is used along with a label. when When the it falls to the line containing the goto statement , it jumps automatically to the same corresponding label.
Program Example Vvoid main( ) { int I,f=1; Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
Q9. Ans.
Q10. Ans.
Q11. Ans.
Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100
Q13. Ans.
Explain the scope of storage classes. Available storage classes for variables are Automatic: declared when entering the block, lost upon leaving the block; the declarations must be the first thing after the opening brace of the block Static: the variable is kept through the execution of the program, but it can only be accessed by that block Extern: available to all functions in the file AFTER the declaration; use extern to make the variable accessible in other files Register: automatic variable which is kept in fast memory; actual rules are machine-dependent, and compilers can often be more efficient in choosing which variables to use as registers. The scope of an object can be local to a function or block, local to a file, or completely global. Local to a function/block: automatic or static variables, which can only be used within that function/block. Function parameters (arguments) are local to that function. Global (local to a file): static variables declared outside all functions, accessible to any functions following the declaration External (accessible in several files): declared as extern, accessible to functions in that file following the declaration, even if the variable is defined in another file.
Prepared By:- Vaishnoo Maa Computers, SCO 145, Chotti Baradari, Patiala. Ph. 0175-2205100, 2215100