#include <stdio.h>
int main(void)
{
int luckyNumber = 5;
float radius = 2;
char myName[15] = "John";
char initial = 'J';
printf("Hello World\n");
printf("My lucky number is %d\n",
luckyNumber);
printf("My name is %s\n",myName);
printf("My first initial is
%c\n",initial);
printf("The area of a circle
with radius %f is %f\n", 3.14*radius*radius);
return 0;
}
Conversion specify ers
The conversion characters tell the compiler what data
type the variable or string will be.
%d int
(the d is
short for decimal)
%f float
or double
%e float
or double, output in scientific notation. (also
known as standard form)
%c character
%s character
string (char *)
Formatted Input
The standard library function scanf is used for
formatted input. It asks the user to input a string or
any other data type. Similar to its use in printf, the
format string can contain strings and conversion specify
ers I
scanf returns an integer, either the number of values
read in, or EOF if an end of file is reached. EOF is a
special termination character, specified in stdio.h, which
designates the end of a file. If no values are successfully
read, scanf returns 0. To use scanf in a program, the
file stdio.h must be included.
#include <stdio.h>
int main(void)
{
float radius;
char yourName[15];
int number;
printf("Enter the radius
of circle: ");
scanf("%f",&radius);
printf("A circle of radius
%f has /
area
of %f\n",radius,3.14*radius*radius);
printf("Enter a number
from 1 to 1000: ");
scanf("%d",&number);
printf("Your number is
%d\n",number);
printf("Enter your name:
");
scanf("%s",&yourName);
printf("Hello %s\n",name);
return 0;
}
With one exception scanf will skip over white space such
as blanks, tabs and new lines in the input stream. The
exception is when trying to read single characters with
the conversion specify ers %c. In this case, white space
is read in. So, it is more difficult to use scanf for
single characters. An alternate technique, using getchar,
will be described later in this lesson.
getchar
getchar reads a single character from standard input.
An example is:
int getchar();
It requires the user to press enter after entering
putchar
putchar writes a single character to standard output.
An example is:
int putchar(int value);
Here is a sample program using both both getchar and
putchar
#include <stdio.h>
int main(void)
{
int c;
while ((c = getchar()) !=
EOF)
{
putchar(c);
}
return 0;
}
gets
gets reads a line of input into a character array. An
example is:
gets(name of string)
puts
puts writes a line of output to standard output. An
example is:
int puts(name of string);
It terminates the line with a new line, '\n'. It will
return EOF is an error occurred. It will return a positive
number on success.
Here is a long example of strings and their definitions.
The example will discuss the use of strings and how
they should be declared.
#include <stdio.h>
int main()
{
/* character
arrays are sized properly for data */
char name[60];
char address[120];
char city[60];
char state[20];
char zip[15];
/* For responses that can include white space such
as blanks
use gets. For single word responses scanf is easier
*/
printf("Enter your name:
");
if ((pt = gets(name)) == NULL)
{
printf("Error
on read\n");
return(1);
}
printf("Enter your address:
");
if ((pt = gets(address)) ==
NULL)
{
printf("Error
on read\n");
return(1);
}
printf("Enter your city:
");
scanf("%s",&city);
printf("Enter your state:
");
scanf("%s",&state);
printf("Enter your zip:
");
scanf("%s",&zip);
/*
Output user information */
printf("%s\n",name);
printf("%s\n",address);
printf("%s, %s %s\n",city,state,zip);
return 0;
}
back to top go
to next lesson