Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
November 29, 2013 Is this a bug ? | ||||
---|---|---|---|---|
| ||||
Here is my code : It compiles but in run time terminal shows a segmentation fault. Am I doing something wrong ? Or do I have to file a Bug ? -------------------------------------------------------------------- import std.stdio : write, readf; void funcion(int a, int t) { int temp, bd, an; temp=t; temp*=20; temp+=402; temp*=5; temp+=3; bd=temp-a; temp=t; temp*=5; temp+=50; temp*=20; temp+=1013; an=temp-a; write(" %d: %d\n", bd, an); } void main() { int r, f, count, b; write("Input your birth year : "); readf(" %d", &b); write("Input the range for the calculations (# #) : "); readf(" %d %d", &r, &f); for(count=r; count<=f; count++) { funcion(b, count); } } --------------------------------------------------------------------------------- |
November 29, 2013 Re: Is this a bug ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Binarydepth | On Friday, 29 November 2013 at 00:35:30 UTC, Binarydepth wrote: > It compiles but in run time terminal shows a segmentation fault. works for me without segfaulting. What data did you input? > write(" %d: %d\n", bd, an); This should be writef instead of write. writef uses the format string, plain write just literally outputs its arguments. |
November 29, 2013 Re: Is this a bug ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Friday, 29 November 2013 at 00:40:29 UTC, Adam D. Ruppe wrote:
> On Friday, 29 November 2013 at 00:35:30 UTC, Binarydepth wrote:
>> It compiles but in run time terminal shows a segmentation fault.
>
> works for me without segfaulting. What data did you input?
>
>> write(" %d: %d\n", bd, an);
>
> This should be writef instead of write. writef uses the format string, plain write just literally outputs its arguments.
Right forgot to change that. I'm passing C code to D code and D code to C code and still make those mistakes.
So weird I get seg fault. which OS are you using ?
|
November 29, 2013 Re: Is this a bug ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | Adam D. Ruppe:
>> write(" %d: %d\n", bd, an);
>
> This should be writef instead of write. writef uses the format string, plain write just literally outputs its arguments.
A statically typed language should give a compile-time error for such mistake. It's a leftover from the weakly typed C mindset. In many cases format strings are known at compile-time.
Bye,
bearophile
|
November 29, 2013 Re: Is this a bug ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Friday, 29 November 2013 at 01:35:31 UTC, bearophile wrote:
> A statically typed language should give a compile-time error for such mistake.
It is perfectly valid to pass a string to the write function. He simply called the wrong function.
|
November 29, 2013 Re: Is this a bug ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Binarydepth | On Friday, 29 November 2013 at 01:17:36 UTC, Binarydepth wrote:
> So weird I get seg fault. which OS are you using ?
linux
|
November 29, 2013 Re: Is this a bug ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Friday, 29 November 2013 at 01:37:15 UTC, Adam D. Ruppe wrote:
> On Friday, 29 November 2013 at 01:35:31 UTC, bearophile wrote:
>> A statically typed language should give a compile-time error for such mistake.
>
> It is perfectly valid to pass a string to the write function. He simply called the wrong function.
Yeah it's ok for the function but it will print %d: %d, and then the values of the following variables. without a new line.
And actually I was intending to translate that line to write function but I forgot.
I prefer the write function I simply write : write(var, var, "\n"); or writeln(var, var); much better than formatting everything. :)
|
November 29, 2013 Re: Is this a bug ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | Adam D. Ruppe:
> It is perfectly valid to pass a string to the write function. He simply called the wrong function.
Sorry.
Bye,
bearophile
|
Copyright © 1999-2021 by the D Language Foundation