Hi everyone,
Some questions in my previous post is still left unanswered.
Here it is again:

How can i accept decimal numbers from STDIN and perform arithmetic operations on it in C.

How can i create a subroutine within a program, that can be called again & again at different part of a program.
THANKS :mrgreen:

Dani AI

Generated

— the symptom you described (typing 23.45 and the program taking 23 and leaving .45 for the next input) is exactly what happens when the first input conversion does not accept the dot and stops at it, leaving the rest in stdin. is right that there are format specifiers for floats, and is also right to warn you that naive use of scanf() is brittle. The reliable pattern is: read a whole line from stdin and then parse that line. That avoids leftover characters, gives you clear error detection, and works predictably in interactive programs.

A robust reading routine using standard C:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

double read_double(const char *prompt)
{
    char buf[128], *end;
    if (prompt) { fputs(prompt, stdout); fflush(stdout); }
    if (!fgets(buf, sizeof buf, stdin)) return 0.0; /* handle EOF as needed */

    double v = strtod(buf, &end);
    while (*end && isspace((unsigned char)*end)) end++;
    if (end == buf) {
        fprintf(stderr, "Not a valid number\n");
        /* handle error */
    }
    return v;
}

Use strtod() (or strtof()/strtol() for other types) so you can detect conversion failure and any trailing garbage.

If you must use scanf(), be careful to match specifiers to types: scanf("%f", &f) expects a float*, scanf("%lf", &d) expects a double*. Passing the wrong pointer is undefined behavior. Never use fflush(stdin) to clear input (it's undefined); instead consume the rest of the line with fgets().

About subroutines: C uses functions. Declare a prototype (or place the function above main), implement the function, then call it wherever needed. Functions may call themselves (recursion) and may be called many times from different places. Use static for file-local functions and headers (.h) to share prototypes between files.

Quick troubleshooting checklist:

  • Confirm you used the right scanf specifier for the variable type.
  • Avoid mixing token-based input and line-based input without clearing the buffer.
  • Prefer line-reading + strtod() for user input to get reliable parsing and error messages.

Recommended Answers

All 4 Replies

Hi everyone,
I not very clear what you are talking about.

Receving decimal numbers???

Do you mean returning decimal numbers from a function

declare a variable as float
float a;
a = rew( ); // Is this what you mean

As for your other question

You cannot call a function within its own function.
You can declare the function externally being public or private
and call it
if there is arguments and a return value
a = gow(c,d); // Is this what you mean

if it has no arguments or any return values

gow(); //Is this what you mean

I really hope this helps you

Yours Sincerely

Richard West

For reading you use scanf() with format specifier "%f","%g","%e" or printf();
the variable tyoe is float and functions are in math.h

You need functions->read tutorials

OK, let me clear things up, suppose i want to enter 23.45 using SCANF, the program will only accept the 23 and use the .45 for the other SCANF, how can i avoid it.

First, avoid scanf. Second, use a floating point type to enter a floating point value.

#include <stdio.h>
  
  int main()
  {
     double number;
     fputs("Enter number: ", stdout);
     fflush(stdout);
     if ( scanf("%lf", &number) == 1)
     {
  	  printf("number = %g\n", number);
     }
     return 0;
  }
  
  /* my output
  Enter number: 23.45
  number = 23.45
  */
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.