I'm having trouble with k&r 1.5.3. Obviously I'm a complete beginner. Below is the code exactly from the book and exactly as I typed it. It compiled fine and runs. It returns characters but just never prints the line count. I'm using ssh into a Ubuntu machine. Can the key on my wife's mac not be interpreted as '\n'?
#include <stdio.h>
/*count lines in input*/
main()
{
int c, n1;
n1 = 0;
while ((c = getchar()) != EOF)
if (c == '\n')
++n1;
printf("%d\n", n1);
}
Correct. Mac uses \r as line ending: http://en.wikipedia.org/wiki/Newline
Update your code like this:
#include <stdio.h>
/*count lines in input*/
main()
{
int c, n1;
n1 = 0;
while ((c = getchar()) != EOF)
if (c == '\r') /* use \r for Macs */
++n1;
printf("%d\n", n1);
}
However
When I try to do the same, I have to Ctrl-D to enter an EOF and trigger the program to print the line count.
Related
I run the following simple C code.
int main()
{
int c;
while ((c = getchar()) != EOF)
{
putchar(c);
printf("%d\n", c);
}
return 0;
}
The output of code when I enter character A as input from keyboard is as follow:
>A
>A65
>
>10
>
Why does this code print the number 10 after each inner while loop?
Think about what you're doing when the program is asking for input.
Are you just hitting A?
No, you're hitting AEnter, and hitting the Enter key results in a newline, so your while loop is actually doing this:
1. Enter loop
2. getchar() waits for input from stdin
2. You enter 'A', hit 'Enter'
3. c is assigned '65' (integral value of 'A')
4. putchar(c) and then printf("%d\n"), so you see 'A65'
5. getchar() immediately gets next character from stdin, which is linefeed
6. c is assigned '10' (integral value of '\n')
7. putchar(c) and printf("%d\n"), so you see a newline followed by 10.
You can make your code ignore the linefeeds/non-printable characters if you want (this depends on your locale), using isprint:
#include <stdio.h>
#include <ctype.h>
int main() {
for(int c; (c = getchar()) != EOF;) {
if (!isprint(c)) {
printf("Ignoring character with value %d\n", c);
} else {
putchar(c);
printf("%d\n", c);
}
}
return 0;
}
And if I run this:
root#6f67da78fe9a:~# gcc -o chartest source.c
root#6f67da78fe9a:~# ./chartest
A
A65
Ignoring character with value 10
I'm a newcomer to programming so please forgive me for my stupid questions. I ran the following code on repl.it and JDoodle respectively.
#include<stdio.h>
int main()
{
int nc;
nc = 0;
while(getchar() != EOF)
nc = nc + 1;
printf("%d\n", nc);
}
On JDoodle, the results are always x+1 where x is the number I expected. For example, if I input 123123, the result is 7. Besides, if I leave the input empty, JDoodle tells me that Your Program may have a endless loop. Why these happen?
On repl.it, after I type the input on the right side of the screen and press Enter, there is no response. However the input copying program
int c;
c = getchar();
while(c != EOF){
putchar(c);
c = getchar();
}
works well on repl.it. Why the character counting program cannot work on repl.it? I will appreciate any help with these situations.
the following proposed code:
corrects the problem discussed in the comments to the question.
The differences between the two web pages is of no consequence to the problem seen by the OP.
And now the proposed code:
#include <stdio.h>
int main( void )
{
int nc = 0;
int ch; // the character input from stdin
while( (ch = getchar()) != EOF && '\n' != ch)
nc = nc + 1;
printf("%d\n", nc);
}
I am just learning c, and I am using Linux terminal. I have written the following simple code, but when I type the inputs, the file does not exit and thus does not count the number of characters. Can anyone help me with it? I have tried other input codes as well. It's the same with all my input related codes. What am I doing wrong? Please help.
main()
{
/* count characters in input */
printf("Type some characters and the program will count the number of characters: ");
int c = getchar();
while(c!=EOF && c!= '\n')
++c;
printf("Number of characters typed: %1d\n", c);
}
So to take notice of the helpful comments;
#include <stdio.h>
int main(){
int c;
int count = 0;
while((c=getchar()) != '\n' && c != EOF)
count++;
printf("%d\n", count);
};
This code works as expected.
I'm new to C programming and I'm having probably a simple issue.
I've looked all over the site looking for an answer to this but my no luck.
So my program runs fine if at the end of the input file there is a space. For example running the program from command line in unix
./program-name < file.txt
if the file.txt is
//with a space whith | | being a space.
The Cat Ran.| |
I get the correct output 1 1 1
if it is
The Cat Ran.(With no space I get 1 1)
//I have to use C for this program.
#include <stdio.h>
int main(void){
int i=0;
char c;
int NOV=0;
while( (c=getchar())!=EOF && c !='\n' && c !=10 ){
if( c=='a'||c=='e'||c=='i'||c=='o'||c=='u' ){
NOV++;
}
if(c==' '){
printf("%d ",NOV);
NOV=0;
}
}
printf("\n");
return 0;
}
Any Help is appreciated.
The trouble is that when you get EOF without a space, you exit the loop, and nothing after the loop prints the number of vowels.
Add if (NOV != 0) printf("%d", NOV); after the loop, perhaps.
Separately, your use of 10 is odd; '\n' is control-J or 10. You might be thinking of control-M, aka carriage return or '\r'.
Also, as Blue Pixy pointed out in a comment, you should always use int c; to receive the return from getchar() or getc() of fgetc(). The functions return an int, which may be any value that fits in an unsigned char or EOF, a negative value. You run into problems if you use char c instead.
Your program should read like this (a lot of useless clutter has been removed) :
int main(void)
{
int c;
int nov = 0;
while( (c = getchar()) != EOF && c != '\n')
{
if( c=='a'||c=='e'||c=='i'||c=='o'||c=='u' )
nov++;
}
printf("%d\n", nov) ;
return 0;
}
I have just started learning C, been reading a C textbook by Keringhan and Ritchie. There was this example in the textbook, counting characters from user input. Here's the code:
#include <stdio.h>
main()
{
long nc;
nc = 0;
while(getchar() != EOF) {
if (getchar() != 'q')
++nc;
else
break;
}
printf("%ld\n", nc);
}
The problem is, when I execute the code, if I input only one character per line, when I input "q" to break, it doesn't do so. I have to type some word per line, only after that it will break the loop. Also, it only counts the half of the characters of the word. I.e. if I input
a
b
russia
it will only print '5' as final result.
Could you please explain to me why is this happening?
This works, but only when you finish off with an Enter. So, this will count the characters until the first "q" appears. That is just how getchar() and getc(stdin) work.
#include <stdio.h>
int main() {
char c = 0;
long count = 0;
short int count_linebreak = 1; // or 0
while((c = getchar()) != EOF) {
if(c != 'q' && (count_linebreak || (!count_linebreak && c != '\n'))) {
++count;
}else if(c == 'q') {
printf("Quit\n");
break;
}
}
printf("Count: %ld\n",count);
return 0;
}
A StackOverflow question about reading stdin before enter
C read stdin buffer before it is submit