1 / 19

Variables and Constants

FTSM. Variables and Constants. Knowledge: Understand the concept of storage location representation as identifiers Skill: Identify and define data types in C programs. Let’s recap… Which one is the preprocessor instruction? Which one is the main function?. Introduction.

valiant
Download Presentation

Variables and Constants

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. FTSM Variables and Constants Knowledge: Understand the concept of storage location representation as identifiers Skill: Identify and define data types in C programs Computer Science Department

  2. Let’s recap… Which one is the preprocessor instruction? Which one is the main function? Introduction Consider the following example: #include <stdio.h> void main() { printf(“Welcome to UKM\n”); } TK1913-C Programming2

  3. Preprocessor Instruction Still remember this diagram? Global Declaration Pengisytiharan globl void main (void) { } Pengisytiharan setempat Local Declaration Statement C Program Structure TK1913-C Programming3

  4. Identifiers Identifiers are: • Variable • Constant • Function • Others TK1913-C Programming4

  5. What are C standard identifiers? What are C reserved words? Identifiers Syntax Rules: • Consist of only letters, digits and underscores • Cannot begin with a digit • Cannot use C reserved words • Try not to use/redefine C standard identifiers TK1913-C Programming5

  6. Identifiers • C Reserved Word • A word that has special meaning in C • C Standard Identifier • A word having special meaning but may be redefined (but is not recommended!!) Examples of reserved word: int, void, double, return Examples of standard identifiers: printf, scanf TK1913-C Programming6

  7. Variable • A name associated with a memory cell whose value can change • Needs to be declared: variable_type variable_name; Example:int x; int entry_time, charge; TK1913-C Programming7

  8. Variable Types of variable: • Character: char • An individual character value – a letter, a digit or a symbol (e.g. ‘A’, ‘4’, ‘*’) • Integer: int • Whole numbers (e.g. +16, 568, -456) • Float: float • A real number which has a decimal point (e.g. 8.00, 3.1416) • High-level Float: double TK1913-C Programming8

  9. Variable Variable Declaration: Example 1: char letter; letter is a character-type variable Example 2: float matric; matric is a ??? variable TK1913-C Programming9

  10. Variable Example: Calculate and display the price of a number of apples if the quantity in kg and price per kg are given. • Input:quantity and price_per_kg • Output:price • Process:price = quantity * price_per_kg TK1913-C Programming10

  11. Why did we declare it as int? Why did we declare them as float? Variable Example: int quantity; float price_per_kg; float price; TK1913-C Programming11

  12. number1 number2 ? ? Variable - Value Assignment Example: int number1, number2; number1 = 25; number2 = 23; number1 = number2; … … 25 23 23 TK1913-C Programming12

  13. Variable - Value Assignment • Algorithm • variable  expression • Syntax • variable = expression; • Rules • Expression’s type must be the same as variable’s type Valid Example:Invalid Example: int x; int y; x = 12; y = 5.75; TK1913-C Programming13

  14. Variable - Value Assignment Example: int quantity; float price_per_kg, price; quantity = 5; price_per_kg = 4.50; price = quantity * price_per_kg; … How does this program work? TK1913-C Programming14

  15. quantity ? price_per_kg ? price ? Variable - Value Assignment Example: int quantity; float price_per_kg, price; quantity = 2; price_per_kg = 4.50; price = quantity * price_per_kg; … 2 4.50 9.00 TK1913-C Programming15

  16. Variable - Value Assignment Example: int number1, number2; number1 = 25; number2 = 23; number1 = number2; … … How does this program segment work? TK1913-C Programming16

  17. Constant • A value that will not change • Consists of: • Float(e.g. 2.3 2.73F 1.2e-5) • Integer (e.g. 3 67 -2) • Character(e.g. ‘z’ ‘3’ ‘$’ ‘\n’) • String (e.g. “UKM” “1” “5a”) TK1913-C Programming17

  18. Exercise To practice what you’ve learned, try exercises on page 83 (Latih Diri) from Pengaturcaraan C TK1913-C Programming18

  19. What’s next? …INPUT AND OUTPUT on the way … • If you want to excel: • revise chapter 4 • read chapter 5 & 6 for next week… • Otherwise, you may watch tv, sleep etc. as a preparation for next week classes. End of Lecture 4 TK1913-C Programming19

More Related