<?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>non-zero initialization in NCS</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/72418/non-zero-initialization-in-ncs</link><description>Dears, 
 How do I set a non-zero initialization variable in an NCS development environment? thanks! 
 Best Regards! 
 Ricky Yan</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 08 Mar 2021 11:43:54 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/72418/non-zero-initialization-in-ncs" /><item><title>RE: non-zero initialization in NCS</title><link>https://devzone.nordicsemi.com/thread/298343?ContentTypeID=1</link><pubDate>Mon, 08 Mar 2021 11:43:54 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:77195d5c-6f9f-4255-865e-1a9b0af852a3</guid><dc:creator>Simon</dc:creator><description>&lt;p&gt;This seems be a more C/compiler-related question. In C, static and global variables are initialized to zero and variables declared inside functions (on the stack) can be set to anything. Check out these answers on Stack Overflow:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://stackoverflow.com/a/1597426"&gt;https://stackoverflow.com/a/1597426&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="https://stackoverflow.com/a/7975099"&gt;https://stackoverflow.com/a/7975099&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I&amp;nbsp;tested&amp;nbsp;this by modifying the sample zephyr/samples/hello_world like shown below,&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;/*
 * Copyright (c) 2012-2014 Wind River Systems, Inc.
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include &amp;lt;zephyr.h&amp;gt;
#include &amp;lt;sys/printk.h&amp;gt;

void testFunc(){
	int x[10];
	int y;
	int z;
	static int f[10];
	for(int b=0;b &amp;lt; sizeof(x)/sizeof(int); b++){
		printk(&amp;quot;x[%d]: %d\n&amp;quot;,b ,x[b]);
	}
	for(int l=0;l &amp;lt; sizeof(f)/sizeof(int); l++){
		printk(&amp;quot;f[%d]: %d\n&amp;quot;,l ,f[l]);
	}
	printk(&amp;quot;y: %d\n&amp;quot;, y);
	printk(&amp;quot;z: %d\n&amp;quot;, z);
}

int q[10];

void main(void)
{
	for(int b=0;b &amp;lt; sizeof(q)/sizeof(int); b++){
		printk(&amp;quot;q[%d]: %d\n&amp;quot;,b ,q[b]);
	}

	int a[10];
	int b;
	int c;
	//printk(&amp;quot;Hello World! %s\n&amp;quot;, CONFIG_BOARD);
	for(int b=0;b &amp;lt; sizeof(a)/sizeof(int); b++){
		printk(&amp;quot;a[%d]: %d\n&amp;quot;,b ,a[b]);
	}
	printk(&amp;quot;b: %d\n&amp;quot;, b);
	printk(&amp;quot;c: %d\n&amp;quot;, c);
	testFunc();
}
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;and got the following result:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;*** Booting Zephyr OS build v2.4.99-ncs1  ***
q[0]: 0
q[1]: 0
q[2]: 0
q[3]: 0
q[4]: 0
q[5]: 0
q[6]: 0
q[7]: 0
q[8]: 0
q[9]: 0
a[0]: 1
a[1]: 1323
a[2]: 19767
a[3]: 46
a[4]: 19805
a[5]: 15761
a[6]: 0
a[7]: -536810240
a[8]: 536873156
a[9]: 0
b: 0
c: 0
x[0]: 1
x[1]: 1323
x[2]: 19767
x[3]: 46
x[4]: 19805
x[5]: 15761
x[6]: 0
x[7]: -536810240
x[8]: 536873156
x[9]: 0
f[0]: 0
f[1]: 0
f[2]: 0
f[3]: 0
f[4]: 0
f[5]: 0
f[6]: 0
f[7]: 0
f[8]: 0
f[9]: 0
y: 0
z: 0&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;I also got the following warnings when building it for the nrf9160dk_nrf9160&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;[1/8] Building C object CMakeFiles/app.dir/src/main.c.obj
../src/main.c: In function &amp;#39;testFunc&amp;#39;:
../src/main.c:21:2: warning: &amp;#39;y&amp;#39; is used uninitialized in this function [-Wuninitialized]
   21 |  printk(&amp;quot;y: %d\n&amp;quot;, y);
      |  ^~~~~~~~~~~~~~~~~~~~
../src/main.c:22:2: warning: &amp;#39;z&amp;#39; is used uninitialized in this function [-Wuninitialized]
   22 |  printk(&amp;quot;z: %d\n&amp;quot;, z);
      |  ^~~~~~~~~~~~~~~~~~~~
../src/main.c: In function &amp;#39;main&amp;#39;:
../src/main.c:40:2: warning: &amp;#39;b&amp;#39; is used uninitialized in this function [-Wuninitialized]
   40 |  printk(&amp;quot;b: %d\n&amp;quot;, b);
      |  ^~~~~~~~~~~~~~~~~~~~
../src/main.c:41:2: warning: &amp;#39;c&amp;#39; is used uninitialized in this function [-Wuninitialized]
   41 |  printk(&amp;quot;c: %d\n&amp;quot;, c);
      |  ^~~~~~~~~~~~~~~~~~~~&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;It seems like non initialized variables that are not an array, will get defined as &amp;#39;uninitialized&amp;#39; and will get a value of 0, while non-initialized arrays will conform to the behaviour I explained in my initial answer (initialized to 0 if static of global, or indeterminate if declared on the stack).&lt;/p&gt;
&lt;p&gt;Could you elaborate what you mean by&amp;nbsp;&lt;span&gt;non-zero initialization? What do you want to achieve? I would recommend you to just pay attention to the build log, and if get any warnings regarding uninitalized variables.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Best regards,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Simon&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>