TCS Placement: Sample Questions 104 - 104 of 502

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 104

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: