-7
Int i,num=10;

For(i=1;i<=num;i++);

{

Printf("%d",i);

}

Whats is the output and why is that output only single number?

  • If you know that the output is a single number, why do you ask what the output is? What did you expect as output? – mkrieger1 Oct 02 '15 at 14:35
  • since this is not the code you compiled and ran, why did you post it here? – KevinDTimm Oct 02 '15 at 14:37
  • Looks dudes...i want to know what really happened in for loop...why is it printing only one Number. Last one... Ya can also write and run it in any language – khan faizan Oct 02 '15 at 14:45
  • @khanfaizan Check the answer. Let me know if it is not clear still. –  Oct 02 '15 at 14:49
  • 1
    @khanfaizan I understand you were trying to ask about loops in the abstract, and your code is meant only as an example of the *sort* of problem you're having, but it is still very important to post *complete programs that exhibit **only** the problem you're trying to ask about*, and to ask a separate question about each problem, so we can be sure we are all talking about the same thing. Please read http://stackoverflow.com/help/mcve and http://stackoverflow.com/help/how-to-ask . And don't use pseudocode unless you're asking a pure algorithms question. – zwol Oct 02 '15 at 15:01

4 Answers4

3

You need to put your code within the for block. Remove the semicolon

For(i=1;i<=num;i++)

{
// code here
}
ergonaut
  • 6,929
  • 1
  • 17
  • 47
2

Rather than marking you down and commenting on the question, I would like to emphasise so that you understand what is going on.

int i,num=10;

for(i=1;i<=num;i++);
{
    printf("%d",i);
}

The purpose of ';' -> semi colon is to terminate a statement. So when you use it at the end of the for loop you code becomes as follows. Then it treats the printf as a separate statement and executes it irrespective of the for loop.

for(i=1;i<=num;i++)
    ;

{
    printf("%d",i);
}

Coming back to the for loop it runs 10 times and increments the value of i from 1 to 10, breaks when it is 11. The loop doesn't do anything since we have a semicolon immediately. Once the loop finishes, it comes down to the printf and displays the latest value of i which is 11. Also you do not place an '&' before i in a printf. An '&' before a variable in 'C' is used to refer to its address and u use it only in scanf when you enter a value and store it in the address of the variable.

I think you are a beginner and though many have voted down, the reason I explained it because it took more than a year to understand what happens when I place a ';' at the end of for loop.

0

The number isn't a single number,actually all the number outputs were displayed all together without any spaces between them.You should use "\n" to display each output separately to the next line.Also remove that ampersand(&) from "printf" as it shows the memory address instead of the actual number.Also no semicolon should be used in the for loop statement. Here's a correction of your code-

int i,num=10;
for(i=1;i<=num;i++){
printf("%d\n",i);
}
Taufiq Rahman
  • 5,600
  • 2
  • 36
  • 44
0

There is at least one error on almost every line of your code.

I am going to assume that you quoted only the body of your main() and that you did, in fact, #include <stdio.h> at the top.

Int i,num=10;

C is case sensitive: Int is not the same as int. The compiler should have said something like

test.c:3:3: error: use of undeclared identifier 'Int'; did you mean 'int'?
For(i=1;i<=num;i++);

Similarly, For is not the same as for. This will be interpreted as a function call, to a function named For, which you haven't bothered to declare. Since you can't put semicolons inside a function-call expression, you should get a whole cascade of error messages:

test.c:4:3: warning: implicit declaration of function 'For' is invalid in C99
test.c:4:10: error: expected ')'
test.c:4:6: note: to match this '('
test.c:4:21: error: extraneous ')' before ';'

Correcting For to for reveals another problem, which I think is the problem you were originally asking about.

test.c:4:22: warning: for loop has empty body
test.c:4:22: note: put the semicolon on a separate line to silence this warning

By putting a semicolon at the end of the loop header you made that be the body of the loop. The brace block afterward is executed only once. (C allows brace blocks to appear anywhere a regular statement could appear. This is rarely useful anymore.)

Printf("%d",&i);

And again, Printf is not the same as printf.

test.c:6:3: warning: implicit declaration of function 'Printf' is invalid in C99

But that's not the only problem with this: you are trying to print the value of the variable i but you pass printf the address of i.

test.c:6:15: warning: format specifies type 'int' but the argument has type 'int *'

The correct call would be printf("%d", i); (also you might want an \n in that format string). I suspect you have been confused by the asymmetry between printf and scanf. In most cases you do not have to take the address of each argument to printf, whereas in most cases you do have to take the address of each argument to scanf, because scanf needs to write to those variables. It's best to forget scanf exists, because it should never be used, but it is still important to understand this difference because it will come up every time a C function needs to emulate out-parameters.

All error messages from clang ("Apple LLVM version 7.0.0"; I have no idea which version of upstream LLVM that corresponds to) in -std=c99 -W -Wall -pedantic mode. You should probably use that mode yourself. (The same command line options are accepted by GCC. I don't know what the equivalent is for MSVC.)

Community
  • 1
  • 1
zwol
  • 135,547
  • 38
  • 252
  • 361
  • I knw that dude ..just assume i write only a pseudocode here..without main and all – khan faizan Oct 02 '15 at 14:59
  • 1
    If I assumed you are writing pseudocode then I couldn't help you at all, because how would I know that your pseudocode syntax is case-insensitive but otherwise *exactly* like C including all its quirks? – zwol Oct 02 '15 at 15:05