Initialization of Variable in C

Thread Starter

Parth786

Joined Jun 19, 2017
642

I am confusion on initialization of variable in c. C variables are names used for storing a data value to locations in memory
Declaration of variable in c can be done using following syntax:
data_type variable_name;
Example: int i; where i is variable name. this is example of variable declaration with single name
or
data_type variable1, variable2,…,variablen;
Example: int i, j,k; this is example of variable declaration with multiple name

assign value to variable name
example: int i = 6;

Than initialization of variable look like this
syntax: for(initialization; test; increment)
Example: for(i=0; <5; i++);

Declaration and initialization
int i;
for(i=0;i<5;i++);
compiler set i=0 and then increment by 1
program output 0,1,2,3,4

can we initialization like this? I think no

int i=2;
for(i=0;i<5;i++);
this program show output 0,1,2,3,4. why does it not show 2,3,4 while int i=2;
what happens to declared but uninitialized variable. does it have a value?
 

shteii01

Joined Feb 19, 2010
4,667
int i=2: means that you have variable i that is integer and you store 2 in this variable. here you did two things, initialize i and store a value in it.
but
then in the for loop you say that you will store zero in variable i. so you replaced 2 with a zero. you did not initialize anything when you wrote for loop.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
int i=2: means that you have variable i that is integer and you store 2 in this variable. here you did two things, initialize i and store a value in it.
but
then in the for loop you say that you will store zero in variable i. so you replaced 2 with a zero. you did not initialize anything when you wrote for loop.
I understood so the correct statement is
Code:
int main ()
{
      unsigned int i;
      for (i=0; i<4; i++);
}
what happens to declared but uninitialized variable. does it have a value?
 

Brian Griffin

Joined May 17, 2013
63
Before the loop, you set the i to '2'. Then afterwards, the conditions for the 'for' loop has the i being set to '0'. Therefore it only shows 0,1,2,3,4.
 

Brian Griffin

Joined May 17, 2013
63
I understood so the correct statement is
Code:
int main ()
{
      unsigned int i;
      for (i=0; i<4; i++);
}
what happens to declared but uninitialized variable. does it have a value?
The 'i' is still being used in the loop. Therefore, the 'i' has a value in the loop. The compiler will pick this up as a warning if it is uninitialized.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
I googled for local and global variables. A local variable is a variable that is declared inside a function. A global variable is a variable that is declared outside all functions. A local variable can only be used in the function where it is declared. A global variable can be used in all functions.

example for local variable
Code:
int main ()
{
      unsigned int i;  // Local variable
      for (i=0; i<4; i++);
}
example for global variable
Code:
     void delay ()
     unsigned int i; // Global variables
     for (i=0; i<4; i++);

    int main()
    {
     /* program code
    }
when do we need global or local variables in c program?
 

Papabravo

Joined Feb 24, 2006
12,407
Variable are initialized in the C startup routine that is done before the call to main(). Uninitialized variable are set to zero by most C startup routines I have seen. The initialization does not set a variable to zero and then increment it to get to some non-zero value. That would be a**holish.
 

djsfantasi

Joined Apr 11, 2010
5,616
Uninitialized variable are set to zero by most C startup routines I have seen. The initialization does not set a variable to zero and then increment it to get to some non-zero value. That would be a**holish.
But that is not reliable. Your compiler may set it to anything or not at all. Knowing this, you should adopt the practice of always initialize your variables upon declaration.
 

MrChips

Joined Oct 2, 2009
19,280
Initialized variable does not mean that it is initialized to zero.
Uninitialized variable means that its value could be any value.

There is no need to initialize your variables.

Always assign a value to a variable before you read the variable as data in any function, expression or control variable.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
I have doubt in my post number 6. there tried to explain local and global variable and not sure that I understand correct logic of local and global variable. do you find any mistake.
when do we need to use global and local variable in program?
 

MrChips

Joined Oct 2, 2009
19,280
As you have correctly described, local variables are accessible in the function where they are declared.
Global variables are accessible everywhere.

Here is one example of usage of local and global variables:

Code:
// create a global variable
int Sum;

void function1(void)
{
int i;
for (i = 0; i < 10; i++)
{
  Sum = 0;
  function2(35);
}

void function2(int n)
{
int i;
for (i = 0; i < n; i++)
{
  function3( );
  Sum += Data[i];
}
}
Here, we can reuse the loop counter i in nested functions without conflict because the scope of each i is local to the function.

Sum is an example of a globally declared variable.
We can initialize the value of Sum in one function and then accumulate Sum in another function.

(These are just examples and are not intended to represent any useful code.)
 

MrChips

Joined Oct 2, 2009
19,280
Your example in post #6 is technically incorrect.
Here is your example modified:
Code:
unsigned int i; //global variable

void delay (unsigned int n)
{
     unsigned int i; // local variable
     for (i=0; i<n; i++);
}

void main(void)
{
     /* program code
    delay(4);
}
 

JohnInTX

Joined Jun 26, 2012
3,809
@Parth786 One of your major problems is the text you are using (posted on another thread). It gives minimal, show and tell examples that have some value but won't teach you much about C. Get a copy of The C Programming Language 2ed ed. or later. Most if not all of your C questions will be answered in a concise, clear manner in that book. The fact that you can't figure out when to use a specific 'storage class' and 'scope' means that your current resources are lacking in depth (storage class and scope are not even discussed in your text) and you need something more.

There are also a gazillion tutorial sites online but I prefer to get my C straight from the guys who wrote the language.
I would also download the manual for the compiler you are using and read it from cover to cover.
Good luck.
 
Last edited:
Top