200 likes | 300 Vues
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’
E N D
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’ • String: “anything between double quotes”
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.
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
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.
A variable is identified by its: • Name • Data type • Value • Scope, • Lifetime • To use a variable, you must declare it in the program.
Declaration: • Syntax: dataType variableName; • Ex: int age; float price;
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.
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
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!
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;
Initialization: • At declaration time: double hours = 35; • After declaration: float wage; wage = 10.75;
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 %
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!”);
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
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.
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>
Syntax: scanf(“format specifiers”, &variable_names); • Address Operator &: • Specifies the address of the variable in memory.
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);
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;