When i run the line counting code in the C programming language book, i do not get any output.ie nothing is returned that reflects the number of lines. The code is below:
#include <iostream>
//This program counts lines in its input
//
int main() {
int c, nl;
nl = 0;
while ((c = getchar())!= EOF )
if (c == '\n')
++nl; //nl = nl +1
printf("%d\n", nl);
}
The output is below:
/home/xxx/xxxx/lineCounting1/cmake-build-debug/lineCounting1
line1
line2
line3
line4
^D
Process finished with exit code 0
I use ctrl+D to stop the execution and get an output. But nothing (except ^D is returned). What am i doing wrong?
this is a C program so we have to include stdio.h instead of iostream.
in the main we have declared 2 variables
data type of variable name "c" should be "char" and nl is a counter which is currently set to 0.
getchar() is a library function which takes only the first character of of the string.
we are assigning the value of getchar to the c. and traversing it till end of file (EOF).
if condition is true then we are entering into the while.
there is a if condition inside the while which states that if(c == '\n') then incrementing the counter (nl) and displays the count.
code will be look like this but still it won't work because it is entering into the infinite loop
we have to assign something to the variable c.
`#include<stdio.h>
//This program counts lines in its input
int main()
{
char c;
int nl;
nl = 0;
printf("Enter the character\n");
while((c = getchar())!= EOF )
{
if (c == '\n')
{
++nl; //nl = nl +1
printf("%d\n", nl);
}
}
}`
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 beginner and I've recently started learning C and here is an example in "The C Programming Language by Brian W. Kernighan, Dennis M. Ritchie" that I don't understand. This program is supposed to count new lines in input and print out the final result. This is the exact same program that is in the book (page 19). The output of it is nothing. I can input forever and it just goes to a new line...
main()
{
int c, nl;
nl = 0;
while ((c = getchar()) != EOF)
if (c == '\n')
++nl;
printf("%d\n", nl);
}
If I put the "printf("%d\n", nl)" statement in the body of the if statement, the output would be printed each time on a new line and the value of "nl" wouldn't reset either. it just increments every time I input something and the program wouldn't terminate.
Why doesn't the example work?
int main()
{
int c, nl;
nl = 0;
while ((c = getchar()) != EOF)
if (c == '\n')
++nl;
printf("%d\n", nl);
return 0;
}
I ran it on my computer using Code::Blocks and it worked just fine. maybe you just forgot the int before main and return 0 ^^
#include <stdio.h>
#include <stdlib.h>
int main()
{
int c;
c = getchar();
while (c != EOF) {
putchar(c);
}
return 0;
}
when I compile and give input ABC and then press enter, the never ending loop starts like AAAAAAAAA....
And now look at this code below
#include <stdio.h>
#include <stdlib.h>
int main()
{
int c;
c = getchar();
while (c != EOF) {
putchar(c);
c = getchar (); // added this single line
}
return 0;
}
In this program, when I input ABC, the output is ABC.
Can anyone please explain why it is not showing just a single A as output?
Look at the below code you mentioned
int main(void){
int c;
c = getchar();
while (c != EOF) {
putchar(c);
}
return 0;
}
When c = getchar(); executes & if you provided input as ABC at runtime & press ENTER(\n), that time c holds first character A.
Next come to loop, your condition is c!=EOF i.e A!=EOF which always true & it will print A infinitely because you are not asking second time input so c holds A.
correct version of above code is
int main(void){
int c;
while ( (c = getchar())!=EOF) { /* to stop press ctrl+d */
putchar(c);
}
return 0;
}
case 2 :- Now looks at second code
int main(void){
int c;
c = getchar();
while (c != EOF) { /*condition is true */
putchar(c);
c = getchar ();/*After printing ABC, it will wait for second input like DEF, unlike case-1 */
}
return 0;
}
Can anyone please explain why it is not showing just a single A as output ? Why it should prints only A, it prints whatever input you given like ABC & so on. Just note that getchar() works with buffered input i.e when you press ENTER getchar() will read upto that & when there is nothing left to read getchar() returns EOF.
When i run this code it prints the dos 0 character(space) once, puts a space and prints dos 1 character(smiley face with white eyes and mouth).
int c = 0, b = 1;
printf("%c %c", c, b);
But when i run this code below the result is being printed twice. I either get 2 spaces or 2 smiley faces.
while(c != -1)
{
c = getchar() != EOF;
putchar(c);
}
Edit: My code doesnt have paranthesis so it compares every single entered character with EOF and assigns the result (0 or 1) to c and then prints c as a char. In order to prevent this, we can use paranthesis to force the program to do c = getchar() first and then compare that value with EOF.
while((c = getchar()) != EOF)
Your problem is that a '\n' character is not equal to EOF. The '\n' is being read in as a second character.
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;
}