sends the formatted data to the standard output stream (stdout) int printf ( format_string, argument_1, argument_2,... ) ;

Similar documents
Unit 4. Input/Output Functions

BSM540 Basics of C Language

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

2. Numbers In, Numbers Out

2. Numbers In, Numbers Out

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

Object Oriented Programming Using C++ Mathematics & Computing IET, Katunayake

Reserved Words and Identifiers

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

C Program. Output. Hi everyone. #include <stdio.h> main () { printf ( Hi everyone\n ); }

Should you know scanf and printf?

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

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Introduction to Computing Lecture 03: Basic input / output operations

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Learning to Program with Haiku

UNIT-I Input/ Output functions and other library functions

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

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

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

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

ANSI C Programming Simple Programs

1/31/2018. Overview. The C Programming Language Part 2. Basic I/O. Basic I/O. Basic I/O. Conversion Characters. Input/Output Structures and Arrays

Arithmetic Expressions in C

Fundamentals of Programming

Fundamental of Programming (C)

THE FUNDAMENTAL DATA TYPES

INTRODUCTION TO C++ C FORMATTED INPUT/OUTPUT. Dept. of Electronic Engineering, NCHU. Original slides are from

Fundamentals of Programming. Lecture 11: C Characters and Strings

A Fast Review of C Essentials Part I

Chapter 3 Basic Data Types. Lecture 3 1

Fundamentals of Programming Session 8

Lecture 3. More About C

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

Lecture 4. Console input/output operations. 1. I/O functions for characters 2. I/O functions for strings 3. I/O operations with data formatting

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

Advanced C Programming Topics

Programming in C Quick Start! Biostatistics 615 Lecture 4

Functions. Systems Programming Concepts

Standard C Library Functions

ET156 Introduction to C Programming

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

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

Programming and Data Structure

Data Type Fall 2014 Jinkyu Jeong

Beginning C Programming for Engineers

C: How to Program. Week /Mar/05

CSC 1107: Structured Programming

C Programming

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

Lesson 3 Introduction to Programming in C

Input / Output Functions

BLM2031 Structured Programming. Zeyneb KURT

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

Chapter 2 - Introduction to C Programming

Stream Model of I/O. Basic I/O in C

BSM540 Basics of C Language

Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 2

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

Formatting functions in C Language

File Handling in C. EECS 2031 Fall October 27, 2014

CSC 1107: Structured Programming

C Tutorial: Part 1. Dr. Charalampos C. Tsimenidis. Newcastle University School of Electrical and Electronic Engineering.

Computers in Engineering. Moving From Fortran to C Part 2 Michael A. Hawker

Goals of C "" The Goals of C (cont.) "" Goals of this Lecture"" The Design of C: A Rational Reconstruction"

Pointers and scanf() Steven R. Bagley

Computers in Engineering COMP 208. Roots of a Quadratic in C 10/25/2007. Moving From Fortran to C Part 2 Michael A. Hawker

C Basics And Concepts Input And Output

Input/output functions

بسم اهلل الرمحن الرحيم

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

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

Multiple Choice Questions ( 1 mark)

Binghamton University. CS-211 Fall Input and Output. Streams and Stream IO

Fundamentals of Programming Session 4

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Standard File Pointers

Programming for Engineers Introduction to C

Chapter 7. Basic Types

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

Introduction to C Language

COMP 208 Computers in Engineering

The Design of C: A Rational Reconstruction (cont.)

Functions. Autumn Semester 2009 Programming and Data Structure 1. Courtsey: University of Pittsburgh-CSD-Khalifa

Engineering 12 - Spring, 1999

Computer Programming

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

Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. C Data Types

tablica: array: dane_liczbowe

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

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

211: Computer Architecture Summer 2016

Program Organization and Comments

Input/output functions

Binghamton University. CS-220 Spring Includes & Streams

Number Systems, Scalar Types, and Input and Output

Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below:

Intermediate Programming, Spring 2017*

Transcription:

INPUT AND OUTPUT IN C Function: printf() library: <stdio.h> sends the formatted data to the standard output stream (stdout) int printf ( format_string, argument_1, argument_2,... ) ; format_string it is a string constant (in quotes) containing: ordinary characters (which are simply copied to the screen) formatting codes of given arguments: %c a single character %s string (array of characters) %d signed decimal number %f floating-point number (decimal notation) %e floating-point (scientific notation: mantissa/exponent) %g floating-point (the shortest representation: %e or %f %) %u unsigned decimal integer %x unsigned hexadecimal integer %o unsigned octal integer h l L length subspecifiers (short int / long int / long double) int x = 10; long y = 20; double s; s = x + y; printf ( %s of calculation %d + %ld = %f, Result, x, y, s ); effect on the screen Result of calculation 10 + 20 = 30.000000 length codes (with and precision subspecifiers) can be added to determine the number of digits that are printed: %Xd %X.Xf e.g. %4d decimal number at 4 positions %10f floating (real) number at 10 positions %10.2f floating at 10 positions with 2 digits after decimal point %.3f floating-point number with 3 digits after the decimal point M.Piasecki: Introduction To Programming 1 (L2) standard input/output

Function: scanf() <stdio.h> reads the data from the standard input stream (stdin), according to the specified format and stores them under the specified memory addresses int scanf ( format_string, pointer_1, pointer_2,... ) ; format_string it is a string constant (in quotes) containing instructions how to treat the next data loading from stream (what types of variables are at addresses pointer_1, pointer_2...) Formatting codes, similar to the printf (): %c a single character %s array of characters - string %d signed integer %f lub %e floating-point number %u unsigned integer %x hexadecimal integer %o octal integer h l length suffix for: d u x o (short or long int) l length suffix for: f e (double) L length suffix for: f e (long double) & reference / address operator (returns the address of the variable) int x; double y; char letter; printf( Enter an integer number: ); scanf ( %d, &x ); printf( Specify the one real number and one character: ); scanf ( %lf %c, &y, &letter ); Printout Enter an integer number: Reading 123 Printout Specify the one real number and one character: Reading 456.789 a The result of reading: x == 123, y == 456.789, letter == a M.Piasecki: Introduction To Programming 2 (L2) standard input/output

BASIC INSTRUCTIONS OF C / C ++ grouping instruction - braces are used to group a number of declarations and statements in a compound statement (one block). int a = 10, b = 20 ; int a = 30 ; // 'override' the previous definition of variable a printf( A = %d, B = %d \n, a, b ); // printout: A=30, B=20 printf( A = %d, B = %d \n, a, b ); // printout: A=10, B=20... if( a > 0 ) printf( Enter the new value of A = ); scanf( %d, &a ); conditional statement (can be one of two forms): if (expression ) internal_instruction ; this instruction checks if the expression is true (a nonzero value) e.e.. if ( expression ) is equivalent to if ( expression!= 0 ) int number; printf( Specify any integer A = ); scanf( %d, &number ); if( number % 2 == 0) // if the remainder of the division by 2 is 0 printf( The number is even ); M.Piasecki: Introduction To Programming 3 (L2) Conditional statements

or complex form of conditional statement (with ): if ( expression ) instruction_1 ; instruction_2 ; char L; printf( Give any a capital letter L = ); scanf( %c, &L ); if( L >= A && L <= Z ) printf( \n\n Well done! It is a capital letter ); printf( \n\n Wrong! This is NOT a capital ); printf( \n\n Press ENTER to complete the program ); fflush(stdin); // clearing the stream buffer <stdin>, here the keyboard getchar( ); Further examples for the conditional statement: int A, B, C; printf( Enter the first number: ); scanf( %d, &A ); printf( Enter a second number: ); scanf( %d, &B ); printf( Enter a third number: ); scanf( %d, &C ); if( A > B && A > C ) printf( Maximum = %d, A ); if( B > A && B > C ) printf( Maximum = %d, B ); if( C > A && C > B ) printf( Maximum = %d, C ); // The maximum value of the three numbers printf( \n\n Press ENTER to complete the program ); fflush(stdin); getchar(); // What happens when two or three numbers are equal? M.Piasecki: Introduction To Programming 4 (L2) Conditional statements

Other versions of the same (maximum) program include <stdio.h> int main() // The maximum value of the three numbers int A, B, C; printf( Enter the first number: ); scanf( %d, &A ); printf( Enter a second number: ); scanf( %d, &B ); printf( Enter a third number: ); scanf( %d, &C ); if( A > B ) if( A > C ) printf( Maximum = %d, A ); printf( Maximum = %d, C ); if( B > C ) printf( Maximum = %d, B ); printf( Maximum = %d, C ); fflush(stdin); getchar(); // The maximum value of the three numbers int A, B, C, max; printf( Enter the first number: ); scanf( %d, &A ); printf( Enter a second number: ); scanf( %d, &B ); printf( Enter a third number: ); scanf( %d, &C ); max = A; if( max < B ) max = B; if( max < C ) max = C; printf( \n The maximum value = %d, max ); fflush(stdin); getchar(); M.Piasecki: Introduction To Programming 5 (L2) Conditional statements

/ Program solving quadratic / trinomial equation Ax 2 +Bx+C=0 as an illustration of nesting conditional statements / #define _USE_MATH_DEFINES // in <math.h> Microsoft library #include <math.h> // a mathematical function of the square root: sqrt double a, b, c, delta, x1, x2; printf( "Enter the first coefficient A= " ); scanf( "%lf", &a ); // Caution!!! %lf not a %f printf( " Enter the second B= " ); scanf( "%lf", &b ); printf( "Enter the third C= " ); scanf( "%lf", &c ); delta = b b 4 a c; if( delta < 0 ) printf( "\n No solutions" ); if( delta == 0 ) x1 = x2 = b/(2 a); printf( "There is one solution: x1=x2= %f", x1 ); x1 = ( b sqrt(delta)) / (2 a); x2 = ( b + sqrt(delta)) / (2 a); printf( "There are two solutions: x1= %.2f, x2= %.2f", x1, x2 ); printf( \n\n Press ENTER to complete the program ); fflush(stdin); M.Piasecki: Introduction To Programming 6 (L2) Conditional statements

Nested -if: Multiple selection: if ( condition_1 ) statement_1; if ( condition _2 ) statement _2; if ( condition _3 ) statement _3; default_statement_4; switch ( control_expression ) case value_1 : statement_1; case value_2 : case value_3 : case value_4 : statement _234; default : default_statement; int number; printf( " Specify an integer value A =" ); scanf( "%d", & number ); switch( number ) case 0 : printf( You have entered the zero ); case -5 : printf( You have entered minus five ); case 7 : printf( You have entered the number seven ); case 9 : printf( You have entered the number nine ); default: printf( " A number different than: 0, -5, 7, 9 " ); M.Piasecki: Introduction To Programming 7 (L2) Conditional statements