C language standard input and output

A, putchar and getchar

These two functions are processed to individual characters, a character in the putchar is a standard output device, and a character getchar is made on a standard input device, we look at the following example, the input characters from the keyboard, and is displayed on the display, when faced with the letter x, the program exits:

#include <stdio.h>

main()

{

char ch = 0;

while (ch != ‘x’)

{

ch = getchar();

putchar(ch);

}

}

a

a

b

b

x

x

Two, puts and gets

This is for two string manipulation functions, the puts a character string is displayed on the standard output device, and gets a string is acquired from the standard input device. Let's look at how to use them:

#include <stdio.h>

main()

{

char str[20];

gets(str);

puts(str);

}

Hello World!

Hello World!

Note that this definition of the char str [20] is a defined character array has 20 elements, can not be defined as char * str; then want the gets (str); from the keyboard to the input string str. Here it comes to the relationship between arrays and pointers, we will have a special principle arrays and pointers chapter is concerned.

Three, printf and scanf

Wherein the format is a printf function input, meaning that the form definition beginning%, the letters represent the following:

d output signed integers in decimal form (not the number of output symbols n)

o Output octal unsigned integer (not output the prefix 0)

x, X output in hexadecimal unsigned integer (not output the prefix Ox)

u output unsigned integer in decimal form

f output unit in decimal form, double precision real

e, E exponentially output single and double precision real

g, G or in% f% e output the shorter width of the output single and double precision real

c output a single character

s output string

  • The results left-justified, fill the space on the right
  • Output sign (plus or minus)

Output value is positive preceded by a space, known as a negative number is negative

About scanf is opposite printf, it is the function according to the input format, for example:

#include <stdio.h>

main()

{

char ch;

int a,b;

float c,d;

char str[20];

scanf("%c,%d,%d,%f,%f,%s", &ch, &a, &b, &c, &d, str);

printf("%c,%d,%d,%f,%f,%s\n", ch, a, b, c, d, str);

}

a,1,2,3.3,4.4,Hello

a,1,2,3.300000,4.400000,Hello

About scanf There is also a need to place the reader's attention, before calling scanf function when we passed its parameter variables to join an ampersand to represent the memory address of this variable, with the exception of array variables.

Published 261 original articles · won praise 4 · Views 4260

Guess you like

Origin blog.csdn.net/it_xiangqiang/article/details/105206361