Advertising (This ad goes away for registered users. You can Login or Register)

[Tutorial] Introduction to programming using C (V)

Discuss about your favorite (gaming...or not) devices here. The most popular ones will end up getting their own categories
Programming discussions for your favorite Device
Forum rules
Forum rule Nº 15 is strictly enforced in this subforum.
User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

[Tutorial] Introduction to programming using C (V)

Post by m0skit0 » Fri May 20, 2011 11:09 pm

Back to index
<< Prev Next >>

Variables

Variables are storage for data. In C, all variables have a data type, which determines their size and their default behaviour. Since we already saw data types on the previous part, let's see what variables offer to us.

(I suggest copy-pasting the code into an editor)

Code: Select all

#include <stdio.h>

// Note the use of "void"
// This means "empty" (or "not defined" on other C contexts)
int main(void)
{
   // Declaring 3 variables: a, b and c
   // All are signed integers
   // ALWAYS ASSIGN INITIAL VALUES FOR YOUR VARIABLES!!
   // = is the assignment operator, it gives values for your variables
   int a = 3;
   int b = 2;
   // Value stored in c is b value + a value
   int c = a + b;   
   printf("c = b + a\t%d + %d = %d\n", a, b, c);
   printf("\n");
   
   // c is assigned another value. Previous value is lost
   c = a - b;
   printf("c = b - c\t%d - %d = %d\n", a, b, c);
   printf("\n");
   
   // Multiplication
   c = a * b;
   printf("c = b * c\t%d x %d = %d\n", a, b, c);
   printf("\n");
   
   c = a / b;
   printf("c = b / c\t%d / %d = %d\n", a, b, c);
   printf("\n");
   
   // a + b is computed first, then the value is stored in a
   a = a + b;   
   printf("a = a + b\ta = %d, b = %d\n", a, b);
   printf("\n");
   
   // This is the same as the previous one
   a += b;
   printf("a += b\ta = %d, b = %d\n", a, b);
   printf("\n");
   
   // c is assigned value stored on a, then a is incremented by one
   c = a++;
   printf("c = a++\tc = %d, a = %d\n", c, a);
   printf("\n");
   
   // a is incremented by one, then its value is assigned to c
   c = ++a;
   printf("c = ++a\tc = %d, a = %d\n", c, a);
   printf("\n");
   
   // Note operator precedence (which operators are computed first)
   c = a++ * b + c;
   printf("c = a++ * b + c\ta = %d, b = %d, c = %d\n", a, b, c);
   printf("\n");
   
   // Division remainder or module (this arithmetic operator is widely used, learn it well)
   c = a % b;
   printf("c = a mod b\ta = %d, b = %d, c = %d\n", a, b, c);
   printf("\n");
   
   // Bit right shift
   b = a >> 3;
   printf("b = a >> 3\ta = %d, b = %d, c = %d\n", a, b, c);
   printf("b = a >> 3\ta = %08X, b = %08X, c = %08X\n", a, b, c);
   printf("\n");
   
   // Bit left shift
   c = b << a;
   printf("c = b << a\ta = %d, b = %d, c = %d\n", a, b, c);
   printf("c = b << a\ta = %08X, b = %08X, c = %08X\n", a, b, c);
   printf("\n");
   
   // Binary NOT
   c = ~a;   
   printf("c = ~a\ta = %d, b = %d, c = %d\n", a, b, c);
   printf("c = ~a\ta = %08X, b = %08X, c = %08X\n", a, b, c);
   
   // Binary AND
   c = a & b;   
   printf("c = a & b\ta = %d, b = %d, c = %d\n", a, b, c);
   printf("c = a & b\ta = %08X, b = %08X, c = %08X\n", a, b, c);
   printf("\n");
   
   // Binary OR
   c = a | b;   
   printf("c = a | b\ta = %d, b = %d, c = %d\n", a, b, c);
   printf("c = a | b\ta = %08X, b = %08X, c = %08X\n", a, b, c);
   printf("\n");
   
   // Binary XOR
   c = a ^ b;
   printf("c = a ^ b\ta = %d, b = %d, c = %d\n", a, b, c);
   printf("c = a ^ b\ta = %08X, b = %08X, c = %08X\n", a, b, c);
   printf("\n");   
   
   return 0;
}
Please note the "ALWAYS ASSIGN INITIAL VALUES FOR YOUR VARIABLES!!" comment. This is because C compilers do not intialize your variables, so it can have any random value before you store something on it. Never use unassigned variables in operations (except assigning them a value, of course :P).

As an additional exercise you might want to guess the values that will be displayed on the screen before running the program.

See on the next part, until then have fun!

<< Prev Next >>
Advertising
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

irfanhb7
Posts: 466
Joined: Wed Jan 26, 2011 2:46 pm
Location: In your nightmares

Re: [Tutorial] Introduction to programming using C (V)

Post by irfanhb7 » Sat May 21, 2011 6:19 am

Keep up with the tutorials.
Advertising
Languages I know : C , C++ & Java
Image
Boy : There is something wrong with my phone.
Girl : What ?
Boy: It don't have your Phone Number.
Image

Halvhjearne
Posts: 664
Joined: Mon Mar 14, 2011 1:58 am
Location: Denmark

Re: [Tutorial] Introduction to programming using C (V)

Post by Halvhjearne » Sat May 21, 2011 12:06 pm

ok i wont just "reveal" what this is, but i do have a question tho ... the compiler comes up with some errors (alltho i dont see any errors in the code), but am i right when thinking this is becorse you gave it more than one value, but only stores the one of them?
Dr. Evil wrote:I used to use Windows, but it was designed by freakin' idiots.
Now i use linux allowing me to conrol the "lasers" on my "death star" with ease.
I'm Dr. Evil, and I'm aspiring to take over the world.

User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: [Tutorial] Introduction to programming using C (V)

Post by m0skit0 » Sat May 21, 2011 12:36 pm

The code was definitely wrong. They're warnings, but you should always assure your code doesn't have any warnings. Let's take this chance to explain what the errors were:

Code: Select all

m0skit0@soviet:~/Temp$ cc -Wall -o foo foo.c 
foo.c: In function ‘main’:
foo.c:34:8: warning: too few arguments for format
foo.c:39:8: warning: too few arguments for format
foo.c:59:8: warning: unknown conversion type character ‘b’ in format
As you can see they're warnings. Let's check them one by one.

Code: Select all

foo.c: In function ‘main’:
This helps us know in which function the errors are located.

Code: Select all

foo.c:34:8: warning: too few arguments for format
This was the old line of code:

Code: Select all

printf("a = a + b\ta = %d, b = %d\n", a);
Obviously, as the compiler states: too few arguments for format. We have two %d arguments on printf format string, but only passed one value (a). The fix is:

Code: Select all

printf("a = a + b\ta = %d, b = %d\n", a, b);
Same goes for next warning:

Code: Select all

foo.c:39:8: warning: too few arguments for format

Code: Select all

printf("a += b\ta = %d, b = %d\n", a);

Code: Select all

printf("a += b\ta = %d, b = %d\n", a, b);
Last error might be a bit more cryptic and harder to spot.

Code: Select all

foo.c:59:8: warning: unknown conversion type character ‘b’ in format

Code: Select all

printf("c = a % b\ta = %d, b = %d, c = %d\n", a, b, c);
Notice that for printf the % character has a special meaning. What the compiler is telling us is that this "c = a % b\ta = %d, b = %d, c = %d\n" conversion character is not recognized. So I'll just change it with "mod" (for module, also used on other programming languages such as one of C's daddies, Pascal).

Code: Select all

printf("c = a mod b\ta = %d, b = %d, c = %d\n", a, b, c);
And sorry for the errors :oops:
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

User avatar
ChemDog
Posts: 139
Joined: Wed Dec 14, 2011 11:39 pm

Re: [Tutorial] Introduction to programming using C (V)

Post by ChemDog » Tue May 29, 2012 3:18 am

m0skit0 wrote: // Bit right shift
b = a >> 3;
printf("b = a >> 3\ta = %d, b = %d, c = %d\n", a, b, c);
printf("b = a >> 3\ta = %08X, b = %08X, c = %08X\n", a, b, c);
printf("\n");

// Bit left shift
c = b << a;
printf("c = b << a\ta = %d, b = %d, c = %d\n", a, b, c);
printf("c = b << a\ta = %08X, b = %08X, c = %08X\n", a, b, c);
printf("\n");

// Binary NOT
c = ~a;
printf("c = ~a\ta = %d, b = %d, c = %d\n", a, b, c);
printf("c = ~a\ta = %08X, b = %08X, c = %08X\n", a, b, c);

// Binary AND
c = a & b;
printf("c = a & b\ta = %d, b = %d, c = %d\n", a, b, c);
printf("c = a & b\ta = %08X, b = %08X, c = %08X\n", a, b, c);
printf("\n");

// Binary OR
c = a | b;
printf("c = a | b\ta = %d, b = %d, c = %d\n", a, b, c);
printf("c = a | b\ta = %08X, b = %08X, c = %08X\n", a, b, c);
printf("\n");

// Binary XOR
c = a ^ b;
printf("c = a ^ b\ta = %d, b = %d, c = %d\n", a, b, c);
printf("c = a ^ b\ta = %08X, b = %08X, c = %08X\n", a, b, c);
printf("\n");
I have never seen equations like this, what exactly is happening when you run them? I plugged in a couple different numbers to try and see what was happening, but I wasn't really understanding how it got to the answer.

User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: [Tutorial] Introduction to programming using C (V)

Post by m0skit0 » Tue May 29, 2012 7:50 am

Those are not "equations" properly speaking, they're binary operations. All these binary operations are native to CPU, and thus they are one of the fastest operations available.

If you're not familiar with numeral systems, I suggest you do so because they're very important on programming (specially the positional ones).

I'll explain a bit anyway: we humans (nowadays) use the positional decimal numeral system. That is, we use a base 10 numeral system, where position matters, that is the value of a number depends on its position. Taking 1 and 2, 12 is not the same as 21, even if you're using the same numbers on both values. Why this? Because its position has a "weight". Usually you express this as powers, so for example 12 = 2 x 10^0 + 1 x 10^1, because 2 is in the position 0, and 1 is in the position 1. We use 10 as base because as we said our numbering system is based on 10 (and thus has 10 symbols: 0 - 9).

On the other hand, computers use position binary numeral system, which means they use base 2 and not base 10. This means they only have 2 symbols: 0 and 1. So for example 12 in binary would be 0 x 2^0 + 0 x 2^1 + 1 x 2^2 + 1 x 2^3 = 1100. Keep in mind that both representation are actually the same value. They're not different numbers, it's only the representation that changes. This concept is very important, and a lot people new to programming do not understand this.

Hexadecimal is another widely used numeral system in computer engineering because it relates very closely and easily to binary system. It mainly allows to represent binary values in a much more compact way. I suggest you getting a good grasp of it as well. But still keep in mind it's only a representation: the value is the same, no matter the numeral system used.

Hope this helps.
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

User avatar
ChemDog
Posts: 139
Joined: Wed Dec 14, 2011 11:39 pm

Re: [Tutorial] Introduction to programming using C (V)

Post by ChemDog » Wed May 30, 2012 6:29 am

This helps alot, thank you very much! Your explanation was wonderful, and those links have all the information I need to know. Also, these tutorials are great, thanks for spreading your knowledge :)

User avatar
N6v7d8
Posts: 31
Joined: Fri May 25, 2012 6:01 am

Re: [Tutorial] Introduction to programming using C (V)

Post by N6v7d8 » Sun Jul 08, 2012 4:14 am

TS Teacher I have a question :> about this line


// a + b is computed first, then the value is stored in a
a = a + b;
printf("a = a + b\ta = %d, b = %d\n", a, b);
printf("\n");


a = 3;
b = 2;
c = a + b;

they became 5

but I don't get why there is another a?

I mean this "a = a + b" <<<< will the 'a' above also lost like what happen to c after adding a + b?

I'm confuse sorry xD

User avatar
Xian Nox
Retired Mod
Posts: 2749
Joined: Fri Nov 05, 2010 5:27 pm
Location: Over the hills and far away

Re: [Tutorial] Introduction to programming using C (V)

Post by Xian Nox » Sun Jul 08, 2012 4:46 am

N6v7d8 wrote: // a + b is computed first, then the value is stored in a
a = a + b;
printf("a = a + b\ta = %d, b = %d\n", a, b);
printf("\n");


a = 3;
b = 2;
c = a + b;

they became 5

but I don't get why there is another a?

I mean this "a = a + b" <<<< will the 'a' above also lost like what happen to c after adding a + b?

I'm confuse sorry xD
These two do the same thing:

Code: Select all

a = 3;
b = 2;
a = a + b;    // a is 5 now

Code: Select all

a = 3;
b = 2;
c = a + b;    // c is 5 now
a = c;    // a is 5 now
The a is there to demonstrate that first the value on the right is computed, then it is assigned to the variable on the left. And yes, the value is overwritten, so the previous value stored in a is lost.

User avatar
N6v7d8
Posts: 31
Joined: Fri May 25, 2012 6:01 am

Re: [Tutorial] Introduction to programming using C (V)

Post by N6v7d8 » Sun Jul 08, 2012 5:41 am

Xian Nox wrote:These two do the same thing:

Code: Select all

a = 3;
b = 2;
a = a + b;    // a is 5 now

Code: Select all

a = 3;
b = 2;
c = a + b;    // c is 5 now
a = c;    // a is 5 now
The a is there to demonstrate that first the value on the right is computed, then it is assigned to the variable on the left. And yes, the value is overwritten, so the previous value stored in a is lost.
Now I get it xD Thanks again ^_^

Image

Post Reply

Return to “Programming and Security”