Beruflich Dokumente
Kultur Dokumente
We are now ready to start discussing C programming with a little more detail.
Engineering Programming 1
Datatypes
operators
Language constucts
If statement
while loops
for loops
arrays
Engineering Programming 1
Variable Names
variable Names are made up of letters (a-z, A-Z, _ ) and digits (numbers 0-9)
Upper and lower case letters are distinct. so x and X are two different names.
Traditional C practice is to use lower case for variable names, and all upper case for
symbolic constants.
Keywords like if , else , int , float , etc., are reserved: you can't use them as variable
names. They must be in lower case.
Engineering Programming 1
Data Types and Sizes
When you declare a variable in C you have to give it a data type.
There are only a few basic data types in C:
char
int
float
double
In addition, there are a number of qualifiers that can be applied to these basic types.
Short
long
Engineering Programming 1
Arithmetic Operators ( + - / * )
int result;
int a = 5;
Int b = 6;
result = a + b;
result = a b;
a = a + 1;
a = a 1;
a += 1; or a++;
a -= 1; or a--;
Engineering Programming 1
Language constructs
if statement
if ( <condition>)
Statement;
if ( <condition>) {
statements;
statements;
}
Engineering Programming 1
Language constructs
if statement
if ( <condition>) {
statements;
statements;
}
-------------------------------------------------------------------------<condition> is things like:
a==1
b>2
a>b
Engineering Programming 1
Language constructs
if statement
if ( <condition>) {
statements;
statements;
}
-------------------------------------------------------------------------if (a > b)
z = a;
else
z = b;
-------------------------------------------------------------------------'Statements' are the things you want the program to do
Engineering Programming 1
Lets put what we know to together and write a program to print the biggest of 3 numbers.
Lets use something called 'pseudo code'. (That's just saying lets use english.)
Say we have variables x,y,z with some numbers stored in them, then our program would
have to be something like the following.
Compare x to y and remember the higher value (store the higher in a new variable)
now compare the value (in the new variable) with z and store the higher value again.
Engineering Programming 1
Let's write that a bit more like a program (this is yet English not 'C')
If x is_greater than y
Store x in temp_variable
Else
Store y in temp_variable
// now compare z with temp variable
If z is_greater than temp_variable
Store z in temp_variable
Else
// no need to do anything.
Print the value in temp_variable, this will be the highest value.
Engineering Programming 1
int x,y,z, temp_var;
If x is_greater than y
Store x in temp_variable
Else
Store y in temp_variable
if ( x > y)
temp_var = x;
else
temp_var =y;
if ( z > temp_var)
temp_var = z;
// else not needed.
Engineering Programming 1
While loop
While ( <condition> ){
Statements;
}
int i=0;
while ( i < 10) {
printf(i = %d\n, i);
i = i + 1;
}
Engineering Programming 1
for loop
int i=0;
while ( i < 10) {
printf(i = %d\n, i);
i = i + 1;
}