1

So I wrote this code using PICO through my terminal...

#include<stdio.h>
#include<stdlib.h>

int main()
{
    int lineCount =0;
    int c;

    while((c=getchar()) != EOF)
    {
            if (c=='\n'){
            ++lineCount;

            printf("%d\t%c",lineCount,c);
            }
    }
    exit(0);
}

/*print out the "c" which is the words you want to print and then print out 
the line number before every new line, think of %d as placeholder */

So now I want to use this code and apply it to a "hello world" script in C. In my terminal, I have both line_number and hello world files compiled and in 1 folder. I go to the folder and type out line_number < hello.c and it gives me an error...

-bash: line_number: command not found

my goal is to apply my line number code to my hello world script so that each line of my hello world code has a number in the beginning. My line_number code might also be faulty but if I can get help on getting the command to execute I can resolve the bugs and formatting through trial and error.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
maritaslag
  • 23
  • 1
  • 5
  • The `printf` is inside the loop, so it's going to print the count so far every time it reads a character. Incidentally, the fact that you used Pico to edit the source file has nothing to do with the problem. – Keith Thompson Jan 31 '18 at 20:58
  • CORRECTION: You print the line number every time you read a `'\n'` character, not every time you read a character. So if the input is 3 lines, it will print three lines of output: `1`, `2`, and `3`. Each line number is followed by the `'\n'` character you just read, but you never print any other characters from the input. – Keith Thompson Jan 31 '18 at 23:28

0 Answers0