Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
C syntax
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Global structure=== After preprocessing, at the highest level a [[C (programming language)|C]] [[computer program|program]] consists of a sequence of declarations at file scope. These may be partitioned into several separate source files, which may be compiled separately; the resulting object modules are then [[Linker (computing)|linked]] along with implementation-provided run-time support modules to produce an executable image. The declarations introduce [[Function (programming)|functions]], [[Variable (programming)|variables]] and [[Data type|types]]. C functions are akin to the subroutines of [[Fortran]] or the procedures of [[Pascal (programming language)|Pascal]]. A ''definition'' is a special type of declaration. A variable definition sets aside storage and possibly initializes it, a function definition provides its body. An implementation of C providing all of the standard library functions is called a ''hosted implementation''. Programs written for hosted implementations are required to define a special function called [[Entry point|{{code|main}}]], which is the first function called when a program begins executing. Hosted implementations start program execution by invoking the {{code|main}} function, which must be defined following one of these prototypes (using different parameter names or spelling the types differently is allowed): <syntaxhighlight lang=C> int main() {...} int main(void) {...} int main(int argc, char *argv[]) {...} int main(int argc, char **argv) {...} // char *argv[] and char **argv have the same type as function parameters </syntaxhighlight> The first two definitions are equivalent (and both are compatible with C++). It is probably up to individual preference which one is used (the current C standard contains two examples of {{code|main()}} and two of {{code|main(void)}}, but the draft C++ standard uses {{code|main()}}). The return value of {{code|main}} (which should be {{code|int}}) serves as ''termination status'' returned to the host environment. The C standard defines return values {{code|0}} and {{code|EXIT_SUCCESS}} as indicating success and {{code|EXIT_FAILURE}} as indicating failure. ({{code|EXIT_SUCCESS}} and {{code|EXIT_FAILURE}} are defined in [[stdlib.h|{{code|<stdlib.h>}}]]). Other return values have implementation-defined meanings; for example, under [[Linux]] a program killed by a [[Signal (computing)|signal]] yields a return code of the numerical value of the signal plus 128. A minimal correct C program consists of an empty {{code|main}} routine, taking no arguments and doing nothing: <syntaxhighlight lang=C> int main(void){} </syntaxhighlight> Because no <code>return</code> statement is present, <code>main</code> returns 0 on exit.<ref name="bk21st" /> (This is a special-case feature introduced in [[C99]] that applies only to <code>main</code>.) The {{code|main}} function will usually call other functions to help it perform its job. Some implementations are not hosted, usually because they are not intended to be used with an [[operating system]]. Such implementations are called ''free-standing'' in the C standard. A free-standing implementation is free to specify how it handles program startup; in particular it need not require a program to define a {{code|main}} function. Functions may be written by the programmer or provided by existing libraries. Interfaces for the latter are usually declared by including header files—with the {{code|#include}} [[preprocessing directive]]—and the library objects are linked into the final executable image. Certain library functions, such as [[printf|{{code|printf}}]], are defined by the C standard; these are referred to as the [[ISO C standard library|standard library]] functions. A function may return a value to caller (usually another C function, or the hosting environment for the function {{code|main}}). The {{code|printf}} function mentioned above returns how many characters were printed, but this value is often ignored.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)