Printf And scanf

Computer Education, Training & Tutorial Resources - ComputerEducationWorld.com
Home » Free Educations » Free Programming Training » C Programming » C For Beginners »

printf offers more structured output than putchar. Its arguments are, in order; a control string, which controls what gets printed, followed by a list of values to be substituted for entries in the control string.

#include <stdio.h>

int main()
{
   printf(”John Q. Doe\n”);
   printf(”1234 Main Street\n”);
   printf(”(505) 555-1212\n”);

   return 0;
}

/* Result of execution

John Q. Doe
1234 Main Street
(505) 555-1212

*/

Consider the following program.
#include <stdio.h> main() /* FORMATS.C */
{
char c = ‘#’; static char s[] = “helloandwelcometoclanguage”;
printf(”Characters:\n”);
printf(”%c\n”, c);
printf(”%3c%3c\n”, c, c);
printf(”%-3c%-3c\n”, c, c);
printf(”Strings:\n”);
printf(”%s\n”, s);
printf(”%.5s\n”, s);
printf(”%30s\n”, s);
printf(”%20.5s\n”, s);
printf(”%-20.5s\n”, s);
}
Output :

Characters:
  #
  # #
# #
Strings: helloandwelcometoclanguage
hello
  helloandwelcometoclanguage
  hello
hello

printf(”%.5s\n”,s) means,print the first five characters of the array s. printf(”%30s\n”, s) means that the array s is printed with leading spaces, to a field width of thirty characters.Similarly printf(”%20.5s\n”, s) means that the first five characters are printed in a field size of twenty with leading spaces.

%.nd integer (optional n = number of columns; if 0, pad with zeroes)
%m.nf float or double (optional m = number of columns,
  n = number of decimal places)
%ns string (optional n = number of columns)

The scanf function allows you to accept input from keyboard. The scanf function can do a lot of different things, but it is generally unreliable unless used in the simplest ways. It is unreliable because it does not handle human errors very well. But for simple programs it is good enough and easy-to-use.

#include

int main(void) {
  int n;
  while (scanf(”%d”, &n) == 1)
  printf (”%d\n”, n);
  return 0;
}


456
123
789
456
12
456
1
2378


• • •
 



captcha PHP Script Free PHP captcha script free php
Website Design by WebWalas.com