Skip to content

Commit

Permalink
Added information about scanf
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhishekm0410 committed Jul 19, 2023
1 parent f20c258 commit 4b7d234
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 0 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Empty file.
Binary file added .vs/HPC-Training/v17/.wsuo
Binary file not shown.
3 changes: 3 additions & 0 deletions .vs/ProjectSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"CurrentProjectSetting": null
}
12 changes: 12 additions & 0 deletions .vs/VSWorkspaceState.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"ExpandedNodes": [
"",
"\\.github",
"\\.github\\workflows",
"\\src",
"\\src\\chapter2",
"\\src\\chapter5"
],
"SelectedNode": "\\src\\chapter2\\stdlib.md",
"PreviewInSolutionExplorer": false
}
Binary file added .vs/slnx.sqlite
Binary file not shown.
77 changes: 77 additions & 0 deletions src/chapter2/scanf.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# 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.

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().

int scanf( const char *format, ... );
Here,

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

%lld to accept input of long long integers

%f to accept input of real number.

%c to accept input of character types.

%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:

1)>0: The number of values converted and assigned successfully.<br>
2)0: No value was assigned.<br>
3)<0: Read error encountered or end-of-file(EOF) reached before any assignment was made.<br>

Why &?
While scanning the input, scanf needs to store that input data somewhere. To store this input data, scanf needs to known the memory location of a variable. And here comes the ampersand to rescue.

& 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:


// C program to implement
// scanf
#include <stdio.h>

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;
}

//Output:
Enter the first value:58
Enter the second value:12
58 + 12 = 70


...Program finished with exit code 0

0 comments on commit 4b7d234

Please sign in to comment.