getchar

Input buffer flush

大兔子大兔子 提交于 2019-12-11 02:33:44
问题 Consider the code below: #include<stdio.h> int main(){ char c; while( ( c=getchar() ) != EOF ) printf("%c\n",c); return 0; } I gave the input as: hi^Z The output was: h i (an arrow pointing towards left) [Sorry, I couldn't find character for the above arrow. ] Can someone please explain the output ? Since ^Z is part of the character string and there are characters to flush, I think ^Z shouldn't have been passed and thus the output should be, h i (new line ) P.S - I am on windows and ^Z thus

Mafia

China☆狼群 提交于 2019-12-11 00:57:36
戳戳看题 这道题的核心就是教你建双向边。我们发现本题的权值是在点上的。那么我们可以将1个点拆成2个点(一个点是入,一个点是出),中间的权值为val[i]。 上马! # include <cstdio> # include <cctype> # include <queue> # include <iostream> # include <cstring> using namespace std ; const int N = 90002 , lim = ( 1 << 30 ) - 1 ; int val [ N ] , s , t , d [ N ] , cur [ N ] , n , m , cnt , nxt [ N ] , flow [ N ] , to [ N ] , head [ N ] ; inline int read ( ) { int x = 0 , f = 1 ; char s = getchar ( ) ; while ( ! isdigit ( s ) ) { if ( s == '-' ) f = - 1 ; s = getchar ( ) ; } while ( isdigit ( s ) ) { x = ( x << 1 ) + ( x << 3 ) + ( s ^ 48 ) ; s = getchar ( ) ; } return x * f ; }

Linux-C P4 输入输出

吃可爱长大的小学妹 提交于 2019-12-10 15:34:07
文章目录 Linux-C P4 输入输出 基础输入输出 字符输入(getchar) 字符输出(putchar) 格式化输入(scanf) 格式化输出(printf) 字符串输入(gets) 字符串输出(puts) 文件输入输出 读取文件中一个字符(getc、fgetc) 写入文件中一个字符(putc、fputc) 格式化读取文件数据(fscanf) 格式化写入文件数据(fprintf) 读取文件字符串(fgets) 写入文件字符串(fputs) 读取文件数据(fread) 写入文件数据(fwrite) 数据到字符串 写入格式化数据到字符串(sprintf) 写入指定尺寸的格式化数据到字符串(snprintf) 输入输出项目案例 总结 梳理 更多内容 Linux-C P4 输入输出 基础输入输出 基本输入输出会讲到经常会用到的一些 输入输出函数 ,下面先来介绍关于 字符 的输入与输出(有关字符等数据类型的内容 参见 Linux-C P2 数据类型 ) 字符输入(getchar) 内容 函数名称 getchar 所需头文件 #include <stdio.h> 函数原型 int getchar(void) 函数功能 在键盘上读取一个字符 函数传入值 无 函数返回值 成功:返回读到的字符 失败:-1 字符输入函数 (getchar),根据函数表可以看出需要调用头文件#include

getchar() and reading line by line

寵の児 提交于 2019-12-10 11:32:09
问题 For one of my exercises, we're required to read line by line and outputting using ONLY getchar and printf. I'm following K&R and one of the examples shows using getchar and putchar. From what I read, getchar() reads one char at a time until EOF. What I want to do is to read one char at a time until end of line but store whatever is written into char variable. So if input Hello, World!, it will store it all in a variable as well. I've tried to use strstr and strcat but with no sucess. while (

【AtCoder】ARC067

馋奶兔 提交于 2019-12-10 02:15:41
ARC067 C - Factors of Factorial 这个直接套公式就是,先求出来每个质因数的指数幂,然后约数个数就是 \((1 + e_{1})(1 + e_{2})(1 + e_{3})\cdots(1 + e_k)\) #include <bits/stdc++.h> #define fi first #define se second #define pii pair<int,int> #define mp make_pair #define pb push_back #define space putchar(' ') #define enter putchar('\n') #define eps 1e-10 #define MAXN 100005 //#define ivorysi using namespace std; typedef long long int64; typedef unsigned int u32; typedef double db; template<class T> void read(T &res) { res = 0;T f = 1;char c = getchar(); while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); } while(c >=

getchar() and putchar()

为君一笑 提交于 2019-12-09 17:44:14
问题 in the example: #include <stdio.h> main() { long nc; nc = 0; while (getchar() != EOF) ++nc; printf("%ld\n", nc); } I don't quite understand it. putchar() would put the character out, but why is it that after EOF it puts all the characters out, and where is it remembering all these characters? Thanks. 回答1: It's called buffering and it's done by the operating system. Usually it does line buffering where it just saves every character you put to it in memory, and then writes it all to the file

The program control flow doesn't work as expected

时光怂恿深爱的人放手 提交于 2019-12-09 03:58:54
问题 This is a problem in C . The program Control flow is not as expected. It ask to enter the character in but fail to ask to enter character x. int foo(); int main(int argc, const char * argv[]) { foo(); return 0; } int foo(){ char in; char x; printf("Do you wanna party \n"); if((in = getchar()) == 'y') printf("Go Sleep!, I was kidding\n"); else printf("Oh! you are so boaring..\n"); printf("\nOk, Another Question\n"); printf("Wanna Go to Sleep\n"); if((x = getchar()) == 'y') printf("ok lets go,

read char from console

喜你入骨 提交于 2019-12-09 02:58:05
问题 I write console application which performs several scanf for int And after it ,I performs getchar : int x,y; char c; printf("x:\n"); scanf("%d",&x); printf("y:\n"); scanf("%d",&y); c = getchar(); as a result of this I get c = '\n' ,despite the input is: 1 2 a How this problem can be solved? 回答1: This is because scanf leaves the newline you type in the input stream. Try do c = getchar(); while (isspace(c)); instead of c = getchar(); 回答2: You can use the fflush function to clear anything left

the input buffer in getchar() and scanf

↘锁芯ラ 提交于 2019-12-08 23:09:44
Sorry because I can't think of any better title I have a problem dealing with the input Here is my test code which get the input into a string array then print it on screen again, quite simple The 1st test code work fine as expected #include <stdio.h> int main() { int i, index; char d, c[20]; scanf("%d", index); for(i=0; i<index; i++){ *(c+i) = getchar(); if (*(c+i)=='$') break; } printf("the string is %s", c); return 0; } the 2nd test code, I did use pointer but c[i] instead and the program didn't run. It's quite strange since *(c+i) and c[i] is equivalent I changed *(c+i) = getchar(); // (0)

洛谷 P5690 [CSP-SJX2019]日期

倖福魔咒の 提交于 2019-12-08 21:34:34
传送门 思路 大水题一道,判断一下即可 输入直接用快读读两个数就行了,不需要读一个 \(char\) 类型的字符 年月不能为 \(0\) ,月份不能超过 \(12\) ,天数不能超过 \(31\) 另外在二月天数的时候不能超过 \(28\) ,在 \(4、6、9、11\) 月天数不能超过30 这样就好啦 代码 #include <cstdio> #include <cstring> #include <iostream> using namespace std; inline int read() { char c = getchar(); int x = 0, f = 1; for( ; !isdigit(c); c = getchar()) if(c == '-') f = -1; for( ; isdigit(c); c = getchar()) x = x * 10 + c - 48; return x * f; } int n, m, ans = 0; int main() { n = read(), m = read(); if(n > 12 || n == 0) ans++; if(m > 31 || ((n == 4 || n == 6 || n == 9 || n == 11)&&(m > 30)) || m == 0 || (n == 2 && m > 28))