C言語のclrscr()とは?C言語でコンソール画面をクリアする

PDF形式でダウンロードPDF形式でダウンロード

clrscr()関数は、Turbo CやTurbo C++などの古いCコンパイラでMS-DOSコンソール画面をクリアするためのものでした。clrscr()は標準のC関数ではありません。clrscr()を含むプログラムをGCCやClangなどの最新のコンパイラでコンパイルしようとすると、「function not declared(関数が宣言されていません)」や「not declared in this scope(このスコープでは宣言されていません)」というエラーが表示されます。では、プログラムでコンソール画面をクリアする必要がある場合はどうすればよいのでしょうか?この記事では、clrscr()をsystem()関数に置き換えてC言語で画面をクリアする方法を紹介します。

  1. How.com.vn 日本語: Step 1 stdlib.h
    ヘッダファイルをコードに追加する system()関数は、ターミナルやコンソールにコマンドを渡すためのもので、stdlib.hヘッダファイルで宣言します。[1]
    • clrscr()は、ヘッダファイルで定義します。clrscr()を削除してsystem()に置き換えるので、conio.hヘッダファイルは削除してもかまいません。
  2. Step 2 Windowsの場合は、clrscr()をsystem("cls")に置き換える 
    clsは、Windowsコマンドプロンプトで実行してコンソール画面をクリアするコマンドです。clssystem()関数に渡すと、画面がきちんとクリアされます。
  3. Step 3 LinuxまたはmacOSの場合は、clrscr()をsystem("clear")に置き換える 
    system()は、clearコマンドをコンソールに渡す関数です。コンソール画面をクリアするLinuxコマンド(macOSコマンド)はclearなので、system("clear")でコンソール画面がクリアされます。[2]
    広告

このHow.com.vn記事について

How.com.vn 日本語: How.com.vn編集チーム
共著者 ::
How.com.vnスタッフライター
この記事は、経験豊富なHow.com.vnの編集者と調査員から成るチームによって執筆されています。調査員チームは内容の正確性と網羅性を確認しています。

How.com.vnのコンテンツ管理チームは、編集チームが編集した記事を細心の注意を払って精査し、すべての記事がHow.com.vnの高品質基準を満たしているかどうかを確認しています。 この記事は7,744回アクセスされました。
カテゴリ: コンピューター
このページは 7,744 回アクセスされました。

この記事は役に立ちましたか?

⚠️ Disclaimer:

Content from Wiki How 日本語 language website. Text is available under the Creative Commons Attribution-Share Alike License; additional terms may apply.
Wiki How does not encourage the violation of any laws, and cannot be responsible for any violations of such laws, should you link to this domain, or use, reproduce, or republish the information contained herein.

Notices:
  • - A few of these subjects are frequently censored by educational, governmental, corporate, parental and other filtering schemes.
  • - Some articles may contain names, images, artworks or descriptions of events that some cultures restrict access to
  • - Please note: Wiki How does not give you opinion about the law, or advice about medical. If you need specific advice (for example, medical, legal, financial or risk management), please seek a professional who is licensed or knowledgeable in that area.
  • - Readers should not judge the importance of topics based on their coverage on Wiki How, nor think a topic is important just because it is the subject of a Wiki article.

広告