From WikiChip
Editing c/program structure

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.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.

This page supports semantic in-text annotations (e.g. "[[Is specified as::World Heritage Site]]") to build structured and queryable content provided by Semantic MediaWiki. For a comprehensive description on how to use annotations or the #ask parser function, please have a look at the getting started, in-text annotation, or inline queries help pages.

Latest revision Your text
Line 1: Line 1:
{{c title|Program Structure}}
+
{{C Guide}}
The way a program is written and its makeup is usually referred to as '''program structure'''. Understanding the general flow a C code is crucial to being able to read and understand any piece of C code.
+
Before moving on, it's important to understand the basic structure of a [[C]] program.
  
 
== Reserved Words ==
 
== Reserved Words ==
{{main|c/reserved words|l1=Reserved words}}
+
{{main|Reserved words - C|l1=Reserved words}}
C programs are made up of a series of grouped characters and symbols known as tokens. {{C|Reserved words}} are a set of words that have special meaning within your [[C]] program; they make up the basic functionality of your program. Reserved words may only be used within their appropriate context - they '''may be not''' be used as ''variable names'', or ''any other type of identifier''; we will expand on that later.
+
 
 +
C programs are made up of a series of grouped characters and symbols known as tokens. [[Reserved words - C|Reserved words]] are a set of words that have special meaning within your [[C]] program; they make up the basic functionality of your program. Reserved words may only be used within their appropriate context - they '''may be not''' be used as ''variable names'', or ''any other type of identifier''; we will expand on that later.
  
 
== "Hello, World!" Example ==
 
== "Hello, World!" Example ==
Line 37: Line 38:
  
 
* '''functions''': Contain statements that specify the operation to be performed.
 
* '''functions''': Contain statements that specify the operation to be performed.
* {{C|Variables}}: A way of naming and storing values for later use by the program.
+
* [[Variables - C|variables]]: A way of naming and storing values for later use by the program.
  
 
In our "Hello, World!" example program, main is a function that we defined. Normally, you would be allowed to name your functions whatever you like, but "'''main'''" happens to be a special function. All C programs begin executing at the beginning of function '''main()'''; as a corollary, this also means that every program must have a '''main()''' function somewhere.
 
In our "Hello, World!" example program, main is a function that we defined. Normally, you would be allowed to name your functions whatever you like, but "'''main'''" happens to be a special function. All C programs begin executing at the beginning of function '''main()'''; as a corollary, this also means that every program must have a '''main()''' function somewhere.
Line 45: Line 46:
 
<source lang="C">#include <stdio.h></source>
 
<source lang="C">#include <stdio.h></source>
  
tells the compiler to include information about the standard '''I/O''' (''Input/Output'') library; that line is part of the {{C|preprocessor}}.
+
tells the compiler to include information about the standard '''I/O''' (''Input/Output'') library; that line is part of the [[Preprocessor - C|pre-processor]].
  
 
One way that functions communicate with one another is calling a function in order to provide a list of values to the function being called; these values are called '''[[arguments]]'''. The parenthesis after a function name are used to surround the ''argument list''. In our "Hello, World!" example, "'''()'''" is used to indicate an empty list of arguments. A function has the ability to return a value back to its caller. The type of value a function returns is specified in front of the function name. In our example, we return an <code>int</code> - this is a ''requirement'' when you define function '''main()'''.
 
One way that functions communicate with one another is calling a function in order to provide a list of values to the function being called; these values are called '''[[arguments]]'''. The parenthesis after a function name are used to surround the ''argument list''. In our "Hello, World!" example, "'''()'''" is used to indicate an empty list of arguments. A function has the ability to return a value back to its caller. The type of value a function returns is specified in front of the function name. In our example, we return an <code>int</code> - this is a ''requirement'' when you define function '''main()'''.
Line 64: Line 65:
 
calls the function '''puts()''' with the argument "Hello, World!". '''puts()''' is a standard library function that prints output - in this case a string of characters between the double quotes.  
 
calls the function '''puts()''' with the argument "Hello, World!". '''puts()''' is a standard library function that prints output - in this case a string of characters between the double quotes.  
  
In C, a sequence of characters surrounded by double quotes like "Hello, World!" is called a '''{{C|string constant}}'''. For the time being, the only use of strings is that they be printed out by the '''puts()''' function, or as argument to other functions.  
+
In C, a sequence of characters surrounded by double quotes like "Hello, World!" is called a '''[[string constant - C|string constant]]'''. For the time being, the only use of strings is that they be printed out by the '''puts()''' function, or as argument to other functions.  
  
 
The last statement of '''main()''',
 
The last statement of '''main()''',
Line 73: Line 74:
  
 
== Formatting Style ==
 
== Formatting Style ==
The indentations, which we used in our code, are meant to provide us a much more pleasurable visual layout for reading, thus allowing for a better grasp on the meaning of the code. Character spaces, horizontal tabs, new-lines, vertical tabs, and form-feeding are collectively called [[white space]]. Because the compiler works on a series of tokens and ignores all extra white space, you have a considerable amount of freedom concerning how you format your program. C is a [[free-format language]] - the positioning of characters in the source file is insignificant. There are no rules for where you should place brackets or how you should indent your code. As you will see, this is both a blessing and a curse. In fact, the following rewrite of the example program we have above is perfectly legal in C:
+
The indentations, which we used in our code, are meant to provide us a much more pleasurable visual layout for reading, thus allowing for a better grasp on the meaning of the code. Character spaces, horizontal tabs, new-lines, vertical tabs, and form-feeding are collectively called [[white space]]. Because the compiler works on a series of tokens and ignores all extra white space, you have a considerable amount of freedom concerning how you format your program. C is a [[[free format]] language - the positioning of characters in the source file is insignificant. There are no rules for where you should place brackets or how you should indent your code. As you will see, this is both a blessing and a curse. In fact, the following rewrite of the example program we have above is perfectly legal in C:
  
 
<source lang="C">#include <stdio.h>
 
<source lang="C">#include <stdio.h>
Line 101: Line 102:
  
 
== Comments ==
 
== Comments ==
{{main|c/comments|l1=Comments}}
+
{{main|Comments - C|l1=Comments}}
Even though our "Hello, World!" example works, it lacks one very important thing: documentation. Every program should be documented enough so that in the future you, or, more importantly, other programmers know some general things about, such as: the author, what the program does, and, perhaps, the license associated with the program. In C, such information is placed in {{C|comments}}. The characters '''/*''' introduce a comment and the characters '''*/''' terminate it. For example:
+
 
 +
Even though our "Hello, World!" example works, it lacks one very important thing: documentation. Every program should be documented enough so that in the future you, or, more importantly, other programmers know some general things about, such as: the author, what the program does, and, perhaps, the license associated with the program. In C, such information is placed in [[comments - C|comments]]. The characters '''/*''' introduce a comment and the characters '''*/''' terminate it. For example:
  
 
<source lang="C">/* This is a comment. */
 
<source lang="C">/* This is a comment. */
Line 140: Line 142:
 
// Name:        hello.c
 
// Name:        hello.c
 
// Description: Prints "Hello, World!"
 
// Description: Prints "Hello, World!"
// Author:      David
+
// Author:      David Schor
 
#include <stdio.h>
 
#include <stdio.h>
  

Please note that all contributions to WikiChip may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see WikiChip:Copyrights for details). Do not submit copyrighted work without permission!

Cancel | Editing help (opens in new window)