<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>fgets in C Archives - NeuralLantern.com</title>
	<atom:link href="https://www.NeuralLantern.com/tag/fgets-in-c/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.NeuralLantern.com/tag/fgets-in-c/</link>
	<description></description>
	<lastBuildDate>Tue, 15 Jul 2025 02:58:42 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0</generator>

<image>
	<url>https://www.NeuralLantern.com/wp-content/uploads/2025/04/cropped-2025-04-04-Lantern-03-32x32.png</url>
	<title>fgets in C Archives - NeuralLantern.com</title>
	<link>https://www.NeuralLantern.com/tag/fgets-in-c/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How to Input Double Precision Floating Point Numbers in C: Step-by-Step Guide with Error Handling</title>
		<link>https://www.NeuralLantern.com/how-to-input-double-precision-floating-point-numbers-in-c-step-by-step-guide-with-error-handling/</link>
					<comments>https://www.NeuralLantern.com/how-to-input-double-precision-floating-point-numbers-in-c-step-by-step-guide-with-error-handling/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Tue, 15 Jul 2025 02:58:41 +0000</pubDate>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[C input validation]]></category>
		<category><![CDATA[C programming]]></category>
		<category><![CDATA[C tutorial]]></category>
		<category><![CDATA[coding tutorial]]></category>
		<category><![CDATA[double precision float]]></category>
		<category><![CDATA[error handling in C]]></category>
		<category><![CDATA[fgets in C]]></category>
		<category><![CDATA[input in C]]></category>
		<category><![CDATA[programming for beginners]]></category>
		<category><![CDATA[sscanf in C]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=180</guid>

					<description><![CDATA[<p>Learn to input double precision floats in C with this easy guide! Covers fgets, sscanf, error handling, and code examples for beginners.</p>
<p>The post <a href="https://www.NeuralLantern.com/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</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="How to Input Double Precision Floating Point Numbers in C: Step-by-Step Guide with Error Handling" width="1380" height="776" src="https://www.youtube.com/embed/-sBQdPUd-IM?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p class="wp-block-paragraph">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!</p>



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



<p class="wp-block-paragraph">Thanks for watching!</p>



<p class="wp-block-paragraph">Find us on other social media here:</p>



<ul class="wp-block-list">
<li>https://www.NeuralLantern.com/social</li>
</ul>



<p class="wp-block-paragraph">Please help support us!</p>



<ul class="wp-block-list">
<li>Subscribing + Sharing on Social Media</li>



<li>Leaving a comment or suggestion</li>



<li>Subscribing to our Blog</li>



<li>Watching the main &#8220;pinned&#8221; video of this channel for offers and extras</li>
</ul>



<p class="wp-block-paragraph">Hey there!</p>



<p class="wp-block-paragraph">In this video I&#8217;m going to show you how to input a double precision floating point number</p>



<p class="wp-block-paragraph">in C from the user into your program and decide whether or not it has succeeded or failed.</p>



<p class="wp-block-paragraph">Okay so for starters I just want you to know that this video is not about generating makefiles.</p>



<p class="wp-block-paragraph">own make files or how to compile link execute or even the basics of c see my other videos for now</p>



<p class="wp-block-paragraph">i&#8217;m just going to assume that you know how to make a make file here it is basically very quickly</p>



<p class="wp-block-paragraph">but i&#8217;m just i&#8217;m just compiling a simple program here&#8217;s my program here it&#8217;s called main.c it</p>



<p class="wp-block-paragraph">doesn&#8217;t really have anything else to it i&#8217;m going to start off by importing a couple includes or a</p>



<p class="wp-block-paragraph">few includes that&#8217;ll just help me get some stuff that i need so here&#8217;s an include for the standard</p>



<p class="wp-block-paragraph">input output here&#8217;s an include for the standard library here&#8217;s an include that</p>



<p class="wp-block-paragraph">helps with c string functions here&#8217;s an include that helps with limits not</p>



<p class="wp-block-paragraph">actually sure every single one of those includes is needed but we&#8217;ll see anyway</p>



<p class="wp-block-paragraph">here&#8217;s my main program or I guess my main function since this program only is</p>



<p class="wp-block-paragraph">just one module notice how we have a double called value here and then we</p>



<p class="wp-block-paragraph">make a call to a function called get input and we give a pointer to to the</p>



<p class="wp-block-paragraph">double as an argument to get input so basically get input should be able to just return to us</p>



<p class="wp-block-paragraph">the value by reference and then get input will be able to tell us whether or not it has succeeded</p>



<p class="wp-block-paragraph">as in has the user entered valid input by its return type or not not its return type but its</p>



<p class="wp-block-paragraph">return value so the result is going to be a long from get input and that&#8217;ll indicate</p>



<p class="wp-block-paragraph">runs we&#8217;re just going to print you know get input returned and then print the return value that it</p>



<p class="wp-block-paragraph">returned and then it&#8217;s going to print the double that it inputted from the user and then a new line</p>



<p class="wp-block-paragraph">so nothing uh nothing too big of a deal let me show you the main workhorse function that has</p>



<p class="wp-block-paragraph">all the logic you need to input from the user and check that the input was valid so this is called</p>



<p class="wp-block-paragraph">get input i&#8217;m sticking it on top of the main function because i don&#8217;t want to deal with</p>



<p class="wp-block-paragraph">because I don&#8217;t want to deal with prototypes right now.</p>



<p class="wp-block-paragraph">It&#8217;s a better idea to put prototypes up at the top of your source code file</p>



<p class="wp-block-paragraph">if your source code is even a little bit complicated.</p>



<p class="wp-block-paragraph">But in this case, it&#8217;s just one other function.</p>



<p class="wp-block-paragraph">So I will just put get input on top of main.</p>



<p class="wp-block-paragraph">That way, when the compiler is scanning the source code,</p>



<p class="wp-block-paragraph">it will have already seen that get input exists before main calls it,</p>



<p class="wp-block-paragraph">and it&#8217;ll compile.</p>



<p class="wp-block-paragraph">So first thing I&#8217;m going to do is say that get input returns along</p>



<p class="wp-block-paragraph">get input returns along like we just saw and it takes in a pointer to a double no references just</p>



<p class="wp-block-paragraph">a pointer then we have to create a character buffer and what that&#8217;s going to be for is basically</p>



<p class="wp-block-paragraph">you know the user is going to type some characters and i want that to go into the buffer so um</p>



<p class="wp-block-paragraph">a line max is the number of characters that the buffer can hold and that&#8217;s uh i think where we</p>



<p class="wp-block-paragraph">I think where we have limits, I think limits is providing that for us.</p>



<p class="wp-block-paragraph">But basically, you know, you could stick a number here if you wanted to like an eight</p>



<p class="wp-block-paragraph">character, eight kilobyte buffer, if you wanted like eight, one, nine, two, or just whatever.</p>



<p class="wp-block-paragraph">I&#8217;m just putting line max so that it sort of aligns with the system&#8217;s idea of what</p>



<p class="wp-block-paragraph">the maximum size of a line should be.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">So then I don&#8217;t know why I have it say grab side one.</p>



<p class="wp-block-paragraph">I&#8217;ll just say grab input here.</p>



<p class="wp-block-paragraph">Let me change my solution real fast.</p>



<p class="wp-block-paragraph">Grab input.</p>



<p class="wp-block-paragraph">That&#8217;s probably because that was part of a program.</p>



<p class="wp-block-paragraph">that&#8217;s probably because that was part of a program in the past so then the function that I&#8217;m going to</p>



<p class="wp-block-paragraph">actually use is called fget s which basically is saying let&#8217;s uh let&#8217;s input a string a c string</p>



<p class="wp-block-paragraph">from a file and we&#8217;re providing the target for the input so that&#8217;s going to be the character string</p>



<p class="wp-block-paragraph">right there and then we&#8217;re going to provide um the maximum number of characters that we&#8217;ll accept</p>



<p class="wp-block-paragraph">length of the buffer and then what file are we going to input from standard</p>



<p class="wp-block-paragraph">input if you don&#8217;t know standard input see my other videos but it&#8217;s basically a</p>



<p class="wp-block-paragraph">special file handle that means when the program first launches you know the</p>



<p class="wp-block-paragraph">operating system sort of gives it a special file handle for standard input</p>



<p class="wp-block-paragraph">standard output standard error and so this will be basically the user typing</p>



<p class="wp-block-paragraph">on the terminal so f get s will essentially take input from standard</p>



<p class="wp-block-paragraph">terminal until they you know hit like a new line or they disconnect the input</p>



<p class="wp-block-paragraph">buffer it will take up to this many characters line max which should match</p>



<p class="wp-block-paragraph">the buffer whatever value you put in the buffer and then it will write the</p>



<p class="wp-block-paragraph">characters into the buffer that&#8217;s why you have to match the length of the</p>



<p class="wp-block-paragraph">buffer so basically here this function call will return something if it returns</p>



<p class="wp-block-paragraph">a zero then that means we have failed so if it returns null then we&#8217;re going to return zero</p>



<p class="wp-block-paragraph">meaning fail so get input is kind of a boolean even though we don&#8217;t have booleans in c and it&#8217;s</p>



<p class="wp-block-paragraph">basically going to return a zero on a failure and a one on success so like you know zero is a falsy</p>



<p class="wp-block-paragraph">value and one is a truely value so we&#8217;re not going to use system style return codes where zero is</p>



<p class="wp-block-paragraph">codes where zero is success we&#8217;re going to use boolean style return codes where a zero is failure</p>



<p class="wp-block-paragraph">and one is uh success so you can see i&#8217;ve kind of chained two commands here i&#8217;ve decided to say all</p>



<p class="wp-block-paragraph">right um we&#8217;ll first grab some input into the buffer array with f get s if that is null then</p>



<p class="wp-block-paragraph">we immediately return zero because this this logical or operator says if one or the other thing</p>



<p class="wp-block-paragraph">is equal to a true then just go ahead and evaluate to true so here I&#8217;m asking like did the call</p>



<p class="wp-block-paragraph">equal null if the answer is yes then that part will evaluate to true and then it&#8217;ll basically</p>



<p class="wp-block-paragraph">short circuit the rest of that logical expression and just return zero right away and then if the</p>



<p class="wp-block-paragraph">previous command succeeds like the fget s succeeds then it will look at the buffer and try to scan it</p>



<p class="wp-block-paragraph">for valid input.</p>



<p class="wp-block-paragraph">So the left side is just taking raw input.</p>



<p class="wp-block-paragraph">The right side is parsing for certain data type.</p>



<p class="wp-block-paragraph">So you can see sscanf,</p>



<p class="wp-block-paragraph">we&#8217;re scanning a buffer that we put into the first argument.</p>



<p class="wp-block-paragraph">So that&#8217;s at that point, by the time sscanf gets called,</p>



<p class="wp-block-paragraph">the buffer should be filled</p>



<p class="wp-block-paragraph">with whatever input the user typed.</p>



<p class="wp-block-paragraph">So we&#8217;re scanning that</p>



<p class="wp-block-paragraph">and then here&#8217;s a little formatting argument,</p>



<p class="wp-block-paragraph">which is very similar to,</p>



<p class="wp-block-paragraph">use the same tokens basically as printf. So, you know, here we&#8217;re saying percent lf. So we&#8217;re</p>



<p class="wp-block-paragraph">looking for a long float, which means a double precision floating point number. And that</p>



<p class="wp-block-paragraph">percentage just means like, here&#8217;s a special token that comes after it to describe what sort of</p>



<p class="wp-block-paragraph">value we&#8217;re looking to scan for. So then if we&#8217;re successful, if it actually scans,</p>



<p class="wp-block-paragraph">the double. So fscanf expects a pointer to the variable that&#8217;s going to receive the value that</p>



<p class="wp-block-paragraph">has been scanned if it is scanned correctly. So notice how the double we don&#8217;t have an ampersand</p>



<p class="wp-block-paragraph">in front of it because it&#8217;s already a pointer because it came into us as a pointer in the first</p>



<p class="wp-block-paragraph">place. So we&#8217;re going to look at the buffer that the user just typed, the user just provided,</p>



<p class="wp-block-paragraph">and we&#8217;re going to try to scan for a double precision floating point number. If we&#8217;re</p>



<p class="wp-block-paragraph">then the system should write that double into the variable referred to by the</p>



<p class="wp-block-paragraph">pointer known as the double if all that succeeded then the value should be</p>



<p class="wp-block-paragraph">greater than or equal to zero if these if this command failed then the value</p>



<p class="wp-block-paragraph">should be less than one so that basically means if we have a value of</p>



<p class="wp-block-paragraph">less than one then we have failed and we&#8217;re going to return zero so now whether</p>



<p class="wp-block-paragraph">the initial scanning sorry whether the initial input fails or the parsing of</p>



<p class="wp-block-paragraph">the users input fails then we&#8217;re gonna end up returning zero for fail so then</p>



<p class="wp-block-paragraph">in main you can see all we&#8217;re doing is we&#8217;re just calling on input we&#8217;re giving</p>



<p class="wp-block-paragraph">it a pointer to the double that we want to populate and then we&#8217;re looking at</p>



<p class="wp-block-paragraph">the result so in your you know program logic you can do something like if the</p>



<p class="wp-block-paragraph">result is zero then assume we failed and if the result is one assume we succeeded</p>



<p class="wp-block-paragraph">And if the result is one, assume we succeeded and, you know, respond to the user or, you</p>



<p class="wp-block-paragraph">know, do something that&#8217;s appropriate for success or fail.</p>



<p class="wp-block-paragraph">I think that&#8217;s everything that I need to explain.</p>



<p class="wp-block-paragraph">Let&#8217;s try to run the program now.</p>



<p class="wp-block-paragraph">Open a terminal.</p>



<p class="wp-block-paragraph">I want to do make run and see if that works.</p>



<p class="wp-block-paragraph">Maybe I&#8217;ll do a clear here.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">So I didn&#8217;t print any, you know, prompting or anything special.</p>



<p class="wp-block-paragraph">so the cursor is just blinking at this point i can just enter 33.12 i guess whatever hit enter</p>



<p class="wp-block-paragraph">and the program now says get it get input returned one which means success so the value that i entered</p>



<p class="wp-block-paragraph">was a valid double and the value is now 33.12 so it successfully grabbed the float keep in mind</p>



<p class="wp-block-paragraph">these zeros or just precision related issues are related to the printing of the double not</p>



<p class="wp-block-paragraph">not necessarily related to what is inside of the double so I&#8217;m just saying</p>



<p class="wp-block-paragraph">you know print along float you can you can mess with print F a little bit more</p>



<p class="wp-block-paragraph">if you want to have a different value printed or more precision or less</p>



<p class="wp-block-paragraph">percent precision printed let&#8217;s try to do this with some invalid input I&#8217;m just</p>



<p class="wp-block-paragraph">going to do some letters and hit enter notice how the value didn&#8217;t actually</p>



<p class="wp-block-paragraph">change it&#8217;s it started or actually it did change it got set to a zero because</p>



<p class="wp-block-paragraph">initialized it as junk data so it got set to a zero but then more importantly get input returns</p>



<p class="wp-block-paragraph">zero as its return code so when you see zero that means the command failed so you should not consider</p>



<p class="wp-block-paragraph">the double as valid in any way you shouldn&#8217;t use it you should complain to the user or do whatever</p>



<p class="wp-block-paragraph">and that is how to get input let&#8217;s just try something for fun double value equals 3.333</p>



<p class="wp-block-paragraph">bad input i&#8217;m assuming it might but i haven&#8217;t done this in a while so we&#8217;ll see so bad input</p>



<p class="wp-block-paragraph">and looks like it did not change it so whatever junk data was already in value by the time you</p>



<p class="wp-block-paragraph">called on get input if get input failed then the value should probably be unchanged but again more</p>



<p class="wp-block-paragraph">importantly the return value is zero meaning fail because zero is a falsy value so i&#8217;m going to get</p>



<p class="wp-block-paragraph">so i&#8217;m going to get rid of that so i can go back to my solution there okay so now hopefully you are</p>



<p class="wp-block-paragraph">uh you&#8217;re on your way to start getting input in c at least in the in in terms of longs uh you can</p>



<p class="wp-block-paragraph">look up the formatting codes for print f if you would like to input different types of data like</p>



<p class="wp-block-paragraph">so this video is just for long floats doubles uh you can you can look up all other all sorts of</p>



<p class="wp-block-paragraph">five years I&#8217;ll make another video that has different types of tokens but I guess that&#8217;s</p>



<p class="wp-block-paragraph">it for this video. Thanks for watching I hope you learned a little bit and had a little bit of fun.</p>



<p class="wp-block-paragraph">See you in the next one.</p>



<p class="wp-block-paragraph">Hey everybody thanks for watching this video again from the bottom of my heart I really</p>



<p class="wp-block-paragraph">appreciate it. I do hope you did learn something and have some fun. If you could do me a please</p>



<p class="wp-block-paragraph">subscribe and follow this channel or these videos or whatever it is you do</p>



<p class="wp-block-paragraph">on the current social media website that you&#8217;re looking at right now it would</p>



<p class="wp-block-paragraph">really mean the world to me and it&#8217;ll help make more videos and grow this</p>



<p class="wp-block-paragraph">community so we&#8217;ll be able to do more videos longer videos better videos or</p>



<p class="wp-block-paragraph">just I&#8217;ll be able to keep making videos in general so please do do me a</p>



<p class="wp-block-paragraph">kindness and and subscribe you know sometimes I&#8217;m sleeping in the middle of</p>



<p class="wp-block-paragraph">the night and I just wake up because I know somebody subscribed or followed it</p>



<p class="wp-block-paragraph">followed it just wakes me up and I get filled with joy that&#8217;s exactly what</p>



<p class="wp-block-paragraph">happens every single time so you could do it as a nice favor to me or you could</p>



<p class="wp-block-paragraph">you control me if you want to just wake me up in the middle of the night just</p>



<p class="wp-block-paragraph">subscribe and then I&#8217;ll just wake up I promise that&#8217;s what will happen also if</p>



<p class="wp-block-paragraph">you look at the middle of the screen right now you should see a QR code which</p>



<p class="wp-block-paragraph">you can scan in order to go to the website which I think is also named</p>



<p class="wp-block-paragraph">somewhere at the bottom of this video and it&#8217;ll take you to my main website</p>



<p class="wp-block-paragraph">where you can just kind of like see all the videos I published and the services</p>



<p class="wp-block-paragraph">videos I published and the services and tutorials and things that I offer and all that good stuff</p>



<p class="wp-block-paragraph">and uh if you have a suggestion for uh uh clarifications or errata or just future videos</p>



<p class="wp-block-paragraph">that you want to see please leave a comment or if you just want to say hey what&#8217;s up what&#8217;s going on</p>



<p class="wp-block-paragraph">you know just send me a comment whatever I also wake up for those in the middle of the night I</p>



<p class="wp-block-paragraph">get I wake up in a cold sweat and I&#8217;m like it would really it really mean the world to me I</p>



<p class="wp-block-paragraph">it. So again, thank you so much for watching this video and enjoy the cool music as I fade</p>



<p class="wp-block-paragraph">into the darkness, which is coming for us all.</p>



<p class="wp-block-paragraph">Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/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</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/how-to-input-double-precision-floating-point-numbers-in-c-step-by-step-guide-with-error-handling/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
