Thursday, September 18, 2008

Learnigns of the Week (ROLLORATA)

VARIABLES, CONSTANTS, OPERATORS AND EXPRESSION

- Identifiers are composed of a sequence of letters. Digits, and the special character _ (underscore).
- Avoid using names that are too short or too long.
- Limit the identifiers from 8 to 15 characters only.

* Variables are identifiers that can store a changeable value. These can be different data types.


Rules for defining or naming identifiers
- It must consist only of letters, digits, and underscore.
- Example: _duh, num_1 (correct)
- It should not begin with a digit.
- Example: 1name, 3to3 (incorrect)
- An identifier defined in the C standard library should not be redefined.
- Example: printf, scanf (incorrect)
- It is case sensitive; meaning uppercase is not equal to the lowercase.
- Example: ans != Ans != aNs
- Do not include embedded blanks.
- Example: large num (incorrect)
- Do not use any of the C language keywords as your variable/ identifier.
- Do not call your variable / identifier by the same name as other functions.

Variable Declaration
- All variables must be declared before they may be used. The general form of declaration is shown here:
Type variable list;
Example: int i,j, k;
short i,j,k;
- Note: Before declaring variables, specify first the data type of the variable/s.
- Variables must be separated by comma.
- All declarations must be terminated by a semicolon (;).

Two kinds of variables
- Local Variables - Variables that are declared inside a function are called local variables. It can only be referenced by statements that are inside the block in which the variables are declared.

- Global Variables - Global variables are known throughout the entire program and may be used by any piece of code. Global variables are created by declaring them outside of any function.


- Constants are identifier / variables that can store a value that cannot be changed during program execution.
- Example: const int count = 100;
- Where integer count has a fixed value of 100.

Arithmetic, Logical, Relational, and Bitwise Operations
- Operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations.
- There are three classes of operators in C: arithmetic, logical and relational, and bitwise.

Arithmetic Operators
Operator Action
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus Divisor
-- Decrement a value
++ Increment a value

Relational and Logical Operators
- In the term relational operator, the word relational refers to the relationship values can have with one another.
- In the term logical operator, the word logical refers to the ways these relationships can be connected together using the rules of formal logic.

Relational Operators

Operators Action
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
== equal
!= Not equal

Logical Operators

Operators Action Truth Table
&& AND true && true = true
true && false = false
false && true = false
false && false = false

OR true true = true
true false = true
false true = true
false false = false

! NOT !true = false
!false = true

Bitwise Operator
- Bitwise operators are the testing, setting or shifting of the actual bits in a byte or a word, which corresponds to C’s standard char and int data types and variants.
- Bitwise operators cannot by used on type float, double, long double, void or other more complex types.

The ? Operator
? Operator is a very powerful and convenient operator that can be used to replace certain statements of the if-then-else form.
- Example: y= x > 9 ? 100: 200
is equivalent to
If (x>9)
y=100;
Else
Y=200;

Evaluation of Expression
- Expression refers to anything that evaluates to a numeric value.

Order of Precedence
- Order of Precedence
()
!, unary +, -
*. /. %
binary + , -
<, <=, >, >=
==, !=
&&


STRUCTURE OF A SIMPLE C PROGRAM

#include
#define directive
main()
{
variable declaration section;
______________________
______________________
}

- #include directive – contains information needed by the program to ensure the correct operation of C’s Standard library functions.
- #define directive – used to shorten the keywords in the program.
- Variable declaration section – it is the place where you declare your variables.
- Body of the program – start by typing main() and the { and }. All statements should be written inside the braces.

C is a case sensitive program, therefore use lowercase letters only.

Commonly used include files in C language.
- alloc.h – declares memory management functions.
- conio.h – declares various functions used in calling IBM-PC ROM BIOS.
- ctype.h – contains information used by the calssification and character convertion macros.
- math.h – declares prototype for the math functions.
- stdio.h – defines types and macros needed for standard I/O.
- string.h – declares several string manipulation and memory manipulation routines.

Important Symbols
- \n – is a line char used to move the cursor to the next line
- ‘ ‘ – single quote is used for single character / letter.
- “ “ – double quote is used for two or more character
- { - open curly brace signifies begin
- } – close curly brace signifies end
- & - address of operator
- * - indirection operator / pointer

No comments: