0

Purpose:

If user input b is a float number prints floor(b), round(b), ceil(b).

Else prints scanf error: (%d)\n.

The instruction (provided by our teacher) has a code like this, which I don't understand.

Here's my code: `

#include <stdio.h>
#include <math.h>

int main(void) {
    float b;
    printf("Eneter a float number");
    int a=0;
    a=5;
    a=scanf("%d", &b);
    if (a=0)
    {
        printf("scanf error: (%d)\n",a);
    }
    else
    {
        printf("%g %g %g",floor(b), round(b), ceil(b));
    }
    return 0
}
8
  • perhaps reading this may help - man scanf Commented Jan 20, 2017 at 17:46
  • 2
    You can't use "%d" for a float. Commented Jan 20, 2017 at 17:46
  • @EdHeal: Also compiling with -Wall (or equivalent, depending on compiler). GCC -Wall would've picked up both the a=0 and the %d errors. Commented Jan 20, 2017 at 17:54
  • 1
    "The instruction (provided by our teacher) has a code like this, which I don't understand. " --> Ask the teacher. Else you don't have a "teacher". Commented Jan 20, 2017 at 17:55
  • 1
    Also, please fix the spelling of Enter Commented Jan 20, 2017 at 18:14

2 Answers 2

4

Mistake # 1

if (a=0)  // condition will be always FALSE

must be

if (a==0)

or better

if (0 == a)

Mistake # 2

scanf("%d", &b); // when b is float

instead of

scanf("%f", &b);

UPDATE:

Actually, for case of checking results of scanf I personally prefer to use != with number of values entered with the last scanf. E.g. if two comma separated integers required to continue calculation snippet can be:

int x, y;
int check;
do{
    printf("Enter x,y:");
    check = scanf("%d,%d", &x, &y); // enter format is x,y
    while(getchar()!='\n'); // clean the input buffer
}while(check != 2);

that loop will re-ask for input if check is not 2, i.e. if it is 0 (when even the first value is incorrect, e.g. abc,12) or if it is 1 (when user forgot comma or enter not a number after comma, e.g. 12,y

Sign up to request clarification or add additional context in comments.

4 Comments

Yoda is not better (modern compilers can pick this problem up)
@EdHeal you have proven his teacher is stupid on the basis of standards but he is not coz he use compilers and not just read those standards .
Why is 0 == a better than a==0?
0 == a better than a==0 because compiler find error 0=a at compile time, but think that a=0 is correct when you make a mistake in a==0
1

Code with corrections and comments - also available here - http://ideone.com/eqzRQe

#include <stdio.h>
#include <math.h>

int main(void) {
    float b;
//    printf("Eneter a float number");
    printf("Enter a float number"); // Corrected typo
    fflush(stdout); // Send the buffer to the console so the user can see it
    int a=0;
//    a=5; -- Not required
    a=scanf("%f", &b); // See the manual page for reading floats
    if (a==0) // Need comparison operator not assignemnt
    {
        printf("scanf error: (%d)\n",a); // A better error message could be placed here
    }
    else
    {
        printf("%g\n", b); // Just to check the input with ideone - debugging
        printf("%g %g %g",floor(b), round(b), ceil(b));
    }
    return 0; // You need the semi-colon here
}

For VenuKant Sahu benefit

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.

1 Comment

Please tell me if I'm right. scanf("%f", &b) is a function, after it runs it returns 1 or 0, 1 means it runs, 0 means it does not. Can I consider it as a Boolean value then?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.