Monday, October 29, 2018

Formatted Input and Output

Functions

The standard library functions are predefined functions provided by the C compiler. Using them, specify the header files indicating where to find these functions (include directive)

The function defined by the programmer or the user-defined function are functions created by the programmer / developer for use in a program.




Output Operation
To show data/text on your display, some of the standard libray functions are needed such as:


printf();
Example : 
int main ()
{ printf("Hello World!!!");
}
The output will be : Hello World!!!

Output has formatted specifications:
%[flags][width][.precision]type
Example 
printf("%10s",Algo); >>>>Algo......
printf("%-10s",Algo); >>>>......Algo
printf (“%-8.3f”, 3.14159 );>>>3.141...

putchar();
Example
char ch ='A'
putchar(ch);
output=A

putch();
Output will be Ascii character.
char ch=’b’;
putch(ch);

puts();
puts(”Hello”);
puts(”World”);
Output:
Hello
World

Input Operation
scanf();
Format=int scanf( const char *format [,argument]... );
Example :
int a;
scanf(”%d”,&a); 
Input format=Type
Type
//%d integer
//%f float
//%lf double
//%s string/array of character
//%c single character
//[^...] scan until ...
// [^\n] scan until it find a new line
// [^a] scan until it scan letter a

Multiple variable scanning:
int a,b,c;
total=scanf("%d %d %d",&a,&b,&c);
if your input is more than 3, the total counted value of total will still be 3.
getchar();
Example:
char ch; ch = getchar();
getch(); 
Example:
char buffer[40];
char *ptr;
ptr = gets(buffer);
getche();
gets();




By :Steven
2201852132
Computer Science and Statistics











No comments:

Post a Comment