Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Key Concepts:  Data types in C.  What is a variable?  Variable Declaration  Variable Initialization  Printf()  Scanf()  Working with numbers in.

Similar presentations


Presentation on theme: "1 Key Concepts:  Data types in C.  What is a variable?  Variable Declaration  Variable Initialization  Printf()  Scanf()  Working with numbers in."— Presentation transcript:

1 1 Key Concepts:  Data types in C.  What is a variable?  Variable Declaration  Variable Initialization  Printf()  Scanf()  Working with numbers in C

2 2 Types of data:  Numeric: Integer = whole numbers Floating point = real numbers  Non-numeric: Character: ‘a’ String: “anything between double quotes”

3 3 Data Types for numeric data:  Whole Numbers: char 1 byte -128.. 127 short 2 Bytes -32,768.. 32,767 int 2 bytes long 4 bytes -2 billion.. 2 billion  Real numbers: float 4 bytes (7 digits after.) double 8 bytes (15 digits after.) The size/range of each data type depends on your computer.

4 4 Cont…  There is no data type for strings (textual data) in C.  Char data type can be used to store: Single character, ex: ‘*’ A small integer: 44

5 5 What is a variable?  Is a place holder in the computer’s memory (RAM).  Is used to store data values in the computer’s memory, to be used later on in the program.  The value stored in a variable can be changed.

6 6 A variable is identified by its: 1. Name 2. Data type 3. Value 4. Scope, 5. Lifetime  To use a variable, you must declare it in the program.

7 7 Declaration:  Syntax: dataType variableName;  Ex: int age; float price;

8 8 What happens at declaration time?  Ex: Declare a variable to store age of the student: int age;  By this statement: 4 bytes gets allocated in computer’s RAM. The name age will be given to that address in memory.

9 9 Variable Name:  Can include letters, digits, and _  Cannot start with a digit,  Cannot be a keyword,  May not include space or other characters.  Ex: Legal names: a, apple, hours, net_pay  Ex: Illegal names: 2b, #n, first name, double

10 10 Naming guidelines in C:  Start with lower case letter: hours  If the name has multiple word, capitalize the consecutive words: firstName or use _ to separate the words: first_name C is case sensitive:  hours and Hours are different names!

11 11 Assignment Operator: =  Is used to assign a value to a variable. variableName = value;  Remember: Lvalue: The left hand side of = is a location in memory. Rvalue: The right hand side is a value  Ex: int age; age = 23;

12 12 Initialization: At declaration time: double hours = 35; After declaration: float wage; wage = 10.75;

13 13 How to display variables?  Use Format Specifications: %dto display an int variable %ld to display a long %fto display float %lf to display a double %e display floating point with e notation %E display floating point with E notation %c display single character %s display a string % display %

14 14 printf( ) function:  Is used to write to the standard output (screen).  Syntax: printf(“character string [and format specifiers]”, [variables or values]);  Header file required: #include  EX1: Simplest form: printf (“Hello World!”);

15 15 Display variables:  Ex2: Display the value stored in variable age to the screen: int age; age = 22; printf (“Your age is = %d”, age);  Ex3: Declare a variable to store the hourly wage of a worker. Initialize it to $9.50, and display it to the screen : float wage = 9.50; printf (“The wage is = %f dollars per hour”, wage);  See Computer Demo

16 16  Ex4: Do it during the lecture: Declare variables to store the hours a worker has worked and the hourly wage. Store 40, and 12.90 in hours and wage respectively. Display both figures with descriptive message to the screen.

17 17 scanf() function:  Is used to read data from the standard input (keyboard).  Like printf(), it is part of standard I/O library.  It requires: #include

18 18 Syntax: scanf(“format specifiers”, &variable_names);  Address Operator &: Specifies the address of the variable in memory.

19 19  Ex: Declare a variable to store user’s age: int age; Prompt the user to enter his/her age: printf (“ How old are you? “); Read / scan the entered value into the variable: scanf (“%d”, &age);

20 20 Working with numbers:  Ex: Given: float n1, n2; float sum, product, mean; n1 = 4; n2 = 12;  Add ( + ) n1, n2 and store the result in sum: sum = n1 + n2;  Multiply ( * ) n1, n2 and store the result in product: product = n1 * n2;  Compute the average of n1, n2, store the result in mean: mean = sum / 2;


Download ppt "1 Key Concepts:  Data types in C.  What is a variable?  Variable Declaration  Variable Initialization  Printf()  Scanf()  Working with numbers in."

Similar presentations


Ads by Google