From d430bc38464e8a8afb55361456e0c33f8d181ec2 Mon Sep 17 00:00:00 2001 From: William Dale <45247508+wdale2011@users.noreply.github.com> Date: Tue, 21 Jan 2025 15:46:02 -0800 Subject: [PATCH 1/3] Add scanf() documentation for C --- .../basic-output/terms/scanf/scanf.md | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 content/c/concepts/basic-output/terms/scanf/scanf.md diff --git a/content/c/concepts/basic-output/terms/scanf/scanf.md b/content/c/concepts/basic-output/terms/scanf/scanf.md new file mode 100644 index 00000000000..ee163e28694 --- /dev/null +++ b/content/c/concepts/basic-output/terms/scanf/scanf.md @@ -0,0 +1,67 @@ +--- +Title: 'scanf()' +Description: 'Read a user input and assign it to a variable.' +Subjects: + - 'Code Foundations' + - 'Computer Science' +Tags: + - 'Functions' + - 'Input' +CatalogContent: + - 'learn-c' + - 'paths/computer-science' +--- + +The **`scanf()`** ("scan formatted string") function reads a user input, and then converts that input into a value that it assigns to a variable. + +## Syntax + +```pseudo +scanf(string, &variable, &variable2, ... &variableN) +``` + +The `scanf()` function takes a format specifier string as input, along with a variable for each format specifier in the string. The format specifier string acts as a template that the function will use to convert the user input into values for the variables. + +For a short list of format specifiers you can use in the input string, see the 'Format Specifiers' table in the [printf()](../printf/printf.md) documentation. + +> **Note:** `scanf()` does not have any intrinsic validation of the user input. This can cause errors if the user input does not match the format specifiers in the string. + +## Examples + +```c +#include + +int main(void) { + int yourNumber; + + printf("Type your favorite number:"); + scanf("%d", &yourNumber); + printf("Your favorite number is: %d", yourNumber); +} +``` + +The format specifier `%d` tells the function to expect an integer, then assigns it to the variable `yourNumber`. This means that if the user typed in `3`, the output would be: + +```shell +Your favorite number is: 3 +``` + +Here is another example using two variables: + +```c +#include + +int main(void) { + int yourNumber; + char yourLetter; + + printf("Type your favorite number and letter:"); + scanf("%d%c", &yourNumber, &yourLetter); + printf("Your favorite number is: %d, and your favorite letter is: %c", yourNumber, yourLetter); +} +``` + +If the user typed in `4B`, the output would be: +```shell +Your favorite number is: 4, and your favorite letter is: B +``` \ No newline at end of file From 50cd0a3d7596c299016ee2775df7379da3403ac7 Mon Sep 17 00:00:00 2001 From: Sriparno Roy <89148144+Sriparno08@users.noreply.github.com> Date: Thu, 23 Jan 2025 12:14:56 +0530 Subject: [PATCH 2/3] Update scanf.md --- .../basic-output/terms/scanf/scanf.md | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/content/c/concepts/basic-output/terms/scanf/scanf.md b/content/c/concepts/basic-output/terms/scanf/scanf.md index ee163e28694..c5d33b55eb8 100644 --- a/content/c/concepts/basic-output/terms/scanf/scanf.md +++ b/content/c/concepts/basic-output/terms/scanf/scanf.md @@ -1,6 +1,6 @@ --- Title: 'scanf()' -Description: 'Read a user input and assign it to a variable.' +Description: 'Reads a user input and assigns it to a variable.' Subjects: - 'Code Foundations' - 'Computer Science' @@ -12,22 +12,24 @@ CatalogContent: - 'paths/computer-science' --- -The **`scanf()`** ("scan formatted string") function reads a user input, and then converts that input into a value that it assigns to a variable. +The **`scanf()`** (scan formatted string) function reads a user input and then converts that input into a value that it assigns to a variable. ## Syntax ```pseudo -scanf(string, &variable, &variable2, ... &variableN) +scanf(string, &variable, &variable2, ..., &variableN) ``` The `scanf()` function takes a format specifier string as input, along with a variable for each format specifier in the string. The format specifier string acts as a template that the function will use to convert the user input into values for the variables. -For a short list of format specifiers you can use in the input string, see the 'Format Specifiers' table in the [printf()](../printf/printf.md) documentation. +For a short list of format specifiers you can use in the input string, see the 'Format Specifiers' table in the [`printf`](https://www.codecademy.com/resources/docs/c/basic-output/printf) documentation. > **Note:** `scanf()` does not have any intrinsic validation of the user input. This can cause errors if the user input does not match the format specifiers in the string. ## Examples +In the following example, the format specifier `%d` tells the function to expect an integer, then assigns it to the variable `yourNumber`: + ```c #include @@ -40,7 +42,7 @@ int main(void) { } ``` -The format specifier `%d` tells the function to expect an integer, then assigns it to the variable `yourNumber`. This means that if the user typed in `3`, the output would be: +If the user typed in `3`, the output would be: ```shell Your favorite number is: 3 @@ -57,11 +59,12 @@ int main(void) { printf("Type your favorite number and letter:"); scanf("%d%c", &yourNumber, &yourLetter); - printf("Your favorite number is: %d, and your favorite letter is: %c", yourNumber, yourLetter); + printf("Your favorite number is: %d and your favorite letter is: %c", yourNumber, yourLetter); } ``` If the user typed in `4B`, the output would be: + ```shell -Your favorite number is: 4, and your favorite letter is: B -``` \ No newline at end of file +Your favorite number is: 4 and your favorite letter is: B +``` From 33eccc4ced110e0c4f7cf25cb5bc82398caee220 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Thu, 23 Jan 2025 20:32:30 +0530 Subject: [PATCH 3/3] Update scanf.md minor fixes and formatting --- .../concepts/basic-output/terms/scanf/scanf.md | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/content/c/concepts/basic-output/terms/scanf/scanf.md b/content/c/concepts/basic-output/terms/scanf/scanf.md index c5d33b55eb8..0d362a51f38 100644 --- a/content/c/concepts/basic-output/terms/scanf/scanf.md +++ b/content/c/concepts/basic-output/terms/scanf/scanf.md @@ -12,21 +12,22 @@ CatalogContent: - 'paths/computer-science' --- -The **`scanf()`** (scan formatted string) function reads a user input and then converts that input into a value that it assigns to a variable. +The **`scanf()`** function (short for 'scan formatted') reads user input, interprets it according to the specified format, and assigns the converted value to a variable. ## Syntax ```pseudo -scanf(string, &variable, &variable2, ..., &variableN) +int scanf(const char *format, &variable1, &variable2, ..., &variableN); ``` -The `scanf()` function takes a format specifier string as input, along with a variable for each format specifier in the string. The format specifier string acts as a template that the function will use to convert the user input into values for the variables. +- `format`: A string containing format specifiers (e.g., `%d`, `%f`, `%s`) that define the type of input expected. +- `&variable1, &variable2, ..., &variableN`: Pointers to variables where the values from the input will be stored. Each pointer corresponds to a format specifier in the `format` string. For a short list of format specifiers you can use in the input string, see the 'Format Specifiers' table in the [`printf`](https://www.codecademy.com/resources/docs/c/basic-output/printf) documentation. > **Note:** `scanf()` does not have any intrinsic validation of the user input. This can cause errors if the user input does not match the format specifiers in the string. -## Examples +## Example 1 In the following example, the format specifier `%d` tells the function to expect an integer, then assigns it to the variable `yourNumber`: @@ -48,6 +49,8 @@ If the user typed in `3`, the output would be: Your favorite number is: 3 ``` +## Example 2 + Here is another example using two variables: ```c @@ -56,15 +59,15 @@ Here is another example using two variables: int main(void) { int yourNumber; char yourLetter; - + printf("Type your favorite number and letter:"); scanf("%d%c", &yourNumber, &yourLetter); printf("Your favorite number is: %d and your favorite letter is: %c", yourNumber, yourLetter); } ``` -If the user typed in `4B`, the output would be: +If the user typed in `4Bh`, the output would be: ```shell -Your favorite number is: 4 and your favorite letter is: B +Type your favorite number and letter:Your favorite number is: 48 and your favorite letter is: h ```