1 / 34

C Language

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

hans
Download Presentation

C Language

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C Language Variables, Data Types and Arithmetic Expressions

  2. Topics • identifiers • data types • constants, variables • expressions, statements • operators • conversions • printf( ) formats

  3. 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

  4. 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

  5. sum Sum total_number student10 totalNumber total$s square root 7eleven int C Identifiers Valid: Invalid:

  6. C Data Types • Whole numbers or integers (no decimal) • Real numbers or floating point (decimal) • Characters (ascii representation)

  7. Integer Data Types • Whole numbers or integers (0, 12, -128) • Stored in binary number format • int keyword • Usage: int age; int total;

  8. 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)

  9. 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

  10. 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)

  11. Examples int year; long int USDeficit; long USDeficit char middleInitial; float average; double standardDeviation; long double atomicSize;

  12. 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

  13. 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)

  14. 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

  15. Character Constants • Enclosed in single quotes (e.g. ‘A’) • Alphanumerics (e.g. ‘A’, ‘7’) and special characters (e.g. ‘$’, ‘%’)

  16. 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)

  17. 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;

  18. 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 }

  19. 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

  20. C Statements • An expression followed by a semicolon - ; • Building blocks of functions • {} – compound statement or block; the only statement not requiring a terminating ;

  21. C Operators • Assignment (=) and arithmetic operators (+-*/%) • Use parentheses () for more control of the grouping Always use parentheses Do not assume precedence / associativity.

  22. 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

  23. Arithmetic Operators Unary • Plus + (+ a) equivalent to (0 + a) • Minus - (- a) equivalent to (0 – a)

  24. 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)

  25. 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)

  26. 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

  27. Again: Always use parentheses.

  28. 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

  29. 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;

  30. 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

  31. 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)

  32. 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)

  33. 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); }

  34. 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

More Related