C/Console/Printf — различия между версиями
Admin (обсуждение | вклад) м (1 версия: Импорт контента...) |
|
(нет различий)
|
Текущая версия на 13:22, 25 мая 2010
Содержание
Demonstrates printing the results of simple equations
<source lang="cpp">
- include <stdio.h>
int main() {
int term = 3 * 5; printf("Twice %d is %d\n", term, 2*term); printf("Three times %d is %d\n", term, 3*term); return (0);
}
</source>
Displaying printable characters
<source lang="cpp">
- include <stdio.h>
- include <ctype.h>
void main() {
int i = 0; /* Loop counter */ char ch = 0; for(i = 0 ; i<128 ; i++) { ch = (char)i; if( ch % 2 == 0) printf("\n"); printf(" %4d %c",ch,(isgraph(ch) ? ch : " ")); } printf("\n");
}
</source>
Displaying printable characters plus whitspace names
<source lang="cpp">
- include <stdio.h>
- include <ctype.h>
void main() {
int i = 0; /* Loop counter */ char ch = 0; /* Character code value */ for(i = 0 ; i<128 ; i++) { ch = (char)i; if(ch%2==0) printf("\n"); printf(" %4d",ch); if(isgraph(ch)) printf(" %c",ch); else switch(ch) { case "\n": printf(" newline",ch); break; case " ": printf(" space",ch); break; case "\t": printf(" horizontal tab",ch); break; case "\v": printf(" vertical tab",ch); break; case "\f": printf(" form feed",ch); break; default: printf(" "); break; } } printf("\n");
}
</source>
printf usage
<source lang="cpp">
- include <stdio.h>
int main() {
printf("Hello World\n"); return (0);
}
</source>
Prints 3 characters forward and backward to demonstrate character printing
<source lang="cpp">
- include <stdio.h>
int main() {
char char1; /* first character */ char char2; /* second character */ char char3; /* third character */ char1 = "A"; char2 = "B"; char3 = "C"; printf("%c%c%c reversed is %c%c%c\n", char1, char2, char3, char3, char2, char1); return (0);
}
</source>
Return value from printf
<source lang="cpp">
- include <stdio.h>
main() {
int i = 0; i=printf("abcde"); printf("total characters printed %d\n",i);
}
</source>