UNIT-I Input/ Output functions and other library functions

Similar documents
Unit 4. Input/Output Functions

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

Strings and Library Functions

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

Data Input and Output 187

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

Fundamentals of Programming. Lecture 11: C Characters and Strings

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

PES INSTITUTE OF TECHNOLOGY (BSC) I MCA, First IA Test, November 2015 Programming Using C (13MCA11) Solution Set Faculty: Jeny Jijo

Chapter 8 Character Arrays and Strings

Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. C Data Types

Chapter 8 - Characters and Strings

Multiple Choice Questions ( 1 mark)

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

today cs3157-fall2002-sklar-lect05 1

UNIT 3 OPERATORS. [Marks- 12]

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

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

A function is a named group of statements developed to solve a sub-problem and returns a value to other functions when it is called.

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

THE FUNDAMENTAL DATA TYPES

Chapter 8 C Characters and Strings

Course organization. Course introduction ( Week 1)

Strings. Daily Puzzle

LESSON 4. The DATA TYPE char

Fundamentals of Computer Programming Using C

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

C Libraries. Bart Childs Complementary to the text(s)

SWEN-250 Personal SE. Introduction to C

Should you know scanf and printf?

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

Advanced C Programming Topics

printf("%c\n", character); printf("%s\n", "This is a string"); printf("%s\n", string); printf("%s\n",stringptr); return 0;

Introduction to string

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

C: How to Program. Week /May/28

Approximately a Test II CPSC 206

Programming in C Quick Start! Biostatistics 615 Lecture 4

Fundamentals of Programming

Fundamental of Programming (C)

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

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

C mini reference. 5 Binary numbers 12

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

Introduction to Computing Lecture 03: Basic input / output operations

Basics of Programming

ONE DIMENSIONAL ARRAYS

3. Functions. Modular programming is the dividing of the entire problem into small sub problems that can be solved by writing separate programs.

Computers Programming Course 5. Iulian Năstac

C Programming. Unit 9. Manipulating Strings File Processing.

C PROGRAMMING. Characters and Strings File Processing Exercise

ANSI C Programming Simple Programs

Computer Programming 5th Week loops (do-while, for), Arrays, array operations, C libraries

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

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

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

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

Lecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch

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

C - Basic Introduction

Have the same meaning as variables in algebra Single alphabetic character Each variable needs an identifier that distinguishes it from the others a =

Programming and Data Structures

Data Types and Variables in C language

BSM540 Basics of C Language

Chapter 9 Strings. With this array declaration: char s[10];

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

Data Type Fall 2014 Jinkyu Jeong

Characters, c-strings, and the string Class. CS 1: Problem Solving & Program Design Using C++

Vidyalankar. F.E. Sem. II Structured Programming Approach Prelim Question Paper Solution

Characters and Strings

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

Arrays, Strings, & Pointers

C Programs: Simple Statements and Expressions

Characters in C consist of any printable or nonprintable character in the computer s character set including lowercase letters, uppercase letters,

SU 2017 May 11/16 LAB 2: Character and integer literals, number systems, character arrays manipulation, relational operator

CS3157: Advanced Programming. Outline

Fundamentals of Programming Session 8

Chapter 7. Basic Types

BSM540 Basics of C Language

Outline. Computer Programming. Structure of a function. Functions. Function prototype. Structure of a function. Functions

11 Console Input/Output

Standard I/O in C and C++

Intermediate Programming, Spring 2017*

C Programming

2. Numbers In, Numbers Out

Contents. Preface. Introduction. Introduction to C Programming

CSC 1107: Structured Programming

Input / Output Functions

Announcements. Strings and Pointers. Strings. Initializing Strings. Character I/O. Lab 4. Quiz. July 18, Special character arrays

Number Systems, Scalar Types, and Input and Output

The detail of sea.c [1] #include <stdio.h> The preprocessor inserts the header file stdio.h at the point.

CSE 1320 INTERMEDIATE PROGRAMMING - OVERVIEW AND DATA TYPES

Lecture 02 C FUNDAMENTALS

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

Fundamentals of Programming Session 4

Computer Science & Engineering 150A Problem Solving Using Computers

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

A Fast Review of C Essentials Part I

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

Part II Answer to all the questions (2 Marks):

Transcription:

Input and Output functions UNIT-I Input/ Output functions and other library functions All the input/output operations are carried out through function calls. There exists several functions that become standard for input and output operations in 'C'. These functions are collectively known as the standard I/O library. 1. #include<stdio.h> - standard input output header file. Each program that uses a standard input/output function must contain this statement. The instruction # include<stdio.h> tells the compiler 'to search for a file named stdio.h and place its contents at this point in the program'. Three types of I/O functions: a) Character I/O functions i. getchar() ii. putchar() b) Formatted I/O functions i. scanf() ii. printf() c) string I/O functions i. gets() ii. puts() getchar(): The function getchar() is used to read a single character from the keyboard. It is an unformatted input function. Form: variablename=getchar(); variablename is a valid 'C' name that has been declared as char type. char ch; ch=getchar(); putchar(): The function putchar() is used to write character to the standard output function. It is an unformatted output function. Form: putchar(variablename); Prepared by Aravinda Kasukurthi, CSE, RVRJCCE 1

scanf(): char ch='a'; putchar(ch) or putchar('a'); Example program to read a character and print that character. #include<stdio.h> main() { char ch; printf("enter a character\n"); ch=getchar(); printf("the character is:"); putchar(ch); } Output: Enter a character b The character is:b This is a formatted input function. General form: scanf("control string", arg1,arg2,...,argn); The "control string" specifies the field format (format specifiers) in which the data is to be entered and The arguments arg1, arg2,, argn specify the address of locations where the data is stored. Control string contains i. Field (or format) specifications, ii. Consisting of the conversion character %, iii. A data type character (or type specifier), iv. And an optional number, specifying the field width. The data type character indicates the type of data that is to be assigned to the variable associated with the corresponding argument. Integer input: %wd '%' indicates that a conversion specification follows. W is an integer number that specifies the field width of the number to be read. Prepared by Aravinda Kasukurthi, CSE, RVRJCCE 2

'd' known as data type character. Examples: int n1, n2; scanf("%d%d",&n1,&n2); scanf("%2d%5d",&n1,&n2); An input may be skipped by specifying * in the place of field width. Int a,b; scanf("%d%*d%d",&a,&b); will assign the data 123 456 789 as follows 123 to a 456 is skipped 789 to b Inputting Real numbers: The field width of real numbers is not to specified and scanf reads real numbers using the simple specification %f for both the notations, namely decimal point notation and exponential notation. scanf("%f%f%f",&x,&y,&z); 475.89 43.21E-1 678 x=475.89 y=4.321 z=678 double: The field specification is %lf Inputting a character using scanf: %c is the specifier. Inputting a character strings: scanf function can input string containing more than one character. Specification: %ws Prepared by Aravinda Kasukurthi, CSE, RVRJCCE 3

Example program: #include<stdio.h> main() { char s[10]; printf("enter a string\n"); scanf("%4s",s); printf("string is:"); printf("%s\n",s); } Output: Enter a string Anuradha String is:anur %s terminates reading at the encounter of a blank space. (%s adds a null character at the end ('\0')) #include<stdio.h> main() { char s[10]; printf("enter a string\n"); scanf("%s",s); printf("string is:"); printf("%s\n",s); } Enter a string Anu Radha String is:anu Some versions of scanf for strings: %[characters] and %[^characters] The specification %[characters] means that only the characters specified within the brackets are permissible in the input string. If any other character encountered, then the string will be terminated. The specification %[^characters] allows the characters specified after the (^) circumflex are not permitted in the input string. Prepared by Aravinda Kasukurthi, CSE, RVRJCCE 4

gets(): The gets() function is used to read a sequence of characters including blank spaces. (where scanf reads only uo to blank space) up to new line('\0'); at the end it adds null character ('\0'). gets(stringname); where string name is of type character array. scanf Format codes code Meaning %c read a single character %d read a decimal integer %e, %f, %g read a floating point value %h read a short integer %i read a decimal, hexadecimal, or octal integer %o read an octal integer %s read a string (character array) %u read an unsigned decimal integer %x read a hexadecimal integer %[..] read a string of word(s) Formatted ouput: 1. printf() General form: scanf("control string", arg1,arg2,...,argn); i. Control string consists of Characters that will be printed on the screen as they appear. Format specifications. Escape sequence characters. It specifies many arguments and their types. ii. The arguments arg1, arg2,, argn are variables whose values are formatted and printed according to the specifications of the control string. %wp type specifier Prepared by Aravinda Kasukurthi, CSE, RVRJCCE 5

where 'w' is an integer number that specifies the total number of columns for the output value. 'p' specifies the number of digits after decimal point (real) or number of characters to be printed from a string. Output of Integer numbers: %wd printf("%6d",9876); 9 8 7 6 printf("%-6d",9876); printf("%06d",9876); 9 8 7 6 0 0 9 8 7 6 Output of Real numbers: 1. Decimal notation %w.pf 2. Exponential notation %w.pe float y=98.7654; printf("%7.4f",y); printf("%7.2f",y); 9 8. 7 6 5 4 9 8. 7 7 printf("%10.2e",y); 9. 8 8 e + 0 0 1 Prepared by Aravinda Kasukurthi, CSE, RVRJCCE 6

printf("%e",y); 9. 8 7 6 5 4 0 e + 0 0 1 printf("%-10.2e",y); 9. 8 8 e + 0 0 1 Printing a single character: %wc The character will be displayed right justified in the field of w columns. For left justification place a minus sign (-) before the integer w. Default value of 'w' is 1. Printing of strings: %w.ps Where 'w' specifies the field width for display. 'p' instructs that only the first p characters of the string are to be displayed. #include<stdio.h> main() { printf("%s\n","new DELHI 110001"); printf("%20s\n","new DELHI 110001"); printf("%20.10s\n","new DELHI 110001"); printf("%.5s\n","new DELHI 110001"); } Output: NEW DELHI 110001 NEW DELHI 110001 NEW DELHI NEW D printf("%s\n","new DELHI 110001"); NEW DELHI 110001 Prepared by Aravinda Kasukurthi, CSE, RVRJCCE 7

printf("%20s\n","new DELHI 110001"); N E W D E L H I 1 1 0 0 0 1 printf("%20.10s\n","new DELHI 110001"); printf("%.5s\n","new DELHI 110001"); puts(): N E W D N E W D E L H I The function is used to print string of values or group of characters. General Form: puts(stringname); where string name is of type character array. Here, there is no need to specify '\n', explicitly, after printing string, the cursor is at the initial position of next line. printf Format codes code Meaning %c print a single character %d print a decimal integer %e print a floating point value in exponential form %f print a floating point value %g print a floating point value either e-type or f- type depending on value %h print a short integer %i print a signed decimal integer %o print an octal integer, without leading zero %s print a string (character array) %u print an unsigned decimal integer %x print a hexadecimal integer, without leading 0x Prepared by Aravinda Kasukurthi, CSE, RVRJCCE 8

Library functions: 1. <stdio.h> - Standard I/O library functions Function Data type Task returned printf( ) int Send data items to the standard output device scanf( ) int Enter data items from the standard input device gets() char* Enter a string from standard input device puts(s) int Send string s to the standard output device getchar(void) int Enter a single character from the standard input device putchar( c) int Send a single character to the standard output device 2. <math.h> - Mathematical functions Function Data type Task returned pow(d1,d2) double Return d1 raised to the d2 power sqrt(d) double Return the square root of d ceil(d) double Return a value rounded up to the next higher integer floor(d) double Return a value rounded down to the next lower integer fabs(d) double Return the absolute value of d 3. <ctype.h> - Character testing and conversion functions Function Data type Task returned isalpha( c) int Determine if argument is alphabetic. Return nonzero value if true; 0 otherwise. isascii( c) int Determine if argument is ASCII character. Return nonzero value if true; 0 otherwise. isdigit( c) int Determine if argument is decimal digit. Return nonzero value if true; 0 otherwise islower( c) int Determine if argument is lowercase. Return nonzero value if true; 0 otherwise isupper( c) int Determine if argument is uppercase. Return Prepared by Aravinda Kasukurthi, CSE, RVRJCCE 9

nonzero value if true; 0 otherwise toascii( c) int Convert value of argument to ASCII. tolower( c) int Convert letter to lowercase. toupper( c) int Convert letter to uppercase. 4. <stdib.h> Function Data type Task returned abs(i) int Return the absolute value of i exit(u) void Close all files and buffers, and terminate the program (Value of the 'u' is assigned by the function to indicate the status. free(p) void Free a block of allocated memory whose beginning is indicated by p. malloc(u) void* Allocate u bytes of memory. Return a pointer to the beginning of the allocated space. rand(void) int Return a random positive integer. 5. <string.h> - String manipulation functions Function Data type Task returned strcmp(s1,s2) int Compare two strings lexicographically. Return a negative value if s1<s2; 0 if s1 and s2 are identical; and a positive value if s1>s2. strcpy(s1,s2) char* copy string s2 to string s1. strlen(s) int Return the number of characters in string s. strcat(s1,s2) char* Concatenation s2 is appended at the end of s1. Prepared by Aravinda Kasukurthi, CSE, RVRJCCE 10