Languages [3i Infotech Placement]: Sample Questions 356 - 357 of 546

Glide to success with Doorsteptutor material for competitive exams : get questions, notes, tests, video lectures and more- for all subjects of your exam.

Question 356

Describe in Detail Essay▾

What is the use of typedef?

Edit

Explanation

  • Typedef is keyword used to assign alternative names to existing datatypes.
  • Mostly used with user defined datatype, when names of the datatypes become slightly complicated to use in programs.

Syntax for using typedef:

Typedef < existing_name >< alias_name >

Application of typedef:

  • Typedef can used to a name to user defined data type as well.
  • For example structure:

typedef struct

{

type member1;

type member2;

type member3;

} type_name;

Question 357

Describe in Detail Essay▾

What is the output of the following program?

  1. main ()
  2. {
  3. void swap ();
  4. int x =10, y =8;
  5. swap (&x, &y);
  6. printf ( “x =%d y =%d”, x, y);
  7. }
  8. void swap ()
  9. int a, int b
  10. {
  11. a^=⚹b,⚹b^=⚹a,⚹a^=⚹b;
  12. }
Edit

Explanation

In the program using ^ swaps two variables without using a temporary variable and that too in a single statement.

Table Shows the Program
void swap () ;
  • Swap is a function
  • Take any number of arguments and returns nothing.
int x = 10, y = 8;
  • Define integer variable x = 10 and y = 8
swap (&x, &y) ;
  • Call swap (&x, &y) ; that has two arguments.
printf ( “x =% d y =% d” , x, y) ;
  • printf prints the swapping value of x and y
Modern style of declaration:

void swap (int ⚹ a, int ⚹ b)

{

⚹ a ^ =⚹ b, ⚹ b ^ =⚹ a, ⚹ a ^ =⚹ b;

}

void swap ()

int ⚹ a, int ⚹ b

{

⚹ a ^ =⚹ b, ⚹ b ^ =⚹ a, ⚹ a ^ =⚹ b;

}

  • This convention is historic pre-ANSI style (referred to as Kernighan and Ritchie style) style of function declaration.
  • Swap function is defined with arguments following the () .
  • So the declaration for swap looks void swap () which means the swap can take any number of arguments.