Mesics lecture 5   input – output in ‘c’
Upcoming SlideShare
Loading in...5
×
 

Mesics lecture 5 input – output in ‘c’

on

  • 1,069 views

 

Statistics

Views

Total Views
1,069
Views on SlideShare
932
Embed Views
137

Actions

Likes
0
Downloads
35
Comments
0

1 Embed 137

http://www.eshikshak.co.in 137

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Mesics lecture 5   input – output in ‘c’ Mesics lecture 5 input – output in ‘c’ Presentation Transcript

  • Input – Output in ‘C’ www.eshikshak.co.in
  • Introduction• Reading input data, processing it and displaying the results are the three tasks of any program.• There are two ways to accept the data. – In one method, a data value is assigned to the variable with an assignment statement. • int year = 2005; char letter = ‘a’; int x = 12345; – Another way of accepting the data is with functions.• There are a number of I/O functions in C, based on the data type. The input/output functions are classified in two types. – Formatted functions – Unformatted functions
  • Formatted function• With the formatted functions, the input or output is formatted as per our requirement.• All the I/O function are defined as stdio.h header file.• Header file should be included in the program at the beginning.
  • Input and Output FunctionsFormatted Functions Unformatted Functions printf() scanf() getch() putch() getche() putchar() getchar() puts() gets()
  • Formatted Functions Unformatted Functions• It read and write all types • Works only with character of data values. data type• Require format string to • Do not require format produce formatted result conversion for formatting• Returns value after data type execution
  • printf() function• This function displays output with specified format• It requires format conversion symbol or format string and variables names to the print the data• The list of variables are specified in the printf() statement• The values of the variables are printed as the sequence mentioned in printf()• The format string symbol and variable name should be the same in number and type
  • printf() function• Syntax printf(“control string”, varialbe1, variable2,..., variableN);• The control string specifies the field format such as %d, %s, %g, %f and variables as taken by the programmer
  • void main(){ int NumInt = 2; float NumFloat=2.2; char LetterCh = ‘C’; printf(“%d %f %c”, NumInt, NumFloat, LetterCh);}Output :2 2.2000 C
  • void main(){ int NumInt = 65; clrscr(); printf(“%c %d”, NumInt, NumInt);}Output :A 65
  • void main(){ int NumInt = 7; clrscr(); printf(“%f”, NumInt); return 0;}Output :Error Message : “Floating points formats not linked”
  • void main(){ int NumInt = 7; clrscr(); printf(“%f”, NumInt); return 0;}Output :Error Message : “Floating points formats not linked”
  • • All the format specification starts with % and a format specification letter after this symbol.• It indicates the type of data and its format.• If the format string does not match with the corresponding variable, the result will not be correct.• Along with format specification use – Flags – Width – Precision
  • • Flag – It is used for output justification, numeric signs, decimal points, trailing zeros. – The flag (-) justifies the result. If it is not given the default result is right justification.• Width – It sets the minimum field width for an output value. – Width can be specified through a decimal point or using an asterisk ‘*’.
  • void main(){ clrscr(); printf(“n%.2s”,”abcdef”); printf(“n%.3s”,”abcdef”); printf(“n%.4s”,”abcdef”);}OUTPUTab abc abcd
  • void main(){ int x=55, y=33; clrscr(); printf(“n %3d”, x – y); printf(“n %6d”, x – y);}OUTPUT22 22
  • void main(){ int x=55, y=33; clrscr(); printf(“n %*d”, 15, x – y); printf(“n %*d”, 5,x – y);}OUTPUT 22 22
  • void main(){ float g=123.456789; clrscr(); printf(“n %.1f”, g); printf(“n %.2f”, g); printf(“n %.3f”, g); printf(“n %.4f”, g);}OUTPUT123.5123.46123.457123.4568
  • Sr. No Format Meaning Explanation1 %wd Format for integer w is width in integer and d output is conversion specification2 %w.cf Format for float w is width in integer, c numbers specifies the number of digits after decimal point and f specifies the conversion specification3 %w.cs Format for string w is width for total output characters, c are used displaying leading blanks and s specifies conversion specification
  • scanf() function• scanf() function reads all the types of data values.• It is used for runtime assignment of variables.• The scanf() statement also requires conversion symbol to identify the data to be read during the execution of the program.• The scanf() stops functioning when some input entered does not match format string.
  • scanf() functionSyntax :scanf(“%d %f %c”, &a, &b, &c); Scanf statement requires ‘&’ operator called address operator The address operator prints the memory location of the variable scanf() statement the role of ‘&’ operator is to indicate the memory location of the variable, so that the value read would be placed at that location.
  • scanf() function The scanf() function statement also return values. The return value is exactly equal to the number of values correctly read. If the read value is convertible to the given format, conversion is made.
  • void main(){ int a; clrscr(); printf(“Enter value of ‘A’ : “); scanf(“%c”, &a); printf(“A : %c”,a);}OUTPUTEnter value of ‘A’ : 8A:8
  • void main(){ char a; clrscr(); printf(“Enter value of ‘A’ : “); scanf(“%d”, &a); printf(“A : %d”,a);}OUTPUTEnter value of ‘A’ : 255A : 255Enter value of ‘A’ : 256A : 256
  • Sr. No Format Meaning Explanation1 %wd Format for integer w is width in integer and d input is conversion specification2 %w.cf Format for float w is width in integer, c point input specifies the number of digits after decimal point and f specifies the conversion specification3 %w.cs Format for string w is width for total input characters, c are used displaying leading blanks and s specifies conversion specification
  • Data Type Format stringInteger Short Integer %d or %i Short unsigned %u Long signed %ld Long unsigned %lu Unsigned hexadecimal %u Unsigned octal %oReal Floating %f or %g Double Floating %lfCharacter Signed Character %c Unsigned Character %c String %sOctal number %oDisplays Hexa decimal %hxnumber in lowercaseDisplays Hexa decimal %pnumber in lowercaseAborts program with %nerror
  • Escape Sequence Escape Sequence Use ASCII value• printf() and scanf() statement n New Line 10 follows the combination of characters called escape b Backspace 8 sequence f Form feed 12• Escape sequence are special ’ Single quote 39 characters starting with ‘’ Backslash 92 0 Null 0 t Horizontal Tab 9 r Carriage Return 13 a Alert 7 ” Double Quote 34 v Variable tab 11 ? Question mark 63
  • void main(){ int a = 1, b = a + 1, c = b + 1, d = c + 1; clrscr(); printf(“t A = %dnB = %d ’C = %d’”,a,b,c); printf(“nb***D = %d**”,d); printf(“n*************”); printf(“rA = %d B = %d”, a, b);}OUTPUT A=1B=2 ‘C = 3’***D=4**A = 1 B = 2******
  • Unformatted Functions• C has three types of I/O functions – Character I/O – String I/O – File I/O – Character I/O
  • getchar• This function reads a character type data from standard input.• It reads one character at a time till the user presses the enter key.• Syntax VariableName = getchar();• Example char c; c = getchar();
  • putchar• This function prints one character on the screen at a time, read by the standard input.• Syntax – puncher(variableName)• Example char c = ‘C’; putchar(c);
  • getch() and getche()• These functions read any alphanumeric character from the standard input device.• The character entered is not displayed by the getch() function.• The character entered is displayed by the getche() function.• Exampe  ch = getch();  ch = getche();
  • gets()• This function is used for accepting any string through stdin keyword until enter key is pressed.• The header file stdio.h is needed for implementing the above function.• Syntax char str[length of string in number]; gets(str); void main() { char ch[30]; clrscr(); printf(“Enter the string : “); gets(); printf(“n Entered string : %s”, ch); }
  • puts()• This function prints the string or character array.• It is opposite to gets() char str[length of string in number]; gets(str); puts(str);