Thread overview
Is this a bug ?
Nov 29, 2013
Binarydepth
Nov 29, 2013
Adam D. Ruppe
Nov 29, 2013
Binarydepth
Nov 29, 2013
Adam D. Ruppe
Nov 29, 2013
bearophile
Nov 29, 2013
Adam D. Ruppe
Nov 29, 2013
Binarydepth
Nov 29, 2013
bearophile
November 29, 2013
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
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
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
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
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
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
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
Adam D. Ruppe:

> It is perfectly valid to pass a string to the write function. He simply called the wrong function.

Sorry.

Bye,
bearophile