Warning: Cannot modify header information - headers already sent by (output started at /mnt/112/sdb/a/6/biotrans/includes/fonctions.php:1) in /mnt/112/sdb/a/6/biotrans/includes/conf.php on line 165

Warning: Cannot modify header information - headers already sent by (output started at /mnt/112/sdb/a/6/biotrans/includes/fonctions.php:1) in /mnt/112/sdb/a/6/biotrans/includes/conf.php on line 166

Warning: Cannot modify header information - headers already sent by (output started at /mnt/112/sdb/a/6/biotrans/includes/fonctions.php:1) in /mnt/112/sdb/a/6/biotrans/includes/conf.php on line 167

Warning: Cannot modify header information - headers already sent by (output started at /mnt/112/sdb/a/6/biotrans/includes/fonctions.php:1) in /mnt/112/sdb/a/6/biotrans/includes/conf.php on line 177

Warning: Cannot modify header information - headers already sent by (output started at /mnt/112/sdb/a/6/biotrans/includes/fonctions.php:1) in /mnt/112/sdb/a/6/biotrans/includes/conf.php on line 178

Warning: Cannot modify header information - headers already sent by (output started at /mnt/112/sdb/a/6/biotrans/includes/fonctions.php:1) in /mnt/112/sdb/a/6/biotrans/includes/conf.php on line 179
Bio Trans

See another lesson

Retour au menu

Quiche Team

Lesson 2 - Variables


1) Definition
2)
Your first program with variable




1) Definition

        A variable is a box where you can store information: integer, real, string... But when you exit your program, this information is erased. If you want to keep information, you have to use another way. I will explain later because it is especially for high score and of course, we are not still here. :-( You must be calm and go step by step, the Eiffel Tower did not build in one day (this proverb does not exist but I am French. If you are not happy, it is the same! :-) ).
        To program, you need store information that you use at the beginning, at the end, in the middle or anywhere of your code. For example, if you want to display the score of player at the end of game, you have to count points.
        In C language, you have to declare your variables. The declaration is done in starting of your code but you cannot declare your variable anywhere in your code. When you declare a variable, you must give a name and the type of variable. The name is useful to reuse the stored information otherwise the variables are not useful. But you cannot name a variable with the same name of Tios function, which creates a mistake in compilation; for example you cannot use: clrscr, like a name of variable. When you declare a variable, you need tell the type to compiler because the compiler need know the size of memory for your variable. In C language and in all languages, you have several types of variable: integer, real, character,...
 

Type:

available values:
short int -32768 to 32767
long int -2147483648 to 2147483647
signed char -128 to 127 (in fact, each character is a number in the ascii code)
signed int -32768 to 32767 (signed is default)
[or -2147483648 to 2147483647 if '-mnoshort' is given]
signed short int -32768 to 32767
signed long int -2147483648 to 2147483647
unsigned char 0 to 255
unsigned int 0 to 65535
[or 0 to 4294967296 if '-mnoshort' is given]
unsigned short int 0 to 65535
unsigned long int 0 to 4294967296
long long int -4294967296 to 4294967295
unsigned long long int 0 to 8589934592

 
        In fact, the memory of type is calculated in byte which means the size of character is 2^8, the size of short integer is 2^16=65536, the size of long integer is 2^32=4294967296, the size of double-long integer is 2^33=8589934492. And a number use one of memory that is why unsigned short integer goes 0 to 65535 because the number 0 uses one digit. And also, the sign of number use one digit. For example, the memory of character sounds like ---- ---- in a box, you can put 0 or 1 and there are eight boxes so you have 2*2*2*2*2*2*2*2 = 2^8 = 256 possibilities.

float is a single precision floating point data type
double is a double precision floating point data type also 1e-999 to 9.999999999999999e999 in magnitude

        Another type of variable is boolean, yet in C this type does not exit. But there is a trick, we can use two constants: TRUE and FALSE. The value of TRUE is 1 and one of FALSE is 0 therefore the type of declaration is 'int'. Like this, we can create the boolean type. It is great, isn’t it? If you begin in programming, do not upset you because I explain later how it does work.
In theory, the syntax of variable declaration is: type name=value . The initialization is optional but you must be careful because you do not know the value of variable and later you must initialize the variable. When you exit your program, the value can modify and make sure you cannot use an uninitialized variable, the uninitialized value of variable is random.
        'name' is the name of the variable. 'value' is the value of the variable. I say the variable is initialized which means the value: 'value' is its first value. '=' is the acronym of assignment in C.
        I can say 'name' is lvalue and 'value' is rvalue. Rvalue means the side which is right to '=' and lvalue means the side which is left to '='.
 

2) Your first program with variable


                         


  // C Source File
  // Created 19/05/2002; 16:11:28

  #define USE_TI89              // Produce .89z File
  #define USE_TI92PLUS          // Produce .9xz File

  #define OPTIMIZE_ROM_CALLS    // Use ROM Call Optimization

  #define SAVE_SCREEN           // Save/Restore LCD Contents

  #include <tigcclib.h>         // Include All Header Files

  // Main Function
  void _main(void)
  {
  // different ways to declare and initialized or not variables
  // the type is integer  
    int a = 0;
    int i = 0, j = 0;
    int variable = 4;
    int bob, count = 17;

  /* 
  the type is positive short integer. If we forget 'unsigned',
  the compiler create a mistake because the number would be too great 
  */
    unsigned short int s = 64000, key;
  // the type is short integer and the type is signed by default    
    short int sign = -30000;
  // the type is positive long integer    
    unsigned long int l = 78000;
  // the type is a float or a real as you prefer  
    float real = 2108458745, r = 254.1317;


    clrscr(); // clear the screen but i think you know

  // see explications in the lesson 2 - variable
    printf ("Display:%d ,it's an integer\n", variable);
    printf ("one real: %E\nanother real: %f\n", real, r);

    ngetchx();

    clrscr();
    l = l - 32000;
    i++;
    j--;
    a += 2;
    bob = 2;
    count -= (bob *= 3) - a ;
    printf ("content of i:%d\n", i);
    printf ("content of j:%d\n", j);
    printf ("content of a:%d\n", a);
    printf ("content of bob:%d\n", bob);
    printf ("content of count:%d\n", count);

    key = ngetchx();

    printf ("The key you pressed is: %d", key);
    printf ("content of i:%d\ncontent of j:%d\ncontent of a:%d\ncontent of bob:%d\ncontent of count:%d\n", i, j, a, bob, count);

    ngetchx();
  }


        I think the comments of beginning code are enough and you can understand the beginning of code. It is especially to show you different kinds of declaration.
        printf("Display:%d ,it is an integer\n",variable) print 'Display:4 ,it is an integer'. In fact, every time the compiler sees '%', it is for it a flag. After it sees 'd' and it knows it has to put a signed decimal integer, the type of the variable. Finally it finds this number in the variable: variable and put the value of variable in the flag '%'. If you want to print '%', you have to write '%' twice or else the compiler thinks it is a flag, ok?
        printf("one real: %e\nanother real: %f\n",real,r) print 'one real: -4.740082.e+8' and on the next line, 'another real: 254.1317'. It is the same explication except 'e' means floating point whose the syntax is [-]d.dddde[sign]ddd, it is an exponential format and f means floating point whose the syntax is [-]dddd.dddd .
 

% [{h|l}] type

 

Size {h|l}

Meaning

h

Force short integer

l

Force long integer

 

Type

Meaning

d, i

Signed decimal integer

u

Unsigned decimal integer

o

Octal integer (this option is non-ANSI, i.e. TI specific)

b

Binary integer (this option is non-ANSI, i.e. TI specific)

x

Lowercase hexadecimal integer

X

Uppercase hexadecimal integer

e

Floating point, format [-]d.dddde[sign]ddd (exponential format)

E

Like 'e' but with uppercase letter for the exponent

f

floating point, format [-]dddd.dddd

g

Floating point: most compact float format available ('e' or 'f'); this is the most common option, used for most dialog floats

G

Like 'g' but with uppercase letter for the exponent

r

Floating point, engineering form (this option is non-ANSI, i.e. TI specific)

R

Like 'r' but with uppercase letter for the exponent

y

Floating point, mode specified float format (this option is non-ANSI, i.e. TI specific)

Y

Like 'y' but with uppercase letter for the exponent

c

Character

s

String

p

Pointer; principally the same as 'x' - do not use without 'l' modifier

%

None; the character '%' is printed instead


        I have given you all the definition but it is hard to remember and I advise you to see Tigcc documentation. In fact, I only made a copy and a paste. Remember that the work of Zeljko Juric is done to be useful! :-)
        l=l-32000 is your first operation on a variable. I can go to break down the syntax so as to be simpler. Firstly, the lvalue is l and the rvalue is l-32000. After the compiler is interested by the rvalue, it takes the value of variable: 'l' and subtracts 32000. Therefore it does l-32000=78000-32000=46000. After it assigns the value: 46000 to the variable: 'l'. It is simple, isn’t it? Do not worry, it will soon be easy for you.
        i++ means i=i+1. In fact, you take the value of 'i', you add 1, and you assign the sum to the variable 'i'. If you want, you can replace i++ by i=i+1. It is the same thing but 'i++' is a short cut, which is interesting when you write several times in your code.
        j-- means j=j-1, it is another short cut. Also a+=2 means a=a+2, C is great for short cut. Here, others shortcuts: variable operator = value which means variable = variable operator value. 'value' can also be the content of a variable and 'operator' can be +,-,*,/,% and others you can find in Tigcc documentation. The hardest to understand is '%' which gives the remainder. 'exp1 % exp2' gives the remainder of exp1 divided by exp2. We will see later one use of this function because it can be very useful and give a smaller and more efficient code.
        bob=2 is an initialization of variable. Of course, you can initialize a variable after the declaration of variables.
        ‘count -= (bob*=3) – a’ is a complex enough assignment. I am going to divide the problem into several parts. When you see the syntax, you note there are two '=' but why is there no problem? It is the magic of C language! You must understand like the compiler, indeed, the compiler firstly sees the first assignment acronym which is left because the compiler reads left to right and top to bottom. Therefore the lvalue is 'count' and the rvalue is '(bob*=3) - a'. Here the compiler analyzes (bob*=3) in first because it is like mathematics, the assignments between brackets are the priority. Also 'bob' is the lvalue and '3' is the rvalue. We can deduce bob *= 3 = bob*3 = 2*3 = 6. Also we reach to ‘count -= 6 – a’. We can evaluate the rvalue which is egal to 6-a = 6-2 = 4 and after we reach count -= 4 = count - 4 = 17 - 4 = 13. It is great even if it is hard for you at beginning but with the time, it will be as easy as doing two lines. If it is too hard for you, the most important is to make at your way.
        After we print the contents of some variables. An equivalent of this part of code can be printf("content of i:%d\ncontent of j:%d\ncontent of a:%d\ncontent of bob:%d\ncontent of count:%d\n",i,j,a,bob,count). But this syntax is not inevitably simpler than the previous syntax, you must do a choice among your preferences. Remember that you are the programmer and the main is you to enjoy yourself! :-)
        The last difficulty is key=ngetchx(). This function is able to give the name of hit key and we have to pick this result we can store in a variable. Also we can deal with this result later in the code. The name of key is a character or a number: 0 to 255, it is an ascii code. Each key has a different ascii code and there are constants, which are equal to ascii code. For example, the constant of ascii code of escape key is KEY_ESC and all keys have a constant and these constants are called common keys. When you need know the name of constant, you go to see in Tigcc documentation. There is a problem with some keys like arrow keys because they are different between Ti89 and Ti92+.

        For the boolean variables, there is a specific operator: '!'. In fact, a boolean variable takes two values: TRUE=1 and FALSE=0 and when you put this operator before a boolean variable, the compiler swaps its current value for the other value. For example,



  // C Source File
  // Created 08/06/2002; 16:22:37

  #define USE_TI89              // Produce .89z File
  #define USE_TI92PLUS          // Produce .9xz File

  #define OPTIMIZE_ROM_CALLS    // Use ROM Call Optimization

  #define SAVE_SCREEN           // Save/Restore LCD Contents

  #include <tigcclib.h>         // Include All Header Files

  // Main Function
  void _main(void)
  {
    int b = TRUE; // it's a boolean variable

    b = !b; // the new value is FALSE
    b = !(!b); // the new value is always FALSE
  }


        I want to explain the last line, I think you should understand the others. I am going to divide the problem into several steps as the compiler works. In fact, the compiler evaluates the expression between brackets, it is like mathematics! :-) Therefore, the value of this expression is TRUE and after there is still the operator '!' so the final value of rvalue is FALSE. That is why after assignment, the new value of variable is always FALSE.

        You must be careful because in C there is a difference between minuscule and majuscule. In fact, for the compiler two variables are different if they are different only with minuscule and majuscule. Let us illustrate this point:


                         


  // C Source File
  // Created 22/04/2003; 18:15:29

  #define USE_TI89              // Compile for TI-89
  #define USE_TI92PLUS          // Compile for TI-92 Plus
  #define USE_V200              // Compile for V200

  #define OPTIMIZE_ROM_CALLS    // Use ROM Call Optimization

  #define MIN_AMS 100           // Compile for AMS 1.00 or higher

  #define SAVE_SCREEN           // Save/Restore LCD Contents

  #include <tigcclib.h>         // Include All Header Files

  // Main Function
  void _main(void)
  {
    int Fred;
    int FRED;

    clrscr();

    Fred = 17;
    FRED = 13;
    printf ("Fred:%d\nFRED:%d", Fred, FRED);

    ngetchx();
  }

 





See another lesson
Retour au menu