Languages-C & C Plus Plus [3i Infotech Placement]: Sample Questions 141 - 142 of 354
Glide to success with Doorsteptutor material for competitive exams : get questions, notes, tests, video lectures and more- for all subjects of your exam.
Question 141
Describe in Detail Essay▾
What is the problem with the following code segment?
While ( (fgets (receiving array, 50, file_ptr) ) ! = EOF) ;
EditExplanation
- fgets returns a string pointer not file pointer (string pointer can be NULL not EOF) . So the correct end of file check is checking for! = NULL.
- Function char ⚹ fgets (char ⚹ str, int n, FILE ⚹ stream) reads a line from the specified stream and stores it into the string pointed to by str.
- It stops when (n-1) characters are read, the newline character is read, or the end-of-file is reached, whichever comes first.
Example:
- Let us assume, we have a text file file. txt, which has the following content. This file will be used as an input for our example program:
“We are in 2017”
- Now, let us compile and run the above program that will produce the following result:
We are in 2017
Question 142
Describe in Detail Essay▾
Find the output for the following C program
#define swap1 (a, b)a =a +b; b =a-b; a =a-b;
void main ()
{
int x =5, y =10;
swap1 (x, y);
printf ( “%d %d”, x, y);
swap2 (x, y);
printf ( “%d %d” ,x, y);
}
int swap2 (int a, int b)
{
int temp;
temp =a;
a=b;
b =temp;
return;
}
Explanation
- In the program
#define swap1 (a, b) a = a + b; b = a-b; a = a-b; |
|
|
|
int x = 5, y = 10; |
|
swap1 (x, y) ; |
|
printf ( “% d % d” , x, y) ; |
|
swap2 (x, y) ; |
|
printf ( “% d % d” , x, y) ; |
|