Presentation is loading. Please wait.

Presentation is loading. Please wait.

Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.

Similar presentations


Presentation on theme: "Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change."— Presentation transcript:

1 Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change values. All variables must be declared at the top of the program. There are three basic types of variables in C: int: for integer (whole numbers). double (or float): for real (floating point numbers). char: for characters.

2 Declaring Variables in ‘C’
All variables in ‘C’ must be declared. Compiler should know The variable name Type of the variable Why? In order to allocate enough memory to it , before the variable can be used. What are data types? char int, long float, double, long double void

3 How to declare? /*declare variable of type character.*/  This Denotes a comment in C char a_character; char letter; /*declare variable of type integer.*/ int an_integer; int number; /*declare variable of type float.*/ float floating_point_number; float average; The following is also valid: int age, number, mark;

4 Where to Declare? Declaration MUST happen at the top of the program, that is, the very first thing that we do, MUST be declaring the variables.

5 Identifiers All variables must have names. There are strict rules for variable names. These rules will apply to function names later so we will call these names identifiers. A declaration is done with the type followed by the identifier ;. Ex: int lifespan; double mass; char letter;

6 Hard rules for identifiers
Rule #1: An identifier must not be a reserved word. Reserved words are used by C exclusively. Here are a few: double, char, int, do, float, if, return, sizeof, void,while, typedef, struct, switch, for, else. See the complete list in the Documents section of the course website. Rule #2: An identifier must contain only letters, digits or underscores. Abc8 is valid, Abc-8 is not. _xyz is valid, atom number is not.

7 Hard rules for identifiers
Rule #3: An identifier must never begin with a digit. U238 and _765 are valid, 7abc and 67_q are not.

8 Soft rules for identifiers
Rule #4: An identifier should not be a standard identifier. A standard identifier is a name used by C but is not a reserved word. printf, scanf are examples of standard identifiers. Rule #5: All-capital names should be used only for constant macros. Variables and function should never use capital letters. Never mix upper-case and lower-case letters in a name.

9 How to select names for variables, functions, etc. in ‘C’?
Use the set of rules for picking up VALID names in C Use meaningful and descriptive names so we need less comments. Rules For Picking up Names 1. Names in ‘C’ are Case Sensitive, i.e., ‘C’ makes distinction between upper and lower-case letters. e.g., int number; int Number; /* number and Number are two different integer variables.*/ 2. User defined names cannot be the same as C keywords. i.e., names such as for, while, if are NOT valid user defined names. int for; /* invalid variable name. for is C keyword. */

10 Rules For Picking up Names
3. Every name must start with A letter of alphabet (upper, lower) Underscore ( _ ) e.g., int age; float Salary; double _code; 4. The remainder of the name may be Upper or lower case letters of alphabets Decimal digits (0-9) Underscore

11 What is a ‘C’ Program? A collection of keywords, variables, operators, expressions, statements, different data types, etc. that together performs one or more than one task. Therefore, we need to know the followings: C Keywords If For While C Different data types Integer  int Floating Point  float Character  char Void  void

12 Valid C Variables int month; float average; double PI; char name; C Operators Arithmetic Assignment and Compound Assignment Sizeof Relational Logical Conditional Increment/Decrement C Statements C Expressions, A valid sequence of operands and operators to calculate a value, usually expressing a math, logical calculation, …

13 ‘C’ Keywords Terms with pre-defined meaning in ‘C’. We must use the keywords within their intended meanings. Keyword definition cannot be changed.

14 Example: case used as part of switch
sizeof gives the size of the variable in byte for used for looping purposes do used as part of do-while char a data type double a data type long a data type return used to return a value from a function static used in combination with data types void a data type int a data type float a data type if used for conditional purposes else used a part of if switch used for selection purposes struct used to define a data structure enum used to define user defined data type while used for looping purposes

15 Numbers in C in a table int integer 2 -32,768 to +32,767 long
Data Type Purpose Bytes Range int integer 2 -32,768 to +32,767 long long integer 4 -2,147,483,648 to +2,147,483,647 unsigned unsigned integer 0 to 65,535 unsigned long unsigned long integer 0 to 4,294,967,295 float floating point 3.4E+/-38 double double float 8 1.7E+/-308 long double long double float 16 1.7E+/-4932

16 Are used to format the print out.
Escape Sequences Are used to format the print out. (\) symbol is referred to as the escape character AND is used to signify a escape sequence. Sequence Purpose \n New line \t Tab \" To print a double quote \\ To print a backslash

17 Note On the Use of Different Data Types
Use the data type that conserves memory and still accomplishes the desired purpose. For example, depending on your machine, you may need 1 byte for characters, 2 bytes for integers and 4 bytes for float. You also know that integers are in fact a subset of float & as such you may decide to use float type for all numerical values. Although this is mathematically correct however you would not have an efficient memory management here. You could have used 2-byte for integers but you used 4-byte instead!

18 Placeholders in I/O Statements
Placeholders (or conversion specifiers) will substitute the value of the variable inside the output string (printf). You must absolutely match the placeholder with the variable type. %lf: long floating point (double). %d: decimal (int). %c: character (char). \n: a special character meaning that a new line will be inserted.

19 Integer placeholders %d is the default integer placeholder. When used it will simply display the value as is without any padding. To add padding, to have columns for example, we need formatted placeholders. %nd will reserve n places to display the number. Justification will be to the right. The negative sign takes one place. If the value is 17 and %4d is used, then it will display 2 spaces followed by 17 on the screen. __17 These are spaces, not underscores.

20 Integer placeholders The number is always displayed in its entirety, even when the format is too narrow. With and a %3d placeholder, you would see -1234, therefore using 5 spaces instead of the 3 requested. A negative number change the justification to the left of the field. With a value of and a %-8d placeholder, you will get -1234___ . 3 trailing blanks

21 Double placeholders By default the %lf (or %f) placeholder displays the number with 6 decimal digits and no padding (may vary depending of computer system). The formatted double placeholder has this format: %w.dlf, where w is the total width of the field (including sign and decimal point) and d the number of decimal digits. If the value is 4.56 and the placeholder is %6.3lf then the display will be _ leading blank

22 Double placeholders With a double formatted, you always get the requested number of decimal digits (even if the field is not wide enough). You also always (like the integer placeholder) get all the significant numbers. However, if there are more decimal precision in the value than in the placeholder, the value is truncated and rounded-up if need be.

23 Double placeholders If the value is and the placeholder is %7.3lf, then you will get 3 decimal digits as requested plus the significant numbers: If the value is and the placeholder is %8.3lf, then you will get 3 decimal digits as requested plus the significant numbers and padding: _ See the rounding-up effect. A %8.7lf for a value of will produce a display of Note: The internal value is unaffected by the placeholder, only its screen appearance.

24 Double placeholders By default the %lf (or %f) placeholder displays the number with 6 decimal digits and no padding (may vary depending of computer system). The value is 5.87 and the placeholder is %6.3lf then the display will be b5.870 If the value is and the placeholder is %7.3lf ,then you will have 3 decimal digits and the display will be: If the value is and the placeholder is %8.3lf ,then you will have: b ROUNDING-UP effect. Also you could have TRUNCATE. A %8.7lf will produce: Note!!!!!! The internal value is UNAFFECTED by the placeholder, only its screen appearance.

25 PLEASE CHECK CLASS EXAMPLES FOR PLACEHOLDERS

26 Simple Assignment Operator (=)
Assign a value to a variable Does not mean equality, it means assignment Var1='a'; /* Var1  a */ Var2=15; /* Var2 15 */ Var3=27.62; /* Var3  */

27 Initializing Variable
Giving a value to the variable at the time that the variable is declared. char Var1='X'; int Var2=1095; float Var3= ;

28 Scanf() Another way to fill a variable is to ask the user for its value. We do that with the scanf statement. printf(“Enter your score:”); Scanf(“%d”,&score);


Download ppt "Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change."

Similar presentations


Ads by Google