跳到正文

科技·工程

追根溯源:C/C++是如何输入输出的?

zhuziyue321约 25 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 25 分钟

版本

  1. v1当前
C++
/*
Talk is cheap,show me the code.
Enviroment:linux 64bit
Compiler:gcc
Version:C++14
*/
int io_return;
__attribute__((always_inline))inline void read(const int&fd,void*buf,const unsigned long&count){
	__asm__ __volatile__(
		"syscall"
		:"=a"(io_return)
		:"a"(0),"D"(fd),"S"(buf),"d"(count)
		:"rcx","r11","memory"
	);
}
__attribute__((always_inline))inline void write(const int&fd,const void*buf,const unsigned long&count){
    __asm__ __volatile__(
        "syscall"
        :"=a"(io_return)
        :"a"(1),"D"(fd),"S"(buf),"d"(count)
        :"rcx","r11","memory"
    );
}
char buf[128];
int main(){
	read(0,buf,128);
    write(1,buf,io_return);
}

input:

代码
Hello,world!

output:

代码
Hello,world!

通常意义下无头文件的快读板子

C++
namespace mstd{
	long long io_return;
	__attribute__((always_inline))inline void read(const int&fd,void*buf,const unsigned long&count){
		__asm__ __volatile__(
			"syscall"
			:"=a"(io_return)
			:"a"(0),"D"(fd),"S"(buf),"d"(count)
			:"rcx","r11","memory"
		);
	}
	__attribute__((always_inline))inline void write(const int&fd,const void*buf,const unsigned long&count){
	    __asm__ __volatile__(
	        "syscall"
	        :"=a"(io_return)
	        :"a"(1),"D"(fd),"S"(buf),"d"(count)
	        :"rcx","r11","memory"
	    );
	}
	inline void read_until(const int&fd,void*buf,const unsigned long&count){
		do{
			read(fd,buf,count);
		}while(!io_return);
	}
	namespace stdin{
		constexpr unsigned long bufsiz=1048576;
		unsigned char base[bufsiz];
		long cnt=0;
		unsigned char*ptr=base;
		__attribute__((always_inline))inline unsigned char getchar(){
			return cnt--?*ptr++:(read_until(0,base,bufsiz),cnt=bufsiz-1,*(ptr=base)++);
		}
	}
	namespace stdout{
		constexpr unsigned long bufsiz=1048576;
		unsigned char base[bufsiz];
		long cnt=bufsiz;
		unsigned char*ptr=base;
		struct destruct{
			~destruct(){
				write(1,base,ptr-base);
			}
		}inst;
		__attribute__((always_inline))inline void putchar(const unsigned char&ch){
			cnt--?(cnt--,*ptr++=ch):(write(1,base,ptr-base),cnt=bufsiz-1,*(ptr=base)++=ch);
		}
		__attribute__((always_inline))inline void puts(const void*buf,const unsigned long&len){
			cnt>=len?(__builtin_memcpy(ptr,buf,len),ptr+=len,cnt-=len):(write(1,base,ptr-base),write(1,buf,len),ptr=base,cnt=bufsiz);
		}
	}
	namespace stderr{
		__attribute__((always_inline))inline void putchar(const unsigned char&ch){
			write(2,&ch,1);
		}
		__attribute__((always_inline))inline void puts(const void*buf,const unsigned long&len){
			write(2,buf,len);
		}
	}
	using stdin::getchar;
	using stdout::putchar;
	using stdout::puts;
	#if ONLINE_JUDGE
	#include<sys/stat.h>
	#include<sys/mman.h>
	char*in_ptr;
	struct init{
		init(){
			struct stat s;
			fstat(0,&s);
			in_ptr=static_cast<char*>(mmap(0,s.st_size,1,2,0,0));
		}
	}init_inst;
	#define getchar() *in_ptr++
	#endif
  	int decimal_precision=6;
	constexpr bool isspace[]={
		0,0,0,0,0,0,0,0,
		0,1,1,1,1,0,0,0,
		0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,
		1,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,
		0,0,0,0,0,0,0,0,
	},isnspace[]={
		1,1,1,1,1,1,1,1,
		1,0,0,0,0,1,1,1,
		1,1,1,1,1,1,1,1,
		1,1,1,1,1,1,1,1,
		0,1,1,1,1,1,1,1,
		1,1,1,1,1,1,1,1,
		1,1,1,1,1,1,1,1,
		1,1,1,1,1,1,1,1,
		1,1,1,1,1,1,1,1,
		1,1,1,1,1,1,1,1,
		1,1,1,1,1,1,1,1,
		1,1,1,1,1,1,1,1,
		1,1,1,1,1,1,1,1,
		1,1,1,1,1,1,1,1,
		1,1,1,1,1,1,1,1,
		1,1,1,1,1,1,1,1,
	};
	#define expand_definition {\
		unsigned char ic=getchar();\
		while(isspace[ic]){\
			ic=getchar();\
		}\
		while(isnspace[ic]){\
			*x++=ic;\
			ic=getchar();\
		}\
	}
	inline void scanf_unsigned(unsigned char*x)expand_definition
	inline void scanf_unsigned(char*x)expand_definition
	inline void scanf(unsigned char*x)expand_definition
	inline void scanf(char*x)expand_definition
	#undef expand_definition
	#define expand_definition {\
		do{\
			x=getchar();\
		}while(isspace[x]);\
	}
	inline void scanf_unsigned(unsigned char&x)expand_definition
	inline void scanf_unsigned(char&x)expand_definition
	inline void scanf(unsigned char&x)expand_definition
	inline void scanf(char&x)expand_definition
	#undef expand_definition
	#define expand_definition {\
		x=0;\
		unsigned char ic=getchar()^48;\
		while(ic>9){\
			ic=getchar()^48;\
		}\
		while(ic<=9){\
			x=(x<<3)+(x<<1)+ic;\
			ic=getchar()^48;\
		}\
	}
	inline void scanf_unsigned(unsigned short&x)expand_definition
	inline void scanf_unsigned(unsigned int&x)expand_definition
	inline void scanf_unsigned(unsigned long long&x)expand_definition
	inline void scanf_unsigned(short&x)expand_definition
	inline void scanf_unsigned(int&x)expand_definition
	inline void scanf_unsigned(long long&x)expand_definition
	inline void scanf(unsigned short&x)expand_definition
	inline void scanf(unsigned int&x)expand_definition
	inline void scanf(unsigned long long&x)expand_definition
	#undef expand_definition
	#define expand_definition {\
		x=0;\
		unsigned char ic=getchar()^48;\
		bool neg=0;\
		while(ic>9){\
			if(__builtin_expect(ic==29,0)){\
				neg=1;\
				ic=getchar()^48;\
				break;\
			}\
			ic=getchar()^48;\
		}\
		while(ic<=9){\
			x=(x<<3)+(x<<1)+ic;\
			ic=getchar()^48;\
		}\
		neg&&(x=-x);\
	}
	inline void scanf(short&x)expand_definition
	inline void scanf(int&x)expand_definition
	inline void scanf(long long&x)expand_definition
	#undef expand_definition
	#define expand_definition {\
		x=0;\
		unsigned char ic=getchar()^48;\
		while(ic>9){\
			ic=getchar()^48;\
		}\
		while(ic<=9){\
			(x*=10)+=ic;\
			ic=getchar()^48;\
		}\
		if(__builtin_expect(ic==30,1)){\
			ic=getchar()^48;\
			double decimal=1;\
			while(ic<=9){\
				x+=ic*(decimal*=0.1);\
				ic=getchar()^48;\
			}\
		}\
	}
	inline void scanf_unsigned(float&x)expand_definition
	inline void scanf_unsigned(double&x)expand_definition
	inline void scanf_unsigned(long double&x)expand_definition
	#undef expand_definition
	#define expand_definition {\
		x=0;\
		unsigned char ic=getchar()^48;\
		bool neg=0;\
		while(ic>9){\
			if(__builtin_expect(ic==29,0)){\
				neg=1;\
				ic=getchar()^48;\
				break;\
			}\
			ic=getchar()^48;\
		}\
		while(ic<=9){\
			(x*=10)+=ic;\
			ic=getchar()^48;\
		}\
		if(__builtin_expect(ic==30,1)){\
			ic=getchar()^48;\
			double decimal=1;\
			while(ic<=9){\
				x+=ic*(decimal*=0.1);\
				ic=getchar()^48;\
			}\
		}\
		neg&&(x=-x);\
	}
	inline void scanf(float&x)expand_definition
	inline void scanf(double&x)expand_definition
	inline void scanf(long double&x)expand_definition
	#undef expand_definition
	#define expand_definition {\
		puts(x,__builtin_strlen(reinterpret_cast<const char*>(x)));\
	}
	inline void printf_unsigned(char*x)expand_definition
	inline void printf_unsigned(unsigned char*x)expand_definition
	inline void printf_unsigned(const char*x)expand_definition
	inline void printf_unsigned(const unsigned char*x)expand_definition
	inline void printf(char*x)expand_definition
	inline void printf(unsigned char*x)expand_definition
	inline void printf(const char*x)expand_definition
	inline void printf(const unsigned char*x)expand_definition
	inline void printf_fixed(char*x)expand_definition
	inline void printf_fixed(unsigned char*x)expand_definition
	inline void printf_fixed(const char*x)expand_definition
	inline void printf_fixed(const unsigned char*x)expand_definition
	#undef expand_definition
	#define expand_definition {\
		putchar(x);\
	}
	inline void printf_unsigned(const char&x)expand_definition
	inline void printf_unsigned(const unsigned char&x)expand_definition
	inline void printf(const char&x)expand_definition
	inline void printf(const unsigned char&x)expand_definition
	inline void printf_fixed(const char&x)expand_definition
	inline void printf_fixed(const unsigned char&x)expand_definition
	#undef expand_definition
	#undef expand_definition
	#define expand_definition {\
		static char buf[20],*end=buf+20;\
		char*ptr=end;\
		do{\
			*--ptr=x%10|48;\
			x/=10;\
		}while(x);\
		puts(ptr,end-ptr);\
	}
	inline void printf_unsigned(unsigned short x)expand_definition
	inline void printf_unsigned(unsigned int x)expand_definition
	inline void printf_unsigned(unsigned long long x)expand_definition
	inline void printf_unsigned(short x)expand_definition
	inline void printf_unsigned(int x)expand_definition
	inline void printf_unsigned(long long x)expand_definition
	inline void printf(unsigned short x)expand_definition
	inline void printf(unsigned int x)expand_definition
	inline void printf(unsigned long long x)expand_definition
	inline void printf_fixed(unsigned short x)expand_definition
	inline void printf_fixed(unsigned int x)expand_definition
	inline void printf_fixed(unsigned long long x)expand_definition
	#undef expand_definition
	#define expand_definition {\
		static char buf[20],*end=buf+20;\
		if(x>>31){\
			putchar('-');\
			x=-x;\
		}\
		char*ptr=end;\
		do{\
			*--ptr=x%10|48;\
			x/=10;\
		}while(x);\
		puts(ptr,end-ptr);\
	}
	inline void printf(short x)expand_definition
	inline void printf(int x)expand_definition
	inline void printf(long long x)expand_definition
	inline void printf_fixed(short x)expand_definition
	inline void printf_fixed(int x)expand_definition
	inline void printf_fixed(long long x)expand_definition
	#undef expand_definition
	#define expand_definition {\
		static char buf[40],*end=buf+20,*start=buf+21;\
		char*ptr=end;\
		double y=__builtin_fmod(x,1.0);\
		while(x>=1){\
			*--ptr=static_cast<int>(__builtin_fmod(x,10.0))|48;\
			x/=10.0;\
		}\
		char*lst=start;\
		*lst++='.';\
		int i=decimal_precision;\
		while(i--){\
			*lst++=static_cast<int>(__builtin_floor(y*=10))|48;\
			y=__builtin_fmod(y,1.0);\
		}\
		while(*--lst=='0');\
		if(*lst^'.'){\
			lst++;\
		}\
		puts(ptr,lst-ptr);\
	}
	inline void printf_unsigned(float x)expand_definition
	inline void printf_unsigned(double x)expand_definition
	inline void printf_unsigned(long double x)expand_definition
	#undef expand_definition
	#define expand_definition {\
		static char buf[40],*end=buf+20,*start=buf+21;\
		char*ptr=end;\
		if(x<0){\
			putchar('-');\
			x=-x;\
		}\
		double y=__builtin_fmod(x,1.0);\
		while(x>=1){\
			*--ptr=static_cast<int>(__builtin_fmod(x,10.0))|48;\
			x/=10.0;\
		}\
		char*lst=start;\
		*lst++='.';\
		int i=decimal_precision;\
		while(i--){\
			*lst++=static_cast<int>(__builtin_floor(y*=10))|48;\
			y=__builtin_fmod(y,1.0);\
		}\
		while(*--lst=='0');\
		if(*lst^'.'){\
			lst++;\
		}\
		puts(ptr,lst-ptr);\
	}
	inline void printf(float x)expand_definition
	inline void printf(double x)expand_definition
	inline void printf(long double x)expand_definition
	#undef expand_definition
	#define expand_definition {\
		static char buf[40],*end=buf+20,*start=buf+21;\
		char*ptr=end;\
		if(x<0){\
			putchar('-');\
			x=-x;\
		}\
		double y=__builtin_fmod(x,1.0);\
		while(x>=1){\
			*--ptr=static_cast<int>(__builtin_fmod(x,10.0))|48;\
			x/=10.0;\
		}\
		char*lst=start;\
		*lst++='.';\
		int i=decimal_precision;\
		while(i--){\
			*lst++=static_cast<int>(__builtin_floor(y*=10))|48;\
			y=__builtin_fmod(y,1.0);\
		}\
		puts(ptr,lst-ptr);\
	}
	inline void printf_fixed(float x)expand_definition
	inline void printf_fixed(double x)expand_definition
	inline void printf_fixed(long double x)expand_definition
	#undef expand_definition
	template<typename T,typename...Args>
	__attribute__((always_inline))void scanf(T&x,Args&...args){
		scanf(x);
		scanf(args...);
	}
	template<typename T,typename...Args>
	__attribute__((always_inline))void scanf_unsigned(T&x,Args&...args){
		scanf(x);
		scanf_unsigned(args...);
	}
	template<typename T,typename...Args>
	__attribute__((always_inline))void printf(const T&x,const Args&...args){
		printf(x);
		printf(args...);
	}
	template<typename T,typename...Args>
	__attribute__((always_inline))void printf_unsigned(const T&x,const Args&...args){
		printf_unsigned(x);
		printf_unsigned(args...);
	}
	template<typename T,typename...Args>
	__attribute__((always_inline))void printf_fixed(const T&x,const Args&...args){
		printf_fixed(x);
		printf_fixed(args...);
	}
	inline void setprecision(const int&new_precision){
		decimal_precision=new_precision;
	}
}
int n,a,sum;
int main(){
	mstd::scanf(n);
	while(n--){
		mstd::scanf(a);
		sum+=a;
	}
	mstd::printf(sum);
}
原文