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 (programming language)
(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!
== <span class="anchor" id="HELLOWORLD"></span>"Hello, world" example == {{See also|"Hello, World!" program}} [[File:Hello World Brian Kernighan 1974.jpg|thumb|"Hello, World!" program by [[Brian Kernighan]] (1978)]] The "hello, world" example that appeared in the first edition of ''[[The C Programming Language|K&R]]'' has become the model for an introductory program in most programming textbooks. The program prints "hello, world" to the [[standard output]], which is usually a terminal or screen display. The original version was:{{sfnp|Kernighan|Ritchie|1978|p=6}} <syntaxhighlight lang="c"> main() { printf("hello, world\n"); } </syntaxhighlight> A standard-conforming "hello, world" program is:{{efn|The original example code will compile on most modern compilers that are not in strict standard compliance mode, but it does not fully conform to the requirements of either C89 or C99. In fact, C99 requires that a diagnostic message be produced.}} <!-- READ THIS BEFORE YOU EDIT! If you think there is a better way, first see talk page archive No. 8 for why. If you still want to change it, discuss it first. --> <syntaxhighlight lang="c"> #include <stdio.h> int main(void) { printf("hello, world\n"); } </syntaxhighlight> The first line of the program contains a [[preprocessing directive]], indicated by <code>#include</code>. This causes the compiler to replace that line of code with the entire text of the <code>[[stdio.h]]</code> header file, which contains declarations for standard input and output functions such as <code>printf</code> and <code>scanf</code>. The angle brackets surrounding <code>stdio.h</code> indicate that the header file can be located using a search strategy that prefers headers provided with the compiler to other headers having the same name (as opposed to double quotes which typically include local or project-specific header files). The second line indicates that a function named <code>main</code> is being defined. The <code>[[Entry point|main]]</code> function serves a special purpose in C programs; the [[run-time environment]] calls the <code>main</code> function to begin program execution. The type specifier <code>int</code> indicates that the value returned to the invoker (in this case the run-time environment) as a result of evaluating the <code>main</code> function, is an integer. The keyword <code>void</code> as a parameter list indicates that the <code>main</code> function takes no arguments.{{efn|The <code>main</code> function actually has two arguments, <code>int argc</code> and <code>char *argv[]</code>, respectively, which can be used to handle [[command-line argument]]s. The ISO C standard (section 5.1.2.2.1) requires both forms of <code>main</code> to be supported, which is special treatment not afforded to any other function.}} The opening curly brace indicates the beginning of the code that defines the <code>main</code> function. The next line of the program is a statement that ''calls'' (i.e. diverts execution to) a function named <code>[[printf]]</code>, which in this case is supplied from a system [[Library (computing)|library]]. In this call, the <code>printf</code> function is ''passed'' (i.e. provided with) a single argument, which is the [[Memory address|address]] of the first character in the [[string literal]] <code>"hello, world\n"</code>. The string literal is an unnamed [[Array (data type)|array]] set up automatically by the compiler, with elements of type <code>char</code> and a final [[Null character|NULL character]] (ASCII value 0) marking the end of the array (to allow <code>printf</code> to determine the length of the string). The NULL character can also be written as the [[Escape sequences in C|escape sequence]] <code>\0</code>. The <code>\n</code> is a standard escape sequence that C translates to a ''[[newline]]'' character, which, on output, signifies the end of the current line. The return value of the <code>printf</code> function is of type <code>int</code>, but it is silently discarded since it is not used. (A more careful program might test the return value to check that the <code>printf</code> function succeeded.) The semicolon <code>;</code> terminates the statement. The closing curly brace indicates the end of the code for the <code>main</code> function. According to the C99 specification and newer, the <code>main</code> function (unlike any other function) will implicitly return a value of <code>0</code> upon reaching the <code>}</code> that terminates the function.{{efn|Prior to C99, an explicit <code>return 0;</code> statement was required at the end of the <code>main</code> function.}} The return value of <code>0</code> is interpreted by the run-time system as an exit code indicating successful execution of the function.<ref name="bk21st">{{cite book |last1=Klemens |first1=Ben |author-link=Ben Klemens |title=21st Century C |publisher=[[O'Reilly Media]] |year=2013 |isbn=978-1-4493-2714-9}}</ref>
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)