Languages-C & C Plus Plus [3i Infotech Placement]: Sample Questions 197 - 198 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 197

C & C Plus Plus
Edit

Describe in Detail

Essay▾

Predict the output or error (s) for the following:

  1. char ⚹someFun()
  2. { char ⚹temp =“stringconstant”; returntemp; } intmain() { puts(someFun()); }

Explanation

  • In the program
Table Showing the Program
puts (someFun () ) ;
  • The puts function includes someFun () function
  • int puts (const char ⚹ str) writes a string to stdout up to but not including the null character.
char ⚹ someFun ()

{char ⚹ temp = “string constant” ; return temp;}

  • The program suffers no problem and gives the output correctly because the character constants are stored in code/data area and not allocated in stack.
  • So this doesn՚t lead to dangling pointers.

Question 198

C & C Plus Plus
Edit

Describe in Detail

Essay▾

What does static variable mean?

Explanation

  • Static variable is available throughout the lifetime.
  • At the time of starting the program execution, static variables allocations take place.
  • They cover a scenario where one variable is to used by all the functions, or the variable needs to survive across function invocations.
  • Variables are declared static in a C program.
  • For variables declared in the function, the value of the variable is persisted between successive calls to functions- access qualifier limits the scope but causes the variable to exist for the lifetime of the program.
  • Function static variable is thus not seen outside the function in which it is declared but remains until the program terminates.
  • The value of variable will remain and may be seen even after calls to a function.

Developed by: