Programming, Music, Games. Creation.

Archive for November, 2012

C Language Operators

What are operators? 
Programming languages typically support a set of operators: operations which differ in the calling of syntax and/or the argument passing mode from the language’s functions. Common examples that differ by syntax are mathematical arithmetic operations, e.g. “>” for “greater than”, with names often outside the language’s set of identifiers for functions, and called with a syntax different from the language’s syntax for calling functions. Common examples that differ by argument passing mode are boolean operations, e.g. a short-circuiting conjunction that only evaluates later arguments if earlier ones are not false, in a language with strict call-by-value functions.
Wikipedia

.

.

Learning C with Bermuda

.

1. Assignment Operator(=) and Arithmetic Operators(+, -, *, /, %)

Operator Function Direction of Function
= Substitute the value on the right to the left
Ex) a=42
+ Add Left value to the right value
Ex) a=45+6
Subtract the value on the right from the left
Ex) a=5-4
* Multiply the values
Ex) a=4*3
/ Divide the value on the left with the right
Ex) a=2/6
% Divide the value on the left and show the remainder
Ex) a=2%6

If you doubt what these are (except %), please consider going over your Primary 1 books.

.

.

.

2. MORE Assignment Operators

a = a + b a += b
a = a – b a -= b
a = a * b a *= b
a = a / b a /= b
a = a % b a %= b

The functions on the left is same as the functions on the right :)
Don’t get confused with += and =+!!

.

.

.

3. MORE Arithmetic Operators

Operator Function Direction of Function
++a Add 1 and carry out further functions
Ex) a=1
printf(“a is %d”, ++a);[ a is 2 ], a=2
a++ Carry out the function and add 1
Ex) a=1
printf(“a is %d”, a++);[ a is 1 ], a=2
–a Subtract 1 and carry out further functions
Ex) a=1
printf(“a is %d”, –a);[ a is 0 ], a=0
a– Carry out the function and subtract 1
Ex) a=1
printf(“a is %d”, a–);[ a is 1 ], a=0

.

.

.

4. Relational Operators

Operator Function Direction of Function
< a<b
Is a smaller than b?
> a>b
Is a greater than b?
== a==b
Does a equal to b?
!= a!=b
Does a not equal to b?
<= a<=b
Is a smaller or equal to b?
>= a>=b
Is a greater or equal to b?

They always give a boolen value :)
therefore..

int val1 = 12;
int val2 = 10;

int result1 = (val1==val2);

printf(“Result : %d”, result1);

wil give

Result : 0

since val1 does not equal to val2.
(1=true, 0=false)

.

.

.

5. Logical Operators

Operator Function Direction of Function
&& If both are true, value is true (and)
Ex) a&&b
|| If both any one is true, value is true (or)
Ex) a || b
! If it’s true, value is false
If it’s false, value is true
Ex) !a

Once again, boolen values :)

.

.

.

6. Comma(,) Operator

This operator is used in many ways. it is used when declaring multiple variables, calculating two functions in a single line and much more.

A good example is shown as below

#include <studio.h>

int main(void)
{
int a=4, b=2;//comma here
printf(“Hello “), printf(“World!\n”);//comma

printf(“%d”, a), printf(“%d”, b);
return 0;
}

This will give..

Hello World!
42

:D


Two Things to be Aware when you Declare a Variable

1. INTRODUCE FIRST

int main(void)
{
   int a;//declare
   int b;//declare

   a=5;//input
   b=1;//input

printf(“%d + %d = %d\n”, a, b, a+b);
return 0;
}

CORRECT :)

int main(void)
{
int a;//declare
   a=5;//input
   int b;//declare
   b=1;//input

printf(“%d + %d = %d\n”, a, b, a+b);
return 0;
}

WRONG :(

————————————————–

2. NAME IT PROPERLY

3 SIMPLE things! :D

1. Variables consist of Alphabets, Numbers and Underscore(_)

2. C Language differentiates  small letters and CAPITAL LETTERS :D
Therefore val and Val are different variables :)

3. Variable CANNOT start with a Numb3r or be a Key Word! It should also not contain any Space or Special Letters