Skip to content

Commit

Permalink
Merge pull request #59 from a-rokay/issue-33
Browse files Browse the repository at this point in the history
Audit input-functions.md
  • Loading branch information
CameronGray1210 authored Nov 17, 2021
2 parents 7cee8e1 + 89dd707 commit b7491f7
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 62 deletions.
12 changes: 6 additions & 6 deletions docs/D-Modularity/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ The module named `hello.c` starts executing at statement `int main(void)`, outpu
We can sub-divide a programming project in different ways. We select our modules so that each one focuses on a narrower aspect of the project. Our objective is to define a set of modules that simplifies the complexity of the original problem.
<span className="mdImg">
<div class="mdImg">
![modules](https://ict.senecacollege.ca//~ipc144/pages/images/modules.png)
</span>
</div>
Some general guidelines for defining a module include:
Expand Down Expand Up @@ -95,11 +95,11 @@ Consider a module that receives a flag from another module and performs a calcul
The C language is a procedural programming language. It supports modular design through function syntax. Functions transfer control between one another. When a function transfers control to another function, we say that it ***calls*** the other function. Once the other function completes its task and transfers control to the caller function, we say that that other function ***returns*** control to its ***caller***.
<span className="mdImg">
<div class="mdImg">
![function](https://ict.senecacollege.ca//~ipc144/pages/images/function.png)
</span>
</div>
In the example from the introductory chapter on [compilers](../A-Introduction/compilers.md) listed above:
1. the `main()` function calls the `printf()` function
Expand Down Expand Up @@ -231,11 +231,11 @@ identifier(argument, ..., argument)
`identifer` specifies the function being called, while `argument` specifies a value being passed to the function being called.
<span className="mdImg">
<div class="mdImg">
![calling-functions](https://ict.senecacollege.ca//~ipc144/pages/images/calling.png)
</span>
</div>
An argument may be a constant, a variable, or an expression (with certain exceptions). The number of arguments in a function call should match the number of parameters in the function header.
### Pass By Value
Expand Down
120 changes: 66 additions & 54 deletions docs/D-Modularity/input-functions.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
sidebar_position: 4
title: Input Functions
---
# Input Functions

Expand All @@ -11,16 +12,20 @@ After reading this section, you will be able to:

## Introduction

Some programming languages leave input and output support to the libraries developed for the languages. For instance, the core C language does not include input and output specifications. These facilities are available in a set of functions, which are defined in the stdio module. This module ships with the C compiler. Its name stands for standard input and output. Typically, standard input refers to the system keyboard and standard output refers to the system display. The system header file that contains the prototypes for the functions in this module is `<stdio.h>`.
Some programming languages leave input and output support to the libraries developed for the languages. For instance, the core C language does not include input and output specifications. These facilities are available in a set of functions, which are defined in the `stdio` module. This module ships with the C compiler. Its name stands for standard input and output. Typically, standard input refers to the system keyboard and standard output refers to the system display. The system header file that contains the prototypes for the functions in this module is `<stdio.h>`.

This chapter describes some of the input facilities supported by the stdio module, introduces buffered input, describes two library functions that accept formatted and unformatted buffered input and demonstrates how to validate user input.
This chapter describes some of the input facilities supported by the `stdio` module, introduces buffered input, describes two library functions that accept formatted and unformatted buffered input and demonstrates how to validate user input.


## Buffered Input

A buffer is a small region of memory that holds data temporarily and provides intermediate storage between a device and a program. The system stores each keystroke in the input buffer, without passing it to the program. The user can edit their data before submitting it to the program. only by pressing the \n key, the user signals the program to start extracting data from the buffer. The program then only retrieves the data that it needs and leaves the rest in the buffer for future retrievals. The figure below illustrates the buffered input process.

![](https://ict.senecacollege.ca//~ipc144/pages/images/buffer.png)
<div class="mdImg">

![Input buffer diagram](https://ict.senecacollege.ca//~ipc144/pages/images/buffer.png)

</div>

Two functions accept buffered input from the keyboard (the standard input device):

Expand All @@ -29,11 +34,15 @@ Two functions accept buffered input from the keyboard (the standard input device

## Unformatted Input

The function getchar() retrieves the next unread character from the input buffer.
The function `getchar()` retrieves the next unread character from the input buffer.

<div class="mdImg">

![getchar() diagram](https://ict.senecacollege.ca//~ipc144/pages/images/getchar.png)

![](https://ict.senecacollege.ca//~ipc144/pages/images/getchar.png)
</div>

The prototype for getchar() is
The prototype for `getchar()` is

```c
int getchar(void);
Expand All @@ -43,7 +52,7 @@ int getchar(void);
* the character code for the retrieved character
* EOF
The character code is the code from the collating sequence of the host computer. You can find the ASCII collating sequence [here](../Resources-Appendices/ascii-collating-sequence.md). If the next character in the buffer waiting to be read is `'j'` and the collating sequence is ASCII, then the value returned by `getchar()` is 106.
The character code is the code from the [collating sequence](../Resources-Appendices/ascii-collating-sequence.md) of the host computer. If the next character in the buffer waiting to be read is `'j'` and the collating sequence is ASCII, then the value returned by `getchar()` is 106.
`EOF` is the symbolic name for end of data. It is assigned the value -1 in the `<stdio.h>` system header file. On Windows systems, the user enters the end of data character by pressing `Ctrl-Z`; on UNIX systems, by pressing `Ctrl-D`.
Expand All @@ -56,8 +65,8 @@ To synchronize user input with program execution the buffer should be empty. Th
//
void clear(void)
{
while (getchar() != '\n')
; // empty statement intentional
while (getchar() != '\n')
; // empty statement intentional
}
```

Expand All @@ -72,9 +81,9 @@ To pause execution at a selected point in a program, consider the following func
//
void pause_(void)
{
printf("Press enter to continue ...");
while (getchar() != '\n')
; // empty statement intentional
printf("Press enter to continue ...");
while (getchar() != '\n')
; // empty statement intentional
}
```
Expand All @@ -84,7 +93,11 @@ This function will not return control to the caller until the user has pressed `
The `scanf()` function retrieves the next set of unread characters from the input buffer and translates them according to the conversion(s) specified in the format string. `scanf()` extracts only as many characters as required to satisfy the specified conversion(s).
![](https://ict.senecacollege.ca//~ipc144/pages/images/scanf.png)
<div class="mdImg">
![scanf() diagram](https://ict.senecacollege.ca//~ipc144/pages/images/scanf.png)
</div>
The prototype for scanf() is:
Expand Down Expand Up @@ -128,13 +141,13 @@ The following program converts two input fields into data values of int type and

int main(void)
{
int items;
float price;
int items;
float price;

printf("Enter items, price : ");
scanf("%d%f", &items, &price);

printf("Enter items, price : ");
scanf("%d%f", &items, &price);
return 0;
return 0;
}
```
Expand All @@ -158,7 +171,7 @@ We may insert control characters between the `%` and the `conversion character`.
The three control characters are:
* **\*** suppresses storage of the converted data (discards it without storing it)
* **\*** - suppresses storage of the converted data (discards it without storing it)
* ***width*** - specifies the maximum number of characters to be interpreted
* ***size*** - specifies the size of the storage type
Expand All @@ -184,7 +197,7 @@ A conversion specifier that includes an **\*** does not have a corresponding add
### Problems with %c (Optional)
Because `scanf()` only extracts the characters that it needs from the input buffer, problems arise with `%c` conversions. If you encounter such difficulty see the section with this title in the chapter entitled [More Input and Output](../F-Refinements/more-input-and-output.md).
Because `scanf()` only extracts the characters that it needs from the input buffer, problems arise with `%c` conversions. If you encounter such difficulty, see the section with this title in the chapter entitled [More Input and Output](../F-Refinements/more-input-and-output.md).
### Plain Characters (Optional)
Expand All @@ -199,7 +212,6 @@ To input `%` as a plain character (and distinguish it from the symbol identifyin
* 0 indicates that `scanf()` did not fill any address
* 1 indicates that `scanf()` filled the first address successfully
* 2 indicates that `scanf()` filled the first and second addresses successfully
* ...
* `EOF` indicates that `scanf()` did not fill any address **AND** encountered an end of data character
The return code from `scanf()` does not reflect success of **%\*** conversions or any successful reading of plain characters in the format string.
Expand Down Expand Up @@ -232,53 +244,53 @@ void clear(void);
int main(void)
{
int input;
int input;
input = getInt(MIN, MAX);
printf("\nProgram accepted %d\n", input);
input = getInt(MIN, MAX);
printf("\nProgram accepted %d\n", input);
return 0;
return 0;
}
// getInt accepts an int between min and max
// inclusive, returns the value of the int accepted
//
int getInt(int min, int max)
{
int value, keeptrying = 1, rc;
char after;
do {
printf("Enter an integer in range [%d,%d] : ", min, max);
rc = scanf("%d%c", &value, &after);
if (rc == 0)
{
printf("**Bad char(s)!**\n");
clear();
}
else if (after != '\n')
{
printf("**Trail char(s)!**\n");
clear();
}
else if (value < min || value > max)
{
printf("**Out of range!**\n");
}
else
keeptrying = 0;
} while (keeptrying == 1);
return value;
int value, keeptrying = 1, rc;
char after;
do {
printf("Enter an integer in range [%d,%d] : ", min, max);
rc = scanf("%d%c", &value, &after);
if (rc == 0)
{
printf("**Bad char(s)!**\n");
clear();
}
else if (after != '\n')
{
printf("**Trail char(s)!**\n");
clear();
}
else if (value < min || value > max)
{
printf("**Out of range!**\n");
}
else
keeptrying = 0;
} while (keeptrying == 1);
return value;
}
// clear empties the input buffer
//
void clear(void)
{
while (getchar() != '\n')
; // empty statement intentional
while (getchar() != '\n')
; // empty statement intentional
}
```
The above program produces the following output:
Expand Down
5 changes: 3 additions & 2 deletions src/css/custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ html[data-theme='dark'] .docusaurus-highlight-code-line {
background-color: rgba(0, 0, 0, 0.3);
}

.mdImg > p > img{
div.mdImg {
background-color: #ffffff;
}
text-align: center;
}

0 comments on commit b7491f7

Please sign in to comment.