Introduction to C Programming Chapter 2 : Data Input, Processing and Output.

Slides:



Advertisements
Similar presentations
Dale Roberts Basic I/O – printf() CSCI 230 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Department of.
Advertisements

C programming Lec-2 Data type, variables and expression
Lecture 2 Introduction to C Programming
Introduction to C Programming
 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
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 1: Types, Operators and Expressions.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Data types and variables
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
C programming an Introduction. Types There are only a few basic data types in C. char a character int an integer, in the range -32,767 to 32,767 long.
Chapter 2 Data Types, Declarations, and Displays
JavaScript, Third Edition
Introduction to C Programming
Basic Input/Output and Variables Ethan Cerami New York
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations System-oriented Programming, B. Hirsbrunner,
C PROGRAMMING LECTURE 17 th August IIT Kanpur C Course, Programming club, Fall by Deepak Majeti M-Tech CSE
Introduction to programming Language C, Data Types, Variables, Constants. Basics of C –Every program consists of one or more functions and must have main.
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Objectives You should be able to describe: Data Types
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.
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is a legal identifier? what.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
© 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.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Chapter 2 Simple Data Types By C. Shing ITEC Dept Radford University.
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Chapter-4 Managing input and Output operation.  Reading, processing and writing of data are three essential functions of a computer program.  Most programs.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 1.
Module B - Computation1/61 Module-B-Computation Variables Basic Memory Operations Expressions.
© 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?
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
Programming Fundamentals. Summary of Previous Lectures Phases of C++ Environment Data Types cin and cout.
C Building Block Chapter 2. Variables A variable is a space in the computer’s memory set aside for a certain kind of data and given a name for easy reference.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
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.
© Copyright by Deitel 1 Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple.
Chap. 2. Types, Operators, and Expressions
Chapter 2 - Introduction to C Programming
Tokens in C Keywords Identifiers Constants
INC 161 , CPE 100 Computer Programming
Getting Started with C.
Chapter 2 - Introduction to C Programming
Operators and Expressions
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
A First Book of ANSI C Fourth Edition
Lectures on Numerical Methods
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Chapter 2 - Introduction to C Programming
DATA TYPES There are four basic data types associated with variables:
Introduction to C Programming
Presentation transcript:

Introduction to C Programming Chapter 2 : Data Input, Processing and Output

Reading keyboard input To be useful, program must be able to read data from external source, e.g. User input from keyboard Database File Socket scanf library function reads user-typed input from the keyboard.

Scanf function Basic syntax scanf( format-specifier, &var1, &var2, etc.); Examples int a; scanf(“%d”,&a); double x; scanf(“%f”,&x); Blocks program until user enters input!

The printf Executable Statement Sends output to the screen which is the standard out device. General form printf(format descriptor, var1, var2, …); format descriptor is composed of Ordinary characters copied directly to output Conversion specification Causes conversion and printing of next argument to printf Each conversion specification begins with %

Printf() examples Easiest to start with some examples printf(“%s\n”, “hello world”); Translated: “print hello world as a string followed by a newline character” printf(“%d\t%d\n”, j, k); Translated: “print the value of the variable j as an integer followed by a tab followed by the value of the variable k as an integer followed by a new line.” printf(“%f : %f : %f\n”, x, y, z); English: “print the value of the floating point variable x, followed by a space, then a colon, then a space, etc.

More on format statements The format specifier in its simplest form is one of: %s Stringsequence of characters known as a String Not a fundamental datatype in C (really an array of char)Not a fundamental datatype in C (really an array of char) %d%d Decimal integer (ie base ten)Decimal integer (ie base ten) %f%f Floating pointFloating point Note that there are many other options. These are the most common, though, and are more than enough to get started. Note that there are many other options. These are the most common, though, and are more than enough to get started.

List of format specifiers 7 Both printf () and scanf () use the format specifiers example: printf(“%c”, ’a’); scanf(“%d”, &a); More format specifiers %c The character format specifier. %d The integer format specifier. %i The integer format specifier (same as %d). %f The floating-point format specifier. %o The unsigned octal format specifier. %s The string format specifier. %u The unsigned integer format specifier. %x The unsigned hexadecimal format specifier. % Outputs a percent sign.

Invisible characters Some special characters are not visible directly in the output stream. These all begin with an escape character (ie \); \n newline \t horizontal tab \a alert bell \v vertical tab

Example on scanf and printf #include // program reads and prints the same thing int main() { int number ; printf (“ Enter a Number: ”); scanf (“%d”, &number); printf (“Number is %d\n”, number); return 0; } Output : Enter a number: 4 Number is 4

Arithmetic Operations Five simple binary arithmetic operators + “plus”, example, c = a + b - “minus”, example, c = a - b * “times”, example, c = a * b / “divided by”, example, c = a/b % “modulus”, example, c = a % b What are the values of c in each case above if int a = 10, b = 2; float a = 10, b = 2; int a = 10; float b = 2; ??

More Arithmetic Operators 11 Prefix Increment : ++a example: int a=5; b=++a; // value of b=6; a=6; Postfix Increment: a++ example int a=5; b=a++; //value of b=5; a=6;

12 Modulus (remainder): % example: 12%5 = 2; Assignment by addition: += example: int a=4; a+=1; //(means a=a+1) value of a becomes 5 Can use -, /, *, % also More Arithmetic Operators

Relational Operators Four basic operators for comparison of values in C. These are typically called relational operators: > “greater than” < “less than” >= “greater than or equal to” <= “less than or equal to” For the declaration int a=1,b=2,c; what is the value of the following expressions? a > b; a =b;a<=b

Relational Operators, cont. Typically used with conditional expressions, e.g. if (a < 1) then … However, also completely valid expressions which evaluate to a result – either 1 (true) or 0 (false). int c, a=2, b=1; c = (a > b) What is the value of c?

Equality Operators C distinguished between relational and equality operators. This is mainly to clarify rules of order of precedence. Two equality operators == “is equal to” != “is not equal to” These follow all of the same rules for relational operators described on the previous slide.

Logical Operators Logical Operators are used to create compound expressions There are two logical operators in C || “logical or” A compound expression formed with || evaluates to 1 (true) if any one of its components is true && “logical and” A compound expression formed with && evaluates to true if all of its components are true

Logical Operators, cont. Logical operators, like relational operators, are typically used in conditional expressions if ( (a == 1) && (b < 3) || (c == 1) ) etc. However, these can also be used in regular expressions int a = 1, b = 2, c = 3, d; d = ( a > b ) || ( c == (b – 1) ); What is the value of d here?

18 Type Conversions The operands of a binary operator must have a the same type and the result is also of the same type. Integer division: c = (9 / 5)*(f - 32) The operands of the division are both int and hence the result also would be int. For correct results, one may write c = (9.0 / 5.0)*(f - 32) In case the two operands of a binary operator are different, but compatible, then they are converted to the same type by the compiler. The mechanism (set of rules) is called Automatic Type Casting. c = (9.0 / 5)*(f - 32) It is possible to force a conversion of an operand. This is called Explicit Type casting. c = ((float) 9 / 5)*(f - 32)

19 Automatic Type Casting char and short operands are converted to int Lower data types are converted to the higher data types and result is of higher type. The conversions between unsigned and signed types may not yield intuitive results. Example float f; double d; long l; int i; short s; d + f f will be converted to double i / s s will be converted to int l / i i is converted to long ; long result

20 Explicit Type Casting The general form of a type casting operator is (type-name) expression It is generally a good practice to use explicit casts than to rely on automatic type conversions. Example C = (float)9 / 5 * ( f – 32 ) float to int conversion causes truncation of fractional part double to float conversion causes rounding of digits long int to int causes dropping of the higher order bits.

Operator Precedence 21 Meaning of a + b * c ? is it a+(b*c) or (a+b)*c ? All operators have precedence over each other *, / have more precedence over +, -. If both *, / are used, associativity comes into picture. (more on this later) example : 5+4*3 = 5+12= 17.

Precedence Table 22 Highest on top (Postfix) (Prefix) * / % + - > & | && ||