Beginning Concepts for Problem Solving in C BJ Furman 04JUL2009 1 The Plan for Today Roll call 2 Learning Objectives Following completion of today s lab you should be able to List and explain the basic kinds of data Distinguish between variables and constants Declare integer floating point and character variables List the basic data types used in C Identify and explain commonly used operators in C Use C operators correctly according to their placement in the hierarchy chart Write and execute simple C programs in ChIDE Set up and evaluate expressions and equations using variables constants operators and hierarchy of operations 3 What and Why Data pieces of information we deal with in the problem solving process Why is it important to know about data types Need to know about data types to effectively use a programming language It s all just bits the lowest level What would you rather work with int i float r i 3 r cos 2 i PI or 00000010 10000000 00000000 00000110 10000010 10000000 01111111 11111100 11001010 00000001 00000000 00000000 etc 4 Kinds of Data simplified view The basic kinds of data that we will mostly use Numeric Integers Real floating point numbers Character are enclosed by single quotes in C All letters numbers and special symbols Ex A 5 String are enclosed by double quotes in C Are combinations of more than one character Ex programming ME 30 Logical also called Boolean named after George Boole an English mathematician from the early 1800 s True or False 1 or 0 5 Kinds of Data Practice Data 1 17 2 George 3 2 35 4 0 0023 5 25 6 m 7 4 32E 6 8 185 3 9 0 10 1 Type If kind is numeric then is it an integer or a floating point number 6 Constants and Variables Constant A data element that never changes in your program 5 62 37 4 219E 6 record string i j 7 which one is the constant first letter a which one is the constant Variable A data element that can take on different values Its name represents a location address in memory i j 7 which are variables second letter b which is the variable Values are assigned using a single equal sign Read the statement i j 7 NOTE Variables in C must be declared before they can be used 7 Declaring Variables and Data Types Variables must be declared before they can be used Gives info to the compiler about How many bytes of memory to set aside How to interpret the bytes data Example int my var an integer variable my var float sensor1 a float variable sensor1 char Letter01 a a char variable Letter01 a Two parts are needed in the declaration Data type designator e g int note a reserved word Identifier i e a name my var must be a valid identifier 8 Variable Names See the handout Notes on Variable Names Sequence of lower and or upper case letters digits and underscores Case sensitive Initial character may not be a digit May not use reserved words as identifiers Keep length to 31 or fewer characters Make the first 8 characters unique Avoid names used by run time libraries e g log which is used in math h Avoid using names beginning with underscores Try to use lowercase letters for variable names and UPPER CASE letters for macro names e g define PI 3 14159 Choose names that are meaningful e g light level instead of just output 9 Variable Naming Practice Variable 1 bump switch 2 sum1 3 sum3 2 4 num var 5 NAME 6 register 7 1stname 8 lastname 2 9 overflow 10 signal Valid Comments 10 Data Types and Memory Recall that declaring a variable sets aside memory and indicates the type of data that will be stored Considerations in declarations What kind of data will you be working with Just whole numbers Fractions Characters Is speed important Integer operations are fastest How much memory is available Important for embedded systems and microcontrollers What is the range of the data From 0 to n or from n to n etc What kind of precision do you need Precision number of decimal places 11 Hierarchy of Data Types adapted from Darnell P A Margolis P E 1996 C a software engineering approach 3rd ed Springer New York Data Types void Scalar Types Aggregate Types arrays Pointers Arithmetic Types Integral Types unsigned signed char short int long long long structs unions enum Floating Types float double long double 12 Memory Allocation for Arithmetic Data Types See the handout Notes on Data Types Size bytes Name alternate Description char short int short int Range of Values AVR GCC MSVC Character or small integer 1 1 Signed 128 to 127 2 8 1 to 2 8 1 1 Unsigned 0 to 255 0 to 28 1 Signed 128 to 127 Unsigned 0 to 255 Short integer 2 2 Signed 32768 to 32767 Unsigned 0 to 65535 Signed 32 768 to 32 767 Unsigned 0 to 65535 4 Signed 32768 to 32767 Unsigned 0 to 65535 Signed 2 147 483 648 to 2 147 483 647 Unsigned 0 to 4 294 967 295 Signed 2 147 483 648 to 2 147 483 647 Unsigned 0 to 4 294 967 295 Integer 2 AVR GCC MSVC long int long Long integer 4 4 Signed 2 147 483 648 to 2 147 483 647 Unsigned 0 to 4 294 967 295 long long int long long Really long integer 8 8 Signed 9 2E 18 to 9 2E 18 Unsigned 0 to 1 8E 19 Signed 9 2E 18 to 9 2E 18 Unsigned 0 to 1 8E 19 float Single precision floating point number 4 4 1E 38 7 decimal digits of precision 1E 38 7 decimal digits of precision double Double precision floating point number 4 8 1E 38 7 decimal digits of precision 1E 308 15 decimal digits of precision long double Long double precision floating point number 8 1E 308 15 decimal digits of precision 13 Declaration Practice Declare the following variables A character variable named PORTA that will hold only non negative numbers A variable named up down that will hold whole numbers ranging from 20 000 to 20 000 A variable named heat that will hold values from 20 0 to 350 0 A variable named max read that needs 8 decimal digits of precision For each variable declared comment on the number of bytes of memory needed and the data range it will cover 14 Operators Operator a symbol or combination of symbols used to combine variables and constants to produce a value Overland B 1995 C in plain English MIS Press New York The variables and constants that get combined are called operands How the combining is carried out depends on the precedence and associativity of the operator Example identify the operator s operands and results on each line int i j 3 i j 7 15 Operator Precedence and Associativity All operators have the properties of precedence and associativity Precedence has to do with which operations take priority in groupings …
View Full Document