开发者

Reason for an #include

#include <stdio.h>
#include <conio.h>

int main(void)
{
    int num1;
    int num2;
    printf("Enter 2 numbers:\n");

    scanf("%d", &num1);
    scanf("%d", &num2);

    if (num1 == num2){
      printf("Equal");     
    }
    getchar();
    getchar();
}

The tutorial has t开发者_StackOverflow中文版his: #include <conio.h>

i tried it without #include <conio.h> and works ok...

But why did he put it? here is this tutorial.


Because in the tutorial it is getch() not getchar().

getch() is declared in conio.h, whereas getchar() is declared in stdio.h.


conio.h is a non-standard header that was shipped with many old C compilers for MS-DOS. It is not necessary in the code you posted, as none of that code requires its definitions. If including it produces an error, that is because conio.h is not available on your system. You may want to consult a more up-to-date tutorial if the one you're using is referencing DOS-specific headers.

Edit: Ouch, just looked at the linked tutorial. 2009? Someone is including conio.h as boilerplate C code in a tutorial from 2009? What a cruel world this is.


All the functions you are using are defined in stdio.h, it doesn't seem to use anything that is inside conio.h, thus rendering it useless in this case.


It works because all the functions used in the code - scanf, printf, getchar - are declared in stdio.h.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜