The difference between printf, sprintf and fprintf

Source: Internet
Author: User
Tags sprintf stdin

1:fprintf ()
#include <stdio.h>
int fprintf (FILE *stream, const char *format, ...);
The fprintf () function sends information (parameters) according to the specified format (format) to the file specified by the stream (stream). Therefore fprintf () enables the output of the information to the specified file.
Char name[20] = "Mary";
FILE *out;
out = fopen ("Output.txt", "w");
if (out!= NULL)
fprintf (out, "Hello%s\n", name);
The output format parameters are the same as printf ().
fprintf () works like printf (). The return value of fprintf () is the number of characters in the output and a negative value when an error occurs.
in some places, there is this definition: printf (...) =fprintf (stdout,...).

2:EG)
The use of the fprintf function!2007-12-13 21:46
fprintf is for file operations , the prototype is int fprintf (file *stream, const char *format [, argument] ...);

Example usage:
#include <stdio.h>
#include <process.h>

FILE *stream;

void Main (void)
{
int i = 10;
Double fp = 1.5;
Char s[] = "This is a string";
char c = ' \ n ';

stream = fopen ("Fprintf.out", "w");
fprintf (Stream, "%s%c", S, c);

fprintf (Stream, "%d\n", I);
fprintf (Stream, "%f\n", FP);
Fclose (stream);
System ("type Fprintf.out");
}

Screen output:

This is a string
10
1.500000

printf is printing a string of strings on the screen .
The prototype is int printf (const char *format [, argument] ...);
is standard output.

The usage distinction between 3:printf, sprintf and fprintf

1.PRINTF is associated with the standard output file (stdout) , fprintf does not have this limitation.

2.fprintf is for file operation , the prototype is int fprintf (file *stream, const char *format [, argument] ...);

3.SPRINTF is formatted to output to a string , fprintf is formatted to output to a stream, usually to a file .

int fprintf (FILE *stream, const char *format [, argument] ...);
int sprintf (char *buffer, const char *format [, argument] ...);


* Standard input/output stream
#include <stdio.h>

extern FILE *stdin;
extern FILE *stdout;
extern FILE *stderr;

stderr, like Stdin,stdout, is a stream.
The specific is:
STDIN is the standard input stream, which defaults to the keyboard,
StdOut is the standard output stream, which defaults to the screen,
STDERR is the standard error stream, which generally sets the screen to default, or it can be exported to a file.

Stdin,stdout can be redirected to a file, stderr output to the screen;
So if the output error message, do not use printf ("error"), fprintf (stdout, "error");
This is because the message may be redirected to a file;
To use fprintf (stderr, "error");

bitcount function to calculate the number of binary 1 of a few examples Bitcount (5) ==2 52 is 101, there are two 1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.