In the previous few posts, you'll find that I've used the printf () phrase many times, so today we'll talk about how to use the printf () statement.
First, let's look at the basic format of printf:
printf("输出的文字");printf("输出格式",需要输出的变量);
Let's start by demonstrating the first way to use:
#include<stdio.h>int main(void){ printf("Hello World");}
The program output is Hello World, this usage is very good understanding, no longer too much introduction. Let's focus on the second usage:
#include<stdio.h>int main(void){ int a; a=1; printf("%d",a);}
The program outputs a result of 1. Let's start with the output format, the output format is represented by a format character, and his laws are as follows:
Integral type data
format Characters |
Description |
%d |
Signed integer in decimal number (positive not output symbol) |
%o |
unsigned integers in octal (not output prefix 0) |
%x,%x |
unsigned integers in hexadecimal numbers (not output prefix 0x) |
%u |
Sign unsigned integers in decimal numbers |
Real-data
format Characters |
Description |
%f |
Output single, double-precision real numbers in decimal form |
%e,%e |
Output single, double-precision real numbers in exponential form |
Character type data
format Characters |
Description |
%c |
Output a single character |
%s |
Output string |
In other words, what format do you want to output in "", and then write the variables that need to be output.
C-language printf () statement (top)