Upload
nurul-zakiah-zamri-tan
View
596
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Citation preview
BASIC INPUT AND OUTPUT
-C Programming-
PREPARED BY Mrs. Nurul Zakiah
INPUT AND OUTPUT
FUNCTION
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
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
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
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
• 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
• 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.
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
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
BASIC PALCEMENT
Can be said as :
Format specifierFormat string
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'
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
Example
scanf(“%lf”,& x);
Tells C to interpret character type at the keyboard as real number of type double
BASIC CONSTANT
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