basic input and output

16
BASIC INPUT AND OUTPUT -C Programming- PREPARED BY Mrs. Nurul Zakiah

Upload: nurul-zakiah-zamri-tan

Post on 11-Nov-2014

599 views

Category:

Documents


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Basic Input and Output

BASIC INPUT AND OUTPUT

-C Programming-

PREPARED BY Mrs. Nurul Zakiah

Page 2: Basic Input and Output

INPUT AND OUTPUT

FUNCTION

Page 3: Basic Input and Output

scanf() is used to give data to variables using keyboardscanf works very well when the viewer’s input has NO spaces

Input Statement -> SCANF

Output Statement ->PRINTF

SCANF &PRINTF

printf() is used to show the result of the program trough display device

Page 4: Basic Input and Output

gets() function accepts an entire line of input from the standard input device (keyboard)

Input Statement -> GETS

Output Statement ->PUTS

GETS &PUTS

It is complement of gets. Its display a string on the standard output device

Page 5: Basic Input and Output

REVISE LAB 3

#include<stdio.h>#include<conio.h>main(){ char subject[20]; printf("Enter subject name:"); scanf("%s", &subject); printf("Your name is: %s \n",subject); getch(); }

a.

Exercise 1

Enter subject name: Computer ProgrammingYour name is: Computer

#include<stdio.h>#include<conio.h>main(){ char subject[20]; printf("Enter a subject name:"); gets(subject); printf("Your name is: \n"); puts(subject); getch(); }

b.

Enter subject name: Computer ProgrammingYour name is: Computer Programming

Page 6: Basic Input and Output

SCANF

Cannot read a multi-word string such as computer programmingIts read only computer

scanf terminates it input when it encounter a blank space such as space, tab, new

line, form feed, carriage return.

Overcomes the disadvantages of scanf functions

It can read any string length with any numbers of blank space

GETS

Page 7: Basic Input and Output

• getch is used to read a character with pressing the enter

Input Statement -> GETCHAR

Output Statement ->PUTCHAR

GETCHAR &PUTCHAR

• Complement of the getchar function

• To display a single character on the standard output device

• Define in the standard C header stdio.h

Page 8: Basic Input and Output

• getch is used to returns a single character typed in from the standard input device

Input Statement -> GETCH

Output Statement ->PUTCH

GETCH &PUTCH

• Accessed from <stdio.h> standard library

• As soon as key is pressed it stop accessing the keyboard. • User don’t have to press the ENTER key.

Page 9: Basic Input and Output

REVISE LAB 3

#include<stdio.h>#include<conio.h>main(){ char letter; printf("Enter a character:"); letter=getchar(); printf("\nYour input is: \n"); putchar(letter); getch(); }

c.

Exercise 1

Enter a character: Computer ProgrammingYour Input is: C

#include<stdio.h>#include<conio.h>main(){ char letter; printf("Enter a character:"); letter=getch(); printf("\nYour input is: \n"); putch(letter); getch(); }

d.

Enter subject name: Your name is: C

Page 10: Basic Input and Output

a string is a character variable with more than one character.

What is a STRING??

Strings can be abbreviations, words, phrases, sentences, or even paragraphs

Page 11: Basic Input and Output

BASIC PALCEMENT

Can be said as :

Format specifierFormat string

Page 12: Basic Input and Output

Format Specifier

Output Type Output Example

%d Signed decimal integer 76%i Signed decimal integer 76%o Unsigned octal integer 134%u Unsigned decimal integer 76%x Unsigned hexadecimal (small letter) 9c%X Unsigned hexadecimal (capital

letter)9C

%f Integer including decimal point 76.0000%e Signed floating point (using e

notation)7.6000e+01

%E Signed floating point (using E notation)

7.6000E+01

%g The shorter between %f and %e 76%G The shorter between %f and %E 76%c Character ‘7’%s String ‘76'

Page 13: Basic Input and Output

Why Use Format Specifier??

Nature of c requires that I/0 operations be associated with specifier data types.This association is made with format and conversion specifiers.

When use with the input statements, conversion specifiers tell what kind of variable is being read

Page 14: Basic Input and Output

Example

scanf(“%lf”,& x);

Tells C to interpret character type at the keyboard as real number of type double

Page 15: Basic Input and Output

BASIC CONSTANT

Page 16: Basic Input and Output

Basic Constant Effect \a Beep sound \b Backspace \f Formfeed (for printing) \n New line \r Carriage return \t Tab \v Vertical tab \\ Backslash \” “ sign \o Octal decimal \x Hexadecimal \O NULL

Also said as sscape sequences

Its allow you to send nongraphic control characters to a display device