x
Chess - Play & Learn

Chess.com

FREE - In Google Play

FREE - in Win Phone Store

VIEW

C integer tutorial

10binary
Sep 21, 2010, 9:35 AM 0

Chandler's C language integer tutorial.

I am a hobbyist programmer. I know a few languages but I like
C because I use Linux and it's usually available.

However, I call myself a hobbyist because I don't plan on making
money from it or using any product that costs money to play around.

My main experiments involve integer sequences. If you don't know
what an integer, programming language, or computer are,
You aren't ready for this tutorial. Otherwise, you'll learn something
about the C programming language.

The purpose of this is not to teach you the whole language but
just enough to get an idea of how computers work and play with numbers.

You'll need a C compiler to use these examples. If you can manage to
download and install one or use an operating system that already has
gcc, you're geeky enough to understand this.

In my examples, I use the command
"gcc -ansi -Wall -Wextra -pedantic -s 0.c -o 0"
to compile my file named "0.c".
Then I run the program with "./0".
However, in most cases, it's no different than
"gcc 0.c -o 0 && ./0"
I just like to compile my examples with all warnings turned on
so that I can make sure it will work in any C compiler.

Now that I have the boring stuff out of the way, I'll show you some
examples which you can compile and run.

#include
int main()
{
 printf("%d is an integer
",2);
 return 0;
}

The first line includes the "stdio.h" file. This must be in every
program or you won't be able to see any results. It contains the
"printf" function used later as well as many others.

Next, line defines the main function. This is where all the stuff
happens. At first you're probably wondering what a function is, I'll
get to that in a future example. The third and sixth lines
contain the "{" and "}" characters. Anything between them is part of
the function.

The fifth line ends the program, it returns the number 0 to
the operating system. This isn't important for you now but remember
that you must return from the program for it to end.

You'll see that it doesn't really do anything useful. It's
my version of the "Hello World" program that is just as useless.
Actually though, it's not entirely useless if you consider that
it proves your compiler is working fine!

Variables

Now it's time to learn what a variable is. In this case, it's a number
that changes. In this example, you'll see a program that counts to 15.

#include
int main()
{
 int a,b;
 a=0;
 b=16;
 while(a
 {
  printf("%d
",a);
  a=a+1;
 }
 return 0;
}



The line "int a,b;" says that we are making two variables named
"a" and "b". The next line "a=0" means that "a" will be the same as the
number zero in the following lines. Next line sets "b" to sixteen
in the same way.

What you see next "while(a
do something "while" "a" is less than "b". Since 0 is less than
16 in this case, the code between the braces will happen.

Inside, you'll see a printf statement that shows "a". Next, a
command to add 1 to "a".

I hope I haven't lost you so far. If you can understand this then
you have a pretty good idea the kind of things that are possible
in computer programming. If not, please tell me how I can help
explain it.

Certain symbols mean different things. For a detailed explaination
of operators, you can search online or preferably, get a book
that will fill you in on the details. In future examples, I'll use
all the operators that I use frequently.

Experiment with that last example and try setting the variables to
different things. You can make it count extremely high just by changing
"b". What if you added a different number to "a"?

When you've played around a bit, you might want to try the next example.

#include
int main()
{
 int a,b;
 a=2;
 b=4;

 if(a!=b)
 {
  printf("%d is not equal to %d
",a,b);
 }

 if(a
 {
  printf("%d is less than %d
",a,b);
 }
 else if(a>b)
 {
  printf("%d is more than %d
",a,b);
 }
 else if(a==b)
 {
  printf("%d is equal to %d
",a,b);
 }

 return 0;
}

It will tell you "if" the two numbers are equal or not and also
tell you which is more or less if they are not equal.

Another example will show you how to do arithmetic on any two
numbers.

#include
int main()
{
 int a,b;
 a=12;
 b=4;
 printf("the sum of %d plus %d is %d
",a,b,a+b);
 printf("the difference of %d minus %d is %d
",a,b,a-b);
 printf("the product of %d times %d is %d
",a,b,a*b);
 printf("the quotient of %d divided by %d is %d
",a,b,a/b);
 printf("the remainder of %d divided by %d is %d
",a,b,a%b);
 return 0;
}

Most of the time, you could use C or any other programming language
as a calculator in this way. However, C does have limits on how high
integers can go, but most of the numbers we deal with are far less than
the limit. I'd explain more about that but you'd have to know about
machine hardware and the binary numeral system to understand.

    functions

A function is something most programming languages have.
In fact, everything a program does is a function but when a C
programmer talks about a function, they mean a block of code
that can be written once and then run anywhere else in the program.
Sometimes the purpose is to return an integer, sometimes the purpose
is more complicated like graphics,sound, or opening files.

You can make your own functions. You can name them and define what they
do. Most of the time you won't need to because the C standard library
contains many functions that do different things. The most common
example is printf. There are plenty of functions but this tutorial
is meant only to teach how to play around with integers.

So, I will show you one of the funniest(and most useless functions)

#include

int fun()
{
 return 2;
}

int main()
{
 int a;
 a=fun();
 printf("the return value is %d
",a);
 return 0;
}

The function will always return 2 in this case.
However, you can make functions which accept something
called arguments.

#include

int fun(int a)
{
 a++;
 return a;
}

int main()
{
 int b;
 b=fun(5);
 printf("the return value is %d
",b);
 return 0;
}

In the above, we call fun() with 1 argument which is the integer 5.
Then, it becomes the a variable automatically. Then, we add 1 to a
using C's double plus "++" operator. Then we return "a" from
the function. In the main function, we set "b" to the return from fun().

Often, you'll like to use the "++" operator to add 1 to something.
The "--" operator subtracts 1. This was really more of a convenient
thing in the C language because people can get tired of typing "c=c+1"
when they can do "c++".

In fact, these two operations are the foundation of all math.
Think about it, counting is what people learn first. Addition is just
repeatedly adding 1, multiplication is repeateded addition, and
exponentiation is repeated multiplication!

I'll show you a very slow way to add two numbers using "++".

#include

int add(int a,int b)
{
 while(b!=0)
 {
  a++;
  b--;
 }
 return a;
}

int main()
{
 printf("the return value is %d",add(64,32));
 return 0;
}

This means that somebody could write any program using this function
and never use a plus sign again. The reason they don't is because
calling a function is slightly slower and takes more code than "64+32"
because it uses the much faster hardware addition.

Old CPUs did not have multiplication or division built in to them. One
example was the 6502 with which the Nintendo entertainment system was
made. However, anyone can write their own division function! It's not
recommended but below I'll show you how!

#include

int div(int a,int divisor)
{
 int b,quotient;
 quotient=0;
 while(a!=0)
 {
  b=divisor;
  while(b!=0)
  {
   a--;
   b--;
   if (a==0){break;}
  }
  if(b==0){quotient++;}
 }
 return quotient;
}

int main()
{
 printf("the return value is %d
",div(128,3));
 return 0;
}

You may not understand it but that's ok. You'll never need to!
Computers these days can do division much faster than my method
which is repeated subtraction.

Online Now