Monday, February 23, 2009

Placeholders

Placeholders are used to determine when what type of values are going to be input or displayed, depending on what type of functions that you use

Input Placeholders
scanf() function requires input placeholders to allow data to be transferred to the specific variable depending on which placeholder you will use.

Data type..............Placeholder
int.......................... %d
char....................... %c
double................... %lf
float....................... %f
string.................... %s

Note: for data that contains spaces in character arrays(strings) we use the %[^\n] symbol instead of %s because %s will only store strings without spaces.


Output Placeholders
Output placeholders are used to display data onto the screen. printf() function uses the output placeholder when necessary. The only difference with the input placeholders is the double data type placeholder for input function is %lf, while the double data type placeholder for output function is %f.

Data type..............Placeholder
int.......................... %d
char....................... %c
double................... %f
float....................... %f
string.................... %s (can also be used for strings with spaces)

Examples:

#include< stdio.h>
int main()
{
int number1 = 10;
int number2 = 20;
int number3 = 30;

printf("number1 contains %d \n", number1);
printf("number2 contains %d \n", number2);
printf("number3 contains %d \n", number3);

return 0;
}

Output:
number1 contains 10
number2 contains 20
number3 contains 30

#include< stdio.h>
int main()
{
int number1 = 10;
double number2 = 20.532;
double number3 = 30.88812;

printf("number1 contains %d \n", number1);
printf("number2 contains %f \n", number2);
printf("number3 contains %f \n", number3);

return 0;
}

Output:
number1 contains 10
number2 contains 20.532000
number3 contains 30.888120

#include< stdio.h>
int main()
{

char letter = 'A';
char letter2 = 'B';
char letters[] = "ABC";

printf("first letter = %c\n", letter);//character
printf("second letter = %c\n", letter2);//character
printf("letters contain %s", letters);//string
return 0;
}

Output:
first letter = A
second letter = B
letters contain ABC
Examples of inputs that uses placeholders:

#include< stdio.h>
int main()
{
int integernumber;

printf("Integernumber = ");
scanf("%d", &integernumber);
printf("\n variable integer contains: %d", integernumber);
}

Output:
Integernumber = 200
variable integer contains: 200


#include< stdio.h>
int main()
{
double lengthOfWall;

printf("Enter length of wall:");
//note the difference btw %lf for scanf
//and printf, %f for double
scanf("%lf", &lengthOfWall);
printf("\n lengthOfWall is %f", lengthOfWall);
}

Output:
Enter length of wall:35.886
lengthOfWall is 35.886000


#include< stdio.h>
int main()
{
char letter;

printf("enter a letter: ");
scanf("%c", &letter);
printf("\n letter is %c", letter);
}

Output:
enter a letter: E
letter is E

#include< stdio.h>
int main()
{
char fullname[21];

printf("enter full name: ");
//note: %s not used because fullname might
//be of a name with spaces.%[^\n] is used instead.
scanf("%[^\n]", fullname);

//for display, always use %s for string
//eventhough there are spaces
printf("welcome %s", fullname);
}


Output:
enter full name: Kelvin Chan
welcome Kelvin Chan

Note: that the ampersand symbol , & is required in front of the variable name in the input list of scanf() function, on int, double, float and char data types. Strings does not require &.
Escape Sequence/Characters
These characters starts with a frontslash / and the character. These escape characters have special functions, such as \n is to break the current line to form a new line and so on.

The table bellow are some escape characters:

Mathematical Operators and expressions
Mathematical operators can be used for calculation purposes and logical purposes.

Mathematical Operators
These operators compute different types of calculation based on the operators.
These are the basic mathematical operators:
+ (used for addition)
- (used for subtraction)
* (used for multiplication)
/ (used for division)
% is the modulus/modulo operator. This is used to get the remainder of a calculation

Unary expressions
Unary expressions consist of 1 operator and 1 operand. Operands are the numbers or the variables or both.
Example of unary expressions are:
-5, +5.

Binary expressions
Binary expressions consits of 2 operands and 1 operator and commonly seen then the following form:
[operand][operator][operand]
Example:
5 + 2
8*a
a/b

To store a computed value from a binary expression into a variable, the assignment operator is used, denoted as =.This is different from mathematics where = is equal but almost similar.
a= a+1 is not valid in mathematics because a does not equal a+1. In C language or most languages, a = a+1 is perfectly fine as this tells that the value of a plus 1 is then stored into the variable a.
Examples:
a= b*c;
b=(5*2)+c;

NOTE: The assignment operator can only be used to assign values to a variable, where the left hand side is always the variable. Any other expressions such as:
b+c = a
2+3 = a
are invalid in programming languages.

There are 3 forms of assignments: simple assignments, compound assignments and multiple assignments.

  • Simple Assignments
These are normal and commonly used assignments. Examples:
b= 4+a;
a= a*b;

  • Similarly, some simple assignments can be re-written in another form called compound assignments.
Example:
a=a*b can be rewritten to a *= b
a = a/b can be rewritten to a /=b
a = a+b can be rewritten to a +=b
a = a -b can be rewritten to a -=b
a = a%b can be rewritten to a %=b

Therefore, we can conclude that compound assignments can be used when the first operand is the same as the variable is assigned to it.

  • Multiple assignments
This is used to assign the same value to more than 1 different variable. Example
a = b = 1+2
The expression above can be separated as below:
b= 1+2
a=b
This tells us that the value of 1+2 is stored/assigned into b and the value of b is assigned to variable a.

And another note, all expressions and statements are to end with a terminated colon unless specified otherwise.
Postfix and prefix expressions

Postfix expressions
Postfix expressions can be recognized by the following examples.
a++;
i--;
Note that there is no other postfix/prefix expressions such as a// and i**. The symbol ++ means plus one.
However, the variable is first used and is later added by 1 and stored into memory. Thus the word postfix. This can be seen much more clearer in the following animation




Hover on the C statements to understand better.

Prefix expressions
Prefix expressions, as its name suggests, that the value of the variable is added by 1 first and stored in the memory which will be used later. Examples:
++a;
++i;
This is also the same as a = a+1 and i= i+1;

Sunday, February 22, 2009

Logical Operators and Relational Operators


Logical Operators
Logical operators are usually used to convey whether a given condition is true or false.
Since C language does not have data type to convey true and false, C uses a non-zero number as true while false is represented as zero, 0.

There are 3 logical operators: NOT, AND , OR.

The NOT logical operator is denoted as !
The AND logical operator is denoted as &&
The OR logical operator is denoted as ||

The following truth tables clearly explain the usage of the 3 logical operators.
NOT (!)

AND(&&)
As you can see, if either 1 of the condition is false, the whole condition is false.
OR(||)
As you can see, if either 1 of the condition is true the whole condition is true.