1 / 20

Key Concepts:

Key Concepts:. Data types in C. What is a variable? Variable Declaration Variable Initialization Printf() Scanf() Working with numbers in C. Types of data:. Numeric: Integer = whole numbers Floating point = real numbers Non-numeric: Character: ‘a’

dean-hart
Télécharger la présentation

Key Concepts:

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. Key Concepts: • Data types in C. • What is a variable? • Variable Declaration • Variable Initialization • Printf() • Scanf() • Working with numbers in C

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

  3. Data Types for numeric data: • Whole Numbers: char1 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. 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. 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. A variable is identified by its: • Name • Data type • Value • Scope, • Lifetime • To use a variable, you must declare it in the program.

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

  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. 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. 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. 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. Initialization: • At declaration time: double hours = 35; • After declaration: float wage; wage = 10.75;

  13. How to display variables? • Use Format Specifications: • %d to display an int variable • %ld to display a long • %f to 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. 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 <stdio.h> • EX1: Simplest form: printf (“Hello World!”);

  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. 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. 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 <stdio.h>

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

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

More Related