440 likes | 707 Views
C Language. Variables, Data Types and Arithmetic Expressions. Topics. identifiers data types constants, variables expressions, statements operators conversions printf( ) formats. C Identifiers. Programmer's name for some C element, such as a variable or function
E N D
C Language Variables, Data Types and Arithmetic Expressions
Topics • identifiers • data types • constants, variables • expressions, statements • operators • conversions • printf( ) formats
C Identifiers • Programmer's name for some C element, such as a variable or function • Consists of letters, digits or underscore; • must start with letter or underscore ( letter recommended) • Case-sensitive • Number of significant characters varies • Reserved identifiers or keywords, e.g. int, long
Identifiers • Use meaningful names! • I will take off points for poor names • Your friends will hurt you if you use bad names • Projects often define naming conventions • It helps people to read someone else’s code
sum Sum total_number student10 totalNumber total$s square root 7eleven int C Identifiers Valid: Invalid:
C Data Types • Whole numbers or integers (no decimal) • Real numbers or floating point (decimal) • Characters (ascii representation)
Integer Data Types • Whole numbers or integers (0, 12, -128) • Stored in binary number format • int keyword • Usage: int age; int total;
Integer Data Typesshort or long • int can have short or longmodifier (usually write longrather thanlongint) • short / long modifes the capacity of an int (sizes are hardware dependent)
Floating Point Data Types • Real numbers with a decimal and/or exponent (1.5 or 2.67e-3) • Stored in floating point format (mantissa, exponent and sign bit) • Single precision uses float keyword • Double precision uses double keyword (use double, not float) • long modifier can be applied to double
Character Data Type • ASCII character set (A-Za-z0-9, etc.) • Stored in binary number format • Uses char keyword • char is a single byte in size • Also used for small integers Do not use char for integers (unless you have a real good reason)
Examples int year; long int USDeficit; long USDeficit char middleInitial; float average; double standardDeviation; long double atomicSize;
C Constants • Representation of a number, character or group of characters (i.e. A string) • A constant has a value and a type • Integer, floating point, character, string
Integer Constants • Normally, just a number without a decimal point. • Can also have numbers in different bases • Hexadecimal, starts with 0x, digits are 0 – 9, a - f (e.g. 0x5e, 0xff, 0X1234) • Octal, starts with 0, digits are 0 - 7 (e.g. 017)
Floating Point Constants • Use decimal and/or exponential notation, digits are 0 – 9 with e and + or – • e.g. • 1.5, • 6.22e-2, • 3e1, • 2e+9
Character Constants • Enclosed in single quotes (e.g. ‘A’) • Alphanumerics (e.g. ‘A’, ‘7’) and special characters (e.g. ‘$’, ‘%’)
String Constants • Contiguous group of characters • Enclosed in double quotes (e.g. “How did I get here?”) • Null terminated (‘\0’ char is at the end)
Variables • Have a name, type, and value • Type name; e.g. int sum; • Type name = initialValue; e.g. int sum = 0; • Type name1, name2; e.g. int sum1, sum2;
Basic Program Structure // name of function // what it does // how it does it #include <stdio.h> main() { // declarations go at top of block int year = 2008; // working year int sum = 0; // sum of population // get input // do calculations // print or save results }
C Expressions • Chunks of code, building blocks of statements • Evaluate to a type and value • Examples: x x + y (x + y * z) sum = x + y
C Statements • An expression followed by a semicolon - ; • Building blocks of functions • {} – compound statement or block; the only statement not requiring a terminating ;
C Operators • Assignment (=) and arithmetic operators (+-*/%) • Use parentheses () for more control of the grouping Always use parentheses Do not assume precedence / associativity.
Arithmetic Operators Binary • Addition + (a + b) • Subtraction - (a - b) • Multiplication * (a * b) • Division / (a / b) • modulus % (a % b) • * / % have higher precedencethan + - and all associate left-to-right
Arithmetic Operators Unary • Plus + (+ a) equivalent to (0 + a) • Minus - (- a) equivalent to (0 – a)
Precedence of Operators • Defines which operators take effect first • Consult the operator precedence table • a * b + c is equivalent to (a * b) + c instead of a * (b + c)
Associativity of Operators • Defines how operations group when the operators have equal precedence • Consult the operator precedence table • a - b + c is equivalent to (a - b) + c instead of a - (b + c)
Grouping Parentheses • can use parentheses ( ) to force the order of evaluation • a * (b + c) instead of a * b + c • a – (b + c) instead of a – b + c
Again: Always use parentheses.
Conversions • Many conversion rules exist, try to remember the basic ones and look up the rest • Narrowing conversion, assigning a float to an int causes truncation • Widening conversion, assigning an int to a float works okay
Conversions • If assigning a value of one type to a variable of another type • Be explicit in what you want to have happen • typecast the expression • e.g.int someInteger;double realNumber;realNumber = 5.5;someInteger = (int)realNumber;realNumber = (double)someInteger;
printf() • Formatted output to standard output • printf(controlstring, variable argument list) • The control string can be simple text or can be embedded with conversion specifications (these begin with a % and end with a conversion character) • The variable argument list is a comma separated list of expressions, and each argument must correspond to one conversion specification in the control string
printf() Examples • printf(“This is simple text for output”) • printf(“My bowling average is %d”, average) • printf(“The date is %i %i %i”, 2, 5, 2003) • printf(“The answer is %f”, somefloatvalue)
printf() Conversion Characters • d or i – integer in decimal • f – floating point • c – single character • s – character string • consult C Standard Library documentation for details(or see Kochen p. 33, 364)
C Style // poor C style #include <stdio.h> main() { int sum = 5; printf(“Sum = %d\n”,sum); } // good C style #include <stdio.h> main() { int sum = 5; printf(“Sum = %d\n”, sum); }
WJK Programming Maxims • A good program starts with being a readable program • Start with a good structure • Have meaningful variable names • Have meaningful comments • Do not assume the compiler does something for you – be explicit in what you want • Do not get too clever