Programming, Music, Games. Creation.

▶ Learning C

How does the computer save Real Numbers?

How does the computer save real numbers?

Lets look at only 2 bytes. (in reality, the computer uses more than 4 bytes to save real numbers.)


The computer uses the following equation to save real numbers

Why use this equation?

setting ‘m’ as numbers before ‘.’ and setting ‘e’ as numbers after ‘.’ is inefficient and takes a lot of memory to save real numbers.
However, using the equation, the computer is able to save real numbers efficiently, no matter how complicated they are.

Still, the computer cannot save wide range of real numbers and cannot save them accurately. It simply saves the data closest possible to the value we want to save. This is called float-point error.

Float-point error

#include <studio.h>

int main(void)
{
int i;
float f=0.0;

for(i=0; i<100; i++)
f+=0.1

printf(“%f “, f);

return 0;
}

the following is a good example of float error.

Variable f is declared,
until ‘i’ becomes 100, 0.1 is added to ‘f’
since ‘i++’ is used, ‘f’ is added by 0.1 100 times

The output must be 10, however you will see slight difference in the value. For example,

10.00002

Advertisements

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


What is bit & byte ?

Bit is the smallest way to express data for computer using one 2binary(0 or 1).
Byte is formed when 8 bits join together.
… To make things simple, I drew a picture.
It expresses everything ;D


(more…)


What is C (Programming Language)?

Humans can’t speak the Machine Language which computer use.

Even if we try to learn it, it is hard to understand and hardly makes sense to us.

(more…)