C Programming/Preliminaries

From Wikibooks, open books for an open world
Jump to: navigation, search
Previous: Intro exercise Index Next: Compiling



Contents

[edit] Basic Concepts

Before one gets too deep into learning C syntax and programming constructs, it is beneficial to learn the meaning of a few key terms that are central to a thorough understanding of C.

[edit] Block Structure, Statements, Whitespace, and Scope

Now we discuss the basic structure of a C program. If you're familiar with PASCAL, you may have heard it referred to as a block-structured language. C does not have complete block structure (and you'll find out why when you go over functions in detail) but it is still very important to understand what blocks are and how to use them.

So what is in a block? Generally, a block consists of executable statements, or the text the compiler will attempt to turn into executable instructions, and the whitespace that surrounds them.

In C, blocks begin with an opening brace "{" and end with a closing brace "}". Blocks can contain other blocks which can contain their own blocks, and so on. Statements always end with a semicolon (;) character. Since C is a free-format language, several statements can share a single line in the source file. There are several kinds of statements, including assignment, conditional and flow-control. A substantial portion of this book deals with statement construction.

Whitespace refers to the tab, space and newline/EOL (End Of Line) characters that separate the text characters that make up source code lines. Like many things in life, it's hard to appreciate whitespace until it's gone. To a C compiler, the source code

  printf("Hello world"); return 0;

is the same as

  printf("Hello world");
  return 0;

which is the same as

  printf (
  "Hello world") ; 


return 0;

The compiler simply skips over whitespace. However, it is common practice to use spaces (and tabs) to organize source code for human readability. You can use blocks without a conditional, loop, or other statement to organize your code.

In C, most of the time we do not want other functions or other programmer's routines accessing data that we are currently manipulating. This is why it is important to understand the concept of scope.

Scope describes the level at which a piece of data or a function is visible. There are two kinds of scope in C, local and global. When we speak of something being global, we speak of something that can be seen or manipulated from anywhere in the program. When we speak of something being local, we speak of something that can be seen or manipulated only within the block it was declared.


[edit] Basics of Using Functions

Functions are a big part of programming. A function is a special kind of block that performs a well-defined task. If a function is well-designed, it can enable a programmer to perform a task without knowing anything about how the function works. The act of requesting a function to perform its task is called a function call. Many functions require a caller to hand it certain pieces of data needed to perform its task; these are called arguments. Many functions also return a value to the caller when they're finished; this is called a return value.

  • The things you need to know before calling a function are:
    • What the function does
    • The data type (discussed later) of the arguments and what they mean
    • The data type of the return value and what it means

All code other than global data definitions and declarations needs to be a part of a function.

Every executable program needs to have one, and only one, main function, which is where the program begins executing.

We will discuss functions in more detail in a later chapter, C Programming/Procedures and functions.

[edit] The Standard Library

In 1983, when C was in the process of becoming standardized, the American National Standards Institute (ANSI) formed a committee to establish a standard specification of C known as "ANSI C". That standard specification created a basic set of functions common to each implementation of C, which is referred to as the Standard Library. The Standard Library provides functions for tasks such as input/output, string manipulation, mathematics, files, and memory allocation. The Standard Library does not provide functions that are dependent on specific hardware or operating systems, like graphics, sound, or networking. In the "Hello, World", program, a Standard Library function is used printf which outputs lines of text to the standard output stream.

[edit] Comments and Coding Style

Comments are text inserted into the source code of a program that serve no purpose other than documenting the code. C has two types of comments. A comment may begin with /* and end with */. This type of comment may span part of a line or may span many lines. For a one line comment statement, you can use the token "//" at the beginning of the statement. The compiler ignores all text from a "//" until the end of a line.

Good commenting is considered essential to software development, not just because others may need to read your code, but because you may need to revise your code after writing it and will want to quickly understand how it works. In general, it is a good idea to comment anything that is not immediately obvious to a competent programmer (such as the purpose of a function, variable or code). However, you should avoid overdoing comments, since overcommenting may actually make your code more difficult to read and waste space.

Good coding style habits are important to adopt for the simple reason that code should be intuitive and readable, which is, after all, the purpose of a high-level programming language like C. In general, provide ample white space, indent so that the opening brace of a block and the closing brace of a block are vertically aligned, and provide evocative names for your functions and variables. Throughout this text we will be providing more style and coding style tips for you. Do try and follow these tips: they will make your code easier for you and others to read and understand.

Previous: Intro exercise Index Next: Compiling
Personal tools
Namespaces
Variants
Actions
Navigation
Community
Toolbox
In other languages
Sister projects
Print/export