CS110_01c_varsLiterals - EE110 1c Literals& Variables...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: EE110: 1c Literals & Variables Jack Tumblin [email protected] I hope you have already: Read through Chapter 1, and at least Skimmed the rest of Chapter 2, and Tried CodeBlocks on your own pgms... Recall That: C ignores /* comments */ and blank space // (C++ comments also OK, but NOT part of C!) A statement is one complete rule, and C statements always end with semicolon ; A block of statements is always enclosed within braces { } (`curly brackets') A function is a named block of statements (more about functions later) All C programs have a main( ) function: computing always begins and ends in main( ) Numbers: Bits, Bytes and Memory Bit = BInary digiT = a 2-way choice = (1/0, yes/no) = the fundamental unit of information Byte = 8 bits grouped together: 28 = 256 choices Memory is a long list of stored bytes. Sequentially numbered; address. Numbers: Bits, Bytes and Memory Address 0 1 stored bytes (8 bits) bits 2 3 4 5 Memory How C is Organized: Nesting Literals and Variables (with their `data types') Operators Expressions Statements Functions Libraries Programs `Systems' How C is Organized: Nesting Literals and Variables (with their `data types') Operators Expressions Statements Functions Libraries Programs `Systems' Jargon: Know these Definitions Literal:A fixed number (`data') written in a program Variable: A named, reserved piece of memory Operator: `A non-numeric calculator button'; Expression: The smallest grain of computing Statement: Zero or more expressions ended by `;' Function: A named { block } of statements; Library: A collection of re-usable functions in a file Program: a main() function + any functions it calls. `System':A collection of program(s) that work together Variables and Literals Same idea used in algebra: y = 3x +5 x and y are the `variables'; 3,5 are the `constants' oops! No! the `literals' (CS jargon disaster--more about this in next class) But Generalized: a variable is a `placeholder' for one piece of data; numbers, letters, sets, arrays, etc. Its value may be unknown until the program is actually running (CS jargon: `at run-time'). Inside the computer: a variable is a reserved location in memory, and holds data that are allowed to change. Variables: Definition A variable is: a reserved location in memory that has a name an associated type (e.g. integer, character,...) and holds data we can modify Hey Kids! Use Variables Responsibly: ALWAYS declare a variable before use! forgot? error message, or ugly weirdness... ALWAYS set a variable's value before use! forgot? starting value will be unpredictable... What does `declaring' a variable do? reserve a memory location that will store the value of the variable (jargon: `assign an address') assign the variable name to that location. /******************************************* Hydra.c -- a program with variables. *******************************************/ #include < stdio.h > main() { int heads; int eyes; `declare' each variable; write a statement that to set its type and name /* assign a value */ /* compute a value */ heads = 3; eyes = heads * 2; printf("It has %d heads and %d eyes! \n", heads, eyes); } Output: > It has 3 heads and 6 eyes! /******************************************* Hydra.c -- a program with variables. *******************************************/ #include < stdio.h > main() { int heads; int eyes; heads = 3; eyes = heads * 2; /* assign a value */ /* compute a value */ After that, set values of variables printf("It has %d heads and %d eyes! \n", heads, eyes); } Output: > It has 3 heads have 6 eyes! How to `declare' Variables Make declaration statement ; (always ends with ;) The format: data_type var, var, ... ; Example: int counter1, counter2; Where? Declare variables at the start of a function, right after the opening curly-brace { How to `set' a Variable's Value Make an assignment statement: Careful! = means assign , not `equals'! variable = value; Example: int num_students; num_students = 22; Variable Names In C, variable names are built from: the letters of the alphabet the digits 0 through 9 the underscore. (no other special characters!) A variable name must start with a letter or an underscore. Only the first 31 characters of a variable name are significant. The rest are ignored. A variable name must not be the same as a reserved word used by the C language. Variable Names Good, Valid variable names: totalArea counter1 Count_trees temp_in_F $product total% _main isEmpty pNuts m_size main 3rd not-this Invalid variable names: Legal, badly-chosen variable names: l11 (is it L11, L1L, LL1, or LLL?) x (what does it mean?) maximum_number_of_students_in_my_class a23456789_123456789_123456789_12345678 Basic Types of Variables There are 4 basic `built-in' data types in C: Type Integer Floating point Double Character C keyword to use: int float double char printf(), scanf() Format Specifier %d %f %f or %g %c Basic Types of Variables int format specifier: %d Integer variables hold signed, whole numbers (i.e. with no fractional parts). e.g. 10, -8439 Not all computer brands use the same int size! Windows stores integers in 4 bytes: (32 bits: 1 sign bit, 31 bits for numbers) int values can be any whole number between -231 (about -109) and +231 (about 109) Basic Types of Variables float and double format specifier: %e or %g These types store floating-point numbers; they may have a fractional part. e.g. -12.475, 2.0 double has more than twice the precision and more than twice the range of float. Not all computer systems use the same size! for Windows: float stored in 4 bytes, double stored in 8 bytes. Basic Types of Variables Floating Pt. Number = (M)(2E) float: 4 bytes in Windows 24 bit signed Mantissa, 8 bit signed Exponent Smallest Number: 1.0 2-127 (about 5.9 10-39); Largest Number: 2.0 2+128 (about 6.8 10+38). 23-bit mantissa: log10(223)=6.923, or about 7 decimal digits precision double:8 bytes in Windows 52 bit signed Mantissa, 11 bit signed Exponent Smallest Number: 1.0 2-1023 (about 1.1 10-308); Largest Number : 2.0 2+1024 (about 3.6 10+308). 51-bit mantissa: log10(251)=15.35, or about 15 decimal digits precision Basic Types of Variables char format specifier: %c used to store single characters, e.g. `a' , `R'. (Note that the value of a character-type variable is shown enclosed in single quotes). Remember that the string in our earlier example was enclosed in double quotes. Important: characters : single quotes `a' `N' `7' strings : double quotes "Hello" "2B" More Types of Variables How many bytes for each type? A MESS! No standards in the language. But nowadays its almost always: char: 1 or 2 bytes (ASCII or Unicode) int: 4 bytes=32 bits, signed. float: 4 bytes=32 bits, (Pentium? use double) double: 8 bytes=64 bits. Leftover keywords from the Dark Ages: short int, long int, unsigned int, signed int, unsigned char, signed char, double double. Don't use in new programs (except at gunpoint) Literals ==Fixed values written into your program. Same basic data types int cnt; char lastKey; float velocity; double atoms; ... ... ... ... for(cnt=0; cnt<max; cnt++) lastKey = `y'; velocity = 0.0f; atoms = 6.23E+23; Can be Dangerous! Use Sparingly or not at all! (use #define instead, coming next class) Literals ==Fixed values written into your program. Details: Write an character value inside single quotes keypressd = `y'; Write a phrase value, or string (more about this later) inside double quotes: printf("Huh. Dude typed a %c!\n", keypressd); Default: decimal points make a `double' value. You can FORCE `float' data type like this: velocity = 2.768f; For Next Time: Be sure you know the definitions of the parts of C. Everybody should use these words from now on. No one will understand what you mean by "that constant variable-type thingie." Read ahead. Skim chapter 3... Try things. Can you write a program that prints a multiplication table? ...
View Full Document

This note was uploaded on 10/05/2011 for the course COMPUTER S 110-1 taught by Professor Tumblin during the Spring '11 term at Northwestern.

Ask a homework question - tutors are online