Introduction
to C and NQC
-DataTypes
Data types allow the program to
interpet
the data stored in that data type in a particular manner, such as
paticular division methods or concatination.
- int
- integer data types (whole numbers)
- ex. 1, 2, ..100
- float
- single precision floating point number
- ex. 1.2, 2.3,...100.0
- double
- double precision floating point number
- ex. 1.2, 2.3,...100.0
- char
- a single character
- use single quotes to denote ''
- ex. 'a', 'b', ..'Z'
-Variables
A variable is a memory location that can store
data. Variables are defined by giving the type of the variable,
and then giving a name to that variable. Variables can also given
initial values when they are defined. The values of variables are
dynamic, and can change during the course of the program.
Ex.
int x;
//defines an integer data type variable
called x
float y = 0.0;
//defines a
floating point data type variable y with an initial value of 0.0
double d = 1.2, e = 1.7; //defines two double
precision variables. d has an inital value of 1.2 and e has a
value of 1.7
char input = 'd';
// defines a character data type variable called input with an inital
value of 'd'
-Variable Names
Variable names must start with a letter and can then
be followed by an number of letters and numbers. Variable names
cannot use names that include reserved words (i.e. int float double
char are not valid variable names). Underscores _ can be used in
variable names, but they are hard to type so you can avoid them!
-Comments
A comment is text that is ignored by the computer when the
program runs. Comments are used to document the program, telling
a programmer what is happening. There are two types of comments
in C. The first is a single line comment. In a single line
comment all of the text from the comment to the end of the line is
ignored.
Ex. // this is a single line comment This is all ignored.
The second type of comment is the multiline comment. In a
multiline comment all text is ignored from the start indicator /* to
the end indicator */.
Ex.
/* This is the start of a multiline comment
It continues here
and ends here -> */
-Statement
A statement is a single instruction to be executed by the
program, starting from the top down. Statements are delimited
(end with) by semicolons ; .
Ex.
int x, y;
x = 8;
y = 9;
x = x * y;
-Block statement
A block begins with a { and ends with a }. Block
statements contain one or more statements. Once the beginning
statement in a block is executed, all of the statements in the block
are executed
Ex.
{
x = 7;
y = 8;
}
-Methods
A method is a group of statements that are run sequencially when
that method is called. Methods are always followed by a block
statement. Methods are also given names, just like
variables. Methods also have a return type. This is a value
that is returned by that method. An example of the return type
would be the square function (a function that returns x^2).
Ex.
int SquareFour() // the return type is of type int, the
method name is SquareFour, and no arguments (discussed later)
{
// start of the block statement
int y = 4*4;
//statement 1
return
y; //statement 2 that
returns the value of y
}
//end of the block statement and the method
This method is usefull, but it would be nice to be able to square any
variable, not just 4.
-Parameters
A parameter is a variable that is passed into a function, and
intern can be used by the function. This can be used to write an
universal square function.
Ex.
int Square(int x) // the (formal) paramenter in this example is x
and is of type int
{
int y;
y = x*x; //same x that was passed as a parameter
return y;
}
The preceding function is much more useful than the first
function. An example of using the method created is in the block
as follows.
Ex.
{
int t = 16, s = 0; // create two
variables t and s
s = Square(t); /* The
function Square is called with the (actual) parameter t. When the
function finishes it
returns a
value of 256 is returned by the function. This value is stored in
the variable s */
}
-Main Method
The main method is the method that is called when the program is
run on the system. This is a method name that is reserved for
this use. The main method returns an int and can have no
arugments.
int main() //main method with no parameters
{
int x = 9;
return (0); //exits the main program.
Returning 0 usually indicated that the method finished sucessfully.
}
-Boolean
conditions
C has the following boolean operators that can be
applied to integers
==
|
equals
|
!=
|
not equal
|
>
|
greater than
|
<
|
less than
|
>=
|
greater than
or equal to
|
<=
|
less than or
equal to
|
C also has the following boolean connectors
These operators are used to determine the truth and
false of statements in the program, while doing looping and doing
making decisions.
-if statment
The if statment is used to determine what code
should be executed. If the expression following the if statment
is true, then that statement or block statement is executed.
Ex.
if (x == y)
//note there is no semicolon after the if and the () denote an
expression
x = 0;
Ex.
if (j > 9)
{
//an example of
block if
j = 7;
k = j * 9;
}
-if else
statement
The else statment or block statement is executed
when the if statement is false.
Ex.
if (x < 1)
y = 0;
else
y = x*x;
Looping
-Looping allows the program to do some task
repeatedly with out having to retype the commands.
-while loop
The while loop is loop that will run the code inside
the loop as long as that condition is true.
Ex.
int x = 0;
while (x < 10 ) // while x is
less than 10 we will run the code inside this block
{
x = x + 1;
}
Notic that this loop has to have x< 10 when it
starts. While loops also can repeat forever (infinitly) if there
are no statments that can change the boolean expression.
-for loop
The for loop is a loop that is more compact.
It has initilization, condition checking and increment operators built
into the loop. For loops can be used to gaurantee that a block of
statements are ran a determined amount of times.
Ex.
initilization
condition incrementing
for (int i =
0; i <
10; i
= i + 1)
{
x = x * y;
}
-The initilization can contain many initilization operations.
Ex.
x =0, y = 1, z = 2;
//semicolon denotes the end of the initilization
-The condition can contain a number of conditions connected by
boolen connectors
Ex.
x < 10 && y
> 7; //x is less than 10 and y is greater than 7
-The incrementing section can also contain many incrementing
operators
Ex.
i++, x = x -1;
-Cummulative
Function Example
int raiseToThePower(int b, int e)
{
int ReturnInt;
if (b == 0)
{
ReturnInt = 0;
}
else if (b == 1 || e == 0)
{
ReturnInt = 1;
}
else
{
ReturnInt = b;
for(int i = 0; i < e;
i++)
{
ReturnInt = ReturnInt * b;
}
}
return ReturnInt;
}
-Printing to
the screen (console)
Printing to the console is just a function that you
need to call.
Ex.
printf("hello how are
you"); // this will print "hello how are you" on to the screen
You can also print variables to the screen, but you
have to put in formatting charaters to do this.
Ex.
double x = 7;
printf("Hello
x=%d",x); //this function will print "Hello x=7" and then the
decimal value of x
%c
|
char
|
%d
|
decimal |
%s
|
string
|
%o
|
octal
|
Other important charaters are the newline char and
the tab charcter
Ex.
double x = 7;
printf("Hello %s\n","bob");
printf("You are %d",x);
the following will be
printed on the console
-Comiling
To compile you need a compiler. The gcc
comiler will be used for this example.
gcc FileName.c
-Running
To run just type the name of the file you want to
run(the default is a.out/ a.exe)
./a.out //or
./a.exe