Languages-C & C Plus Plus [TCS Placement]: Sample Questions 25 - 26 of 119

Get unlimited access to the best preparation resource for competitive exams : get questions, notes, tests, video lectures and more- for all subjects of your exam.

Question 25

C & C Plus Plus
Edit

Describe in Detail

Essay▾

Is there another function which can be used to convert an integer or a float to a string?

Explanation

  • Some implementations provide a nonstandard function called item () , which converts an integer to string.
  • #include char ⚹ item (int value, char ⚹ string, int radix) ;
  • DESCRIPTION:-The item () function constructs a string representation of an integer.
  • PARAMETERS:-
    • value: Is the integer to be converted to string representation.
    • string: Points to the buffer that holds resulting string. The resulting string may be as long as seventeen bytes.
    • radix: Is the base of the number; must be in the range 2 - 36.
  • A portable solution also exists. One can use sprintf () : char s [SOME_CONST] ; int i = 10; float f = 10.20; sprintf (s, “% d % f” , i, f) ;

Question 26

C & C Plus Plus
Edit

Describe in Detail

Essay▾

What is the difference between printf () and sprintf () ?

Explanation

printf ()

  • printf is used to print text (string/character stream) or values on standard output device.
  • Syntax:- int printf (const char ⚹ format, …) ;
  • “Format” provides the format of the text string to point on the output device with the help of specifier like % s, % d, % f etc.
  • “…” Provides the list of arguments to be print.
  • Return type int returns total number of printed characters on the output screen.
  • Example:
  1. #include<stdio.h>
  2. Intmain()
  3. {
  4.     Printf(“hello, hii”);
  5.     Return0;
  6. }

Output:- hello, hii

sprintf ()

  • sprintf id used to send (copy) formatted text (string or charter stream) to string.
  • Syntax:- int sprintf (char ⚹ str, const char ⚹ format, …) ;
  • Here, char ⚹ str- is character array in which formatted text will be sent.
  • “Format” provides the formatted text with the help of format specifiers.
  • “…” provides the list of arguments to be print.
  • Return type int returns total number of copied characters into the char ⚹ str.
  • Example:
  1. #include<stdio.h>
  2. intmain()
  3. {
  4.     Sprintf((char⚹)str, “Mynameis %s, Iam %dyearold.”, “Krishna”, 20);
  5.     Printf(“Textis:%s”, str);
  6.     Return0;
  7. }

Output:- My name is Krishna, I am 20 years old.

Developed by: