Use of scanf. scanf("%d", &number);

Similar documents
Some Computer Preliminaries

printf( Please enter another number: ); scanf( %d, &num2);

Arithmetic Expressions in C

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

AN OVERVIEW OF C. CSE 130: Introduction to Programming in C Stony Brook University

Reserved Words and Identifiers

Lecture 6. Statements

COMP 202 Java in one week

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

Syntax and Variables

These are reserved words of the C language. For example int, float, if, else, for, while etc.

CpSc 1111 Lab 4 Formatting and Flow Control

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Full file at

C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach

CpSc 1111 Lab 5 Formatting and Flow Control

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

Programming with C++ as a Second Language

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

UNIT- 3 Introduction to C++

Introduction. C provides two styles of flow control:

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

Objectives. In this chapter, you will:

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

CS110: PROGRAMMING LANGUAGE I

Technical Questions. Q 1) What are the key features in C programming language?

Pace University. Fundamental Concepts of CS121 1

2. Numbers In, Numbers Out

CSCI 1061U Programming Workshop 2. C++ Basics

DEPARTMENT OF MATHS, MJ COLLEGE

First Java Program - Output to the Screen

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Fundamental of Programming (C)

2. Numbers In, Numbers Out

Computer System and programming in C

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

YOLOP Language Reference Manual

CSC 467 Lecture 3: Regular Expressions

4 Programming Fundamentals. Introduction to Programming 1 1

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

Data types, variables, constants

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

Define a method vs. calling a method. Chapter Goals. Contents 1/21/13

Chapter 2. Lexical Elements & Operators

DECISION MAKING STATEMENTS

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

BTE2313. Chapter 2: Introduction to C++ Programming

Lecture 3 Tao Wang 1

Data Types and Variables in C language

Program Organization and Comments

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

Programming for Engineers Introduction to C

Fundamentals of C Programming

Fundamentals of Programming Session 4

CS 302: Introduction to Programming

C++ Programming: From Problem Analysis to Program Design, Third Edition

Differentiate Between Keywords and Identifiers

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

CS16 Exam #1 7/17/ Minutes 100 Points total

Chapter 2 Basic Elements of C++

Crude Video Game Simulator Algorithm

Full file at C How to Program, 6/e Multiple Choice Test Bank

Chapter 2 - Introduction to C Programming

Full file at

C Programming Basics

Fundamentals of Programming. Lecture 3: Introduction to C Programming

CpSc 1011 Lab 3 Integer Variables, Mathematical Operations, & Redirection

CpSc 111 Lab 3 Integer Variables, Mathematical Operations, & Redirection

Chapter 1. C++ Basics. Copyright 2010 Pearson Addison-Wesley. All rights reserved

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

ME 172. Sourav Saha. Md. Mahamudul Hossain Kazi Fazle Rabbi Saddam Hossain Joy Kamruzzaman Lecturer,Dept. of ME,BUET

CS313D: ADVANCED PROGRAMMING LANGUAGE

C Introduction Lesson Outline

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

This watermark does not appear in the registered version - Slide 1

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

CS125 : Introduction to Computer Science. Lecture Notes #4 Type Checking, Input/Output, and Programming Style

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

CpSc 1011 Lab 4 Formatting and Flow Control Windchill Temps

Chapter 2: Overview of C. Problem Solving & Program Design in C

Programming with Java

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

CSI33 Data Structures

C: How to Program. Week /Mar/05

Numbers In, Numbers Out

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Key Differences Between Python and Java

CS : Programming for Non-majors, Summer 2007 Programming Project #2: Census Due by 12:00pm (noon) Wednesday June

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

1/25/2018. ECE 220: Computer Systems & Programming. Write Output Using printf. Use Backslash to Include Special ASCII Characters

1 Lexical Considerations

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

CpSc 111 Lab 5 Conditional Statements, Loops, the Math Library, and Redirecting Input

Transcription:

Use of scanf We have now discussed how to print out formatted information to the screen, but this isn't nearly as useful unless we can read in information from the user. (This is one way we can make a program more useful!) When you read in information from the keyboard, you MUST read it into a variable. In particular, you must read it into the address of the variable. (This is where the variable is stored in memory.) In order to do this, you must specify the type of information being read in, and the name of the variable you want the information read into. For example, the following line would read in an integer into the variable number (assuming it is already declared): scanf("%d", &number); The & in front of number means "address of." This simply says to store whatever the user enters into the memory address where number is stored. Leaving out the & will cause your program to work incorrectly! You may also read in multiple pieces of information at once. The following line reads in the month and day of birth into the integer variables month and day, (assuming they are already declared.) scanf("%d%d", &month, &day); When the user enters this information, they must separate what they enter with a space.

Let's use the added feature of being able to read in information from the user to edit our second program: /* Arup Guha My Second C Program, edited 9/2/03 Computes the number of feet user ran. */ #include <stdio.h> #define YARDS_IN_MILE 1760 #define FEET_IN_YARD 3 int main(void) { int feet_in_mile, num_miles; feet_in_mile = YARDS_IN_MILE*FEET_IN_YARD; printf("how many miles did you run?\n"); scanf("%d", &num_miles); printf("you ran %d feet.\n", feet_in_mile*num_miles); } return 0; This program runs and works as before computing the variable feet_in_mile. The picture after this second line of code looks like: feet_in_mile num_miles 5280 Thus, num_miles is currently uninitialized.

Now, the user will be prompted with a message to enter the number of miles they ran. The scanf statement will WAIT until the user enters some information and hits enter. After this is done, the value entered by the user will be stored in the variable num_miles. Note: If the user doesn't enter the proper type of information, then the behavior of the scanf may not be as intended. A good exercise would be to try running a program that is expecting an integer input and actually enter a floating point number or a string and see what happens. Let's say that the user entered 3, now our picture looks like: feet_in_mile num_miles 5280 3 In the final printf statement, an integer is printed out. Note that even though we don't have a variable to store the total number of feet the user ran, we can STILL display that information to the screen! In particular, anytime we print a quantity, we simply have to place the proper arithmetic expression that evaluates to that quantity in the printf list in the appropriate place. The final output for this run of the program will be: You ran 15840 feet.

Programming Style Programming style refers to how you lay out your code. Although we haven't seen too many C constructs, it is instructive to list a few of the common elements of programming style: 1) Include ample white space in your code so it is easy to read. 2) Indent all code in between corresponding curly braces {} 3) Use a header comment that specifies the author's name, the date, and a brief description of the program. 4) Comment each major block of code with a quick description of the function of the block of code. 5) Give your variables meaningful names so that it is easy to determine what information they are intended to store. 6) Use constants (#define) when appropriate and have them as all capital letters. 7) Be consistent with your style.

Common Programming Errors 1) Using the statements we have discussed so far, probably the most common error is: Unterminated string or character constant This means that you have forgotten a close quote to match an open quote. 2) Another very common error for a beginning programmer is forgetting the semicolon at the end of a statement. This seemingly easy error produces a surprisingly varied set of compiler error messages. The reason for this is that the compiler thinks that the current statement is continuing. 3) The proper character code for a double is %lf. But, printf will work for a double with the character code %f. This is tricky because scanf will not always work properly for reading in a double if the character code %f is used instead of %lf. 4) Forgetting the & when reading in a value into a variable. The compiler doesn't catch this error, and instead a run time error is often produced.

Types of Errors 1) Compile-Time Error: This is when your program doesn't use the proper syntax and does not compile. 2) Run-Time Error: Your program compiles, but when it runs, in certain cases it terminates abruptly. An example of this would be a program that attempted to divide by zero while running. 3) Logical Error: Your program compiles and executes without abnormal termination, but does not always produce the desired results. Notes about White Space and Compilation You may have noticed that the compiler sometime ignores white space and that it is important other times. In order to understand when white space makes a difference in the meaning of a program, we have to understand how the compiler breaks a program into tokens. A token is a single element in a program. Here are examples of tokens: variable names keywords curly braces string literal operators & Each of these are naturally separated out when read in. White space is needed between tokens when no separator (like a comma), is between them.

For example, when defining variables, it is okay to omit spaces: int num,val; The comma is a delimiter that signifies the end of a token. This means that a comma can not be part of a variable name. Since the & is a token on its own, we can choose to separate it with white space or not. Both of the following are valid: scanf("%d",&num); scanf("%d", & num); Since operators are tokens, no white space is necessary when other tokens are delimited by these. Thus, both of the following are valid: val=num+1; val = num + 1; Finally, as you might imagine, you can not insert white space within a variable name. Thus, the following would not be valid: v a l = num + 1;

Rules for Identifiers (variable names) 1) Must be comprised of letters, digits and/or underscores. 2) A letter or underscore must be the first character. 3) In most implementations, variable names ARE case sensitive. 4) Must not be a keyword. A keyword is a word reserved in the C language with special meaning. Here are some examples of key words: main, char, int, float, return, and switch. 5) Although this isn't a rule, good programming practice is to choose variable names that specify the function of the variable. Use of Constants Constants (using #define) should be used instead of typing out the literal value for a couple reasons: 1) A symbolic name (such as WATER_DENSITY) has more meaning than its value (which in the case above is 1.) 2) If for any reason you need to change the value of the constant, you only need to change it in one place. So why not just use a variable? 1) To prevent you from changing the value of it during the execution of the program. 2) To distinguish between what values may change in a program and which ones do not.

String and Character Constants A string constant is denoted text inside of double quotes. For now, we have yet to use String variables. String variables are nothing but the concatenation of several char characters. A character constant is a single character inside of a single quote. This is different than a character variable, which must have a valid identifier name. Increment and Decrement Operators The assignment statements of the form: x = x + 1; x = x - 1; are so common that they have the following shorthand notations: x++; or ++x; x--; or --x; When these statements are on a line by themselves, then the first two are equivalent as are the last two. However, in other situations, there is a slight difference between x++ and x--. These will be discussed at a later time. Also note, that even though in mathematics, x = x+1 is a false statement, in C, it's perfectly logical. It takes the value of the variable x and adds 1 to it. When we discuss loops, you will see statements of this form often.

Other Shorthand Assignments We can also take an assignment statement of the form: <var> = <var> <op> <expr> and shorten it to: <var> <op>= <expr> Here are a few examples: x = x + 10; becomes x += 10; x = x - 15; becomes x -= 15; x = x*(3+y); becomes x *= (3+y); x = x/(p*q); becomes x /= (p*q); You are not required to use these shorthand notations in your code, but you should be able to trace through code that uses it. Note about the Assignment Operator We have already discussed how the assignment operator works, however, there are two pieces of information we didn't discuss about the assignment operator: An assignment statement evaluates to the value of the righthand side of the assignment. The associativity of the assignment operator goes right to left, not left to right. These two facts make the following statement possible:

int x = 3; int y = 7; int z = x = (y+2); The last statement is actually grouped as follows: int z = (x = (y+2)); First we evaluate the expression y+2, which is 9. Then, this is the value that x will get assigned to. When x gets assigned to 9, the whole expression (x = (y+2)) also evaluates to 9. Finally, z gets assigned to this value as well. Also, keep in mind that the assignment operator is NOT like an equal sign in mathematics. While in math it is okay to say x+2 = 9, in C, this does not make a valid assignment statement.