Why its showing wrong ?

Hi there,
I am trying to run a code what school going student can do !
But not sure what kind of mistake I am doing.
Take a look the program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<stdio.h>
 void main()
 {
 int X=4;
 do
 {
 printf(ā€œ%dā€,X);
 X=X+1; 
 }whie(X<=10);
    printf(ā€œ ā€);
 } 





The error massage is --expected expression before % token.

Correct answer is 4 5 6 7 8 9 10

Note : Its C language.
Last edited on
https://cboard.cprogramming.com/c-programming/182134-why-its-showing-wrong.html

You're using word processing characters for your double quotes.
There's more wrong than just the use of double quotes. Line 9 it should be while, not whie.

Using a bit of white-space for readability would also be helpful.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>

// the preferred return type for main
int main( void )
{
   int X = 4;

   do
   {
      printf( "% d", X );

      X = X + 1;
      // can be shorted to:
      // X++;
   } while ( X <= 10 );

   // add a carriage return to the output
   printf( "\n" );

   // tell the operating system everything went well
   // not required
   return 0;
}

 4 5 6 7 8 9 10

The do loop can be shortened further to:

1
2
3
    do {
        printf("% d", X);
    } while (++X <= 10);


Also note that in main(), a final return is not needed if the return value is 0.

Also, as there's now only one statement in the do body the {} aren't actually needed. You could use:

1
2
3
    do
        printf("% d", X);
    while (++X <= 10);


Just because you can doesn't mean you should.
Short code != better code
do-while without braces is cursed.
Just because you can doesn't mean you should.

:)

do-while without braces is cursed.


Well it has just been Halloween!
Last edited on
Registered users can post here. Sign in or register to post.