0% found this document useful (0 votes)
12 views

Lecture12 C Input Output Functions

The document provides an overview of input/output functions in C programming, specifically focusing on the scanf() and printf() functions. It explains how scanf() reads user input and stores it in variables, detailing the use of control strings and format specifiers, while also highlighting common pitfalls. Additionally, it covers the printf() function's ability to format and display output based on specified types and formats.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Lecture12 C Input Output Functions

The document provides an overview of input/output functions in C programming, specifically focusing on the scanf() and printf() functions. It explains how scanf() reads user input and stores it in variables, detailing the use of control strings and format specifiers, while also highlighting common pitfalls. Additionally, it covers the printf() function's ability to format and display output based on specified types and formats.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

C Programming:

Input / output functions


Input Functions: scanf()
 Use the scanf() function to read a string
 General form of the function is:
scanf(control string,variable,variable,...)

 The control string specifies how strings of characters, typed on the


keyboard, should be converted into values and stored in the listed variables.
 The scanf() function reads the sequence of characters from the keyboard
and sends the data to the program until the user enters a whitespace
(space, newline, tab, etc.).
 The scanf() function can convert the string input into various forms: integers,
floating point numbers, characters or C strings.
 scanf() has to change the values stored in the parts of computer’s memory
that are associated with the parameters (variables).

© Prof Suvendi Rimer, UJ


Input Functions: scanf()
 The rule to remember:
If you need to read a variable for a non-string type, precede the variable
name with &.
 The rule is that scanf() processes the control string from left to right and
each time it reaches a specifier it tries to interpret what has been typed as a
value.
 If you input multiple values then these are assumed to be separated by white
space - i.e. spaces, newline or tabs. This means you can type:
345
Or 3
4
5
and it doesn't matter how many spaces are included between the items.

© Prof Suvendi Rimer, UJ


Input Functions: scanf()
 The integer values can be typed on the same line or on different lines as long as
there is at least one white space character between them.
 The only exception to this rule is the %c specifier which always reads in the next
character typed no matter what it is.
 You can also use a width modifier in scanf(). In this case, its effect is to limit the
number of characters accepted to the width.
 Example:
scanf("%lOd",&i);
This line would use at most the 10 digits typed as the new value for i.
 The main problem with the scanf() function, which can make it unreliable in
certain cases, is that it ignores white spaces, i.e. the space character.
 If you require your input to contain spaces this can cause a problem.

© Prof Suvendi Rimer, UJ


Input Function: scanf() Example
#include <stdio.h>
int main()
{
char name[20];
printf("Enter name: ");
scanf("%s", name);
printf("Your name is %s.", name);
return 0;
}

 The %d, %f, %c, %s simply lets the compiler know the type of variable value
being read in.
 When the program reaches the scanf() statement it pauses to give the user
time to type something on the keyboard and continues only when the users
press <Enter>, or <Return>, to signal that he, or she, has finished entering
the value.
 scanf("%d %d",&i,&j), will read in two integer values into i and j.
© Prof Suvendi Rimer, UJ
Input Function: scanf() Example
#include <stdio.h>
int main(void)
{
int iAge;
float fBal;
char sPet[100];

printf("Enter your age, wallet balance and favourite pet name. \n");
scanf("%d %f %s", &iAge, &fBal, sPet);
printf("Your age is %d, your wallet balance is R%.2f and your favourite pet is
%s\n", iAge, fBal, sPet);
}//main
 The program stores the value in iAge, fBal and sPet.
 Each time the program is run, the user gets a chance to type in a different value
to the variable and the program also gets the chance to produce a different
result!
 Note: the scanf() function does not prompt for an input. You should
get in the habit of always using a printf() function, informing the user
of the program and what they should type, before a scanf()
© Prof Suvendi Rimer, UJ
function.
Output Function: printf()
 The printf() (and scanf()) functions can take a variable number of
parameters.
 The first parameter of printf() is always a string (e.g. "Hello World") but after
that you can include as many parameters of any type that you want to.
 The format for using the printf() function is:
printf(string,variable,variable,variable...);
where the ... means you can carry on writing a list of variables separated by
commas as long as you want to.
 The string is usually called the control string or the format string and it
specifies the type of each variable in the list and how you want it printed.
 printf() scans the string from left to right and prints on screen any characters
it encounters - except when it reaches a % character.

© Prof Suvendi Rimer, UJ


Output Function: printf()
 The % character is a signal that what follows it is a specification for how the
next variable in the list of variables should be printed.
 printf() uses this information to convert and format the value that was
passed to the function by the variable, and then moves on to process the
rest of the control string and anymore variables it might specify.
 These can be variables, constants or even expressions that are evaluated
first before the value is printed.
 Example: the control string is the phrase in double quotation marks; it
contains two conversion specifiers that correspond to number and ouzo,
which are the two items to be displayed.
printf(“The %d women drank %f glasses of ouzo.\n”, number, ouzo);

© Prof Suvendi Rimer, UJ


Output Function: printf()
NOTE:
 The specifier following % indicates the type of variable to be displayed, as
well as the format in which the value should be displayed;
 If you use a specifier with the wrong type of variable then you may see some
strange things on the screen and the error often propagates to other items in
the printf() list.

© Prof Suvendi Rimer, UJ


Output Function: printf() Example
#include <stdio.h>
#define PI 3.141593
int main (void)
{
int number = 5;
float ouzo = 13.5;
int cost = 3100;

printf("The %d women drank %f glasses of ouzo.\n", number, ouzo);


printf("The value of PI is %f.\n", PI);
printf("Hello World!\n");
printf("%c%d\n", 'R', 2*cost);
}//main

© Prof Suvendi Rimer, UJ


The % Format Specifiers

© Prof Suvendi Rimer, UJ

You might also like