1

When I run the program the printf is not printing.

#include <stdio.h>

void main()
{
    int num, i=1 ;
    scanf("%c",&num);
    for(i=1; 1<num; i++);
        printf("Hello World");
    return 0;
}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
cmscgod
  • 11
  • 2
  • 2
    Hello and welcome to StackOverflow! What input are you typing into the program? On a side note, you need to use `%i` for `int`s instead of `%c`. – Daniel Walker Jan 12 '22 at 03:09
  • It's generally a good idea to output a newline after a message: `printf("Hello World\n");` for example. – Jonathan Leffler Jan 12 '22 at 03:14
  • it still wouldnt print hello world with those changes – cmscgod Jan 12 '22 at 03:15
  • 1
    Your loop has `for(i=1; 1 – Jonathan Leffler Jan 12 '22 at 03:18
  • Thank you everyone! – cmscgod Jan 12 '22 at 03:21
  • 1
    You should also use `int main()` if you include `return 0;` — or preferably `int main(void)`. [Only on Windows can you use `void main(void)` legitimately](https://stackoverflow.com/q/204476/15168), but you can't then have `return 0;` at the end. – Jonathan Leffler Jan 12 '22 at 03:26
  • At least gcc has a warning against misleading semicolons like this. Do yourself a favour and stop troubleshooting bugs that the compiler has already found for you, but not told you about. [What compiler options are recommended for beginners learning C?](https://software.codidact.com/posts/282565) – Lundin Jan 12 '22 at 07:42

3 Answers3

2

Remove the ; after your for loop. It acts as an empty statement and hence your printf isn't inside the for loop.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Rinkesh P
  • 588
  • 4
  • 13
1
#include <stdio.h>

void main()
{
    int num, i=1 ;
    scanf("%c",&num);
    for(i=1; i<num; i++)
        printf("Hello World");
    return 0;
}
Wang YinXing
  • 278
  • 1
  • 6
  • You might want to tell the OP where the bug was and why you only fixed that one bug, and not the others. – Lundin Jan 12 '22 at 07:43
0

Program :

#include <stdio.h>

int main(){
    int num;
    //%d means you want to read an integer value where as %c is used for reading a character
    scanf("%d",&num);
    //Remove the semicolon after for loop
    for(int i=0; i<num; i++)
        printf("Hello World\n");
    return 0;
}

Output :

$ gcc loop.c && ./a.out
4
Hello World
Hello World
Hello World
Hello World
Udesh
  • 2,415
  • 2
  • 22
  • 32