Basic Input/Output and Variables Ethan Cerami New York

Slides:



Advertisements
Similar presentations
Computer Programming w/ Eng. Applications
Advertisements

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
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
CS1061 C Programming Lecture 5: Building Blocks of Simple Programs A. O’Riordan, 2004.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Introduction to Computers and Programming - Class 2 1 Introduction to Computers and Programming Class 2 Introduction to C Professor Avi Rosenfeld.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
1 Introduction to Computers and Programming Class 3 Introduction to C Professor Avi Rosenfeld.
Introduction to C Programming
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
Basic Input - Output. Output functions  printf() – is a library function that displays information on-screen. The statement can display a simple text.
COMPUTER SCIENCE I C++ INTRODUCTION
Chapter 2 Getting Started in C Programming
Chapter 2 Introduction to C Programming Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Agenda  Commenting  Inputting Data from Keyboard (scanf)  Arithmetic Operators  ( ) * / + - %  Order of Operations  Mixing Different Numeric Data.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Input, Output, and Processing
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Week 1 Algorithmization and Programming Languages.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
CECS 121 Test 1. Functions allow you to group program statements under one name C and C++ are case-sensitive so main(), MAIN(), and Main() are all different.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
Variables, Expressions and Statements
S CCS 200: I NTERACTIVE I NPUT Lect. Napat Amphaiphan.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
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 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
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.
SCP1103 Basic C Programming SEM1 2010/2011 Arithmetic Expressions Week 5.
Arithmetic Expressions
Chapter 1: Introduction to computers and C++ Programming
Chapter 2 - Introduction to C Programming
Programming Fundamental
Revision Lecture
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
INPUT & OUTPUT scanf & printf.
Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C Programming
EECE.2160 ECE Application Programming
Getting Started With Coding
Presentation transcript:

Basic Input/Output and Variables Ethan Cerami New York

This Week Quick Review Integer Variables Basic Input: scanf() Float Variables Mathematical Operators

Review: First Program in C /* First Program in C */ #include main() { printf("Hello, World!"); /* Wait for user to Press Enter */ getchar(); } Comments Include Library Files Main Function Pauses Program printf () Statement

printf () Escape Characters \ is an escape character  reserved for special characters such as new line and tab.  Examples:  \n insert a newline  \t tab  \a bell sound  \" double quote  other escape characters listed on page 26 of text.

III: Variables Variable: a small piece or “chunk” of data –Could be a number (5, PI), or someone's name or someone's address. –Could be your bank account balance. –Could be a list of the courses you have registered for this semester.

Data Types Every variable must have both a data type and a name. –Data type: defines the kind of data the variable can hold –Can this variable hold numbers? can it hold text? The “Bucket” Analogy

Integer Data Type Integer: data type that can hold only whole numbers, e.g. 5, 76, Cannot hold numbers with decimal points, such as 4.2 or Cannot hold strings, such as “Hello” or “Ethan” Example: int x = 5;

Integer Program (Program 2.0) #include main () { int x, y, z; x = 5; y = 10; z = x + y; printf ("x: %d\n", x); printf ("y: %d\n", y); printf ("z: %d\n", z); /* Wait for user to Press Enter */ getchar(); } Variable Declaration Data TypeVariable Names Assignment Statements

Printing Variables  To print integer variables:  format specifier: indicates the data type.  %d: print integer values  you must include both a format specifier and a variable name. Example: printf ("x: %d\n", x); Format Specifier Variable Name

Rules for Naming Variables Variable Names:  Can contain letters, digits, or underscores _  Cannot begin with a digit.  Examples of Valid Variable names  int variable1, variable_2;  Examples of Invalid Variable names:  int 1variable, variable#1  C is case sensitive, e.g. variable1  VARIABLE1

V. Basic Input: scanf() Input: any user supplied data. –Keyboard input, mouse input. scanf (): read in keyboard input.

scanf() Example (Program 1.2) #include main() { int integer1, integer2, sum; /* declaration */ printf("Enter first integer\n"); /* prompt */ scanf("%d", &integer1); /* read an integer */ printf("Enter second integer\n"); /* prompt */ scanf("%d", &integer2); /* read an integer */ sum = integer1 + integer2; /* assignment of sum */ printf("Sum is %d\n", sum); /* print sum */ /* Wait for user to Any Key */ getch(); return 0; /* indicate that program ended successfully */ } Variable Declaration

Using scanf()  scanf ("%d", &integer1);  %d indicates integer values (just like printf) Format Specifier Variable Name & Character is required

Other tid-bits getch();  Waits for user to press any key.  getch() requires the #include library  : Console Input/Output  similar to getchar(), but getchar() requires that the user press the ENTER key.  return 0;  Returns the value 0 to the operating system.  Tells the operating system that everything went well.  Anything other than 0 indicates an error.

VI. Mathematical Operators Basic Mathematical Operators + -Addition / Subtraction * Multiplication /Integer Division % Modulus Division

Integer Division - The Problem Suppose you have the following code: Using a calculator, the answer is But x can only hold integer values is clearly not an integer value. int x; x = 7 / 4;

Integer Division - The Solution To understand the solution, you need to remember your 3 rd Grade Math (really.) 7/4 = 1 (Integer Division) 7%4 = 3 (Modulus Division) The answer: 1 remainder 3

Integer/Modulus Division (Program 2.1) /* Integer and Modulus Division */ #include main () { int x = 5, y =10; printf ("5/10: %d\n", x/y); printf ("5%10: %d\n", x%y); getchar(); } Note: % will print a single % 5/10: 0 5%10: 5 Output

Modulus Division (cont) Second Example: No matter what, you answers must be integers. 5/10 = 0 5%10 =

Operator Precedence Here’s another problem. What’s the answer to this: x = * 6; Two Options (depending on the order of operations): Perform addition first: = 10  10 * 6 = 60 Perform multiplication first: 3*6 =18  7+18 = 25 Which option is correct? Cleary, we cannot have this kind of ambiguity.

Operator Precedence Rules for evaluating mathematical expressions. From left to right: –Parentheses are always evaluated first. –Multiplication, division and modulus are evaluated next. –Addition and subtraction are evaluated last.

Operator Precedence Hence, option #2 is correct: To find a student’s grade average, what’s wrong with this? When in doubt, use parentheses. x = * 6; Evaluates to x = = 25 avg = / 4

Float Variables Float Data Type: Data type that can hold numbers with decimal values, e.g. 5.14, 3.14.

Float Example (Program 2.2) /* Float Example Program */ #include main () { float var1, var2, var3, sum; var1 = 87.25; var2 = 92.50; var3 = 96.75; sum = var1 + var2 + var3; printf ("Sum: %.2f", sum); getchar(); } %f: indicates floating values %.2f displays a floating point value with 2 decimal points. Output: Sum: