[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

How do i get pythons range function in C? I tried doing a while

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 20
Thread images: 4

File: images.jpg (16KB, 218x231px) Image search: [Google]
images.jpg
16KB, 218x231px
How do i get pythons range function in C?

I tried doing a while loop and incrementing but it gave weird results, it started counting at negative and ignored the varible i initialized.
>>
Use a for loop, genius.
>>
>>58601886
You're having problems counting from a to b in a loop?
>>
>>58601886
for i in range(100):
print("%d" % i)


Is just a glorified way of writing
i = 0
while i < 100:
print("%d" % i)
i += 1


Which is directly transferable to C:

int i = 0;
while (i < 100) {
printf("%d\n", i);
i += 1;
}


Which can be made convenient with a for loop
int i;
for (i = 0; i < 100; i += 1) {
printf("%d\n", i);
}
>>
>>58601977
Except what im trying to do is:

for i in range(2240000000,2249999999)


And C can't seem to count from a specific number like python does.
>>
>>58602027
>2240000000
Isn't that outside the range of a signed 32-bit int?
>>
>>58602027
Dude. Just use i as the starting point, and loop until your ending point.
>>
>>58602054
I guess i should just use a for loop then huh?
>>
>>58602027
That's because of arithmetic overflow you retard.

Declare i a double instead or just loop from 0 to 9999999, because 2249999999-2240000000 is still 9999999 times
>>
>>58602027
That number is too big for int, use long long or int64_t
>>
>>58602027
use a long int instead of an int
>>
File: CtRm0I.gif (2MB, 320x180px) Image search: [Google]
CtRm0I.gif
2MB, 320x180px
>>58602092
AYYYYYYYYY
>>
>>58602027
In C you have to care about the size of the variables you're using (Python does that for you automatically).

If you declare "int i" then i is required to hold at least from -32767 to 32767 (16-bit), although nowadays integers are normally 32-bit (from -2147483647 to 2147483647).

Your numbers are larger than what an int can hold. Therefore to guarantee that your code would work you would have to declare i as a long long instead:

long long int i;
for (i = 2240000000; i < 2249999999; i++)


Long long signed integers are guaranteed to hold at least from -9223372036854775807 to 9223372036854775807 (64-bits), which is more than enough for your purposes.
>>
>>58602080
Sure.
int i;
for (i = 2240000000; i < 2249999999, i++) {
printf("ayy lmao"\n");
}

Although, you can't use an int like
>>58602112
and
>>58602108
said.

I typed this whole post on my phone, I hope you're happy.
>>
>>58602225
Small mistake, integers commonly hold from -2147483648 to 2147483647 since most systems use a two's complement representation. That's not required by the standard, however.
>>
File: sjjj.png (159KB, 369x251px) Image search: [Google]
sjjj.png
159KB, 369x251px
>>58602225
So many data types so little time.
>>
>>58602249
It's not even given that int is 32-bit wide, it must only be at least 16 bits. On 286, GCC would compile ints to 16 bits, but that's quite a while ago.
>>
File: 1484623969558.jpg (50KB, 500x667px) Image search: [Google]
1484623969558.jpg
50KB, 500x667px
>>58602262
Yes, as I said. I was only correcting what I said that ints (((normally))) go from -2147483647 to 2147483647, when they actually normally go from -2147483648 instead.
>>
>>58602346
> (((normally)))
Yo, I'm slow today, is this a lisp joke?
>>
>>58603836
He's just being a /pol/tard, don't worry about it.
Thread posts: 20
Thread images: 4


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.