Memory fault(coredump)

I am writing a program that copies a program and prints the program with a line count.

this is the program I wrote:

#include <stdio.h>

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

        while((c = getchar()) != EOF){
                if(c = '\n'){
                   nl_cnt++;
                   printf("%10s ", nl_cnt);}

                 else{
                   putchar(c); }
        }

} 

I am testing the program by doing a line count for Hello World.

 

#include <stdio.h>

main()
{
        printf("Hello World\n");
        return 0;
}

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

%10s is for strings but nl_cnt isn't of this type.

changing the s to a d helped a lot. thanks!

this is my new code:

#include <stdio.h>

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

        while((c = getchar()) != EOF){

                if(c == '\n'){
                nl_cnt++;
                printf("%d ", nl_cnt);}

                putchar(c);

        }

}

this is the output I am getting:

$ ./line_number < hello.c
1 
#include <stdio.h>2 
3 
main()4 
{5 
    printf("Hello World\n");6 
}7 
8 
9 

how do I get the numbers and the code to line up correctly?

#include <stdio.h>

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

        while((c = getchar()) != EOF){

                if(c == '\n'){
                nl_cnt++;
                printf("%d ", nl_cnt);}

                putchar(c);

        }

}

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:

  1. 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.
  2. 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)
  3. is also acceptable
  4. /
    ```

    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.