首页 > 解决方案 > #include 的替代方案在 C++ 中

问题描述

为什么在用 C++ 编写程序时必须使用 iostream 库。如果我不想包含这个库,那么这个 iostream 库的替代方案是什么。

这是我的代码,不包括 iostream。我尝试使用 include stdio.h 但它不起作用。

#include<conio.h>

using namespace std;

void biodata ();

main()
{
    biodata ();
}

void biodata()
{
    cout << "Name: Ijlal Hussain.\nFather Name: Iftikhar Hussin.\nAge: 18. \nStudent of Comsats University Islamabad (Attock Campus)";
}

标签: c++

解决方案


如果您只需要输出文本,您可以使用<cstdio>而不是。<iostream>

要打印不带格式的字符串,您可以使用puts. 用于使用printf格式和转换说明符进行打印。

#include<cstdio>

void biodata ();

int main()
{
  biodata ();
  return 0;
}

void biodata()
{    
   //using puts   
   puts("Name: Ijlal Hussain.\nFather Name: Iftikhar Hussin.\nAge: 18. \nStudent of Comsats University Islamabad (Attock Campus)");

   //using printf
    const char* name = "Ijlal Hussain";
    const char* fathername = "Iftikhar Hussin";
    int age = 18;
    printf("Name: %s.\nFather Name: %s.\nAge: %d. \nStudent of Comsats University Islamabad (Attock Campus)", name, fathername, age);     

}

推荐阅读