Professional Documents
Culture Documents
“” : buat string
‘’ : buat karakter
• Syntax:
printf(const char *format[,argument, …]);
Output Example
spasi
• Example 1:
printf (“%6d”, 34); ….34
printf (”%-6d”, 34); 34….
• Example 2
printf (“%10s”, “BINUS”); …..BINUS
printf (“%-10s”, “BINUS”); BINUS…..
printf (“%8.2f”, 3.14159 ); ….3.14
printf (“%-8.3f”, 3.14159 ); 3.141…
• Functionality:
– Displaying character on the monitor at cursor position. After
display, cursor will move to the next position
– Return EOF if error, and return the displayed character after
successfully done
– putchar is a macro similar to : putc(c, stdout )
– Header File : stdio.h
• Example :
char ch=’A’;
putchar(ch);
• Functionality:
– Display ASCII character to the monitor without moving cursor to its
next position
– Return EOF if error, and return the displayed character after
successfully done
– Header file : conio.h
• Example :
char ch=’b’;
putch(ch); COMP6047 - Algorithm and Programming 12
Output Operation: puts() function
• Syntax:
int puts(const char *str);
• Functionality :
– Display string to the monitor and move the cursor to new line
– Return non-negative value when successful and EOF if error
– Header file: stdio.h
• Example :
puts(”Welcome”);
puts(”to Binus”);
Output on monitor:
Welcome
to Binus
Answer:
x : 234
&x : 45678
• Example :
int x,y,z,w;
x = scanf("%d %d %d",&y,&z,&w);
scanf()
function
can use
/* Program Calculating rectangle area v2*/ more than
#include <stdio.h> one
int main(){ argument
int width, height, area;
scanf(“%d %d”,&width, &height);
area = width * height;
return(0);
}
COMP6047 - Algorithm and Programming 19
Input Operation: scanf() function
/* Program different argument*/
#include <stdio.h>
int main()
{
int number; char initial; float money;
scanf(“%d %c %f” ,&number, &initial, &money);
Data type
//other statements
for each
return(0); variable in
} the function
argument
can be
different
• Example :
– Using previous example, if a string “good
morning every one” entered then ss value will
only contain “good”
• Example:
char ss[40];
scanf(”%[^\n]”,ss);
• Functionality:
– Return the next ASCII character from keyboard buffer
– Shown on the monitor screen
– Awaiting for ENTER pressed
– Header file: stdio.h
• Example :
char ch;
ch = getchar();
• Functionality:
– Return the next ASCII character from keyboard buffer
– Does not show on the monitor screen
– Does not wait for ENTER pressed
– Header file: conio.h
• Example :
char ch;
ch = getch();
• Functionality:
– Return the next ASCII character from keyboard buffer
– Shown on the monitor screen
– Does not wait for ENTER pressed
– Header file: conio.h
• Functionality :
– read a string from keyboard till find new-line and save in buffer
– new-line will later on replaced with null character
– will return NULL if error and return its argument (buffer) if success
• Example :
char buffer[40];
char *ptr;
ptr = gets(buffer);