www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Is this a bug ?

reply "Binarydepth" <binarydepth gmail.com> writes:
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);

readf(" %d %d", &r, &f);
for(count=r; count<=f; count++)
	{
		funcion(b, count);
	}
}
---------------------------------------------------------------------------------
Nov 28 2013
parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
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.
Nov 28 2013
next sibling parent reply "Binarydepth" <binarydepth gmail.com> writes:
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 ?
Nov 28 2013
parent "Adam D. Ruppe" <destructionator gmail.com> writes:
On Friday, 29 November 2013 at 01:17:36 UTC, Binarydepth wrote:
 So weird I get seg fault. which OS are you using ?
linux
Nov 28 2013
prev sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
Nov 28 2013
parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
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.
Nov 28 2013
next sibling parent "Binarydepth" <binarydepth gmail.com> writes:
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. :)
Nov 28 2013
prev sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Adam D. Ruppe:

 It is perfectly valid to pass a string to the write function. 
 He simply called the wrong function.
Sorry. Bye, bearophile
Nov 29 2013