Register | Sing In

New posts

Stacks in C programming Linked Lists in C programming File Input/Output in C programming Check prime numbers in C Programming C Programming Enumeration C Programming Union

Archives

2013-10 2013-09 2013-08

Function sprintf in C programming

September 10, 2013 Posted by: GPA Views: 764 0 comments

Problem

Write simple program in C using function sprintf.

Solution

In this article, you will know how to print formatted data into array str - an array of characters. You should include header file <stdio.h> for this program.

Declaration:

int sprintf( char *str, const char *format, ... );

The output information is stored in the pointer of char str. It does not print information on the screen. Returns the number of characters written to str.

Example:

   


#include <stdio.h>
#include <stdlib.h>
//main function
int main(int argc, char *argv[])
{
  char str[100]; //create char array
  double firstNumber;//declare variable for first Number
  double secondNumber;//declare variable for second Number
  printf("globalproganswer.com\n");
  printf("Enter first double value: ");//promt user to enter first double value
  scanf("%lf",&firstNumber);//read first number from keyboard
  printf("Enter second double value: ");//promt user to enter second double value
  scanf("%lf",&secondNumber);//read second number from keyboard
  //call function sprintf
  sprintf(str, "First number is: %0.2f\nSecond number is: %0.2f", firstNumber, secondNumber);
  //show result
  printf("Result of sprintf function is:\n%s\n\n",str);
  system("PAUSE");//delay
  return 0;
}

Output

Function sprintf

Download this example

0 Comments...

Leave a Reply

Please, Sing In to leave a Reply for this material.



Online Chat

LiveZilla Live Help

Popular posts

Function strtok in C programming Check prime numbers in C Programming C Programming Union Linked Lists in C programming File Input/Output in C programming C Programming Enumeration

RSS-subscribe

New materials

Subscribe