Reputation: 558
Is it possible to multiply using i**
in C?
For example, I can increment i
using i++
. Why doesn't i**
work in C?
#include <stdio.h>
int main(void)
{
int result;
for (int i = 2; i < 100; i**){
result = i + 1;
printf("%i\n", result);
}
return 0;
}
Upvotes: 3
Views: 6353
Reputation: 34658
Generally, multiplication operation does not use in for
loop increment/decrement part because suppose our variable(i)
start from 0
, then every time multiplication become 0
.
Upvotes: 1
Reputation: 4314
"i++" is shorthand for "i = i + 1". If there were an "i**" it would, by extension, mean "i = i * 1" and be incredibly useless. So they never implemented it.
Even after editing to clarify grammar, it still isn't clear from your question that you expect "i**" to perform as "i = i * i". I'm guessing that is what you meant from the answer you accepted. If you learn to explain things clearly to others you will find that you think more clearly and can work out the answer to many questions for yourself.
Upvotes: 3
Reputation:
Multiplying by i**
is it possible in C? Like i++
why i**
doesn't work in C?
No,it's not possible. for your second quesition answer is explained like,
Basically , increment and decrement have exceptional usage as pre increment and post increment and a language cannot be extended just if someone needs an additional functionality as it would slow down because of extending its grammer.
So most used ++i,i++,--i,i--
are present and not others
You can use some codes like this for your task:
i*=i;
=i*i;
Upvotes: 0
Reputation: 106102
No, it's not possible. There is no operator like **
in C unlike unary increment (++
) and decrement (--
) operators. You should have try i *= i
.
Upvotes: 4
Reputation: 17698
Possible, but instead of i**
which doesn't work, you need to use:
for (int i = 2; i < 100; i *= i)
Upvotes: 1