3i Infotech Papers: Sample Questions 408 - 409 of 1245

Examrace Placement Series prepares you for the toughest placement exams to top companies.

Question number: 408

» Languages » C & C Plus Plus

Essay Question▾

Describe in Detail

What is the problem with the following code segment?

While ( (fgets (receiving array, 50, file_ptr) )! =EOF);

Explanation

  • 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:

Example shows the usage of fgets() function.

Example Shows the Usage of Fgets () Function.

Example shows the usage of fgets () function.

  • 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 number: 409

» Languages » C & C Plus Plus

Essay Question▾

Describe in Detail

Find the output for the following C program

  1. #define swap1 (ab)a = a + bb = a-ba = a-b;
  2. void main ()
  3. {
  4.     int x = 5y = 10;
  5.     swap1 (xy);
  6.     printf ( “%d %d ”, xy);
  7.     swap2 (xy);
  8.     printf ( “%d %d ” ,xy);
  9. }
  10. int swap2 (int aint b)
  11. {
  12.     int temp;
  13.     temp = a;
  14.     a=b;
  15.     b = temp;
  16.     return;
  17. }

Explanation

  • In the program

Table showing the program

Table showing the program

#define swap1 (a, b) a = a + b; b = a-b; a = a-b;

  • define the swap1 () function using preprocessor directive- swap the value of x and y

  1. int swap2 (int aint b)
  2. {
  3.     int temp;
  4.     temp = a;
  5.     b = a;
  6.     b = temp;
  7.     return;
  8. }
  • define the swap2 () function using actual function- swaps the values

  • define the integer variable temp

  • Temp variable stores the value of a

  • b stored in the a

  • b stored in temp

int x = 5, y = 10;

  • define the integer variable x = 5 and y = 10

swap1 (x, y);

  • Apply the swap1 () function

printf (“%d % d ”, x, y);

  • printf prints the value of x and y swapped by swap1 () function

swap2 (x, y);

  • Apply the swap2 () function

printf (“%d % d ”, x, y);

  • swap2 changes the value inside the function but does not change it outside