C输入输出
c语言的输入输出都是包含在头文件stdio.h中
- scanf() 与 printf()
scanf() 函数用于从标准输入(键盘)读取并格式化
printf() 函数发送格式化输出到标准输出(屏幕)
#include <stdio.h>
int main()
{
int a = 0;
char b = 'a';
char str[100] = {0};
printf("input a b str:\n");
scanf("%d %c",&a,&b); //',' in %d and %c occer printf fmt
scanf("%s",str);
printf("a =%d,b = %c,str=%s\n",a,b,str);
return 0;
}
~
input a b str:
2222 fffffff
a =2222,b = a,str=fffffff
注意:注意上面的scanf("%d,%c,%d",&a,&b,&str);中%d,%d,%d之间有逗号,在输入数据时也要加逗号,如果去掉逗号,输入时就不用逗号,而用空格,tab键或回车键将各个数据隔开。
- getchar() 与 putchar()
getchar()
原型:int getchar(void)
函数从屏幕读取下一个可用的字符,并把它返回为一个整数。这个函数在同一个时间内只会读取一个单一的字符
putchar()
原型:int putchar(int c)
把字符输出到屏幕上,并返回相同的字符。这个函数在同一个时间内只会输出一个单一的字符。
int c;
printf( "Please input a value :");
c = getchar( );
printf( "\nYou input value: ");
putchar( c );
printf( "\n");
[root@localhost Desktop]# ./test
Please input a value :123
You input value: 1
- gets() 与puts 函数:
gets()
原型:char *gets(char *s)
函数从 stdin 读取一行到 s 所指向的缓冲区,直到一个终止符或 EOF。
gets函数功能:从键盘上输入字符,直至接受到换行符或EOF时停止,并将读取的结果存放在buffer指针所指向的字符数组中。读取的换行符被转换为null值,做为字符数组的最后一个字符,来结束字符串。
puts()
原型:int puts(const char *s)
函数把字符串 s 和一个尾随的换行符写入到 stdout。
注意:gets函数由于没有指定输入字符大小,所以会无限读取,一旦输入的字符大于数组长度,就会发生内存越界,从而造成程序崩溃或其他数据的错误。
printf("Input :");
gets(str);
printf("vaule:");
puts(str);
[root@localhost Desktop]# ./test
Input :lslsll
vaule:lslsll
- fgets 和 fputs
char *fgets(char *s, int size, FILE *stream);
fgets函数功能:从文件指针stream中读取字符,存到以s为起始地址的空间里,直到读完size-1个字符,或者读完一行。
注意:调用fgets函数时,最多只能读入size-1个字符。读入结束后,系统将自动在最后加’\0’,并以str作为函数值返回
char str[5];
printf("Input:");
fgets(str,5,stdin); //fgets()函数,stdin ,only read 4 bytes;
printf( "vaule:");
puts(str);
[root@localhost Desktop]# ./test
Input:lslls
vaule:lsll
linux 下的man page:
#include <stdio.h>
int fgetc(FILE *stream);
char *fgets(char *s, int size, FILE *stream);
int getc(FILE *stream);
int getchar(void);
char *gets(char *s);
int ungetc(int c, FILE *stream);
DESCRIPTION
fgetc() reads the next character from stream and returns it as an unsigned
char cast to an int, or EOF on end of file or error
getc() is equivalent to fgetc() except that it may be implemented as a macro
which evaluates stream more than once.
getchar() is equivalent to getc(stdin).
gets() reads a line from stdin into the buffer pointed to by s until either a
terminating newline or EOF, which it replaces with a null byte (’\0’). No
check for buffer overrun is performed (see BUGS below).
fgets() reads in at most one less than size characters from stream and stores
them into the buffer pointed to by s. Reading stops after an EOF or a new‐
line. If a newline is read, it is stored into the buffer. A terminating
null byte (’\0’) is stored after the last character in the buffer.
ungetc() pushes c back to stream, cast to unsigned char, where it is avail‐
able for subsequent read operations. Pushed-back characters will be returned
in reverse order; only one pushback is guaranteed.#include <stdio.h>
int fputc(int c, FILE *stream);
int fputs(const char *s, FILE *stream);
int putc(int c, FILE *stream);
int putchar(int c);
int puts(const char *s);
DESCRIPTION
fputc() writes the character c, cast to an unsigned char, to stream.
fputs() writes the string s to stream, without its terminating null byte
(’\0’).
putc() is equivalent to fputc() except that it may be implemented as a macro
which evaluates stream more than once.
putchar©; is equivalent to putc(c,stdout).
puts() writes the string s and a trailing newline to stdout.
.
C++输入输出
包含在头文件中,该文件定义了 cin、cout、cerr 和 clog 对象,
分别对应于标准输入流、标准输出流、非缓冲标准错误流和缓冲标准错误流。
- 标准输入流(cin)
cin cout 是 iostream 类的一个实例。cin 对象附属到标准输入,cout 是标准输出,cin 是与流提取运算符 >> 结合使用,cout 与<< 结合使用。
char name[50];
int a,b,c=0;
cout << "请输入name a b c:";
cin >> name;
cin >> a >> b >>c;
cout << "name:" << name << endl;
cout<< a << endl<< b << endl << c <<endl;
[root@localhost Desktop]# ./test_inout
请输入name a b c:lsl 1 2 3
name:lsl
1
2
3
GO语言输入输出
Go fmt包下有三个函数,可以在程序运行过程中获取用户输入:
输入类型 解释
fmt.Scan 获取输入
fmt.Scanf 获取输入,但是可以指定格式,go语言会根据格式解析参数
fmt.Scanln 获取一行的输入,只会获取到一行
go fmt包下有三种类型输出函数:
输出类型 解释
fmt.Print 输出
fmt.Println 输出并且换行
fmt.Printf 按照指定格式输出
package main
import "fmt"
func main(){
fmt.Println("Please input a b:")
var a,b,c,d string
fmt.Scan(&a,&b) //输入多个数据可以回车或者空格
fmt.Scanln(&c) //只能接受一个回车
fmt.Scanf("%s",&d) //格式化输入fmt
fmt.Println(a,b,c,d)
}
D:\GO\GO_WORKSPACE\bingfa>bingfa.exe
Please input a b:
1111 3333 4444 5555555
1111 3333 4444 555555
Python3 输入和输出
后期输出
Python2 输入和输出
后期输出