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.