Use scanf in if statement - c

Why the output is '4' ? , while input is 'A'
int main() { int a = 5;
if(scanf("%d",&a))
printf("%d",a+1);
else
printf("%d",a-1);
}

When type sepcifier %d is used then the function expects that a valid signed decimal integer number will be entered. It is evident that symbol 'A' is not a number so the input failed.
Now a question arises: what will the function return in this case?
According to the description of function scanf (the C Standard, 7.21.6.4 The scanf function):
3 The scanf function returns the value of the macro EOF if an input
failure occurs before the first conversion (if any) has completed.
Otherwise, the scanf function returns the number of input items
assigned, which can be fewer than provided for, or even zero, in the
event of an early matching failure.
So the condition in the if statement
if(scanf("%d",&a))
evaluated to 0 (false) and the else statement was executed.
else
printf("%d",a-1);
that outputed 4.
Take into account that in general you may enter symbol 'A' for integers. But in this case the corresponding integer variable must be declared as unsignedand format specifier of scanf must to be %x
For example
#include <stdio.h>
int main( void )
{
unsigned int a = 5;
if ( scanf( "%x", &a ) )
{
printf( "%u\n", a + 1 );
}
else
{
printf( "%u\n", a - 1 );
}
}
In this case the output will be 11.:) scanf will consider symbol 'A' as a hexadecimal representation of 10.

You instructed scanf to read a signed integer. The input didn't even start with a sign or digit, so scanf didn't find a signed integers, so scanf returned 0 (the number of input items successfully matched and assigned), dropping you into the else clause of the if. Since scanf didn't match anything, it didn't assign anything, meaning a wasn't changed, and a-1 is 4.
You didn't specify what you were expecting, so I don't know what fix to suggest.

In scanf() All the arguments you pass are pointers. There's no default-pro­mo­tion among pointers, and it is crucial that you pass the exact format specifier that matches the type of the pointer.
for more clearance:-
Click here!

Related

Reading ASCII character causes an infinite loop

If you enter integer value the loop works just fine but if you enter some ASCII character or float type value the program goes into an infinite loop. Any fix for this issue?
Technically, char is one type of int so this shouldn't be a problem.
#include <stdio.h>
int main(void)
{
int num;
scanf("%d",&num);
while (num < 40)
{
printf("Number is small\n");
scanf("%d",&num);
}
return 0;
}
The function scanf will try to read a decimal integer from stdin. It uses the definition of a decimal integer from strtol:
The valid integer value consists of the following parts:
(optional) plus or minus sign
(optional) prefix (0) indicating octal base (applies only when the base is 8 or ​0​)
(optional) prefix (0x or 0X) indicating hexadecimal base (applies only when the base is 16 or ​0​)
a sequence of digits
In other words, scanf will try to interpret a sequence of characters on stdin as a decimal integer. It will not do a character to integer conversion based on the ASCII table.
The solution to your problem lies in checking the return value of scanf, which is:
Number of receiving arguments successfully assigned (which may be zero in case a matching failure occurred before the first receiving argument was assigned), or EOF if input failure occurs before the first receiving argument was assigned.
So, check if scanf did not return 1. In this case, it has not read a decimal integer and num has not been assigned a value and should not be used.
In the given program, if num has never been assigned a value, its value is indeterminate. This could be a value <40, explaining the infinite loop as scanf repeatedly tries to read a non-integer from stdin, fails and leaves the non-integer on stdin as is. If num has been assigned a value before, it will still hold that value after a failed scanf call.

What is the output of: printf("%d\n",scanf("%d",&i))?

What is the output of this code if I input 25 to scanf()? I run this program, the output is 1 but I don't understand why? Can anyone explain?
#include <stdio.h>
#include <stdlib.h>
int main() {
int i;
printf("%d\n",scanf("%d",&i));
return 0;
}
scanf() returns the number of arguments successfully assigned. In your case, scanf only has one directive which has a respective argument to be assigned, so it returns 1 if the input conversion was done successfully.
1 is given now as argument to printf, which prints 1 into the output.
With the premise that the scanf() conversion was successfully,
printf("%d\n", scanf("%d",&i));
is basically equivalent to
printf("%d\n", 1);
Going straight forward
int i = 0, j = 0;
printf("%d\n", scanf("%d %d", &i, &j));
with the input of
25(enter)
50(enter)
and both conversions done successfully, would gave you the output 2 and
printf("%d\n", scanf("%d %d", &i, &j));
would basically be equivalent to
printf("%d\n", 2);
Side Note:
The return value of scanf() should always be checked by the algorithm but it is more an objective to the program to see if an input failure occurred than to diagnose it directly to the user.
These functions (scanf family) return the number of input items successfully matched and assigned.
So in this case it can return 1 if the scan was successful or 0 if not. If there is an error in the input stream it can also return EOF
Scanf is a basic function in C language.Since every function return a type of value or null,Scanf function returns numbers of inputs scanned successfully.
For Example: Scanf("%d%d") scans for 2 inputs, so it returns value 2.
Scanf("%f%d%d%d") scans for 4 inputs, so it returns value 4.
&variable_name is used to store the scanned value in the variable by using address-of operator.For scanning string, &-address-of operator can be neglible.
Example scanf("%s",variable)
Printf is a function which returns number of characters printed.It can be used with format specifer like %d,%f etc.
In the above program
printf("%d\n",scanf("%d",&i));
Scanf is first executed by scanning one integer input as %d is specified and stored in the variable of type int. Scanf returns value 1 since it is scanning only one input.
Printf function prints the value 1.Printing value does not depend upon input value.It depends only on number of inputs , scanf is scanned successfully.
you are outputting the scanf function which returns the variable numbers not the value in i.

How does a scanf() inside printf() work?

#include<stdio.h>
int main(){
int n;
printf("%d\n",scanf("%d",&n));
return 0;
}
I wonder why the output of this program is always '1' ?!
What's actually happening here ?
I suspect you wanted to use
int n;
scanf("%d", &n);
printf("%d\n", n);
but what you wrote is equivalent to
int n;
int num = scanf("%d", &n); // num is the number of successful reads.
printf("%d\n", num);
The program is just about exactly equivalent to
#include <stdio.h>
int main() {
int n;
int r = scanf("%d", &n);
printf("%d\n", r);
return 0;
}
So if you run this program, and if you type (say) 45, then scanf will read the 45, and assign it to n, and return 1 to tell you it has done so. The program prints the value 1 returned by scanf (which is not the number read by scanf!).
Try running the program and typing "A", instead. In that case scanf should return, and your program should print, 0.
Finally, if you can, try running the program and giving it no input at all. If you're on Unix, Mac, or Linux, try typing control-D immediately after running your program (that is, without typing anything else), or run it with the input redirected:
myprog < /dev/null
On Windows, you might be able to type control-Z (perhaps followed by the Return key) to generate an end-of-file conditions. In any of these cases, scanf should return, and your program should print, -1.
#jishnu, good question and the output which you're getting is also correct as you are only reading one value from standard input (stdin).
scanf() reads the values supplied from standard input and return number of values successfully read from standard input (keyboard).
In C, printf() returns the number of characters successfully written on the output and scanf() returns number of items successfully read. Visit https://www.geeksforgeeks.org/g-fact-10/ and read more about it.
Have a look at the following 2 code samples.
Try the below code online at http://rextester.com/HNJE76121.
//clang 3.8.0
#include<stdio.h>
int main(){
int n, n2;
printf("%d\n",scanf("%d%d",&n, &n2)); //2
return 0;
}
In your code, scanf() is reading only 1 value from the keyboad (stdin), that is why output is 1.
//clang 3.8.0
#include<stdio.h>
int main(){
int n;
printf("%d\n",scanf("%d",&n)); //1
return 0;
}
In your program scanf returns 1 when successfully some value is taken by scanf into n and that is why you are always getting 1 from your printf("%d\n",scanf("%d",&n));.
You should modify your code like following
#include<stdio.h>
int main(){
int n;
scanf("%d",&n);
printf("%d\n",n);
return 0;
}
In man scanf,
Return Value
These functions return the number of input items successfully matched and assigned, which can be fewer than provided for, or even zero in the event of an early matching failure.
In your program, it just match 1 input, so the function scanf will return 1, if you input an legal value that can match a %d, else it will return 0.
In your situation, you might always satisfy this requirements, so it always return 1.
scanf returns the number of successful conversion and assignments. In your case, you're only scanning for one argument, so scanf will either return a 1 on success, a 0 on a matching failure (where the first non-whitespace input character is not a decimal digit), or EOF on end-of-file or error.
When you call printf, each of its arguments is evaluated and the result is passed to the function. In this case, evaluation involves calling the scanf function. The value returned by scanf is then passed to printf.
It's essentially the same as writing
int count = scanf( "%d", &n );
printf( "%d\n", count );
For giggles, see what happens when you enter abc or type CtrlD (or CtrlZ on Windows).
Note that printf also returns a value (the number of bytes written to the stream), so you can write something ridiculous1 like
printf( "num bytes printed = %d\n", printf( "num items read = %d\n", scanf( "%d", &n ) ) );
Joke. Don't do that.
scanf returns number of successful conversions.
Try changing your scanf to as shown below....
you will notice printing 2 after inputting two valid integers.
int n1 =0;
int n2 =0;
int i = scanf("%d %d",&n1,&n2);
In C programming, scanf() takes the inputs from stdin() which is the keyboard and returns the number of successful inputs read
and printf() returns the number of characters written to stdout() which is the display monitor.
In your program scanf() is reading only one input which is getting returned and is printed by printf(). So, whatever may be the input the output is always 1 as it is reading only one input.
Here take a look at this C program:
#include<stdio.h>
int main(){
int n, n2;
printf("%d\n",scanf("%d %d",&n,&n2));
return 0;
}
It takes input or reads two integers from stdin() using scanf(), so the output will always be 2.
**Note: The output is 0 if your input is not integer, as it reads integer only.
The documentation of scanf() says the return value is:
Number of receiving arguments successfully assigned (which may be zero in case a matching failure occurred before the first receiving argument was assigned), or EOF if input failure occurs before the first receiving argument was assigned.
The code you would instead want is:
#include<stdio.h>
int main(){
int n;
scanf("%d",&n);
printf("%d\n",scanf("%d",&n));
return 0;
}
If you use this code as an example,
It shows : warning: implicit declaration of function 'printf' [-Wimplicit-function declaration]
int main()
{
char b[40];
printf(" %d", scanf("%s", b));
getchar();
}
Return value : -1
Outputs of the functions like printf and scanf can be use as a parameter to another function.
scanf() is a function which is integer return type function. It returns total number of conversion characters in it. For ex if a statement is written as :
int c;
c=scanf("%d %d %d");
printf("%d",c);
Then output of this program will be 3.
This is because scanf() is integer return type function and it returns total no of conversion characters, thus being 3 conversion characters it will return 3 to c.
So in your code scanf() returns 1 to printf() and thus output of program is 1.
This is because if more than one statement are used in printf() execution orders starts from right to left.
Thus scanf() is executed first followed by printf().

What happens if C tries to scan character in integer variable [duplicate]

This question already has answers here:
Why does scanf fail with floats?
(3 answers)
Closed 8 years ago.
So I was wondering what happens if the user enters characters in integer variables for example:
main()
{
int number;
printf("Print a number:");
scanf(" %d", &number);
printf("The result is:%d", number);
return 0;
}
I typed in characters and the result is: 1986895412
is this 1986895412 a place in the ram ??
In this case, scanf directive just fails. Quoting this answer (which essentially rephrases the spec's definition):
The %d conversion specifier expects the input text to be formatted as
a decimal integer. If it isn't, the conversion fails and the character
that caused the conversion to fail is left in the input stream.
So, number remains with the same value it had before the directive. As you didn't initialize it with some defined value (like int number = 0), it'll be just some random garbage value. In your case that happened to be equal to 1986895412.
Check the return of scanf(). When you type in characters, the correspond result will occur.
if (1 == scanf("%d", &number)) {
printf("The result is:%d", number);
}
else {
printf("Invalid data entered. `number` not changed\n");
}
Note: Code's int number; is not initialized, so its value could be any int. With invalid input, number was not changed and code printed the uninitialized number which just happeded to have the value of "1986895412". It may differ tomorrow.
Note: leading space in " %d" is not needed as %d itself consume leading whitespace.
As number had not been initialized and scanf() failed the printf() provokes undefined behaviour reading the uninitialised variable number. Anything could happen.
So there are two lessons to learn here:
Initialise variables when they are defined. (If you do not know to what to initialise them you most probably do not need them, at least not where you are trying to define them)
Perform error checking on calls which would return garbage if failed.

entering a char using scanf and saving it in an int can cause undefined behaviour?

I am working on a code, and I tried to enter a char instead of integer, and the result was '2' regardless of the character I entered, is it undefined behaviour or some thing else ?
The code:
#include <stdio.h>
int f1(int n);
int f2(void);
int main(void)
{
int t;
printf("Enter a number: ");
scanf("%d", &t);
/* print proper message */
t ? f1(t) + f2() : printf("zero entered.\n");
return 0;
}
int f1(int n)
{
printf("%d ", n);
return 0;
}
int f2(void)
{
printf("entered.\n");
return 0;
}
when I entered a, the result was "2 entered", and when I entered g the result was "2 entered" and when I entered i,h,k,..... the result was the same. What is that?
If scanf() encounters something it cannot parse based on the specified format string, it simply stops and returns early. So it never writes anything to t (you're just seeing whatever indeterminate value t had before the call).
To handle this, you should always examine the return value of scanf.
It is because scanf failed to parse you input. It expects you to enter a decimal digits since you used %d.
You have to check the return value of scanf to avoid this kind of behavior :
On success, the function returns the number of items of the argument list successfully filled. This count can match the expected number of items or be less (even zero) due to a matching failure, a reading error, or the reach of the end-of-file.
So :
int items_matched;
// ...
items_matched = scanf("%d", &t); // Get the return value
if ( items_matched != 1 ) // Check it
{
printf("Matching failure with scanf.\n");
return 0;
}
else
{
if ( t == 0 )
printf("zero entered\n");
else
printf("%d entered\n", t);
}
You don't need your f1(t) + f2() who is quite confusing...
The problem is, as you say, that scanf using %d format is expecting you to enter one or more ascii digits. It stops scanning the input at a non-digit character, so it never reads what you type and t has whatever value it had before the scant was called.
You need to check the return code of scanf. Have a look at the manpage:
Return Value
These functions return the number of input items successfully matched and assigned, which can be fewer than provided for, or even zero in the event of an early matching failure.
The value EOF is returned if the end of input is reached before either the first successful conversion or a matching failure occurs. EOF is also returned if a read error occurs, in which case the error indicator for the stream (see ferror(3)) is set, and errno is set indicate the error.
In other words, scanf does not write anything into t, it's uninitialized the whole time.
So, instead of this:
scanf("%d", &t);
try this:
int items_matched = scanf("%d", &t);
if (items_matched != 1) {
printf("bad number entered\n");
exit(1);
}
From the Linux man page
The format string consists of a sequence of directives which describe
how to process the sequence of input characters. If processing of a
directive fails, no further input is read, and scanf() returns. A
"failure" can be either of the following: input failure, meaning that
input characters were unavailable, or matching failure, meaning that
the input was inappropriate.
In the C11 standard description of scanf (7.21.6.4 The scanf function) section 3:
The scanf function returns the value of the macro EOF if an input
failure occurs before the first conversion (if any) has completed.
Otherwise, the scanf function returns the number of input items
assigned, which can be fewer than provided for, or even zero, in the
event of an early matching failure.
Emphasis is mine . So as #oli charlesworth said you should check the return value of scanf when in doubt :)

Resources