The difference between the puts () function and the printf function __ function

Source: Internet
Author: User

The puts () function is used only to output strings, without formatting control, which can be either directly a string or a character array name that holds the string.

The printf () function has many output formats and can be formatted with the escape characters in different formats.

The functions of the puts () function are the same as the statement printf ("%s\n", s).

Example:

①:

#include <stdio.h>

int main (void)
{
Puts ("Hello World from Puts!")   ); String, with the last implied ' "' character
}
Output
Hello World from puts!

Ii:

Main ()
{
static char a[] = {' H ', ' I ', '! ', '! '};
Puts (a);
}
The output hi!! Hot Hot Hot hot hot hot hot ironing hot dhaklhdwuhdaghdagdak ... (The back is all garbled)

Cause: A is missing a null character at the end (' the '), so it's not a string, so that puts () doesn't know when to stop the output, it will print out the contents of the memory unit after a, until it touches a null character somewhere.


③:

//==============
Cat hello.c
#include <stdio.h>
int main (void)
{
printf ("Hello world!\n");
}
//========================
Gcc-s hello.c-o Hello.s Generate assembly code
//========================
Cat Hello.s
//=========================
. File "Hello.c"
. section. Rodata
. LC0:
. String "Hello world!"
. text
. GLOBL Main
. type Main, @function
Main
Leal 4 (%ESP),%ecx
Andl $-16,%esp
Pushl-4 (%ECX)
PUSHL%EBP
MOVL%esp,%EBP
PUSHL%ECX
Subl $,%esp
MOVL $. LC0, (%ESP)
Call puts//======== this isn't printf.
Addl $,%esp
POPL%ECX
POPL%EBP
Leal-4 (%ECX),%esp
Ret
. size main,.-main
. Ident "GCC: (GNU) 4.1.2 (UBUNTU4.1.2-0UBUNTU4)"//=======GCC version
. section. Note. Gnu-stack, "", @progbits

After modifying the parameters for printf ()

//==========================
#include <stdio.h>
int main (void)
{
printf ("Hello world! Addr=0x%08x\n ", printf);
}
//==========================
Gcc-s hello.c
//======================
Cat Hello.s
===========.file "HELLO.C"
. section. Rodata
. LC0:
. String "Hello world! Addr=0x%08x\n "
. text
. GLOBL Main
. type Main, @function
Main
Leal 4 (%ESP),%ecx
Andl $-16,%esp
Pushl-4 (%ECX)
PUSHL%EBP
MOVL%esp,%EBP
PUSHL%ECX
Subl $,%esp
MOVL $printf, 4 (%ESP)
MOVL $. LC0, (%ESP)
Call printf//================= compiled assembly code is already printf
Addl $,%esp
POPL%ECX
POPL%EBP
Leal-4 (%ECX),%esp
Ret
. size main,.-main
. Ident "GCC: (GNU) 4.1.2 (UBUNTU4.1.2-0UBUNTU4)"
. section. Note. Gnu-stack, "", @progbits
Reason: The function of puts () functions in conjunction with statement printf ("%s\n", s); GCC may have optimized it.

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.