Casio Z1 Gr User Manual
Have a look at the manual Casio Z1 Gr User Manual online for free. It’s possible to download the document as PDF or print. UserManuals.tech offer 338 Casio manuals and user’s guides for free. Share the user manual or guide on Facebook, Twitter or Google+.
101 /* 100 squares 3 */ /* #include */ double dsquare(x) /* square */ double x; { return (x*x); } main(){ double d; for (d=1.0; d
102 6.1 Constants and Variables 6.1.1 Local variables and global variables Local variables A local variable is one that is declared within a function for use within that particular function only. Since such variables are local, you can use the same variable name in multiple functions without any problem – each variable is treated independently, even though they have the same name. Have a look at the program listed below. /* Local variable */ /* #include */ void pr() /* print function */ { int i; /* local i */ for(i=0;i
103 Here, variable “i” is declared outside of any function, so “i” will be treated as a global variable. Consequently, it will retain its current value regardless of whether execution is in main() or pr(). In this program, variable “i” is assigned a value by the “for” loop in main(), and then the value of “i” is printed by function pr(). Note that any function has access to “i”, not only to read it but as well to modify it. This increases the risk of unexpected software errors and is a major drawback of global variables. If you decide to use global variables, here are some basic rules to follow: • Restrict the usage to variables that make sense to be global, but cannot be considered as a constant. Example: Screen_W, Dist_Unit. • Use long, descriptive names to make obvious the type of value that is stored. 6.1.2 Pointers and variable storage locations Entering values Here, we will write a calculation program that performs specific integer arithmetic operations on values entered via the scanf() standard function. With this program, entry of “33+21 . . “ would for example produce the result “=54”. /* scanf example */ /* #include */ main(){ char op; int x,y,xy; xy=0; scanf(“%d%c%d”,&x,&op,&y); if(op==’+’) xy=x+y; else if(op==’-‘) xy=x-y; else if(op==’*‘) xy=x*y; else if(op==’/‘) xy=x/y; else if(op==’%‘) xy=x%y; else printf(“unknown op”); printf(“=%d”¥n”,xy); } Variable “op” is a character type while “x”, “y”, and “xy” are integer type. Since we are using the scanf() function, arguments are expressed preceded by ampersands, becominf “&x”, “&op” and “&y”. In C, an argument such as “&x” represents the location in memory that the contents of variable “x” are stored. It is called an address. The scanf() function understands arguments to be addresses, and represents the value stored at the address within the expression. See the Command Reference for detailed information on the scanf() function. The line “scanf’”%d%c%d”, &x, &op, &y);” accepts input of integers for variables “x” and “y”, and a character code for variable “op”. The execution of the next statement depends on the character assigned to variable “op”. If it is a plus sign, the first “if” statement is executed, if it a a minus sign, the second “if” statement is executed, etc.
104 Finally, the printf() statement displays the value of “xy” which is the result, no matter which arithmetic operation is performed. 6.1.3 Data types and lengths The following table shows the data types and their respective lengths for the interpreter. These are almost identical on all computers, except for the integer (int) that may change. Type declaration Interpreter char short int long float double 8-bit integer 16-bit integer 16-bit integer 32-bit integer 32-bit single precision floating point 64-bit double precision floating point 6.1.4 Assigning variable names and function names All variables must be declared before they can be used within a program. The following rules apply for variables, constants defined using the #define statement, and function names. • The first character of a variable name must be an alphabetic character (A-Z, a-z), or an underline (_). • Second and subsequent characters in a variable may be an alphabetic character, underline, or number. The computer differentiates between upper case and lower case letters when matching variable names. • Reserved words cannot be used as variable names, though a part of a variable name may be a reserved word. • The length of a variable name is unlimited, but only the first eight characters are used to discriminate one name from another. This means that “abc12345” and “abc12344” will be considered as the same variable, since the initial eight characters of either name are the same. 6.1.5 Data expressions Character constants Characters for the constant type char should be enclosed within single quotation marks as shown below: ‘C’ ‘8’ ‘¥n’ Just as many larger computers, the unit represents characters using ASCII codes, with some exceptions (see character table page 12). In addition, the computer uses control characters such as (NL), (HT), (BS), (CR) which are expressed in C as escape sequences (using the ¥ symbol, when ANSI C uses \ symbol)
105 Code Hex Code Name Meaning ¥a ¥n ¥t ¥b ¥r ¥f ¥¥ ¥’ ¥” ¥0 ¥nnn ¥xmm 0x07 0x0A 0x09 0x08 0x0D 0x0C 0x5C 0x27 0x22 0x00 0xmm Bell (BEL) New Line (NL) Horizontal tab (HT) Backspace (BS) Carriage return (CR) Form feed Yen symbol Single quote Double quote Null Octal notation Hexadecimal notation Sound buzzer Carriage return + Line feed Horizontal tab Backspace (one character) Returns to line start Change page Character ” ¥ ” Character “ ‘ “ Character “ “ “ Equivalent to zero Character code for octal value nnn. Character code for hexadecimal value mm Integer constants Constants of the int type can be bradly classified as decimal, octal, and hexadecimal. The letter “L” or “l” must be affixed for long type integers. Decimals Example: 1121 -128 68000 123456789L (for long) Note: Leading zeros cannot be used, because they mean an octal notation. Octal: Example: 033 0777 012 01234567L (for long) Note: Leading zero required for octal notation. No number above 7! Hexadecimal: Example: ox1B 0xFFFF 0x09 0xffffffL (for long) Note: 0x (or 0X) required for hexadecimal notation. The following show the ranges of each type of notation. Negative integers are accomplished using unary minus operators with unsigned constants. Decimal constants int 0 - 32767 long 32768 - 2147483647 Octal constants int 00 - 077777 unsigned int 0100000 - 0177777 long 0200000 - 017777777777 Hexadecimal constants int 0x0000 - 0x7fff unsigned int 0x8000 - 0xffff long 0x10000 - 0x7fffffff The ranges defined above also apply to entries for the scanf(), fscanf() and sscanf() functions.
106 Floating-point constants and double precision floating-point constants Decimal values can be defined as float type or double type constants using the format shown below. Exponents are indicated by the letter “E” or “e”. Example: 3.1416 -1.4141 1.0e-4 1.23456E5 The following shows the ranges of float and double. float 0, ±1e-63 - ±9.99999e+63 double 0, ±1e-99 - ±9.999999999e+99 String constants String constants are contained in double quotation marks. The structure of character strings is basically the same as for an ANSI compiler, with a 0 (null) being affixed at the end. The following shows an example of character string: “How do you do?” 6.1.6 Storage classes Storage classes are used to specify the memory storage area for a declared variable, as well that to specify the scope (range that program can read from / write to). The following table shows the storage classes, as well as the variables that are available for each class. Storage class Variables auto Used for short-term storage within a program. This is the default case and “auto” can be omitted static Reserves area throughout execution of program. Values accessed and acted upon throughout entire program. register High frequency access. Variables, which are effective for increasing speed of execution by allocating values to microprocessor registers. Most compilers optimize automatically the executable code using registers for the most accessed variables, but it may still make sense to specify “register”. The C interpreter treats “auto” and “register” variables the same way. extern File-external or function external global variables. With the interpreter, file-external can be another program area. The extern statement is key to tell a compiler that the variable is already defined elsewhere, and that it should not book any space for it. When linking the various modules and libraries, the variable will get its address. With the interpreter, it is possible to declare in various program areas the same global variable twice without creating an error. It is nevertheless a good habit to use the “extern” statement for all declarations but one. The only mandatory use of “extern” is when you refer within a function to an extern global variable. Without an extern statement, the interpreter would create a local variable with the same name as the global variable, but superseding it within the function.
107 Note: The interpreter does not allow declaring a local “auto” variable within the code of a function. func(a) double a; { int i,j,x; float fx,fy; long lx,ly; for(i=0;i
108 You can also use a pointer within a character string to isolate single characters from the string. /* Pointer example */ /* #include */ main(){ char *p; p=”Casio”; printf(“%c %s¥n”,*p,p); } Execution of this program will produce the following result: C Casio >_ This is because a string is actually a pointer to the first character, the “null” character (0) being added after the last character to close the string. Now, lets assume we want to access the third letter of the string “Casio”. We need to increment the pointer p by 2. *(p+2) is character “s”. Because p was declared as pointing toward characters (see line 4), the interpreter knows that an increment of 1 will select the next Byte in the memory. If p were pointing toward long integers, each increment would select the next 4 Bytes. As said earlier arrays are closely related to pointers as well. int a[5], *pa pa=a; /* equivalent to pa=&a[0] */ pa++; /* now, pa points toward a[1] */ 6.2 Operators C employs many operators not available with BASIC, FORTRAN or Pascal. Operators are represented by such symbols as “+”, “-“, “*” and “/”, and they are used to alter values assigned to variables. Basically, the C interpreter supports the same operators as a standard ANSI C compiler. Precedence When a single expression contains a number of operators, precedence determines which operator is executed first. Note the following: a+b*c In this case, the operation “b*c” is performed first, and then the result of this is added to “a”. This means that the precedence of multiplication is higher than that of addition. Note the following example: (a+b)*c In this case, the (a+b) operation is performed first, because it is enclosed within parentheses.
109 The following table shows all of the operators used by C and their functions, explained in their order of precedence. Primary Operators ( ), func( ) x[ ], y[ ][ ] Parenthetical, function argument operations. Specify array elements. Unary Operators *px &x -x ++x, --x x++, x-- ~x !x (type)x sizeof(x) Specifies content indicated by a pointer. Address of variable x. Negative value x. +1 / -1 before using variable x. +1 / -1 after using variable x. NOT performed on each bit (inversion). Logical NOT (if x≠0, 0 returned; if x=0, 1 returned). Forced conversion / specification of x (cast operator). Variable x Byte length value, sizeof(type) illegal. Binomial operators x*y, x/y, x%y x+y, x-y xy xy, x>=y, x
110 The following table shows the precedence of associativity of the C operators. Precedence Operators Associativity High ( ), [ ] → Left to right Unary !, ~, ++, --, -, (type), *, &, sizeof ← Right to left Multiplication, division *, /, % → Left to right Addition, subtraction +, - → Left to right Shift → Left to right Relational >, =,