Note: This website is archived. For up-to-date information about D projects and development, please visit

Get a number from the user (uses scanf)

Part of TutorialFundamentals


Shows how you can ask the user for a number during runtime.


import std.c.stdio; /* for scanf */
import std.stdio; /* for writef/writefln */

int main() {
  double n;

  writef("Pick a number: ");
  scanf("%lf", & n);

  writefln("You picked: %s", n);
  return 0;

Sample Batch File

@echo off
set pgm=ScanfExample
dmd %pgm%.d
erase %pgm%.obj

Tested Versions

Tested with Digital Mars D Compiler v1.0 on Windows 2000.