0
#include <iostream>

int main() {
    int i;
    scanf("%d", &i);
    printf("%d\n", i);
    return 0;
}

This is a simplified version of code that I stumbled upon in a blog about problem solving in c++. I noticed something strange as I thought scanf() and printf() are used with stdio.h.

https://en.cppreference.com/w/cpp/header/iostream: this site shows that scanf() and printf() is not included in iostream. But when I copied and pasted the code on Visual Studio Code and compiled it with gcc on my mac terminal, it compiled fine and ran fine. So I am confused as whether scanf() and printf() can also be used just with iostream.

François Andrieux
  • 28,148
  • 6
  • 56
  • 87
the
  • 143
  • 6
  • `scanf` and `printf` are c style io, `` is c++ style. They're completely separate. The standard states which items have to be included in which header. Implementations can however have them included in more than one. – Mgetz Sep 13 '19 at 13:03
  • Any Standard Library header can include any other STandard Library header. In this case `iostream` presumably includes `cstdio`. –  Sep 13 '19 at 13:05
  • 2
    To be clear: although this code **might** work, it is not required to. Accidents happen. You're right that the correct directive is `#include `. – Pete Becker Sep 13 '19 at 13:06
  • 2
    The duplicate asks about `std::string` but the reasoning is the same. – François Andrieux Sep 13 '19 at 13:06
  • 2
    `` doesn't exist in C. This question should not have the C tag. – François Andrieux Sep 13 '19 at 13:07

0 Answers0