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

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

Computer Programming w/ Eng. Applications
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
11-2 Identify the parts of the “main” function, which include Preprocessor Directives main function header main function body which includes Declaration.
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
1 Key Concepts:  Why C?  Life Cycle Of a C program,  What is a computer program?  A program statement?  Basic parts of a C program,  Printf() function?
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
1  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.
Basic Input/Output and Variables Ethan Cerami New York
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
Programming Variables. Named area in the computer memory, intended to contain values of a certain kind (integers, real numbers, characters etc.) They.
Ping Zhang 10/08/2010.  You can get data from the user (input) and display information to the user (output).  However, you must include the library.
Basic Input - Output. Output functions  printf() – is a library function that displays information on-screen. The statement can display a simple text.
Chapter 2 Getting Started in C Programming
Chapter 5: Data Input and Output Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
Chapter 2 : Overview of C By Suraya Alias. /*The classic HelloWorld */ #include int main(void) { printf(“Hello World!!"); return 0; }
Input & Output: Console
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi.
1 CS161 Introduction to Computer Science Topic #3.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Chapter 2 Variables.
S CCS 200: I NTERACTIVE I NPUT Lect. Napat Amphaiphan.
Module B - Computation1/61 Module-B-Computation Variables Basic Memory Operations Expressions.
CMPSC 121- Spring 2015 Lecture 6 January 23, 2015.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
1 09/03/04CS150 Introduction to Computer Science 1 What Data Do We Have.
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
NOTE: C programs consist of functions one of which must be main. C programs consist of functions one of which must be main. Every C program begins executing.
1 A more complex example Write a program that sums a sequence of integers and displays the result. Assume that the first integer read specifies the number.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
Tarik Booker CS 290 California State University, Los Angeles.
USER INTERACTION AND VARIABLES Tarik Booker CS 290 California State University, Los Angeles.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Bill Tucker Austin Community College COSC 1315
User Interaction and Variables
BASIC ELEMENTS OF A COMPUTER PROGRAM
Revision Lecture
Chapter 2 Overview of C.
ICS103 Programming in C Lecture 3: Introduction to C (2)
Variables, Expressions, and IO
IDENTIFIERS CSC 111.
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.
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
INPUT & OUTPUT scanf & printf.
C++ Data Types Data Type
Chapter 2: Java Fundamentals
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Introduction to C Programming
Variables in C Topics Naming Variables Declaring Variables
Getting Started With Coding
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: char 1 byte short 2 Bytes -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: 1. Name 2. Data type 3. Value 4. Scope, 5. 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: %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 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 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 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

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;