diff --git a/.vs/HPC-Training/FileContentIndex/8f3cd32b-5eb5-4052-9960-53ef0791d8fc.vsidx b/.vs/HPC-Training/FileContentIndex/0c78780e-4851-40e0-a7de-a7db7cbb157d.vsidx similarity index 100% rename from .vs/HPC-Training/FileContentIndex/8f3cd32b-5eb5-4052-9960-53ef0791d8fc.vsidx rename to .vs/HPC-Training/FileContentIndex/0c78780e-4851-40e0-a7de-a7db7cbb157d.vsidx diff --git a/.vs/HPC-Training/FileContentIndex/c7d068d1-c4bd-4406-b70d-1430a9df2da5.vsidx b/.vs/HPC-Training/FileContentIndex/19436db1-42a6-4ab7-85db-e559d74c0d13.vsidx similarity index 100% rename from .vs/HPC-Training/FileContentIndex/c7d068d1-c4bd-4406-b70d-1430a9df2da5.vsidx rename to .vs/HPC-Training/FileContentIndex/19436db1-42a6-4ab7-85db-e559d74c0d13.vsidx diff --git a/.vs/HPC-Training/FileContentIndex/64517167-f706-4840-9cfb-b1821218611d.vsidx b/.vs/HPC-Training/FileContentIndex/27142df3-a65f-4650-8aa8-e75ee6302afc.vsidx similarity index 100% rename from .vs/HPC-Training/FileContentIndex/64517167-f706-4840-9cfb-b1821218611d.vsidx rename to .vs/HPC-Training/FileContentIndex/27142df3-a65f-4650-8aa8-e75ee6302afc.vsidx diff --git a/.vs/HPC-Training/FileContentIndex/65361447-e19c-4f56-8af7-9168360962eb.vsidx b/.vs/HPC-Training/FileContentIndex/65361447-e19c-4f56-8af7-9168360962eb.vsidx deleted file mode 100644 index 65d02bd..0000000 Binary files a/.vs/HPC-Training/FileContentIndex/65361447-e19c-4f56-8af7-9168360962eb.vsidx and /dev/null differ diff --git a/.vs/HPC-Training/FileContentIndex/b04b123b-9651-44ce-a58f-de685ef29d40.vsidx b/.vs/HPC-Training/FileContentIndex/b04b123b-9651-44ce-a58f-de685ef29d40.vsidx new file mode 100644 index 0000000..3331fbb Binary files /dev/null and b/.vs/HPC-Training/FileContentIndex/b04b123b-9651-44ce-a58f-de685ef29d40.vsidx differ diff --git a/.vs/HPC-Training/v17/.wsuo b/.vs/HPC-Training/v17/.wsuo index 67693b9..03b2364 100644 Binary files a/.vs/HPC-Training/v17/.wsuo and b/.vs/HPC-Training/v17/.wsuo differ diff --git a/.vs/VSWorkspaceState.json b/.vs/VSWorkspaceState.json index 189ff85..a4e3939 100644 --- a/.vs/VSWorkspaceState.json +++ b/.vs/VSWorkspaceState.json @@ -5,8 +5,9 @@ "\\.github\\workflows", "\\src", "\\src\\chapter2", + "\\src\\chapter3", "\\src\\chapter5" ], - "SelectedNode": "\\src\\chapter2\\stdlib.md", + "SelectedNode": "\\src\\chapter2\\scanf.md", "PreviewInSolutionExplorer": false } \ No newline at end of file diff --git a/.vs/slnx.sqlite b/.vs/slnx.sqlite index 04dad2b..3a53b6c 100644 Binary files a/.vs/slnx.sqlite and b/.vs/slnx.sqlite differ diff --git a/src/chapter2/scanf.md b/src/chapter2/scanf.md index 29784e8..19d3a33 100644 --- a/src/chapter2/scanf.md +++ b/src/chapter2/scanf.md @@ -1,22 +1,24 @@ -# Scanf() -In C programming language, scanf is a function that stands for Scan Formatted String. It is used to read data from stdin (standard input stream i.e. usually keyboard) and then writes the result into the given arguments. +# Scanf -It accepts character, string, and numeric data from the user using standard input. -scanf also uses format specifiers like printf. -## scanf Syntax -The syntax of scanf() in C is similar to the syntax of printf(). +scanf() is a function in the C programming language that stands for "Scan Formatted String." It is part of the standard input/output library (stdio.h) and is used to read data from the standard input stream (usually the keyboard) and store it into specified variables based on format specifiers. -int scanf( const char *format, ... ); -Here, +The general syntax of scanf() is: + + +int scanf(const char *format, ...); +Here's a brief explanation of the parameters: + +format: A string that contains format specifiers to specify the types of data to be read from the input. The format specifiers are preceded by a percent sign %. For example, %d is used to read an integer, %f for a floating-point number, %c for a character, %s for a string, etc. + +...: The ellipsis ... indicates that scanf() can accept a variable number of arguments. This means you can pass multiple variables to scanf() to store the data read from the input. + +To use scanf(), you need to pass the address of the variables where the input data should be stored. This is achieved by using the address-of operator & before the variable name. For example, if you want to read an integer and store it in the variable num, you would write scanf("%d", &num);. -int is the return type. -format is a string that contains the format specifiers(s). -“…” indicates that the function accepts a variable number of arguments. ## Example format specifiers recognized by scanf: %d to accept input of integers. -%ld to accept input of long integers +%ld to accept input of long integersgi %lld to accept input of long long integers @@ -26,14 +28,7 @@ format is a string that contains the format specifiers(s). %s to accept input of a string. -To know more about format specifiers, refer to this article – Format Specifiers in C - -## Example: - -int var; -scanf(“%d”, &var); -The scanf will write the value input by the user into the integer variable var. Return Value of scanf The scanf in C returns three types of values: @@ -47,31 +42,23 @@ While scanning the input, scanf needs to store that input data somewhere. To sto & is also called as address of the operator. For example, &var is the address of var. -## Example of scanf -Below is the C program to implement scanf: +Here's a simple example to illustrate how scanf() works: -// C program to implement -// scanf -#include -int main() -{ - int a, b, c; - printf("Enter the first value:"); - scanf("%d", &a); - printf("Enter the second value:"); - scanf("%d", &b); - c = a + b; - printf("%d + %d = %d\n", a, b, c); - return 0; -} +#include -//Output: -Enter the first value:58 -Enter the second value:12 -58 + 12 = 70 +int main() { + int age; + char name[50]; + printf("Enter your name: "); + scanf("%s", name); -...Program finished with exit code 0 + printf("Enter your age: "); + scanf("%d", &age); + printf("Hello, %s! You are %d years old.\n", name, age); + return 0; +} +In this example, the program prompts the user to enter their name and age. The data is read using scanf() with appropriate format specifiers, and then the information is displayed using printf(). Remember that scanf() stops reading as soon as it encounters whitespace characters, so it's not suitable for reading strings with spaces. \ No newline at end of file