<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>cannot get the variable value from main.c</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/76752/cannot-get-the-variable-value-from-main-c</link><description>Hi, 
 I&amp;#39;m new to C language. 
 In my main.c 
 
 In my Battery.c 
 
 My Question 
 The IDE will give a warning: No such file or directory. How to solve this issue, please? I need to get BatteryLvl_Volts from main.c. 
 Thanks!</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 29 Jun 2021 11:05:11 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/76752/cannot-get-the-variable-value-from-main-c" /><item><title>RE: cannot get the variable value from main.c</title><link>https://devzone.nordicsemi.com/thread/317673?ContentTypeID=1</link><pubDate>Tue, 29 Jun 2021 11:05:11 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:60c5395d-9eb9-40eb-b9f9-3f5118e7382a</guid><dc:creator>Karl Ylvisaker</dc:creator><description>[quote user="StevenW807"]Thank you so much, Karl. Crystal clear![/quote]
&lt;p&gt;No problem at all, I am happy to hear that you found my comment useful! :)&amp;nbsp;&lt;br /&gt;&lt;br /&gt;Please do not hesitate to open another ticket if you should encounter any other issues or questions in the future.&lt;br /&gt;&lt;br /&gt;Good luck with your development!&lt;br /&gt;&lt;br /&gt;Best regards,&lt;br /&gt;Karl&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: cannot get the variable value from main.c</title><link>https://devzone.nordicsemi.com/thread/317649?ContentTypeID=1</link><pubDate>Tue, 29 Jun 2021 09:36:30 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:fa487de1-62ee-49e3-9d57-9186606a8fdc</guid><dc:creator>StevenW807</dc:creator><description>&lt;p&gt;Thank you so much, Karl. Crystal clear!&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: cannot get the variable value from main.c</title><link>https://devzone.nordicsemi.com/thread/317140?ContentTypeID=1</link><pubDate>Fri, 25 Jun 2021 07:57:52 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ef3fca7c-88f5-4dd8-ba67-e1e90e55a9b3</guid><dc:creator>Karl Ylvisaker</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
[quote user=""]I&amp;#39;m new to C language.[/quote]
&lt;p&gt;Thank you for clarifying this - this makes it much easier for us to help you since we know that you are new to C.&amp;nbsp;&lt;/p&gt;
[quote user=""]In my&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;strong&gt;Battery.c&lt;/strong&gt;[/quote]
&lt;p&gt;You do not need to include main.h - because main.c includes all other files, not the other way around. If you need to pass a value from main.c to another file, you can either implement that variable as a global variable (but this can get messy for larger programs), or you can &lt;a href="https://www.tutorialspoint.com/cprogramming/c_function_call_by_reference.htm"&gt;pass it as a parameter to the function that will make use of it&lt;/a&gt;, for example.&lt;br /&gt;In the specific code snippets you share, if you first remove the&amp;nbsp;&lt;em&gt;extern&amp;nbsp;&lt;/em&gt;keyword for the variable, the variable is then a global variable - you may then use it in any one of your functions. Please see &lt;a href="https://www.tutorialspoint.com/cprogramming/c_scope_rules.htm"&gt;this tutorial for a more in-depth explanation&lt;/a&gt;. However, if you use the variable multiple places it could be hard to keep track of everyone accessing it, and I therefore instead recommend that you always pass it as a parameter (either &lt;em&gt;pass-by-value&lt;/em&gt; if the function should not change the variable. or &lt;em&gt;pass-by-reference&lt;/em&gt; if the function should be able to change it) instead, to make it easier for yourself in the long run.&lt;br /&gt;&lt;br /&gt;Furthermore, you should initialize any global variables, so that you do not relay on the implicit declaration and that you may know if it is ready to use or not (i.e if it is initialized to the value 0, and then always have non-zero values following the first update, you can know that the variable is ready use when it is non-zero, for example).&lt;/p&gt;
[quote user=""]In my&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;strong&gt;main.c&lt;/strong&gt;[/quote]
&lt;p&gt;You might be using the extern keyword wrong in your main.c. Please have a look at this &lt;a href="https://www.geeksforgeeks.org/understanding-extern-keyword-in-c/"&gt;blogpost for a more detailed explanation of the&amp;nbsp;&lt;em&gt;extern&amp;nbsp;&lt;/em&gt;keyword&lt;/a&gt;. In essence, the extern keyword is used to tell the compiler that &lt;em&gt;the variable will come from&lt;/em&gt;&amp;nbsp;&lt;em&gt;&lt;/em&gt;elsewhere, not that you intend to use it elsewhere.&amp;nbsp;&lt;br /&gt;&lt;br /&gt;Also, what is your intention with the get function? Is it just to keep the code clean? A global variable will be accessible from anywhere in your program. get functions are usually used to retrieve variables that you do not have access to directly (but it is fine to create a get function to access any variable, as this can help you clean up the program by not having all variables be globally accessible).&lt;/p&gt;
[quote user=""]The IDE will give a warning: No such file or directory. How to solve this issue, please? I need to get&amp;nbsp;&lt;strong&gt;BatteryLvl_Volts&lt;/strong&gt;&amp;nbsp;from main.c.&amp;nbsp;[/quote]
&lt;p&gt;Please post the entire error message that you get. The &amp;quot;No such file or directory&amp;quot; error is a very common C error, and usually means that you have not included the proper source files to your project, or that you have not told your compiler where to find the files you are trying to include in the project, for example.&lt;br /&gt;&lt;br /&gt;Please do not hesitate to ask if any part of my answer is unclear, or if you have further questions!&lt;br /&gt;&lt;br /&gt;Best regards,&lt;br /&gt;Karl&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>