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?
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?
You need to put your code within the for block. Remove the semicolon
For(i=1;i<=num;i++)
{
// code here
}
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.
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);
}
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.)