title: How to check if environment variable is set from C program
body: <p>I'm currently working on a program, and I would like to print special output if an environment variable is set.</p>
  
  <p>For example, suppose I want environment variable <code>"DEBUG"</code>.</p>
  
  <p>In my bash command prompt, I set <code>DEBUG</code> by typing the command:
  <code>DEBUG=</code></p>
  
  <p>Then in my C program, I can verify this environment variable is set by printing out all the content of <code>char **environ</code>. <code>DEBUG</code> does show up in this environment printout.</p>
  
  <p>However, I don't know how to retrieve this environment variable for conditional checking. I've tried using the function getenv like so:</p>
  
  <pre><code>getenv("DEBUG")
  </code></pre>
  
  <p>If I were to try to print out this output like below I get a seg fault:</p>
  
  <pre><code>printf("get env: %s\n", getenv("DEBUG"));
  </code></pre>
  
  <p>I even tried this on a known environment variable like <code>"HOME"</code>:</p>
  
  <pre><code>printf("get env: %s\n", getenv("HOME"));
  </code></pre>
  
  <p>which still produces a seg fault.</p>
  
  <p>Does any one have any experience checking if an environment variable is set from a C program? I'm having issues even pulling a single environment variable which is preventing me from doing so.</p>