- 4 days ago
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
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
Category
🤖
TechTranscript
00:00Hey there! In this video I'm going to show you how to input a double precision floating
00:05point number in C from the user into your program and decide whether or not it has succeeded or
00:14failed. Okay so for starters I just want you to know that this video is not about generating
00:25make files. If you want to learn more about how to make your own make files or how to compile
00:29link execute or even the basics of C see my other videos. For now I'm just going to assume that you
00:35know how to make a make file here it is basically very quickly but I'm just I'm just compiling a
00:41simple program here's my program here it's called main.c it doesn't really have anything else to it
00:47I'm going to start off by importing a couple includes or a few includes that'll just help
00:52me get some stuff that I need so here's an include for the standard input output here's an include for
00:58the standard library here's an include that helps with c string functions here's an include that
01:02helps with limits not actually sure every single one of those includes is needed but we'll see
01:08anyway here's my main program or I guess my main function since this program only is just one module
01:15notice how we have a double called value here and then we make a call to a function called get input
01:20and we give a pointer to uh to the double as an argument to get input so basically get input should
01:28be able to just return to us uh the value by reference and then get input will be able to tell us
01:36whether or not it has succeeded as in has the user entered valid input by its return type or not
01:42not its return type but its return value
01:44so the result is going to be a long from get input and that'll indicate a success or failure
01:51after that function runs we're just going to print you know uh get input returned and then print the
01:58return value that it returned and then it's going to print the double that it inputted from the user
02:03and then a new line so nothing uh nothing too big of a deal let me show you the main workhorse function
02:10that has all the logic you need to input from the user and check uh that the input was valid so this
02:16is called get input i'm sticking it on top of the main function because i don't want to deal with
02:21prototypes right now um it's a better idea to put prototypes up at the top of your source code file
02:27if your source code is even a little bit complicated but in this case it's just one other function
02:31so i will just put get input on top of main that way uh when the compiler is scanning the source code
02:39it will have already seen that get input exists before main calls it and it'll compile so first
02:45thing i'm going to do is say that get input returns along like we just saw and it takes in a pointer
02:51to a double no references just a pointer then we have to create a character buffer and um what that's
03:00going to be for is uh basically you know the user is going to type some characters and i want that to
03:08go into the buffer so um line max is the number of characters that the buffer can hold and that's uh
03:15i think where we have limits i think limits is providing that for us but basically you know
03:20you could stick a number here if you wanted to like an eight character uh eight kilobyte buffer if you
03:26wanted like eight one nine two or just whatever i'm just putting line max uh so that it sort of aligns
03:32with the system's idea of what uh the maximum size of a line should be okay so then i don't know why
03:38i have it say grab side one i'll just say grab input here let me change my solution real fast grab input
03:44that's probably because that was part of a program in the past so then uh the function that i'm going
03:49to actually use is called f get s which basically is saying let's uh let's input a string a c string from
04:00a file and we're providing the target for the input so that's going to be the character string
04:05right there and then we're going to provide um the maximum number of characters that will accept
04:11which is the length of the buffer and then what file are we going to input from standard input if
04:16you don't know standard input see my other videos but it's basically a special file handle that means
04:22um when the program first launches you know the operating system sort of gives it a special file
04:27handle for standard input standard output standard error and so this will be basically the user
04:32typing on the terminal so f get s will essentially take input from standard input the user typing on the
04:40terminal uh until they you know hit like a new line or they disconnect the input buffer it will take up to
04:46this many characters line max which should match the buffer whatever value you put in the buffer
04:50um and then it will write the characters into the buffer that's why you have to match the length of
04:57the buffer so basically here um this function call will return something if it returns null like if it
05:07returns a zero then that means we have failed so if it returns null then we're going to return a zero
05:15meaning fail so get input is kind of a boolean even though we don't have booleans in c and it's
05:21basically going to return a zero on a failure and a one on success so like you know zero is a falsy value
05:31and one is a truly value so we're not going to use system style return codes where zero is success
05:38we're going to use boolean style return codes where uh zero is failure and one is uh success
05:44so you can see i've kind of chained two commands here i've decided to say all right um
05:51we'll first grab uh some input into the buffer array with f get s if that is null then we immediately
05:58return zero because this this logical or operator says if one or the other thing is equal to a uh a true
06:07then just go ahead and evaluate to true so here i'm asking like did the call equal null if the
06:13answer is yes then that part will evaluate to true and then it'll basically short circuit the rest of
06:18that logical expression and just return zero right away and then if the previous command succeeds like
06:25the f get s succeeds then it will look at the buffer and try to scan it and parse it for valid input so
06:34the left side is just taking raw input the right side is parsing for a certain data type so you can
06:40see sscanf we're scanning a a buffer that we put into the first argument so that's at that point by the
06:48time sscanf gets called the buffer should be filled with whatever input the user typed so we're scanning
06:55that and then here's a little formatting argument which is very similar to well we can use the same
07:00tokens basically as print f so you know here we're saying percent lf so we're looking for a long float
07:09which means a double precision floating point number and that percentage just means like here's a special
07:14token that comes after it to describe what sort of uh value we're looking to scan for so then uh if we're
07:22successful if it actually scans then we're going to stick that to value into the double so f scan f
07:30expense expects a pointer to the variable that's going to receive the value that has been scanned
07:37if it is scanned correctly so notice how the double we don't have an ampersand in front of it because
07:42it's already a pointer because it came into us as a pointer in the first place so we're going to look at
07:47the buffer that the user just typed the user just provided and we're going to try to scan for a double
07:53precision floating point number if we're successful then the system should write that double into the
07:59variable uh referred to by the pointer known as the double if all that succeeded then the value should be
08:08greater than or equal to zero if these if this command failed then the value should be less than one
08:14uh so that basically means if we have a value of less than one then we have failed and we're going
08:21to return zero so now whether the initial scanning sorry whether the initial input fails or the uh
08:30parsing of the user's input fails then we're going to end up returning zero for fail so then in main you
08:37can see all we're doing is we're just calling on input we're giving it a pointer to the double that we
08:42want to populate and then we're looking at the result so in your you know program logic you can
08:47do something like if the result is zero then assume we failed and if the result is one assume we succeeded
08:54and you know respond to the user or you know do something that's appropriate for success or fail
09:00um i think that's everything that i need to explain let's try to run the program now open a terminal
09:06um i'm gonna do make run and see if that works maybe i'll do a clear here
09:16okay so i didn't print any you know prompting or anything special so the cursor is just blinking
09:21at this point i can just enter 33.12 i guess whatever hit enter and the program now says get it get input
09:30returned one which means success so the value that i entered was uh was a valid double and the value
09:37is now 33.12 so it successfully grabbed the float keep in mind these zeros or just precision related
09:45issues are related to the printing of the double not necessarily related to what is inside of the double
09:51so i'm just saying you know print a long float you can you can mess with printf
09:55a little bit more if you want to have a different uh value printed or more precision or less percent
10:01precision printed let's try to do this with some invalid input i'm just going to do some letters
10:06and hit enter notice how the value didn't actually change it's it started or actually it did change it
10:12got set to a zero um because we initialized it as junk data so it got set to a zero but then more
10:20importantly uh get input returns zero as its return code so when you see zero that means the command
10:27failed so you should not consider the double as valid in any way you shouldn't use it you should
10:32complain to the user or do whatever um and that is how to get input let's just try something for fun
10:40double value equals 3.333 and then we'll see if it gets changed on bad input i'm assuming it might but
10:47i haven't done this in a while so we'll see so bad input and it looks like it did not change it so
10:54whatever junk data was already in value by the time you called on get input if get input failed then
11:01the value should probably be unchanged but again more importantly the return value is zero meaning fail
11:09because zero is a falsy value so i'm going to get rid of that so i can go back to my solution there
11:14okay so now hopefully you are uh you're on your way to start getting input in c at least in the in
11:21in terms of longs uh you can look up the formatting codes for print f if you would like to input uh
11:29different types of data like so this video is just for long floats doubles uh you can you can look up all
11:35other all sorts of other tokens i don't know maybe in like five years i'll i'll i'll make another video
11:42that has different types of uh tokens but uh i guess that's it for this video thanks for watching
11:48i hope you learned a little bit and had a little bit of fun see you in the next one
11:57hey everybody thanks for watching this video again from the bottom of my heart i really appreciate
12:01it i do hope you did learn something and have some fun uh if you could do me a please a small little
12:07favor could you please subscribe and follow this channel or these videos or whatever it is you do
12:13on the current social media website that you're looking at right now um it would really mean the
12:17world to me and it'll help make more videos and grow this community so we'll be able to do more videos
12:22longer videos better videos or just i'll be able to keep making videos in general so please do do me a
12:29kindness and uh and subscribe you know sometimes i'm sleeping in the middle of the night and i just wake up
12:34because i know somebody subscribed or followed it just wakes me up and i get filled with joy that's
12:38exactly what happens every single time so you could do it as a nice favor to me or you could you control
12:44me if you want to just wake me up in the middle of the night just subscribe and then i'll i'll just
12:47wake up i promise that's what will happen also uh if you look at the middle of the screen right now you
12:53should see a qr code which you can scan in order to go to the website which i think is also named somewhere
12:58at the bottom of this video and it'll take you to my main website where you can just kind of like see
13:03all the videos i published and the services and tutorials and things that i offer and all that good stuff
13:08and uh if you have a suggestion for uh uh clarifications or errata or just future videos that you want to
13:17see 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
13:22comment 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
13:27it would really it would really mean the world to me i would really appreciate it so again thank you
13:34so much for watching this video and um enjoy the cool music as as i fade into the darkness which is
13:42coming for us all
13:54so
14:02so
14:04so
14:10so