Programming in C. Session 2. Seema Sirpal Delhi University Computer Centre

Similar documents
C Input/Output. Before we discuss I/O in C, let's review how C++ I/O works. int i; double x;

Programming in C. Session 8. Seema Sirpal Delhi University Computer Centre

Programming Fundamentals

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

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

CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program

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

Programming in C. Session 7. Seema Sirpal Delhi University Computer Centre

Input/Output Week 5:Lesson 16.1

BSM540 Basics of C Language

Unit 4. Input/Output Functions

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

BSM540 Basics of C Language

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

Console Input and Output

This code has a bug that allows a hacker to take control of its execution and run evilfunc().

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

Chapter 2. Section 2.5 while Loop. CS 50 Hathairat Rattanasook

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

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

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

2. Numbers In, Numbers Out

Fundamentals of Programming

Fundamental of Programming (C)

Lecture 12 CSE July Today we ll cover the things that you still don t know that you need to know in order to do the assignment.

are all acceptable. With the right compiler flags, Java/C++ style comments are also acceptable.

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

SU2017. LAB 1 (May 4/9) Introduction to C, Function Declaration vs. Definition, Basic I/O (scanf/printf, getchar/putchar)

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

Programming Fundamentals

CSC 1107: Structured Programming

LESSON 4. The DATA TYPE char

upper and lower case English letters: A-Z and a-z digits: 0-9 common punctuation symbols special non-printing characters: e.g newline and space.

8. Characters, Strings and Files

Introduction to Computing Lecture 03: Basic input / output operations

7/21/ FILE INPUT / OUTPUT. Dong-Chul Kim BioMeCIS UTA

CSC 1107: Structured Programming

Worksheet 4 Basic Input functions and Mathematical Operators

UNIT-I Input/ Output functions and other library functions

Fundamentals of Programming Session 8

C programming basics T3-1 -

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

C PROGRAMMING. Characters and Strings File Processing Exercise

File IO and command line input CSE 2451

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

2. Numbers In, Numbers Out

MODULE 3: Arrays, Functions and Strings

LSN 3 C Concepts for OS Programming

Input/output functions

Simple Output and Input. see chapter 7

Text Output and Input; Redirection

#include <stdio.h> int main() { char s[] = Hsjodi, *p; for (p = s + 5; p >= s; p--) --*p; puts(s); return 0;

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

Organization of a file

Should you know scanf and printf?

Chapter 8: Character & String. In this chapter, you ll learn about;

Input/output functions

At the end of this module, the student should be able to:

Unit 7. Functions. Need of User Defined Functions

Fundamentals of Programming Session 4

Course Outline Introduction to C-Programming

EECS2031 Software Tools

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 7

Fundamentals of Programming. Lecture 11: C Characters and Strings

Computers Programming Course 5. Iulian Năstac

25.2 Opening and Closing a File

HISTORY OF C LANGUAGE. Facts about C. Why Use C?

The C language. Introductory course #1

CSE101-Lec#17. Arrays. (Arrays and Functions) Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming

1. The programming language C is more than 30 years old. True or False? (Circle your choice.)

Standard File Pointers

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

Array Initialization

CSCE150A. Introduction. Basics. String Library. Substrings. Line Scanning. Sorting. Command Line Arguments. Misc CSCE150A. Introduction.

Compiling and Running a C Program in Unix

C-LANGUAGE CURRICULAM

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 9. Strings. Notes. Notes. Notes. Lecture 07 - Strings

BİL200 TUTORIAL-EXERCISES Objective:

Intermediate Programming, Spring 2017*

Pointers cause EVERYBODY problems at some time or another. char x[10] or char y[8][10] or char z[9][9][9] etc.

A Fast Review of C Essentials Part I

THE FUNDAMENTAL DATA TYPES

Characters and Strings

Day14 A. Young W. Lim Tue. Young W. Lim Day14 A Tue 1 / 15

Hacking C Code - Local Machine

Chapter 5, Standard I/O. Not UNIX... C standard (library) Why? UNIX programmed in C stdio is very UNIX based

Programming & Data Structure

C Basics And Concepts Input And Output

Standard I/O in C and C++

Dept. of CSE, IIT KGP

Outline. Computer Programming. Preprocessing in C. File inclusion. Preprocessing in C

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2

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

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

Computer Programming Unit v

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

b. array s first element address c. base address of an array d. all elements of an array e. both b and c 9. An array elements are always stored in a.

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

Course organization. Course introduction ( Week 1)

EL2310 Scientific Programming

Transcription:

Programming in C Session 2 Seema Sirpal Delhi University Computer Centre

Input & Output Input and Output form an important part of any program. To do anything useful your program needs to be able to accept input data and report back your results. In C, the standard library provides routines for input and output. The standard library has functions for i/o that handle input, output, and character and string manipulation. Presently we consider, all the input functions described read from standard input and all the output functions described write to standard output. Standard input is usually the keyboard. Standard output is usually the monitor.

Formatted Output The standard library function printf is used for formatted output. It takes as arguments a format string and an optional list of variables or literals to output. The variables and literals are output according to the specifications in the format string. Here is the prototype for printf. int printf(const char *format, arg1, arg2, arg3,...);

Example #include <stdio.h> int main() { int luckynumber = 5; float radius = 2; char myname[15] = "John"; char initial = 'J'; printf("hello World\n"); /* The format string contains only ordinary characters. Ordinary characters are output unmodified. A character string in C is of type "char *". */ printf("my lucky number is %d\n", luckynumber); /* The "%d" specifies that an integer value will be output. */ printf("my name is %s\n",myname); /* The %s specifies that a character string will be output. */ printf("my first initial is %c\n",initial); /* The %c specifies that a character will be output. */ printf("the area of a circle with radius %f is %f\n", radius, 3.14*radius*radius); /* %f specifies that a float will be output. */ printf("hello %s or should I say %c\n",myname,initial); /* Multiple arguments of different types may be output. */ return(0); }

Formatted Output Here are the more common conversion specifiers. Specifier Argument Type %d int %f float or double %e float or double, output in scientific notation. %c character %s character string (char *)

Formatted Input The standard library function scanf is used for formatted input. It takes as its arguments a format string and a list of pointers to variables to store the input values. Similar to its use in printf, the format string can contain literals and conversion specifiers. In C, to return a value from a function to a calling routine, a pointer to a variable is passed into the function. A pointer stores the memory address of another variable. Here is the prototype of scanf and a program illustrating its use. int scanf(const char *format, arg1, arg2,...);

Formatted Input Scanf returns an integer, either the number of values read in, or EOF if an end of file is reached. EOF is a special termination character, specified in stdio.h, which designates the end of a file. If no values are successfully read, scanf returns 0. To use scanf in a program, the file stdio.h must be included.

Formatted Input #include <stdio.h> int main() { float radius; char yourname[15]; int number; printf("enter the radius of circle: "); scanf("%f",&radius); /* & is the "address of" operator. The address of radius is passed into scanf. Passing the address of a variable is equivalent to passing a pointer containing the address of the variable. */ printf("a circle of radius %f has area %f\n",radius,3.14*radius*radius); printf("enter a number from 1 to 1000: "); scanf("%d",&number); /* The address of number is passed to scanf */ printf("your number is %d\n",number); printf("enter your name: "); scanf("%s",yourname); /* yourname is a character array. yourname[0] specifies the first element of the array. &yourname[0] specifies the address of the first element of the array. In C, an array name by itself is shorthand for the address of the first element. So, yourname is equivalent to &yourname[0], which is what must be passed into scanf. This will be made clearer in the lesson covering arrays. */ printf("hello %s\n",yourname); return(0); }

Other Useful Standard Library Functions for Input and Output getchar getchar reads a single character from standard input. Its prototype is: int getchar(); It returns int rather than char because the "end of file", EOF, character must be handled. EOF is an int and is too large to fit in a char variable. A newline character in C is '\n'. This designates the end of a line. putchar putchar writes a single character to standard output. Its prototype is: int putchar(int value);

Other Useful Standard Library Functions for Input and Output A simple program which echoes back everything that is typed in. Depending on which operating system you are using, EOF is entered by typing control-z or control-d. So, try running this code, type control-z or control-d to end execution #include <stdio.h> int main() { int c; while ((c = getchar())!= EOF) { putchar(c); } } return(0);

Other Useful Standard Library Functions for Input and Output gets gets reads a line of input into a character array. Its prototype is: char *gets(char *buffer); It returns a pointer to the character string if successful, or NULL if end of file is reached or if an error has occurred. The string is also read into the character array specified as an argument. The character string will be terminated by a "\0", which is standard for C.

Other Useful Standard Library Functions for Input and Output puts puts writes a line of output to standard output. Its prototype is: int puts(const char *buffer); It terminates the line with a newline, '\n'. It will return EOF if an error occurred. It will return a positive number on success. Here is the "echo" program implemented using the functions gets and puts. Type control-z or control-d to end execution.

Other Useful Standard Library Functions for Input and Output #include <stdio.h> int main() { char buffer[120]; /* Holds input and output strings */ char *pt; /* A pointer to datatype char */ int returncode; while ((pt = gets(buffer))!= NULL) { returncode = puts(buffer); if (returncode == EOF) { printf("error on output\n"); } } } return(0);

Practice 1) Write a program to read in the name and address of the user and echo back that information. 2) Write a program to prompt a user for a nickname, the year they were born as an integer, and their weight in pounds as a float. Calculate their age. Calculate their weight in kilograms. Write this information to standard output. Note there are 2.2046 pounds per kilogram.