Академический Документы
Профессиональный Документы
Культура Документы
/* is commenting the line. This means that line marked with /*is not included in compilation.
For instance:
struct position{
int x;
int y;
}pos1, *pos2;
or we can write
(*pos2).x=10;
Structures can contain member pointers to other structures or to structures of same tyme. This way
we can create lists dynamic structure:
struct list{
int listNo;
struct list *next;
}item1, item2;
This way you can assign the pointer with address to another structure:
item1.next=&item2;
(by: http://winavr.scienceprog.com/short-introduction-to-c/avr-gccstructures.html)
Basically Structures are nothing more than collection of variables so called members. Structures
allows to reference all members by single name. Variables within a structure doesn't have to be the
same type. General structure declaration:
struct structure_tag_name{
type member1;
type member2;
...
type memberX
};
or
struct structure_tag_name{
type member1;
type member2;
...
type memberX
} structure_variable_name;
in second example we declared the variable name. Otherwise variables can be declared this way:
struct structure_tag_name var1, var2,var3[3];
Members of structure can be accessed by using member operator (.). The member operator connects
the member name to the structure.
Lets take an example:
struct position{
int x;
int y;
}robot;
robot.x=10;
robot.y=15;
or simply
robot={10,15};
struct status{
int power;
struct position coordinates;
} robotstatus;
x=robotstatus.coordinates.x;
Copy;
Assign;
Access members.
Of course you can treat a structure like a variable type. So you can create an array of structures like:
struct status{
int power;
struct position coordinates;
} robotstatus[100];
pow=robotstatus[15].power;
As I mentioned three looping sentences available in C language one of them is While sentence. Lets
take an example:
#include <stdio.h>
int main(void)
{
int guess, i;
i=1;
guess=5;
while (guess!=i)
{
i=guess;
guess=(i+(10000/i))/2;
}
printf(Square root of 10000 is %d\n, guess);
return 0;
}
While(guess!=i) invokes looping operation. This causes statement to be executed repeatedly at the
beginning the condition guess!=i is checked. As long the argument is TRUE the while sentence will be
continued continuously. When guess becomes equal to i while statement will be skipped. I am not
going to deep in to it as there are tons of information about basic C.
For Loop
As we didn't know how many times the loop should be executed then we used while sentence, but
when we know exactly hof many times we need to execute a sentence, there is another loop sentence
for(s1;s2;s3). For construct takes 3 arguments each separated by semicolons. Without any theories
lets see an example:
for (i = 0; i < 5; i++)
{
printf("value of i");
}
1. The for loop has four components; three are given in parentheses and one in the loop body.
while((c=getchar())!=EOF)
if(c>'0'&&c<'9')
n++;
else
n--;
I bet there is no need of explanation. Simply if first part true then first sentence is executed else other
sentence is executed.
Other case is with if else if sentence structure:
if (condition 1)
simple or compound statement // s1
else if (condition 2)
simple or compound statement // s2
else if ( condition 3)
simple or compound statement // s3
.....
else if ( conditon n )
simple or compound statement // sn
Remember that:
1. You can use if-else if when you want to check several conditions but still execute one
statement.
2. When writing an if-else if statement, be careful to associate your else statement to the
appropriate if statement.
3. You must have parentheses around the condition.
4. You must have a semicolon or right brace before the else statement.
Switch statement
switch (expressions)
{
case constant expressions
}
For example:
switch (i/10)
{
case 0: printf ("Number less than 10"); // A
break;
case 1: printf ("Number less than 20"); // B
break;
case 2: printf ("Number less than 30"); // C
break;
default: printf ("Number greater than or equal to 40"); // D
break; }
Remember that:
1. The switch expression should be an integer expression and, when evaluated, it must have an
integer value.
2. The case constant expression must represent a particular integer value and no two case
expressions should have the same value.
3. The value of the switch expression is compared with the case constant expression in the
order specified, that is, from the top down.
4.
The execution begins from the case where the switch expression is matched and it flows
downward.
5. In the absence of a break statement, all statements that are followed by matched cases are
executed. So, if you don't include a break statement and the number is 5, then all the
statements A, B, C, and D are executed.
6. If there is no matched case then the default is executed. You can have either zero or one
default statement.
7. In the case of a nested switch statement, the break statements break the inner switch
statement.
8. The switch statement is preferable to multiple if statements.
7 > 12
// false
20.1 < 20.2
// true
'b' < 'c'
// true
"abb" < "abc" // true
R1
R2
R1 && R2
R1 || R2
! R1
Next would be ternary operators return values based on the outcomes of relational
expressions. For example, if you want to return the value of 1 if the expression is true and 2 if
it is false, you can use the ternary operator.
Example
If you want to assign the maximum values of i and j to k then you can write the statement
k = ( i>j ) ? i : j;
If i > j then k will get the value equal to i, otherwise it will get the value equal to j.
The general form of the ternary operator is:
If expr1 returns true then the value of expr2 is returned as a result; otherwise the value of expr3 is
returned.
Incremental operators are used to increment or decrement value of variable. Incremental operators
can be applied only to variables as prefix or postfix:
i = 3;
j =4;
k = i++ + -j;
you will get the value of k as 6, i as 4 and j as 3. The order of evaluation is as follows:
1. i gets the value 3.
2. j is decremented to 3.
3. k gets the value 3 + 3.
4. i is incremented
Another important operator when programming microcontrollers is bitwise operators. Bitwise
operators interprets operands as strings of bits. Bitwise operators are six: bitwise AND(&), bitwise
OR(|), bitwise XOR(^), bitwise complement(~), left shift(<<), and right shift(>>). For instance:
x >> 3 causes the variable x to be shifted to the right by three bits prior to its use.
r = r | 0x0c; The hexadecimal number 0x0c is a turned on and all other bits turned off.
Operator precedence rules
Operators
Order of evaluation
Remarks
[] ( ) ->
Left to right
& * ~ (cast)
Right to left
Unary
*/%
Left to right
Binary Multiplicative
+-
Left to right
Binary Additive
>> <<
Left to right
Shift operators
Left to right
Relational operators
== !=
Left to right
Equality operators
&
Left to right
Left to right
Left to right
Bitwise OR operator
&&
Left to right
||
Left to right
Logical Or operator
?:
Left to right
Conditional operator
+ sizeof( ) ! ++
Operators
Order of evaluation
Remarks
Right to left
Assignment
Right to left
Comma
= += -= *= /= %=
Constants in C language
(by: http://winavr.scienceprog.com/short-introduction-to-c/constants-in-clanguage.html)
Constant value is understandable as non changeable value like PI=3.141592... value in math. Usually
you use constants in your programs, but don't realize that they are constants. For instance:
x=x+3; The number 3 is a constant which will be compiled directly in addition operation. Constants
can be character or string. Like in function printf(Hello World\n); Hello World is a string constant
which is placed in program memory and will never changes.
It is usually recommended to declare constants by using identifier with reserved word const:
const int No=44;
By identifying the variable as constant will cause compiler to store this variable in program memory
rather than in RAM, thus saving space in RAM. If special functions used, then constants can be also
stored in EEPROM memory.
Few words for the numeric constants. Numeric constants can be declared in many ways indicating
their base.
Decimal integer constants (base 10) consist of one or more digits, 0 through 9, where 0
cannot be used as the first digit.
Binary constants (base 2) begin with a 0b or 0B prefix, followed by one or more binary digits
(0, 1).
Octal constants (base 8) consist of one or more digits 0 through 7, where the first digit is 0.
an optional sign - or +;
a period;
o
more digits
For example, the following floating-point constant contains both the optional and required
parts: +1.15e-12.
Character constant can be represented as character in quotation marks like 't' or as character code
like '\x74'. Backslash may confuse compiler. Lets say you want to assign backslash as character then
you should write like this '\\'.
{
int i;
/* Define a global variable i */
i = 1;
/* Assign i the value 0 */
{
/* Begin new block of sentences */
int i;
/* Define a local variable i */
i = 2;
/* Set its value to 2 */
}
/* Close block */
/* Here i is again 1 from the outer block */
}
Enumeration of variables
(by : http://winavr.scienceprog.com/short-introduction-to-c/enumeration-ofvariables.html)
This is similar to #define preprocessor where you can describe a set of constatnts. Using
preprocessor we use:
#define zero 0
#define one 1
#define two 3
But there is an alternative of using enumerating using keyword enum:
enum (zero=0,one, two); //zero=0, one=1; two=2
By default enumeration assigns values from 0 and up.
Now you can use enumeration like in following example:
int n;
enum (zero=0,one, two);
n=one; //n=1
You can use enum like this:
enum escapes { BELL = '\a', BACKSPACE = '\b', HTAB = '\t',
RETURN = '\r', NEWLINE = '\n', VTAB = '\v' };
or
enum boolean { FALSE = 0, TRUE };
An advantage of enum over #define is that it has scope This means that the variable (just like any
other) is only visible within the block it was declared within.
Example:
main()
{
enum months {Jan=1, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec};
/*
A
A
|
|
|
|
|
------- list of aliases.
-------------- Enumeration tag.
*/
enum months month;
printf("%d\n", month=Sep);
/* Assign integer value via an alias
* This will return a 9 */
}