0

I'm new to c programming. I'm trying to print numbers 1 to 10.

#include <stdio.h>

int main(){
    int i;
    for(i=1; i<11; i++){
        printf("%s\n", i);
    }
    getchar();
}

It compiles in powershell when I type: gcc .\forloop.c but when I try to run the program with ./a I get this error message:

a.exe has stopped working

Any help would be appreciated.

2
  • 1
    Turn on some more compiler warnings! Commented Apr 23, 2015 at 1:30
  • @CarlNorum I'm using the -Wall option to compile now. Thanks for the tip. Commented Apr 23, 2015 at 3:51

1 Answer 1

4
printf("%s\n", i);

That tries to print a string. i is an integer. So it will crash when it dereferences i as a string.

Try

printf("%d\n", i);
Sign up to request clarification or add additional context in comments.

1 Comment

@AlanAu so we agreed to disagree, then let's please delete all these comments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.