Tuesday, June 13, 2006

Lessons

http://cplus.about.com/od/beginnerctutoria1/l/aa031002a.htm

Lesson 1: Writing And Compiling A First Program
Hello World




Welcome to About.com's C tutorial. The lessons in this tutorial will take you from being a beginner to being able to write real programs in C.

C is a compiled language. The C compiler is a program that reads source code, which is the C code written by a programmer, and produces an executable or binary file that in a format that can be read and executed (run) by a computer. The source file is a plain text file containing your code. The executable file consists of machine code, 1's and 0's that are not meant to be understood or read by people, but only by computers.

The best way to learn anything is to jump right in, so let's start by writing a simple C program.

First Program - Hello World
#include
void main()
{
printf("Hello World From About\n");
}

Line 1: #include
As part of compilation, the C compiler runs a program called the C preprocessor. The preprocessor is able to add and remove code from your source file. In this case, the directive #include tells the preprocessor to include code from the file stdio.h. This file contains declarations for functions that the program needs to use. A declaration for the printf function is in this file.

Line 2: void main()
This statement declares the main function. A C program can contain many functions but must always have one main function. A function is a self-contained module of code that can accomplish some task. Functions are examined in a later tutorial. The "void" specifies the return type of main. In this case, nothing is returned to the operating system.

Line 3: {
This opening bracket denotes the start of the program.

Line 4: printf("Hello World From About\n");
Printf is a function from a standard C library that is used to print strings to the standard output, normally your screen. The compiler links code from these standard libraries to the code you have written to produce the final executable. The "\n" is a special format modifier that tells the printf to put a line feed at the end of the line. If there were another printf in this program, its string would print on the next line.

Line 5: }
This closing bracket denotes the end of the program.

That's it. To get the most of this series of tutorials, you should get access to both a text editor and a C compiler. Some instructions for doing this are in our tutorials on compiling.



C Tutorial - Lesson 2: Variables


Welcome to the second lesson in About.com's C tutorial. This lesson will teach you how to declare and use variables in C. A variable is used to hold data within your program. A variable represents a location in your computer's memory. You can put data into this location and retrieve data out of it. Every variable has two parts, a name and a data type.

Variable Names
Valid names can consist of letters, numbers and the underscore, but may not start with a number. A variable name may not be a C keyword such as if, for, else, or while. Variable names are case sensitive. So, Age, AGE, aGE and AgE could be names for different variables, although this is not recommended since it would probably cause confusion and errors in your programs. Let's look at some variable declarations to better understand these rules. Note that int, float and double are built in C data types as explained latter in this lesson.

List of C Keywords

Which of the following are valid variable names?
int idnumber;
int transaction_number;
int __my_phone_number__;
float 4myfriend;
float its4me;
double VeRyStRaNgE;
float while;
float myCash;
int CaseNo;
int CASENO;
int caseno;

ANSWERS

Data Types
C provides built in data types for character, float and integer data. A mechanism, using the keyword typedef, exists for creating user-defined types. As an aside, in C you may assign a value to a variable when you declare it.

Integer variables are used to store whole numbers. There are several keywords used to declare integer variables, including int, short, long, unsigned short, unsigned long. The difference deals with the number of bytes used to store the variable in memory, long vs. short, or whether negative and positive numbers may be stored, signed vs. unsigned. These differences will be explained in more advanced tutorials. For now, use int to declare integer variables. On most 32-bit systems, int is synonymous with signed long.

Examples:
int count;
int number_of_students = 30;


Float variables are used to store floating point numbers. Floating point numbers may contain both a whole and fractional part, for example, 52.7 or 3.33333333. There are several keywords used to declare floating point numbers in C including float, double and long double. The difference here is the number of bytes used to store the variable in memory. Double allows larger values than float. Long double allows even larger values. These differences will be explained in more advanced tutorials. For now, use float to declare floating point variables.

Examples:
float owned = 0.0;
float owed = 1234567.89;


Character variables are used to store character values. The use of characters and strings will be covered in a latter tutorial. Character variables are declared with the keyword char.

Examples:
char firstInitial = 'J';
char secondInitial = 'K';
Next page > Typedef, Rvalues/Lvalues > Page 1, 2

The next lesson covers declaring and using constants in C.


Lesson 3: Constants



Welcome to About.com's C tutorial. This lesson will teach you how to declare and use constants in C. A constant is similar to a variable in the sense that it represents a memory location. It differs, as I sure you can guess, in that it cannot be reassigned a new value after initialization. In general, constants are a useful feature that can prevent program bugs and logic errors. Unintended modifications are prevented from occurring. The compiler will catch attempts to reassign new values to constants.

Using #define
There are three techniques used to define constants in C. First, constants may be defined using the preprocessor directive #define. The preprocessor is a program that modifies your source file prior to compilation. Common preprocessor directives are #include, which is used to include additional code into your source file, #define, which is used to define a constant and #if/#endif, which can be used to conditionally determine which parts of your code will be compiled. The #define directive is used as follows.
#define pi 3.1415
#define id_no 12345

Wherever the constant appears in your source file, the preprocessor replaces it by its value. So, for instance, every "pi" in your source code will be replace by 3.1415. The compiler will only see the value 3.1415 in your code, not "pi". Every "pi" is just replaced by its value. Here is a simple program illustrating the preprocessor directive #define.
#include

#define monday 1
#define tuesday 2
#define wednesday 3
#define thursday 4
#define friday 5
#define saturday 6
#define sunday 7

int main()
{

int today = monday;

if ((today == saturday) || (today == sunday))

{
printf("Weekend\n");
}
else
{
printf("Go to work or school\n");
}

return 0;
}

Using const variables

The second technique is to use the keyword const when defining a variable. When used the compiler will catch attempts to modify variables that have been declared const.
const float pi = 3.1415;
const int id_no = 12345;

There are two main advantages over the first technique. First, the type of the constant is defined. "pi" is float. "id_no" is int. This allows some type checking by the compiler. Second, these constants are variables with a definite scope. The scope of a variable relates to parts of your program in which it is defined. Some variables may exist only in certain functions or in certain blocks of code. You may want to use "id_no" in one function and a completely unrelated "id_no" in your main program. Sorry if this is confusing, the scope of variables will be covered in a latter lesson.


Lesson 3: Constants


Welcome to About.com's C tutorial. This lesson will teach you how to declare and use constants in C. A constant is similar to a variable in the sense that it represents a memory location. It differs, as I sure you can guess, in that it cannot be reassigned a new value after initialization. In general, constants are a useful feature that can prevent program bugs and logic errors. Unintended modifications are prevented from occurring. The compiler will catch attempts to reassign new values to constants.

Using #define
There are three techniques used to define constants in C. First, constants may be defined using the preprocessor directive #define. The preprocessor is a program that modifies your source file prior to compilation. Common preprocessor directives are #include, which is used to include additional code into your source file, #define, which is used to define a constant and #if/#endif, which can be used to conditionally determine which parts of your code will be compiled. The #define directive is used as follows.
#define pi 3.1415
#define id_no 12345

Wherever the constant appears in your source file, the preprocessor replaces it by its value. So, for instance, every "pi" in your source code will be replace by 3.1415. The compiler will only see the value 3.1415 in your code, not "pi". Every "pi" is just replaced by its value. Here is a simple program illustrating the preprocessor directive #define.
#include

#define monday 1
#define tuesday 2
#define wednesday 3
#define thursday 4
#define friday 5
#define saturday 6
#define sunday 7

int main()
{

int today = monday;

if ((today == saturday) || (today == sunday))

{
printf("Weekend\n");
}
else
{
printf("Go to work or school\n");
}

return 0;
}

Using const variables

The second technique is to use the keyword const when defining a variable. When used the compiler will catch attempts to modify variables that have been declared const.
const float pi = 3.1415;
const int id_no = 12345;

There are two main advantages over the first technique. First, the type of the constant is defined. "pi" is float. "id_no" is int. This allows some type checking by the compiler. Second, these constants are variables with a definite scope. The scope of a variable relates to parts of your program in which it is defined. Some variables may exist only in certain functions or in certain blocks of code. You may want to use "id_no" in one function and a completely unrelated "id_no" in your main program. Sorry if this is confusing, the scope of variables will be covered in a latter lesson.


Lesson 3: Constants


Using enumerations
The third technique to declare constants is called enumeration. An enumeration defines a set of constants. In C an enumerator is of type int. C++ allows variables to be a new enumeration type, which allows the compiler to perform type and bound checking. Unfortunately, C does not provide this. But the enumeration is still useful to define a set of constants instead of using multiple #defines.
#include

enum days {monday=1,tuesday,wednesday,thursday,friday,saturday,sunday};

int main()
{
enum days today = monday;

if ((today == saturday) || (today == sunday))
{
printf("Weekend\n");
}
else
{
printf("Go to work or school\n");
}

return 0;

}

There are a few things to note in this example. First, is clearer to use the enumeration than the #defines used in the earlier example. Next, the variable today is of type int, as are all enumerators. Any expression that can be assigned to an int variable can be assigned to "today". The compiler will not catch bound errors as would occur in C++. For instance, the follow assignment, an error, could appear in the program and not be flagged by the compiler.
today = 9999; /* Error, today should be between 1 and 7 */

Look at the following examples to understand how to assign values to the constants in enumerations.

enum COLOR { RED, BLUE, GREEN};
enum SHAPE {SQUARE, RECTANGLE, TRIANGLE, CIRCLE, ELLIPSE};


Each enumerated constant (sometimes called an enumerator) has an integer value. Unless specified, the first constant has a value of zero. The values increase by one for each additional constant in the enumeration. So, RED equals 0, BLUE equals 1, and GREEN = 2. SQUARE equals 0, RECTANGLE equals 1, TRIANGLE equals 2 and so forth. The values of each constant can also be specified.

enum SHAPE {SQUARE=5,RECTANGLE,TRIANGLE=17,CIRCLE,ELLIPSE};


Here, SQUARE equals 5, RECTANGLE equals 6, TRIANGLE equals 17, CIRCLE equals 18 and ELLIPSE equals 19.


Lesson 3: Constants

Welcome to About.com's C tutorial. This lesson will teach you how to declare and use constants in C. A constant is similar to a variable in the sense that it represents a memory location. It differs, as I sure you can guess, in that it cannot be reassigned a new value after initialization. In general, constants are a useful feature that can prevent program bugs and logic errors. Unintended modifications are prevented from occurring. The compiler will catch attempts to reassign new values to constants.

Using #define
There are three techniques used to define constants in C. First, constants may be defined using the preprocessor directive #define. The preprocessor is a program that modifies your source file prior to compilation. Common preprocessor directives are #include, which is used to include additional code into your source file, #define, which is used to define a constant and #if/#endif, which can be used to conditionally determine which parts of your code will be compiled. The #define directive is used as follows.
#define pi 3.1415
#define id_no 12345

Wherever the constant appears in your source file, the preprocessor replaces it by its value. So, for instance, every "pi" in your source code will be replace by 3.1415. The compiler will only see the value 3.1415 in your code, not "pi". Every "pi" is just replaced by its value. Here is a simple program illustrating the preprocessor directive #define.
#include

#define monday 1
#define tuesday 2
#define wednesday 3
#define thursday 4
#define friday 5
#define saturday 6
#define sunday 7

int main()
{

int today = monday;

if ((today == saturday) || (today == sunday))

{
printf("Weekend\n");
}
else
{
printf("Go to work or school\n");
}

return 0;
}

Using const variables

The second technique is to use the keyword const when defining a variable. When used the compiler will catch attempts to modify variables that have been declared const.
const float pi = 3.1415;
const int id_no = 12345;

There are two main advantages over the first technique. First, the type of the constant is defined. "pi" is float. "id_no" is int. This allows some type checking by the compiler. Second, these constants are variables with a definite scope. The scope of a variable relates to parts of your program in which it is defined. Some variables may exist only in certain functions or in certain blocks of code. You may want to use "id_no" in one function and a completely unrelated "id_no" in your main program. Sorry if this is confusing, the scope of variables will be covered in a latter lesson.



Lesson 3: Constants


Using enumerations
The third technique to declare constants is called enumeration. An enumeration defines a set of constants. In C an enumerator is of type int. C++ allows variables to be a new enumeration type, which allows the compiler to perform type and bound checking. Unfortunately, C does not provide this. But the enumeration is still useful to define a set of constants instead of using multiple #defines.
#include

enum days {monday=1,tuesday,wednesday,thursday,friday,saturday,sunday};

int main()
{
enum days today = monday;

if ((today == saturday) || (today == sunday))
{
printf("Weekend\n");
}
else
{
printf("Go to work or school\n");
}

return 0;

}

There are a few things to note in this example. First, is clearer to use the enumeration than the #defines used in the earlier example. Next, the variable today is of type int, as are all enumerators. Any expression that can be assigned to an int variable can be assigned to "today". The compiler will not catch bound errors as would occur in C++. For instance, the follow assignment, an error, could appear in the program and not be flagged by the compiler.
today = 9999; /* Error, today should be between 1 and 7 */

Look at the following examples to understand how to assign values to the constants in enumerations.

enum COLOR { RED, BLUE, GREEN};
enum SHAPE {SQUARE, RECTANGLE, TRIANGLE, CIRCLE, ELLIPSE};


Each enumerated constant (sometimes called an enumerator) has an integer value. Unless specified, the first constant has a value of zero. The values increase by one for each additional constant in the enumeration. So, RED equals 0, BLUE equals 1, and GREEN = 2. SQUARE equals 0, RECTANGLE equals 1, TRIANGLE equals 2 and so forth. The values of each constant can also be specified.

enum SHAPE {SQUARE=5,RECTANGLE,TRIANGLE=17,CIRCLE,ELLIPSE};


Here, SQUARE equals 5, RECTANGLE equals 6, TRIANGLE equals 17, CIRCLE equals 18 and ELLIPSE equals 19.


C Tutorial - Lesson 3: Constants

More of this Feature
• Page 2: Enumerations

Related Resources
• C Tutorial - See all lessons
• C Programming Tips
• Hello World
• Compiling a C Program with Visual C++
• Compiling a C Program from the Command Line
• Learn to compile

From Other Guides
• Running Perl Scripts from the MS-DOS command line
• Your First JavaScript
• Top 10 Freebies for Web Designers
• Power Toys for Windows XP


by John Kopp

Welcome to About.com's C tutorial. This lesson will teach you how to declare and use constants in C. A constant is similar to a variable in the sense that it represents a memory location. It differs, as I sure you can guess, in that it cannot be reassigned a new value after initialization. In general, constants are a useful feature that can prevent program bugs and logic errors. Unintended modifications are prevented from occurring. The compiler will catch attempts to reassign new values to constants.

Using #define
There are three techniques used to define constants in C. First, constants may be defined using the preprocessor directive #define. The preprocessor is a program that modifies your source file prior to compilation. Common preprocessor directives are #include, which is used to include additional code into your source file, #define, which is used to define a constant and #if/#endif, which can be used to conditionally determine which parts of your code will be compiled. The #define directive is used as follows.
#define pi 3.1415
#define id_no 12345

Wherever the constant appears in your source file, the preprocessor replaces it by its value. So, for instance, every "pi" in your source code will be replace by 3.1415. The compiler will only see the value 3.1415 in your code, not "pi". Every "pi" is just replaced by its value. Here is a simple program illustrating the preprocessor directive #define.
#include

#define monday 1
#define tuesday 2
#define wednesday 3
#define thursday 4
#define friday 5
#define saturday 6
#define sunday 7

int main()
{

int today = monday;

if ((today == saturday) || (today == sunday))

{
printf("Weekend\n");
}
else
{
printf("Go to work or school\n");
}

return 0;
}

Using const variables

The second technique is to use the keyword const when defining a variable. When used the compiler will catch attempts to modify variables that have been declared const.
const float pi = 3.1415;
const int id_no = 12345;

There are two main advantages over the first technique. First, the type of the constant is defined. "pi" is float. "id_no" is int. This allows some type checking by the compiler. Second, these constants are variables with a definite scope. The scope of a variable relates to parts of your program in which it is defined. Some variables may exist only in certain functions or in certain blocks of code. You may want to use "id_no" in one function and a completely unrelated "id_no" in your main program. Sorry if this is confusing, the scope of variables will be covered in a latter lesson.

0 Comments:

Post a Comment

<< Home