mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
Fix stack setting after error return. Some fixes to examples.
This commit is contained in:
committed by
Christoffer Lerno
parent
b1d83e2ccd
commit
bb28f6e61c
25
resources/examples/binarydigits.c3
Normal file
25
resources/examples/binarydigits.c3
Normal file
@@ -0,0 +1,25 @@
|
||||
module binarydigits;
|
||||
import std::math;
|
||||
import libc;
|
||||
fn void main()
|
||||
{
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
String s = bin(i);
|
||||
defer s.destroy();
|
||||
libc::printf("%s\n", s.zstr());
|
||||
}
|
||||
}
|
||||
|
||||
fn String bin(int x)
|
||||
{
|
||||
int bits = 1 + (int)(x == 0 ? 0 : math::log10((double)(x)) / math::log10(2));
|
||||
String str;
|
||||
str.append_repeat('0', bits);
|
||||
for (int i = 0; i < bits; i++)
|
||||
{
|
||||
str.set((usize)(bits - i - 1), x & 1 ? '1' : '0');
|
||||
x >>= 1;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
Reference in New Issue
Block a user