Introduction to Computer Algorithmics and Programming Ceng 113




























- Slides: 28
Introduction to Computer Algorithmics and Programming Ceng 113 Variables and Operators in C
C Language C was developed at Bell Laboratories. (Brian Kernighan and Dennis Richie)-1972 C is known as a high low-level language.
Reference Book In Library: C The Complete Reference, Covers C++ & ANSI C Author; Herbert Schildt Press; Osborne, Mc. Graw-Hill ISBN 0 -07 -881538 -X E-book: The C Programming Language (ANSI C) Authors: Brian W. Kernighan, Dennis M. Ritchie
Program Definitions • Variable: a named location in memory for storing data. • Constant: a variable whose value is determined when a program description is written and doesn’t change from that value during program execution. • Label: a named location in memory for storing a program instruction.
General Form of a C Program global declarations return-type main(parameter list) { • Consist of one or more statement sequence functions. } return-type f 1(parameter list) • “main(. . )”, which is the first { function called, when program statement sequence execution begins. } return-type f 2(parameter list) { statement sequence }. .
A Sample C Program /* Letter guessing game */ #include <stdio. h> #include <time. h> #include <stdlib. h> main() { int tries = 0; char comp. Ans, user. Guess; /* Save the computer's letter */ srand(time(NULL)); /* Randomize the number generator */ comp. Ans = (rand() %26) +65; /* generate a random letter */ printf("I am thinking of a letter. . . "); . . .
A Sample C Program do { printf("What is your guess? "); scanf(" %c", &user. Guess); tries++; /* Add 1 to the guess counter */ if (user. Guess > comp. Ans) { printf(" Your guess was too high n"); printf(" n Try again. . . "); } if (user. Guess < comp. Ans) { printf(" Your guess was too low n"); printf(" n Try again. . . "); } } while (user. Guess != comp. Ans); /* Quit when a match is found */ /* User got it right, announce it */ printf("*** Congratulations! You got it right! n"); printf("It took you only %d tries to guess. ", tries); return 0; }
The Library and Linking All C compilers come with a standard C library of functions that perform most commonly needed tasks. When you call a function that is not parf of your program, the C compiler “remembers” its name. Later, the “linker” combines the code you wrote with the object code already found in the standard library. This process is called linking.
Separate Compilation C allows a program to be contained in many files and lets you compile each file separately. The advantage of separate compilation is that if you change the code of one file, you don’t need to recompile the entire program.
The Terms • Source Code; The text of a program that a user can read. The source code is input in to the C compiler. • Object Code; Translation of the source code of a program into machine code, which the computer can read and execute directly. Object code is the input to the linker. • Linker; A program links separately-compiled functions into one program. The output of the linker is an executable program. • Library; The file containing the standard functions that your program may use. (all I/O operations, useful routins. . )
Basic Data Types Data types; • char • int • float • double Modifiers; • signed • unsigned • long • short Type Approximate Size in Bits Minimal Range char 8 -127 to 127 unsigned char 8 0 to 255 int 16 -32767 to 32767 unsigned int 16 0 to 65535 signed int 16 same as int short int 16 same as int long int 32 -2, 147, 483, 647 to 2, 147, 483, 647 unsigned long int. 32 0 to 4, 294, 967, 295 float 32 Six digits of precision double 64 Ten digits of precision long double 128 Ten digits of precision (long float has the same meaning as double).
Variables • A variable is a named location in memory that is used to hold a value that may be modified by the program. • All variables must be declared before they can be used. • General declaration form is; type variable_list; Example; int i, j, l; unsigned int u; double balance, profit, loss;
Declaration of Variables will be declared in three basic places; 1. inside functions local variables 2. in the definition of function parameters formal parameters 3. outside of all functions global variables
Sample – local variables /* Addition */ #include <stdio. h> #include <stdlib. h> main() { int a, b, c; printf(“n insert a = %d”); scanf(" %d", &a); printf(“n insert b = %d”); scanf(" %d", &b); c = a + b; printf(“n a + b = %d”, c); }
Sample – formal parameters /* Addition */ #include <stdio. h> #include <stdlib. h> int addition(int x, int y); main() { int a, b, c; printf(“n insert a =”); scanf(" %d", &a); printf(“n insert b =”); scanf(" %d", &b); c = addition(a, b); printf(“n a + b = %d”, c); } int addition(int x, int y) { int z; z = x + y; return(z); }
Sample – global parameters /* Addition */ #include <stdio. h> #include <stdlib. h> int a, b, c; void addition( ); main() { printf(“n insert a = “); scanf(" %d", &a); printf(“n insert b = ”); scanf(" %d", &b); addition(); printf(“n x + b = %d”, c); } void addition( ) { c = a + b; }
Local Variables void func 1(void) { int x; x = 10; } void func 2(void) { int x; x = -199; } void f(void) { int t; scanf(“%d”, &t); if (t==1) { char s[80]; printf(“enter name: ”); gets(s); . . /* do something */ } } #include “stdio. h” void f(void) void main(void) { int i; for(i=0; i<10; i++) f(); } void f(void) { int j = 10; printf(“%d”, j); }
Formal Parameters • If a function is to use arguments, it must declare variables that will accept the values of the arguments. These variables are called the formal parameters of the function. /* Return 1 if c is part of string s; 0 otherwise */ is_in(char *s, char c) { while (*s) if (*s==c) return 1; else s++; return 0; }
Global Variables Global variables are known throughout the program and may be used by any piece of code. #include “stdio. h” int count; /* count is global */ void func 1(void); void func 2(void); void main(void) { count = 100; func 1(); } void func 1(void) { int temp; temp = count; func 2(); printf(“count is %d”, count); } void func 2(void) { int count; for (count=1; count<10; count++) putchar(‘. ’); }
const • Variables of type const may not be changed by your program. • A const variable can be given an initial value. const int a = 10; #include “stdio. h” void sp_to_dash(const char *str); void main(void) { sp_to_dash(“this is a test”); } void sp_to_dash(const char *str) { while (*str) { if (*str==‘ ‘) printf(“%c”, ‘-’); else printf(“%c”, *str); str++; } } void sp_to_dash(const char *str) { while (*str) { if (*str==‘ ‘) *str=‘-’; printf(“%c”, *str); str++; } }
Operators • The assignment operator; variable_name = expression; The target, or left part of the assignment must be a variable or a pointer, not a function or a constant. • Multiple assignment ; x = y = z = 0; • The conversion assignment; When variables of one type are mixed with variables of another type, a type conversion will occur. int x; char ch; float f; void func(void) { ch = x; x = f; f = ch; f = x; }
Common type conversion (Samples) Target Type Expression Type Posssible info loss char short int High order 8 bits char int High order 8 bits int long int High order 16 bits int float double Fractional part Precision, result rounded
Arithmetic Operators Operator Action - Subtraction + Addition * Multiplication / Division % Modulus -- Decrement ++ Increment
Relational or Logical Operators Relational Operators Operator Action > Greater than < Less than >= Greater than or equal <= Less than or equal == Equal != Not Equal Logical Operators Operator Action && AND || OR ! NOT In C, true is any value other than zero. False is zero.
Logical Operators p 0 0 1 1 AND q 0 1 p 0 0 1 1 p&&q 0 0 0 1 NOT p !p 0 1 1 0 OR q 0 1 p || q 0 1 1 1
Sample for Logical Operations #include “stdio. h” int xor(int a, int b); void main(void) { printf(“%d”, xor(1, 0)); printf(“%d”, xor(1, 1)); printf(“%d”, xor(0, 0)); } /*perform logical XOR operation using the two argumants. */ int xor(int a, int b); { return (a||b)&& !(a&&b); } a b a XOR b 0 0 0 1 0 1 1 0
Lab. Exercise #1 • Write a program that implements SWAP operation for two integer variables – with using a temp space, – without using a temp space.
Next Course Program Control Statements • Selection – if and switch • Iteration – while, for, do-while • Jump – break, continue, goto, return • Label • Expression • Block – A block begins with a { and ends with a }.