Recent Changes - Search:

Oktatás

* Programozás 1
  + feladatsor
  + GitHub oldal

* Szkriptnyelvek
  + feladatsor
  + quick link

Teaching

* Programming 1 (BI)
  ◇ exercises
  ◇ quick link

teaching assets


Félévek

* 2024/25/2
* archívum


Linkek

* kalendárium
   - munkaszüneti napok '20
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C++
* C#
* Clojure
* D lang.
* Java
* Nim
* Scala


[ edit | logout ]
[ sandbox | passwd ]

Reading from stdin

See std.stdio.

(1) readf()
#include <stdio.h>

int main()
{
    int a, b;

    printf("N1: ");
    scanf("%d", &a);
    printf("N2: ");
    scanf("%d", &b);
    printf("Sum: %d\n", a + b);

    return 0;
}
#!/usr/bin/env rdmd

import std.stdio;  // readf()

void main()
{
    int a, b;

    write("N1: ");
    readf(" %s", &a);
    write("N2: ");
    readf(" %s", b); // the & can be omitted
    writefln("Sum: %s", a + b);
    // writefln("Sum: %d", a + b);  // also good
}
readf(" %s", &a);

Notes:

  • The space before %s means: skip the whitespaces. It's always a good idea to do this, otherwise a previous newline character can stuck in the stdin and will cause problems when you want to use readf() again.
  • The %s placeholder is different from C's %s! Here it means: suitable, i.e. convert the value to a suitable type. It's a joker solution. D looks at the type of the variable (here: int), and the value will be converted to this type.
  • In writefln(), %s means the same as before: suitable type. It'll mean the type of the value that we want to print (here: int). Of course, you can also specify the type manually (like %d for instance).

Example

(1) get_int()
import std.stdio;

int get_int(const string msg)
{
    int n;

    write(msg);
    readf(" %s", n);
    return n;
}

void main()
{
    int n = get_int("n: ");
    writefln("n is %s", n);
}
n: 7
n is 7
Cloud City

  

Blogjaim, hobbi projektjeim

* The Ubuntu Incident
* Python Adventures
* @GitHub
* heroku
* extra
* haladó Python
* YouTube listák


Debrecen | la France


[ edit ]

Edit - History - Print *** Report - Recent Changes - Search
Page last modified on 2025 June 27, 12:14