Thursday, December 24, 2009

Programming in C, is it always necessary to write: #include <stdio.h> ? If not, when do you need it?

Also, is it always necessary to have: int main (...) if all you need is another function that's called ';sum'; (for example). If you do always need ';main'; as a function, where would you put that- and would it simply call your ';sum'; function or would it need to do something else?Programming in C, is it always necessary to write: #include %26lt;stdio.h%26gt; ? If not, when do you need it?
stdio.h is need when you use any function that is in that library. Here's a list.





http://xoax.net/comp/cpp/reference/cstdi鈥?/a>Programming in C, is it always necessary to write: #include %26lt;stdio.h%26gt; ? If not, when do you need it?
Every C\C++ program needs a main() function, so it has to be somewhere in the program - remember a program can be made up of several source files, so your sum function could live in a dedicated file, but you'd still need a main() function somewhere, it's the starting point of any program.





stdio.h is used when you intend to read\write from streams - most commonly the standard output device (the screen), so yes it's generally included, but not all programs would require it. See the source link for an idea of what stdio.h provides for your program.
include %26lt;stdio.h%26gt; is a statement that is telling the compiler to include all the functions that are defined in stio.h Some of the most basic functions you see in that library are:


printf()


Scanf()





There are dozens more. If you try to call these functions without including stdio.h, you will get a compilation error.





for main:





main() is the main function that is required somewhere because it's the place a computer knows to start, if you don't have main when trying to run your program the cpu will get confused and wont know which function to start with. Infact I think you might get a compilation error.





As for int main,


you could put void main if you want which just tells the compiler that it's not going to return any thing. If you put int infront of it, you would most likely see return 0; at the end of the function which just references other programs that you run with it to specify it completed without errors.





Remember that computers are the dumbest things in the world and that us humans are the ones that make them smart (They are just extremely fast at calculation) they only understand true or false. They can't make decisions themselves.





Hope this helped
No, you do not ever have to use %26lt;stdio.h%26gt;











int main (void)


{





return 0;


}





This is a legal ANSC C program- prints nothing but legal





%26lt;stdio.h%26gt;


- typically use it for printf/scanf





===





Other user defined functions- must be declared before they are called: 2 options





1. define sum before main; call in main (or other functions)


2. define sum after main- need function prototype
  • blonde highlight
  • oil offshore
  • printer ink
  • printer ink
  • No comments:

    Post a Comment