user4085386
user4085386

Reputation:

Why int x=08; is valid when int x=078; is invalid in `DMD`?

I was recently looking into the details of literals in the D programming language.

Because octal literals use 0 as prefix to the numbers, int x = 078; is invalid naturally. But then why int x = 08; is valid in DMD?

However, I have tested the same with GCC (C compiler) to find that both are invalid.

import std.stdio;

int main(string[] args)
{
    // int x = 078; // error here: radix 8 digit expected, not '8'
    int x = 08; // but no error here
    writeln("x = ", x);
    return 0;
}

Is this a bug in DMD? Thanks in advance.

N.B. I am using DMD32 D Compiler v2.071.2-b2 (Win32 version).

Upvotes: 5

Views: 365

Answers (1)

mitch_
mitch_

Reputation: 1436

Octal literals are deprecated in D, and should error out if used. The fact that dmd accepts 08 is indicative of a bug. If you do want to use octals, then use the template std.conv.octal

void main()
{
    import std.conv : octal;
    //int a = octal!8; //Error
    int b = octal!7; //Fine
}

Upvotes: 3

Related Questions