both programs compile with gcc. My program that copies and counts lines is line_number.c and is compiled as line_number. I have to use ./ or my terminal can't find the file.
To read the hello world program I type at the command line:
./line_number < hello.c
and I get the following error message: Memory fault(coredump)
I am new to Unix and not sure how to fix this. Thanks for any help!
Cal State Northridge, Northridge(California), Dr, Gabrovsky, Comp322
Your logic is a bit off. Check if c is a newline. If it is, print c, and then print a line number. Otherwise, just print c. You might also want to print a line number before your loop so that your first line is numbered.
A couple of other things:
You should have warnings turned on for your compiler. This would have helped you catch your initial problem much easier. For example, if you were using gcc to compile, you would use (at minimum) the -Wall and -Wextra switches, e.g., text gcc -Wall -Wextra -o myprog myprog.c
Consult your manual pages for specific details about your system.
The second is more nitpicky, but still good to know. The above program is not exactly conforming C - not to C89, and not to C99. In C89, main may be declared as above, but must have an explicit return statement. Under C99, main must be declared as one of the following:
```text
int main(void)
int main(int argc, char argv[]) / int main(int argc, char **argv)
is also acceptable
/
```
Additionally, under C89, an explicit return at the end of main is required. In C99, it is not, and if not provided, a return value of zero is implied. Had you turned your compiler warnings on, it may have told you that you did not return a value from main in your 'cat -n' program.