{"id":6245,"date":"2024-02-17T00:01:25","date_gmt":"2024-02-17T08:01:25","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6245"},"modified":"2024-02-10T15:20:36","modified_gmt":"2024-02-10T23:20:36","slug":"creating-your-own-environment-variables","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6245","title":{"rendered":"Creating Your Own Environment Variables"},"content":{"rendered":"<p>Programs use environment variables, thanks to the <em>getenv()<\/em> function shown in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6237\">last week&#8217;s Lesson<\/a>. They can also create their own environment variables, reset variable values, and remove variables.<br \/>\n<!--more--><br \/>\nTo add your own environment variable, use the <em>setenv()<\/em> function, prototyped in the <code>stdlib.h<\/code> header file. Here is the <em>man<\/em> page format:<\/p>\n<p><code>int setenv(const char *name, const char *value, int overwrite);<\/code><\/p>\n<p>Strings <em>name<\/em> and <em>value<\/em> are strings representing the environment variable name and value to add. The <em>overwrite<\/em> option is zero or non-zero. If <em>overwrite<\/em> is zero and <em>name<\/em> already exists, it&#8217;s not overwritten. Otherwise, if <em>name<\/em> exists (and <em>overwrite<\/em> is non-zero) the new value replaces the original value.<\/p>\n<p>Remember that the function returns zero on success even if <em>overwrite<\/em> is zero and <em>name<\/em> already exists. In this case, <em>name<\/em> isn&#8217;t overwritten, which the function considered a &#8220;success.&#8221;<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_02_17-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2024_02_17-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nint main()\r\n{\r\n    const char newvar[] = \"language\";\r\n    const char value[] = \"C\";\r\n\r\n    printf(\"Setting variable '%s' to '%s'\\n\",\r\n            newvar,\r\n            value\r\n          );\r\n    setenv(newvar,value,0);\r\n\r\n    printf(\"Result: '%s'\\n\",getenv(newvar) );\r\n\r\n    return 0;\r\n}<\/pre>\n<p>This code sets the <code>language<\/code> variable to the value <code>C<\/code> in the environment: <code>language=C<\/code> If <code>language<\/code> already exists, it&#8217;s overwritten. Here&#8217;s a sample run:<\/p>\n<p><code>Setting variable 'langauge' to 'C'<br \/>\nResult: 'C'<\/code><\/p>\n<p>Another function similar to <em>setenv()<\/em> is <em>putenv()<\/em>, also prototyped in the <code>stdlib.h<\/code> header file. The difference between the two is that <em>putenv()<\/em> requires the entire string to be set at once. For example:<\/p>\n<p><code>putenv(\"language=C\");<\/code><\/p>\n<p>Otherwise, both functions perform the same task.<\/p>\n<p>One thing to keep in mind with environment variables is that any variables created or modified in a program are local to that program. When the program ends, the variables are lost. After running the program above, you won&#8217;t find <code>language=C<\/code> in the environment.<\/p>\n<p>To deliberately remove created environment variables from your program. The <em>unsetenv()<\/em> function handles the job, as shown in this code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_02_17-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2024_02_17-Lesson-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nint main()\r\n{\r\n    const char newvar[] = \"language\";\r\n    const char value[] = \"C\";\r\n\r\n    printf(\"Setting variable '%s' to '%s'\\n\",\r\n            newvar,\r\n            value\r\n          );\r\n    setenv(newvar,value,0);\r\n\r\n    printf(\"Result: '%s'\\n\",getenv(newvar) );\r\n\r\n    printf(\"Freeing '%s'...\",newvar);\r\n    unsetenv(newvar);\r\n    printf(\"done!\\n\");\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The <em>unsetenv()<\/em> function&#8217;s sole argument is the environment variable to zap. Like <em>setenv()<\/em>, the function returns zero on success, -1 otherwise.<\/p>\n<p>I&#8217;ve never written code that required an environment variable to created on-the-fly, or even to reset a variable&#8217;s value. A weather program I wrote relies upon the variable <code>WXSTN<\/code> to obtain the local weather station. But this variable is set when the shell starts, not in the program.<\/p>\n<p>The environment is great for storing variables accessible when the program runs. For more detailed information, say longer than a string, use temporary or hidden files that contain the settings. Examples include the &#8220;dot&#8221; files used by many terminal window programs, often found lurking in the user&#8217;s home directory.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A program can add a variable to the environment &mdash; and remove it as well. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6245\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-6245","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6245","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6245"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6245\/revisions"}],"predecessor-version":[{"id":6259,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6245\/revisions\/6259"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}