C Programming First Part
C Programming First Part
Adventure
Animals
Auto
Culture
Entertainment
Health
Lifestyle
Money
Science
Tech
Video
Shows
Quizzes
Computer Electronics
You'll better understand what high-level languages are doing behind the scenes, such as memory
management and garbage collection. This understanding can help you write programs that work
more efficiently.
If you're an information technology (IT) specialist, you could also benefit from learning C. IT professionals often write, maintain and run scripts as part of their job. A
script is a list of instructions for a computer's operating system to follow. To run certain scripts, the computer sets up a controlled execution environment called a
shell. Since most operating systems run shells based on C, C shell is a popular scripting adaptation of C used by IT pros.
This article covers the history behind C, looks at why C is so important, shows examples of some basic C code and explores some important features of C, including
data types, operations, functions, pointers and memory management. Though this article isn't an instruction manual for programming in C, it does cover what makes
C programming unique in a way that goes beyond those first few chapters of the average C programming guide.
Let's start by looking at where the C programming language came from, how it has developed and the role it has in software development today.
What is C?
The simplest way to define C is to call it a computer programming language, meaning you can write software with it that a computer can execute. The result could be
a large computer application, like your Web browser, or a tiny set of instructions embedded in a microprocessor or other computer component.
The language C was developed in the early 1970s at Bell Laboratories, primarily credited to the work of Ken Thompson and Dennis Ritchie. Programmers needed a
more user-friendly set of instructions for the UNIX operating system, which at the time required programs written in assembly language. Assembly programs, which
speak directly to a computer's hardware, are long and difficult to debug, and they required tedious, time-consuming work to add new features [source: King].
Thompson's first attempt at a high-level language was called B, a tribute to the system programming language BCPL on which it was based. When Bell Labs acquired
a Digital Equipment Corporation (DEC) UNIX system model PDP-11, Thompson reworked B to better fit the demands of the newer, better system hardware. Thus, B's
successor, C, was born. By 1973, C was stable enough that UNIX itself could be rewritten using this innovative new higher-level language [source: King].
Before C could be used effectively beyond Bell Labs, other programmers needed a document that explained how to use it. In 1978, the book "The C Programming
Language" by Brian Kernighan and Dennis Ritchie, known by C enthusiasts as K&R or the "White Book," became the definitive source for C programming. As of this
writing, the second edition of K&R, originally published in 1988, is still widely available. The original, pre-standard version of C is called K&R C based on that book.
To ensure that people didn't create their own dialects over time, C developers worked through the 1980s to create standards for the language. The U.S. standard for C,
American National Standards Institute (ANSI) standard X3.159-1989, became official in 1989. The International Organization for Standardization (ISO) standard,
ISO/IEC 9899:1990, followed in 1990. The versions of C after K&R reference these standards and their later revisions (C89, C90 and C99). You might also see C89
referred to as "ANSI C," "ANSI/ISO C" or "ISO C."
C and its use in UNIX was just one part of the boom in operating system development through the 1980s. For all its improvements over its predecessors, though, C
was still not effortless to use for developing larger software applications. As computers became more powerful, demand increased for an easier programming
experience. This demand prompted programmers to build their own compilers, and thus their own new programming languages, using C. These new languages could
simplify coding complex tasks with lots of moving parts. For example, languages like C++ and Java, both developed from C, simplified object-oriented programming, a
programming approach that optimizes a programmer's ability to reuse code.
Now that you know a little background, let's look at the mechanics of C itself.
converted by W eb2PDFConvert.com
converted by W eb2PDFConvert.com
Line 6 -- Every function that returns a value must include a return statement like this one. In C, the main function must always have an integer return type, even though
it's not used within the program. Note that when you're running a C program, though, you're essentially running its main function. So, when you're testing the program,
you can tell the computer to show the return value from running the program. A return value of 0 is preferred since programmers typically look for that value in testing
to confirm the program ran successfully.
When you're ready to test your program, save the file and compile and run the program. If you're using the gcc compiler at a command line, and the program is in a file
called sample.c, you can compile it with the following command:
gcc -o sample.exe sample.c
If there are no errors in the code, you should have a file named sample.exe in the same directory as sample.c after running this command. The most common error is
a syntax error, meaning that you've mistyped something, such as leaving off a semicolon at the end of a line or not closing quotes or parentheses. If you need to
make changes, open the file in your text editor, fix it, save your changes and try your compile command again.
To run the sample.exe program, enter the following command. Note the ./ which forces the computer to look at the current directory to find the executable file:
./sample.exe
Those are the basics of coding and compiling for C, though there's a lot more you can learn about compiling from other C programming resources. Now, let's open the
box and see what pieces C has for building programs.
Functions in C
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>;
converted by W eb2PDFConvert.com
}
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 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.
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));
}
converted by W eb2PDFConvert.com
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.
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.
converted by W eb2PDFConvert.com
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.
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;
you@emailaddress.com
SUBSCRIBE
converted by W eb2PDFConvert.com
HOWSTUFFWORKS
Adventure
Animals
Auto
Culture
Entertainment
Health
Home & Garden
Lifestyle
Money
Science
Tech
MORE STUFF
Store
Blogs
RSS
Maps
Podcasts
Quizzes
Newsletters
Video
Site Map
HSW China
STUFF WEBSITES
BrainStuff
CarStuff
Fw:Thinking
Stuff Mom Never Told You
Stuff of Genius
Stuff They Don't Want You to
Know
Stuff to Blow Your Mind
Stuff You Missed in History Class
Stuff You Should Know
CUSTOMER SERVICE
Advertising
Contact Us
Help
CORPORATE
About Us
Careers @ HSW
Privacy Policy
Visitor Agreement
FOLLOW US
converted by W eb2PDFConvert.com