Unformatted text preview:

COMS 1003 : Introduction to Computer Programming in CVariables, Primitive Types, Operators, and ExpressionsSeptember 20th 2005Outline●Relate to readings●Define “Expressions”●Discuss how to represent “data” in a program–variable name–variable type●List and discuss the properties of primitives types●List and discuss basic operations on variablesReadings●PCP Chapter 4 (today)–Basic Declarations and Expressions●TCPL Chapter 2 (next session)–Types, Operators, and Expressions●TCPL Section 1.2(next session)–Variables and Arithmetic ExpressionsExpressions●A C program is a sequence of statements●A C program is a collection of functions and the data those functions operate on●The building blocks of statements are expressions: cominations of language key words, function calls, operators, and operands that evaluate to a valueReview: What is a Program?●A program is a sequence of instructions that operates on data●A C program is a collection of variables and functions that process the data held in those variables●Computers process long strings of 1's and 0's–need a way to refer to portions of those strings as higher-level data objectsVariablesA variable is a container for dataUnstructured Datadsalkjdlkajdlkajdjfjf028f.f.2q4fqd09024CVvqwE#@FQ#(V(gkljsoive001301001290avvvvv8veaeeee0xrrtlkjaavvvv..eaaf00000010100110100101000100101010..............345MichaelPatient SSN: 9995500003.141592658Structured DataWhat exactly is a Variable?●A variable is the concept of a piece of structured data that can be accessed (read or modified) via well-known, standard rules●A variable is NOT JUST the data it contains!●A variable also has:–a name or identifier that provides a way to refer to it–a type that defines its size (how much memory it uses)–a location or memory address specifying where the data is storedExample:: Simple Integer Values●Suppose we want to write a program for processing a student's grades●Need a variable to hold the total scorevalueVariable name:total_score:345Example:: Variable Declaration●Declaring a variable is a standard action to let the rest of the program know about a piece of data that will be used.●The following program statement declares (that is, tells the computer to set aside a memory location) an integer variable called 'total_score'int total_score;typevariable nameDeclaring Variables●Variables are usually declared at the beginning of the program or function they are used in●Variable names can be any combination of letters, numbers, or underscores, but must start with a letter or underscore●Variable names should be descriptive; avoid names like 'ab', 'x', 'tmp', etc.●Make sure you don't try to name a variable after a reserved word (if, for, while, switch ...)A Simple Statement: Initialization●You can initialize a variable when you declare it–This gives the variable a definite “starting value”–To initialize a variable, use an assignment statement.●An assignment statement computes the value of the expression on the right hand side and stores it in the variable named on the left hand side. –“=” should be read “stores the result of”int total_score = 0;Initializing a Variableint total_score = 0;Computer Memorytotal_score:address value00000000102210231024102510261027C Program//...//...int main(){ //...}Subtle Point About Variable Names●When you program, you see the variable name●When the computer executes your program, it actually sees the variable memory address●In both cases, the data is used behind the scenesTypesVariable Types●A type is a hint to the computer on how to handle the data contained in or referred to by the variable–usually this involves size of the storage allocated●There are 4 primitive (basic) types in C:–int (regular integer)–char (1 character)–float (single precision floating point number)–double (double precision floating point number)Type Modifiers●Types can be augmented by additional information●Some simple “type qualifiers” are listed below:–short (applied to int)–long (applied to int)–unsigned (only positive values and 0)–signed–const (specifies that the value can't be changed)●We usually drop the 'int' when specifying short or longshowsize DemoOutput (sizes are in # of bytes)[michael@xoren code]$ make showsizegcc -Wall -g -o showsize showsize.c[michael@xoren code]$ ./showsize sizeof(int) = 4sizeof(char) = 1sizeof(long) = 4sizeof(short) = 2sizeof(float) = 4sizeof(double) = 8sizeof(long double) = 12[michael@xoren code]$consttest DemoLanguage OperatorsOperators Overview●You are familiar with many operators from basic math and logic:–addition (+), subtraction (-), multiplication (*), division (/)–AND (&&), OR (||), NOT (!)●Operators are basically common functions that take their input and produce some output●Common enough to have their own symbols in a programming language (see above)Operators (cont.)●C has many operators–Some you are familiar with (see previous page)–Some not: mod, bitwise AND, OR, XOR, relational●Operators are:–unary (take one argument, e.g., !-)–binary (take two arguments, e.g., +-*/<>==)–ternary (take three arguments)●Classifications:–arithmetic, logic, relational, assignmentOperator Context●Warning! Operators are represented by symbols. Sometimes, the symbols may mean something completely different based on context. For example:int x = -1; // the '-' operator is negationint x = 4 - 3; //the '-' operator is subtractionArithmetic Operators●Addition is represented by + e.g., sum = x+y;●Subtraction is rep. by - e.g., diff = x - y;●Multiplication is rep. by * e.g., scale = x * y;●Division is rep. by / e.g., quotient = x/y;●Modulus is rep. by % e.g., remainder = x % y;Relational Operators●Assignment operator is =, e.g., int sum = x;●Equality operator is ==, e.g., is_equal = (x==y);●Not equal is expressed by !=, not = (x!=y);●Less than: <●Greater than: >●Less than or equal to <=●Greater than or equal to >=Logical Operators●AND: (x&&y)●OR: (x||y)●NOT: (!x)Bitwise Operators●Like logical operators, but operate on the individual bits of a variable, not the whole logical valueint x = 2;int y = 1;int r = x || y;printf(“r is: %d”,r);int x = 2;int y = 1;int r = x | y;printf(“r is: %d”,r);Output is: r is: 1 Output is: r is: 3Bitwise Operators●bitwise OR: |●bitwise AND: &●bitwise XOR: ^●one's


View Full Document

Columbia COMS 1003 - 03-language-basics

Download 03-language-basics
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view 03-language-basics and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view 03-language-basics 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?