Question:
Why wont this C code work?
N
2013-09-03 05:27:36 UTC
include
#include
void main()
{
int x;
printf("value of =%d",x);
getch();
}


This is from a school textbook. I copied it exactly, im using compiler online to run the program also used codepad.com both have errors

Line 17: error: conio.h: No such file or directory
In function 'main':
Line 4: warning: return type of 'main' is not 'int'

(this is no line 17)
Three answers:
?
2013-09-03 09:19:32 UTC
include

// code pad does not use conio

int main() // main() returns an int (your book is wrong!)

{

int x=1;// you have to give x a value

printf("value of =%d\n",x);

return 0; // you do not need to pause to see the result in codepad.

// but you do need to return an int

}



I would not trust ANY book that had void main()

who knows what else they got wrong....



and conio.h is only for some platforms it is not part of the ANSI C standard



look here:

http://flash-gordon.me.uk/ansi.c.txt
Crpyitc Wonder
2013-09-03 13:29:27 UTC
1. In C AND C++, you never declare the main function using void. This is because void returns nothing, so your program wont actually execute. Two, yes you've declared the variable x and given it a data type, but you haven't initialized it. This means actually giving it a value. Therefore, what do you expect to be printed out?

Finally, you may not have the library conio.h hence why you're getting the error, and plus you left out the hash tag key. But I think you should have the conio.h library if you installed C correctly, so you may have just had a typo.
2013-09-03 12:32:14 UTC
You have not added # in the starting of the code. The stdio.h header file is not executed at runtime.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...