Vous êtes sur la page 1sur 10

Common Programming Concepts in C

Let's take a look at how to put some of the common programming concepts into practice in
your C code. The following is a quick summary of these concepts:
Functions -- As stated earlier, a function is a block of code representing something the
computer should do when the program runs. Some languages call these structures methods,
though C programmers don't typically use that term. Your program may define several
functions and call those functions from other functions. Later, we'll take a closer look at the
structure of functions in C.
Variables -- When you run a program, sometimes you need the flexibility to run the program
without knowing what the values are ahead of time. Like other programming languages, C
allows you to use variables when you need that flexibility. Like variables in algebra, a variable
in computer programming is a placeholder that stands for some value that you don't know or
haven't found yet.
Data types -- In order to store data in memory while your program is running, and to know
what operations you can perform on that data, a programming language like C defines
certain data types it will recognize. Each data type in C has a certain size, measured in
binary bits or bytes, and a certain set of rules about what its bits represent. Coming up, we'll
see how important it is choose the right data type for the task when you're using C.
Operations -- In C, you can perform arithmetic operations (such as addition) on numbers
and string operations (such as concatenation) on strings of characters. C also has built-in
operations specifically designed for things you might want to do with your data. When we
check out data types in C, we'll take a brief look at the operations, too.
Loops -- One of the most basic things a programmer will want to do is repeat an action
some number of times based on certain conditions that come up while the program is
running. A block of code designed to repeat based on given conditions is called a loop, and
the C language provides for these common loop structures: while, do/while, for,
continue/break and goto. C also includes the common if/then/else conditionals and
switch/case statements.
Data structures -- When your program has a lot of data to handle, and you need to sort or
search through that data, you'll probably use some sort of data structure. A data structure is
a structured way of representing several pieces of data of the same data type. The most
common data structure is an array, which is just an indexed list of a given size. C has
libraries available to handle some common data structures, though you can always write
functions and set up your own structures, too.
Preprocessor operations -- Sometimes you'll want to give the compiler some instructions
on things to do with your code before compiling it into the executable. These operations
include substituting constant values and including code from C libraries (which you saw in
the sample code earlier).

C also requires programmers to handle some concepts which many programming languages
have simplified or automated. These include pointers, memory management, and garbage
collection. Later pages cover the important things to know about these concepts when
programming in C.
This quick overview of concepts may seem overwhelming if you're not already a
programmer. Before you move on to tackle a dense C programming guide, let's take a userfriendly look at the core concepts among those listed above, starting with functions.
Functions in C

Prev Next

Most computer programming languages allow you to create functions of some sort.
Functions let you chop up a long program into named sections so that you can reuse those
sections throughout the program. Programmers for some languages, especially those using
object-oriented programming techniques, use the term method instead of function.
Functions accept parameters and return a result. The block of code that comprises a
function is its function definition. The following is the basic structure of a function definition:
<return type> <function name>(<parameters>)
{
<statements>
return <value appropriate for the return type>;
}
At a minimum, a C program has one function named main. The compiler will look for a main
function as the starting point for the program, even if the main function calls other functions
within it. The following is the main we saw in the simple C program we looked at before. It
has a return type of integer, takes no parameters, and has two statements (instructions
within the function), one of which is its return statement:
int main()
{
printf("This is output from my first program!\n");
return 0;
}
Functions other than main have a definition and one or more function calls. A function call is
a statement or part of a statement within another function. The function call names the
function it's calling followed by parentheses. If the function has parameters, the function call
must include corresponding values to match those parameters. This additional part of the
function call is called passing parameters to the function.

But what are parameters? A parameter for a function is a piece of data of a certain data type
that the function requires to do its work. Functions in C can accept an unlimited number of
parameters, sometimes called arguments. Each parameter added to a function definition
must specify two things: its data type and its variable name within the function block. Multiple
parameters are be separated by a comma. In the following function, there are two
parameters, both integers:
int doubleAndAdd(int a, int b)
{
return ((2*a)+(2*b));
}
Next, let's continue our look at functions by zooming out to look at how they fit within a larger
C program.
Function Prototypes

In C, you can add a function definition anywhere within the program (except within another
function). The only condition is that you must tell the compiler in advance that the function
exists somewhere later in the code. You'll do this with a function prototype at the beginning of
the program. The prototype is a statement that looks similar to the first line of the definition.
In C, you don't have to give the names of the parameters in the prototype, only the data
types. The following is what the function prototype would look like for the doubleAndAdd
function:
int doubleAndAdd(int, int);
Imagine function prototypes as the packing list for your program. The compiler will unpack
and assemble your program just as you might unpack and assemble a new bookshelf. The
packing list helps you ensure you have all the pieces you need in the box before you start
assembling the bookshelf. The compiler uses the function prototypes in the same way before
it starts assembling your program.
If you're following along with the sample.c program we looked at earlier, open and edit the
file to add a function prototype, function definition and function call for the doubleAndAdd
function shown here. Then, compile and run your program as before to see how the new
code works. You can use the following code as a guide to try it out:
#include <stdio.h>
int doubleAndAdd(int, int);
int main()
{
printf("This is output from my first program!\n");
printf("If you double then add 2 and 3, the result is: %d \n", doubleAndAdd(2,3));

return 0;
}
int doubleAndAdd(int a, int b)
{
return ((2*a)+(2*b));
}
So far we've looked at some basic structural elements in a C program. Now, let's look at the
types of data you can work with in a C program and what operations you can perform on that
data.

Function Declarations
In C, you'll probably hear the term function declaration more than function prototype,
especially among older C programmers. We're using the term function prototype in this
article, though, because it has an important distinction. Originally, a function declaration did
not require any parameters, so the return type, function name and a pair of empty
parentheses were sufficient. A function prototype, though, gives the compiler important
additional information by including the number and data types of the parameters it will call.
Prototypes have become a best practice approach among coders today, in C and other
programming languages.
Data Types and Operations in C

Prev Next

From your computer's point of view, your program is all just a series of ones and zeros. Data types
in C tell the computer how to use some of those bits.
HEMERA/THINKSTOCK

From your computer's perspective, data is nothing but a series of ones and zeros
representing on and off states for the electronic bits on your hard drive or in your computer's

processor or memory. It's the software you're running on a computer that determines how to
make sense of those billions of binary digits. C is one of few high-level languages that can
easily manipulate data at the bit level in addition to interpreting the data based on a given
data type.
A data type is a small set of rules that indicate how to make sense of a series of bits. The
data type has a specific size plus its own way of performing operations (such as adding and
multiplying) on data of that type. In C, the size of the data type is related to the processor
you're using. For example, in C99, a piece of data of the integer data type (int) is 16 bits long
in a 16-bit processor while for 32-bit and 64-bit processors it's 32 bits long.
Another important thing for C programmers to know is how the language handles signed and
unsigned data types. A signed type means that one of its bits is reserved as the indicator for
whether it's a positive or negative number. So, while an unsigned int on a 16-bit system can
handle numbers between 0 and 65,535, a signed in on the same system can handle
numbers between -32,768 and 32,767. If an operation causes an int variable to go beyond
its range, the programmer has to handle the overflow with additional code.
Given these constraints and system-specific peculiarities in C data types and operations, C
programmers must choose their data types based on the needs of their programs. Some of
the data types they can choose are the primitive data types in C, meaning those built in to
the C programming language. Look to your favorite C programming guide for a complete list
of the data types in C and important information about how to convert data from one type to
another.
C programmers can also create data structures, which combine primitive data types and a
set of functions that define how the data can be organized and manipulated. Though the use
of data structures is an advanced programming topic and beyond the scope of this article,
we will take a look at one of the most common structures: arrays. An array is a virtual list
containing pieces of data that are all the same data type. An array's size can't be changed,
though its contents can be copied to other larger or smaller arrays.
Though programmers often use arrays of numbers, character arrays, called strings, have the
most unique features. A string allows you to save something you might say (like "hello") into
a series of characters, which your C program can read in from the user or print out on the
screen. String manipulation has such a unique set of operations, it has its own dedicated C
library (string.h) with your typical string functions.
The built-in operations in C are the typical operations you'd find in most programming
languages. When you're combining several operations into a single statement, be sure to
know the operator precedence, or the order in which the program will perform each operation
in a mathematical expression. For example, (2+5)*3 equals 21 while 2+5*3 equals 17,
because C will perform multiplication before addition unless there are parentheses indicating
otherwise.

If you're learning C, make it a priority to familiarize yourself with all of its primitive data types
and operations and the precedence for operations in the same expression. Also, experiment
with different operations on variables and numbers of different data types.
At this point, you've scratched the surface of some important C basics. Next, though, let's
look at how C enables you to write programs without starting from scratch every time.
Don't Start from Scratch, Use Libraries

Libraries are very important in C because the C language supports only the most basic
features that it needs. For example, C doesn't contain input-output (I/O) functions to read
from the keyboard and write to the screen. Anything that extends beyond the basics must be
written by a programmer. If the chunk of code is useful to multiple different programs, it's
often put into a library to make it easily reusable.
In our discussion of C so far, we've already seen one library, the standard I/O (stdio) library.
The #include line at the beginning of the program instructed the C compiler to loaded the
library from its header file named stdio.h. C maintainers include standard C libraries for I/O,
mathematical functions, time manipulation and common operations on certain data
structures, such as a string of characters. Search the Web or your favorite C programming
guide for information about the C89 standard library and the updates and additions in C99.
You, too, can write C libraries. By doing so, you can split your program into reusable
modules. This modular approach not only makes it easy to include the same code in multiple
programs, but it also makes for shorter program files which are easier to read, test and
debug.
To use the functions within a header file, add a #include line for it at the beginning of your
program. For standard libraries, put the name of the library's corresponding header file
between greater-than and less-than signs (). For libraries you create yourself, put the name
of the file between double quotes. Unlike statements in other parts of your C program, you
don't have to put a semicolon at the end of each line. The following shows including one of
each type of library:
#include <math.h>
#include "mylib.h"
A comprehensive C programming source should provide the instructions you need to write
your own libraries in C. The function definitions you'll write are not any different whether
they're in a library or in your main program. The difference is that you'll compile them
separately in something called an object file (with a name ending in .o), and you'll create a
second file, called a header file (with a name ending in .h) which contains the function
prototypes corresponding to each function in the library. It's the header file you'll reference in
your #include line in each main program that uses your library, and you'll include the object
file as an argument in the compiler command each time you compile that program.
The C features we've explored so far are typical in other programming languages, too. Next,
though, we'll talk about how C manages your computer's memory.

Some Pointers about Pointers in C

When your C program is loaded into memory (typically the random-access memory, or RAM,
in your computer), each piece of the program is associated with an address in memory. This
includes the variables you're using to hold certain data. Each time your program calls a
function, it loads that function and all of its associated data into memory just long enough to
run that function and return a value. If you pass parameters to the function, C automatically
makes a copy of the value to use in the function.
Sometimes when you run a function, though, you want to make some permanent change to
the data at its original memory location. If C makes a copy of data to use in the function, the
original data remains unchanged. If you want to change that original data, you have to pass
a pointer to its memory address (pass by reference) instead of passing its value to the
function (pass by value).
Pointers are used everywhere in C, so if you want to use the C language fully you have to
have a good understanding of pointers. A pointer is a variable like other variables, but its
purpose is to store the memory address of some other data. The pointer also has a data
type so it knows how to recognize the bits at that memory address.
When you look at two variables side-by-side in C code, you may not always recognize the
pointer. This can be a challenge for even the most experienced C programmers. When you
first create a pointer, though, it's more obvious because there must be an asterisk
immediately before the variable name. This is known as the indirection operator in C. The
following example code creates an integer i and a pointer to an integer p:
int i;
int *p;
Currently there is no value assigned to either i or p. Next, let's assign a value to i and then
assign p to point to the address of i.
i = 3;
p = &i;
Here you can see the ampersand (&) used as the address operator immediately before i,
meaning the "address of i." You don't have to know what that address is to make the
assignment. That's good, because it will likely be different every time you run the program!
Instead, the address operator will determine the address associated with that variable while
the program is running. Without the address operator, the assignment p=i would assign p the
memory address of 3, literally, rather than the memory address of the variable i.
Next, let's look at how you can use pointers in C code and the challenges you'll want to be
prepared for.
Using Pointers Correctly in C

Prev Next

If you want to become proficient in C programming, you'll need a firm grasp of how to effectively
use pointers in your code.
ISTOCKPHOTO.COM/DSGPRO

Once you have a pointer, you can use that in place of a variable of the same data type in
operations and function calls. In the following example, the pointer to i is used instead of i
within a larger operation. The asterisk used with the p (*p) indicates that the operation should
use the value that p is pointing to at that memory address, not the memory address itself:
int b;
b = *p + 2;
Without pointers, it's nearly impossible to divide tasks into functions outside of main in your
C program. To illustrate this, consider you've created a variable in main called h that stores
the user's height to the nearest centimeter. You also call a function you've written named
setHeight that prompts the user to set that height value. The lines in your main function
might look something like this:
int h;
setHeight(h); /* There is a potential problem here. */
This function call will try to pass the value of h to setHeight. However, when the function
finishes running, the value of h will be unchanged because the function only used a copy of it
and then discarded it when it finished running.
If you want to change h itself, you should first ensure that the function can take a pointer to
an existing value rather than a new copy of a value. The first line of setHeight, then, would
use a pointer instead of a value as its parameter (note the indirection operator):
setHeight(int *height) { /* Function statements go here */ }
Then, you have two choices for calling setHeight. The first is to use the address operator for
h as the passed parameter (&h). The other is to create a separate pointer to h and pass that
instead. The following shows both options:
setHeight(&h); /* Pass the address of h to the function */

int *p;
p = &h;
setHeight(p); /* Pass a separate pointer to the address of h to the function */
The second option reveals a common challenge when using pointers. The challenge is
having multiple pointers to the same value. This means that any change in that one value
affects all its pointers at once. This could be a good or bad thing, depending on what you're
trying to accomplish in your program. Again, mastering the use of pointers is an important
key to mastering C programming. Practice with pointers as much as possible so you'll be
ready to face these challenges.
The C features we've explored so far are typical in other programming languages, too. Next,
though, we'll look at C's demands for careful memory management.
The Importance of Memory Management in C

One of the things that makes C such a versatile language is that the programmer can scale
down a program to run with a very small amount of memory. When C was first written, this
was an important feature because computers weren't nearly as powerful as they are today.
With the current demand for small electronics, from mobile phones to tiny medical devices,
there's a renewed interest in keeping the memory requirements small for some software. C is
the go-to language for most programmers who need a lot of control over memory usage.
To better understand the importance of memory management, consider how a program uses
memory. When you first run a program, it loads into your computer's memory and begins to
execute by sending and receiving instructions from the computer's processor. When the
program needs to run a particular function, it loads that function into yet another part of
memory for the duration of its run, then abandons that memory when the function is
complete. Plus, each new piece of data used in the main program takes up memory for the
duration of the program.
If you want more control over all this, you need dynamic storage allocation. C supports
dynamic storage allocation, which is the ability to reserve memory as you need it and free
that memory as soon as you're finished using it. Many programming languages have
automatic memory allocation and garbage collection that handle these memory
management tasks. C, though, allows (and in some cases requires) you to be explicit about
memory allocation with the following key functions from the standard C library:

malloc -- Short for memory allocation, malloc is used to reserve a block of memory
of a given size to story a certain type of data your program needs to process. When
you use malloc, you're creating a pointer to the allocated memory. This isn't
necessary for a single piece of data, such as one integer, which is allocated as soon
as you first declare it (as in int i). However, it is an important part of creating and
managing data structures such as arrays. Alternate memory allocation options in C
are calloc, which also clears the memory when it's reserved, and realloc, which
resizes previously reserved memory.

free -- Use free to force your program to free the memory previously assigned to a
given pointer.

Best practice when using malloc and free is that anything you allocate should be freed.
Whenever you allocate something, even in a temporary function, it remains in memory until
the operating system cleans up the space. To ensure that memory is free and ready to use
immediately, though, you should free it before the current function exits. This memory
management means you can keep your program's memory footprint to a minimum and avoid
memory leaks. A memory leak is a program flaw in which it continues using more and more
memory until there's none left to allocate, causing the program to stall or crash. On the other
hand, don't get so anxious about freeing memory that you free up, and thus lose, something
that you need later in the same function.
Throughout this article, you've learned some of the basic structure and core concepts of the
C programming language. We've looked at its history, the characteristics it has in common
with other programming languages and the important features that make it a unique and
versatile option for coding software. Launch over to the next page for lots more information,
including some programming guides that will carry you further on your journey into C.

Vous aimerez peut-être aussi