You are on page 1of 14

# Kaynak: Now You C it, www.pacsoftware.com, Prof. Dr. Ali Demir, 21.05.

01, Sayfa 1
Array
Assignment
Bit
Break
Byte
C
C++
Case
Char
Continue
Declare
Default
Define
Do
Double
Else
Enum
Expression
Float
Function
Goto
If
Index
Int
Long
Loop
Main
Operators
Pointer
Prototype
Short
Statement
String
Struct
Switch
Variable
Void
While

Kaynak: Now You C it, www.pacsoftware.com, Prof. Dr. Ali Demir, 21.05.01, Sayfa 2
*Array
An array is a series of values, which can be treated as a group. For example, the following declares an array of
10 integers:

int a[10];

The name of the array is "a". To assign a value to the first of the integers, you could use a statement like this:

a[0] = 52;

To assign a value to the last of the 10 integers, you would use:

a[9] = 97;

The value within the brackets is called the "index". It indicates which of the integers you are using. It can be a
constant, as in the examples above, or an expression, like this:

a[5*i] = 19;

You can use an array value anyplace you use a regular variable. For example:
total = a[5] + a[6];

*Assignment
C allows you to assign a value to a variable like this:
a = 5;

This example assigns the value "5" to the variable "a". You can use an assignment within an expression, like
this:

b = (a=5)+2;

This example assigns "5" to "a", then adds 5 to 2 yielding 7, and finally assigns that 7 to b.

*Bit
A bit is the smallest unit of information storage. Each bit can have only two possible states, usually referred to
as "0" or "1". (The states are also called "off" and "on".) Bits are combined together in a computer to store
information.

*Break
A "break" statement can be used two places.

The first place is within a do, while, or for loop. In this case, it causes the loop to end, and the program resumes
execution following the end of the loop. Here's an example:

int i;
int sum=0;
for (i=0; i<10; ++i) {
sum += i;
if (sum > 100) {
break;
}
}
sum += 5;

Here, if the value of "sum" exceeds 100, the "break" statement will be executed. In that case, the "for" loop will
end, and the program will continue running with the "sum += 5" statement.

The second place a "break" statement can be used is in a "switch" statement. It marks the end of the code which
is executed for a given case. Here's an example:

Kaynak: Now You C it, www.pacsoftware.com, Prof. Dr. Ali Demir, 21.05.01, Sayfa 3
switch (a) {

case 5:
b += 5;
c += 6;
break;

case 6:
c = 2;
break;

default:
e = 19;
break;
}

*Byte
A collection of bits. Most commonly, a byte is 8 bits, which is enough to store one character using the ASCII
coding system for characters.

*C
"C" is the third letter of the alphabet.

Perhaps more interesting, it is also a computer programming
language developed by Dennis Ritchie.

*C++
"C++" is a language based on "C", but which incorporates several enhancements. The most significant
enhancement is the addition of object-oriented programming constructs.

Almost everything you learn on this CD about C is also true for C++, so C is a good first step toward learning
the more complex C++.

*Case
The "case" statement is used within a "switch" statement to indicate what code should be executed for a given
value.

For example, consider this "switch" statement:

switch (a) {

case 5:
b=6;
break;

case 10:
b=19;
c=22;
break;

default:
b=0;
break;
}

The "switch" statement causes the expression "a" to be evaluated. It's value will determine which of the other
statements are executed.

If "a" has a value of 5, the statement "b=6" will be executed.

Kaynak: Now You C it, www.pacsoftware.com, Prof. Dr. Ali Demir, 21.05.01, Sayfa 4
If "a" has a value of 10, the statements "b=19" and "c=22" will be executed.

If "a" has any value other than 5 or 10, the statement "b=0" will be executed.

*Char
"char" is one of the basic data types in C. It describes a value large enough to hold a single character. In most
cases, a char variable is 8 bits in size.

Although char variables are generally used to hold characters, they can also be used to hold small integers.

*Continue
A "continue" statement can be used within a do, while, or for loop. It causes a jump to the top of the loop.
Here's an example:

int i;
int sum=0;
int product=0;
for (i=0; i<10; ++i) {
sum += i;
if (product > 100) {
continue;
}
product *= i;
}
sum += 5;

Here, the statement "sum += i" will be executed every time through the loop (that is, 10 times). But, the
statement "product *= i" will be skipped if product exceeds 100. When
the "continue" statement is executed, the rest of the statements in the loop are skipped, but the loop continues.

*Declare
There are two similar but different terms in C, which are easily confused: declare and define. Here are
examples of each:

int sum(int a);
.
.
.
int sum(int a) {
int sum=0;
int i;
for (i=0; i<a; ++i) {
sum += i;
}
return sum;
}

In this example, the first statement ("int sum(int a);") DECLARES the function. That is, it tells the compiler the
functions name, return type, and parameters. The later statements (beginning with "int sum(int a) {") DEFINE
the function, giving the compiler the actual statements to be executed when the function is called.

The declaration of the function is also called its "prototype".

Variables can also be "declared". For example, the statement "int i;" in the above example declares the variable
"i".

*Default
This statement is part of a "switch" statement. It identifies the code that should be executed if none of the other
cases match. For example:

Kaynak: Now You C it, www.pacsoftware.com, Prof. Dr. Ali Demir, 21.05.01, Sayfa 5
switch (a) {

case 5:
b=6;
break;

case 10:
b=19;
c=22;
break;

default:
b=0;
break;
}

Here, the "b=0" statement will be executed if "a" is not 5 and "a" is not 10.

*Define
There are two similar but different terms in C, which are easily confused: declare and define. Here are
examples of each:

int sum(int a);
.
.
.
int sum(int a) {
int sum=0;
int i;
for (i=0; i<a; ++i) {
sum += i;
}
return sum;
}

In this example, the first statement ("int sum(int a);") DECLARES the function. That is, it tells the compiler the
functions name, return type, and parameters. The later statements (beginning with "int sum(int a) {") DEFINE
the function, giving the compilerthe actual statements to be executed when the function is called.

The declaration of the function is also called its "prototype".

*Do
This statement causes a loop to occur. Here is an example:

do {
i += 20;
} while (i < 100);

In this case, the statement inside the loop ("i += 20") will be executed over and over until the condition ("i <
100") is no longer true.

It is important to understand the difference between a "do" loop and a "while" loop. Here's an example of a
"while" loop:

while (i < 100) {
i += 20;
}

This code does *almost* the same thing as the earlier example, but with one difference: the earlier example will
execute the statement inside the loop ("i += 20") at least once, no matter what value "i" had before the loop

Kaynak: Now You C it, www.pacsoftware.com, Prof. Dr. Ali Demir, 21.05.01, Sayfa 6
began. The second example would not execute the interior of the loop even once if "i" was already greater than
100.

*Double
This is one of the basic data types in C. It stores a floating-point number. Here is an example of how to declare
a variable of this type:

double sum;

*Else
This statement is used along with an "if" statement to provide code to be executed if the condition in the "if" is
false. Here's an example:

if (a < 5) {
b = 7;
} else {
b = 8;
}

In this example, "a" will be compared to "5". If it is less, "7" will be assigned to "b"; if it isn't less, "8" will be
assigned to "b".

An "else" statement can appear only right after an "if". It can include braces, as in the above example, or not.
The braces are required if more than one statement is to be controlled by the "else", as in this example:

if (sum < total) {
sum += value;
} else {
total = 0;
goal = reached;
}

*Enum
An "enum" is a type of data which can be declared in C. In a sense, it allows you to declare your own data types.
Here's an example:

enum tempurature {
hot,
normal,
cool
};

This defines an enum type named "temperature". You can then declare variables of this type like this:

enum temperature a;

You can assign values to the variable like this:

a = hot;

It is common to use "typedef" with "enum", like this:

typedef enum {
hot,
normal,
cool
} temperature;

Now a variable of this type can be declared more compactly:

Kaynak: Now You C it, www.pacsoftware.com, Prof. Dr. Ali Demir, 21.05.01, Sayfa 7
temperature a;

*Expression
An "expression" is a collection of constants and variables, combined with operators. For example:

a + 5

In this expression, "a" is a variable, "5" is a constant, and "+" is an operator. This expression causes the
compiler to obtain the value of "a", and add 5 to it.

Expressions are used in many C statements. For example:

b = a + 5;

This takes the value of the expression and stores it in the variable "b".

Expressions can be very simple:

c

or very compilicated:

c * f[22] / 19.5 >> (a > b ? 2 : 1)

*Float
This is one of the basic data types in C. It stores a floating-point number. Here is an example of how to declare
a variable of this type:

float sum;

*Function
A C program is often divided into pieces called "functions". Each function performs a small bit of work, and the
program combines them together to perform a larger piece of work.

Here is an example of a function:

if (a == 0) {
return 0;
}
if (b == 0) {
return 0;
}
return a+b;
}

Data is passed to the function, it performs some work, and it then returns a value. In this case, two integers ("a"
and "b") are passed to the function, and an integer is returned.

To cause the function to be executed, it is "called" from another place in the program. Here is an example of
calling the above function:

This would cause the function to be executed with "a" having a value of "5", and "b" having the same value as
the variable "q". The value calculated by the function will be returned and placed in "sum".

*Goto

Kaynak: Now You C it, www.pacsoftware.com, Prof. Dr. Ali Demir, 21.05.01, Sayfa 8
Normally, a C funciont is executed one statement at a time, starting at the top and working down to the bottom.
The "goto" statement is one of several C statements that allow you to change this order. It causes execution to
continue with a different statement. Here is
an example:

a = 5;
if (b > 6) {
goto wrapup;
}
c = sum(5,22);
e = sum(c,22);
wrapup: f = 0;

The word "wrapup" is called a "label", and is used to identify the statement to be executed next. In this example,
if "b" is greater than "6", the "goto" statement will be executed. That causes the program to skip past the other
two statements and execute "f=0".

*If
The "if" statement in C allows you to have one or more statements executed only if a certain condition is met.
For example:

if (a > 5)
c = 0;

This example will set "c" to "0" only if the condition ("a > 5") is true.

An "if" statement can effect just one statement, as in the above example. It can also effect several statements by
including those statements in braces, like this:

if (a > 5) {
c = 0;
e = 5;
}

In this example, both of the statements ("c = 0" and "e = 5") will be executed only if the condition ("a > 5") is
true.

*Index
An "index" is a value that determines which element of an array is used. For example, suppose you have an
array of 10 integers, like this:

int a[10];

You can assign a value to the first of those 10 integers like this:

a[0] = 55;

In this case, the "0" indicates which of the integers you are working with, and it is called the "index".

*Int
This is one of the basic data types in C. It identifies a variable which can hold an integer value (that is, a whole
number).

For example, here is an integer variable:

int a;

The variable "a" could hold values like "5" or "-22" (but not "1.34").

*Long

Kaynak: Now You C it, www.pacsoftware.com, Prof. Dr. Ali Demir, 21.05.01, Sayfa 9
The "long" keyword can be used when declaring integers or doubles. It can cause additional storage to be used
for the variable, giving it a greater possible range of values.

For example, here is a regular integer:

int a;

and here is a long integer:

long int b;

It is up to the compiler to decide how long each of these variables is. In fact, the compiler *may* decide they
will both be the same length. But a "long int" is always at least as large as an "int".

Here is an example of a "double" and a "long double" variable:

double c;
long double d;

A "long double" is commonly longer than a "double", but again the compiler can decide how long each will be.

*Loop
A "loop" is a set of statements which will be executed over and over. For example:

int i;
int sum=0;
for (i=0; i<10; ++i) {
sum += i;
}

This is a "loop" because it executes the statement "sum += i" 10 times.

*Main
C programs always begin with the computer executing one function in the program. In standard C, this function
is named "main". Because of this, all standard C programs must include a function named "main" so the
program will have a starting point.

There are some environments in which this rule doesn't apply. For example, if you are writing a program for
Microsoft Windows (not DOS), the program will begin with a function named "WinMain" instead of "main".

*Operators
An "operator" is a symbol which indicates that a certain operation should be performed on one or more values.
For example, here is an expression:

a + 5

This expression includes the operator "+" which causes the value of variable "a" to be added to "5".

*Pointer
A pointer is a type of C variable that references a storage location. For example, you can declare an integer
variable like this:

int a;

You can declare an integer pointer like this:

int *b;

You can then make the pointer "point" to the storage location used by the integer "a" like this:

Kaynak: Now You C it, www.pacsoftware.com, Prof. Dr. Ali Demir, 21.05.01, Sayfa 10
b = &a;

(The "&" means "address of".) Now that "b" is pointing to "a", you can use it to change the value of "a", like
this:

*b = 5;

The "*" means "the thing the pointer points to", so that statement assigns 5 to the variable "b" is pointing to. In
this case, that would assign "5" to the variable "a", just as if you had
used this statement:

a = 5;

*Prototype
Functions in C generally consist of two parts: the prototype and the function definition. The prototype tells the
compiler the name of the function, what parameters it uses, and what
type of value it returns. The function definition tells the compiler what statements to execute when the function
is called.

Here is an example of a function prototype:

int sum(int a);

And here is an example of a function definition:

int sum(int a) {
int sum=0;
int i;
for (i=0; i<a; ++i) {
sum += i;
}
return sum;
}

Generally, function prototypes are placed near the beginningof a program. That allows the compiler to know
about the function so it can be called before it has seen the actual function itself. (If the compiler didn't see the
prototype for the function before any calls
to the function, it might not be able to handle the calls properly.)

*Short
The word "short" can be used to change the size of integer variables. For example:

int a;

This declares a "regular" integer. This declares a "short" integer:

short int b;

A short integer is often shorter than a regular integer, which means it uses less storage space, but cannot hold as
large a value.

(Each compiler is allowed to select whatever size it wants, within limits, for both regular and short integers. As
a result, it is possible that a short integer will be the same size as a regular integer for any given compiler.)

*Statement
A C program consists of one or more functions. Within each function are a series of "statements". A
"statement" is simply an instruction that tells the computer to do something. Here is an example of a function:

void myFunction(void) {
a = 5;

Kaynak: Now You C it, www.pacsoftware.com, Prof. Dr. Ali Demir, 21.05.01, Sayfa 11
b = 6 * a;
}

This function has two statements: "a = 5;" and "b = 6 * a;". When the function is executed, the first statement
will be performed, and then the second statement will be performed. The end of each statement is marked with a
";".

*String
A "string" is a series of characters. In C, strings can be stored as constants by enclosing them in quotes, like this:

"abcd"

This represents the string consisting of 5 characters: a, b, c, d, and a null character. The end of a string is always
identified by a null character in C. (A "null character" is a character whose numeric value is zero.)

Strings are often stored in character arrays in C. For example:

char a[10] = "abcd";

This declares an array of 10 characters, and it assigns the first 5 of those characters to be a, b, c, d, and null.

The standard libraries that are included with most C compilers provide a variety of functions that can manipulate
strings. For example, here is a call to a standard function that would copy a new string into the "a" array:

strcpy(a,"different");

*Struct
A "struct" is a type of data in C that consists of other data elements. For example, you could combine three
integers into a struct like this:

struct date {
int year;
int month;
int day;
};

This example simply declares the layout of a struct, it doesn't create any variables. To declare a variable of this
struct, you could do something like this:

struct date a;

This declares the variable "a" to be a struct. You can then work with each of the individual integers within the
struct like this:

a.year = 2010;
a.month = 10;
a.day = 10;

You can use "a.year" anyplace you would use an integer variable.

*Switch
The "switch" statement in C allows you to write several pieces of code, and indicate which of those pieces of
code should be executed based on a variable. For example:

switch (a) {

case 2:
b = 5;
break;

Kaynak: Now You C it, www.pacsoftware.com, Prof. Dr. Ali Demir, 21.05.01, Sayfa 12
case 4:
b = 12;
break;

default:
b = 99;
break;
}

In this case, one of the three statements ("b=5", "b=12", or "b=99") will be executed, depending upon the value
of variable "a". If "a" is 2, the first statement will be executed; if "a" is 4, the second statement will be
executed; if "a" has any other value, the last statement will be executed.

The "switch" statement can use any expression. For example, this would select which statement to be executed
based on the value of the expression "a+5":

switch (a+5) {

case 2:
b = 5;
break;

case 4:
b = 12;
break;

default:
b = 99;
break;
}
*Variable
A "variable" is a location in the computer's memory. Each variable has a "value", which is the information
stored in that location in memory. It also has a "name", which is how that location is identified in a program.

For example, this statement tells the computer to reserve enough memory
to hold an integer value:

int a;

It assigns the name "a" to that location in memory. You can put information into that location like this:

a = 5;

This would store a "5" in that location in memory. You can get the information out of the location by using the
name "a" in another statment, like this:

b = a * 2;

This statement would retrieve the contents of storage location "a", multiply it by two, and store the result in the
storage location named "b".

*Void
Functions in C often have some data sent to them, and they then send some data back. The keyword "void" can
be used to indicate either that no data is being sent to the function, or that no data is being sent back.

For example, here is a function:

int sum(float a) {
int b;
b = a * 5;

Kaynak: Now You C it, www.pacsoftware.com, Prof. Dr. Ali Demir, 21.05.01, Sayfa 13
return b;
}

This function is sent a floating point value, and sends back an integer. If you don't want the function to return a
value, you can put the word "void" for the return value, like this:

void sum(float a) {
int b;
b = a * 5;
}

If you won't be sending any data to the function, you can use "void" like this:

int sum(void) {
int b;
b = 5 * 23;
return b;
}

*While
This statement causes a loop to occur. Here is an example:

while (i < 100) {
i += 20;
}

In this case, the statement inside the loop ("i += 20") will be executed over and over until the condition ("i <
100") is no longer true.

It is important to understand the difference between a "do" loop and a "while" loop. Here's an example of a "do"
loop:

do {
i += 20;
} while (i < 100);

This code does *almost* the same thing as the earlier example, but with one difference: the second example will
execute the statement inside the loop ("i += 20") at least once, no matter what value "i" had before the loop
began. The first example would not execute the interior of the loop even once if "i" was already greater than
100.

*End

Kaynak: Now You C it, www.pacsoftware.com, Prof. Dr. Ali Demir, 21.05.01, Sayfa 14
PAC Software

*PAC Software
PAC Software, Inc. is a small consulting company in Denver, Colorado. This CD, along with others, was
developed by the staff at PAC.

You can obtain further information about PAC from the web page at www.pacsoftware.com.

The name "PAC" stands for "Piece A Cake".