How to Input Double Precision Floating Point Numbers in C: Step-by-Step Guide with Error Handling

How to Input Double Precision Floating Point Numbers in C: Step-by-Step Guide with Error Handling

Want to learn how to input double precision floating point numbers in C? This beginner-friendly tutorial walks you through the process step-by-step, showing how to use fgets and sscanf to get user input, validate it, and handle errors like a pro. With clear code examples and a simple program demo, you’ll see how to check if input succeeds or fails. Perfect for C programming newbies or anyone looking to sharpen their skills. Subscribe for more practical coding tips and tutorials!

Introduction 00:00:00
Overview of Inputting Doubles 00:00:02
Program Setup and Includes 00:00:44
Main Function Explanation 00:01:13
Get Input Function Introduction 00:02:10
Defining Get Input Function 00:02:34
Character Buffer Creation 00:02:54
Using fgets for Input 00:03:49
Parsing Input with sscanf 00:06:33
Handling Success and Failure 00:08:07
Running the Program 00:09:05
Testing with Valid Input 00:09:20
Testing with Invalid Input 00:10:05
Conclusion and Call to Action 00:11:50

Thanks for watching!

Find us on other social media here:

  • https://www.NeuralLantern.com/social

Please help support us!

  • Subscribing + Sharing on Social Media
  • Leaving a comment or suggestion
  • Subscribing to our Blog
  • Watching the main “pinned” video of this channel for offers and extras

Hey there!

In this video I’m going to show you how to input a double precision floating point number

in C from the user into your program and decide whether or not it has succeeded or failed.

Okay so for starters I just want you to know that this video is not about generating makefiles.

own make files or how to compile link execute or even the basics of c see my other videos for now

i’m just going to assume that you know how to make a make file here it is basically very quickly

but i’m just i’m just compiling a simple program here’s my program here it’s called main.c it

doesn’t really have anything else to it i’m going to start off by importing a couple includes or a

few includes that’ll just help me get some stuff that i need so here’s an include for the standard

input output here’s an include for the standard library here’s an include that

helps with c string functions here’s an include that helps with limits not

actually sure every single one of those includes is needed but we’ll see anyway

here’s my main program or I guess my main function since this program only is

just one module notice how we have a double called value here and then we

make a call to a function called get input and we give a pointer to to the

double as an argument to get input so basically get input should be able to just return to us

the value by reference and then get input will be able to tell us whether or not it has succeeded

as in has the user entered valid input by its return type or not not its return type but its

return value so the result is going to be a long from get input and that’ll indicate

runs we’re just going to print you know get input returned and then print the return value that it

returned and then it’s going to print the double that it inputted from the user and then a new line

so nothing uh nothing too big of a deal let me show you the main workhorse function that has

all the logic you need to input from the user and check that the input was valid so this is called

get input i’m sticking it on top of the main function because i don’t want to deal with

because I don’t want to deal with prototypes right now.

It’s a better idea to put prototypes up at the top of your source code file

if your source code is even a little bit complicated.

But in this case, it’s just one other function.

So I will just put get input on top of main.

That way, when the compiler is scanning the source code,

it will have already seen that get input exists before main calls it,

and it’ll compile.

So first thing I’m going to do is say that get input returns along

get input returns along like we just saw and it takes in a pointer to a double no references just

a pointer then we have to create a character buffer and what that’s going to be for is basically

you know the user is going to type some characters and i want that to go into the buffer so um

a line max is the number of characters that the buffer can hold and that’s uh i think where we

I think where we have limits, I think limits is providing that for us.

But basically, you know, you could stick a number here if you wanted to like an eight

character, eight kilobyte buffer, if you wanted like eight, one, nine, two, or just whatever.

I’m just putting line max so that it sort of aligns with the system’s idea of what

the maximum size of a line should be.

Okay.

So then I don’t know why I have it say grab side one.

I’ll just say grab input here.

Let me change my solution real fast.

Grab input.

That’s probably because that was part of a program.

that’s probably because that was part of a program in the past so then the function that I’m going to

actually use is called fget s which basically is saying let’s uh let’s input a string a c string

from a file and we’re providing the target for the input so that’s going to be the character string

right there and then we’re going to provide um the maximum number of characters that we’ll accept

length of the buffer and then what file are we going to input from standard

input if you don’t know standard input see my other videos but it’s basically a

special file handle that means when the program first launches you know the

operating system sort of gives it a special file handle for standard input

standard output standard error and so this will be basically the user typing

on the terminal so f get s will essentially take input from standard

terminal until they you know hit like a new line or they disconnect the input

buffer it will take up to this many characters line max which should match

the buffer whatever value you put in the buffer and then it will write the

characters into the buffer that’s why you have to match the length of the

buffer so basically here this function call will return something if it returns

a zero then that means we have failed so if it returns null then we’re going to return zero

meaning fail so get input is kind of a boolean even though we don’t have booleans in c and it’s

basically going to return a zero on a failure and a one on success so like you know zero is a falsy

value and one is a truely value so we’re not going to use system style return codes where zero is

codes where zero is success we’re going to use boolean style return codes where a zero is failure

and one is uh success so you can see i’ve kind of chained two commands here i’ve decided to say all

right um we’ll first grab some input into the buffer array with f get s if that is null then

we immediately return zero because this this logical or operator says if one or the other thing

is equal to a true then just go ahead and evaluate to true so here I’m asking like did the call

equal null if the answer is yes then that part will evaluate to true and then it’ll basically

short circuit the rest of that logical expression and just return zero right away and then if the

previous command succeeds like the fget s succeeds then it will look at the buffer and try to scan it

for valid input.

So the left side is just taking raw input.

The right side is parsing for certain data type.

So you can see sscanf,

we’re scanning a buffer that we put into the first argument.

So that’s at that point, by the time sscanf gets called,

the buffer should be filled

with whatever input the user typed.

So we’re scanning that

and then here’s a little formatting argument,

which is very similar to,

use the same tokens basically as printf. So, you know, here we’re saying percent lf. So we’re

looking for a long float, which means a double precision floating point number. And that

percentage just means like, here’s a special token that comes after it to describe what sort of

value we’re looking to scan for. So then if we’re successful, if it actually scans,

the double. So fscanf expects a pointer to the variable that’s going to receive the value that

has been scanned if it is scanned correctly. So notice how the double we don’t have an ampersand

in front of it because it’s already a pointer because it came into us as a pointer in the first

place. So we’re going to look at the buffer that the user just typed, the user just provided,

and we’re going to try to scan for a double precision floating point number. If we’re

then the system should write that double into the variable referred to by the

pointer known as the double if all that succeeded then the value should be

greater than or equal to zero if these if this command failed then the value

should be less than one so that basically means if we have a value of

less than one then we have failed and we’re going to return zero so now whether

the initial scanning sorry whether the initial input fails or the parsing of

the users input fails then we’re gonna end up returning zero for fail so then

in main you can see all we’re doing is we’re just calling on input we’re giving

it a pointer to the double that we want to populate and then we’re looking at

the result so in your you know program logic you can do something like if the

result is zero then assume we failed and if the result is one assume we succeeded

And if the result is one, assume we succeeded and, you know, respond to the user or, you

know, do something that’s appropriate for success or fail.

I think that’s everything that I need to explain.

Let’s try to run the program now.

Open a terminal.

I want to do make run and see if that works.

Maybe I’ll do a clear here.

Okay.

So I didn’t print any, you know, prompting or anything special.

so the cursor is just blinking at this point i can just enter 33.12 i guess whatever hit enter

and the program now says get it get input returned one which means success so the value that i entered

was a valid double and the value is now 33.12 so it successfully grabbed the float keep in mind

these zeros or just precision related issues are related to the printing of the double not

not necessarily related to what is inside of the double so I’m just saying

you know print along float you can you can mess with print F a little bit more

if you want to have a different value printed or more precision or less

percent precision printed let’s try to do this with some invalid input I’m just

going to do some letters and hit enter notice how the value didn’t actually

change it’s it started or actually it did change it got set to a zero because

initialized it as junk data so it got set to a zero but then more importantly get input returns

zero as its return code so when you see zero that means the command failed so you should not consider

the double as valid in any way you shouldn’t use it you should complain to the user or do whatever

and that is how to get input let’s just try something for fun double value equals 3.333

bad input i’m assuming it might but i haven’t done this in a while so we’ll see so bad input

and looks like it did not change it so whatever junk data was already in value by the time you

called on get input if get input failed then the value should probably be unchanged but again more

importantly the return value is zero meaning fail because zero is a falsy value so i’m going to get

so i’m going to get rid of that so i can go back to my solution there okay so now hopefully you are

uh you’re on your way to start getting input in c at least in the in in terms of longs uh you can

look up the formatting codes for print f if you would like to input different types of data like

so this video is just for long floats doubles uh you can you can look up all other all sorts of

five years I’ll make another video that has different types of tokens but I guess that’s

it for this video. Thanks for watching I hope you learned a little bit and had a little bit of fun.

See you in the next one.

Hey everybody thanks for watching this video again from the bottom of my heart I really

appreciate it. I do hope you did learn something and have some fun. If you could do me a please

subscribe and follow this channel or these videos or whatever it is you do

on the current social media website that you’re looking at right now it would

really mean the world to me and it’ll help make more videos and grow this

community so we’ll be able to do more videos longer videos better videos or

just I’ll be able to keep making videos in general so please do do me a

kindness and and subscribe you know sometimes I’m sleeping in the middle of

the night and I just wake up because I know somebody subscribed or followed it

followed it just wakes me up and I get filled with joy that’s exactly what

happens every single time so you could do it as a nice favor to me or you could

you control me if you want to just wake me up in the middle of the night just

subscribe and then I’ll just wake up I promise that’s what will happen also if

you look at the middle of the screen right now you should see a QR code which

you can scan in order to go to the website which I think is also named

somewhere at the bottom of this video and it’ll take you to my main website

where you can just kind of like see all the videos I published and the services

videos I published and the services and tutorials and things that I offer and all that good stuff

and uh if you have a suggestion for uh uh clarifications or errata or just future videos

that you want to see please leave a comment or if you just want to say hey what’s up what’s going on

you know just send me a comment whatever I also wake up for those in the middle of the night I

get I wake up in a cold sweat and I’m like it would really it really mean the world to me I

it. So again, thank you so much for watching this video and enjoy the cool music as I fade

into the darkness, which is coming for us all.

Thank you.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply