Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Without initialization variable

Tags:

c++

logic

I have got a problem from my friends. The problem is Without initialization of variable how to print 0 to 10?

I know method to print using for loop using initialization

 for(int i=0;i<=10;i++) {
     cout<<i;
 }

Here int i = 0 was initialized. Then how can one print 0 to 10 values without initialization of the relevant variable? It is possible or not?

like image 539
user1250007 Avatar asked Mar 05 '12 14:03

user1250007


People also ask

What happens if you don't initialize a variable in C?

In C and C++, local variables aren't initialized by default. Uninitialized variables can contain any value, and their use leads to undefined behavior. Warning C4700 almost always indicates a bug that can cause unpredictable results or crashes in your program.

Do you need to initialize variables in C?

In the low-level efficiency tradition of C and C++ alike, the compiler is often not required to initialize variables unless you do it explicitly (e.g., local variables, forgotten members omitted from constructor initializer lists).

Can you declare a variable without initializing?

If you declare a final variable later on you cannot modify or, assign values to it. Moreover, like instance variables, final variables will not be initialized with default values. Therefore, it is mandatory to initialize final variables once you declare them.

What happens if you forget to initialize a variable?

An uninitialized variable is a variable that has not been given a value by the program (generally through initialization or assignment). Using the value stored in an uninitialized variable will result in undefined behavior.


1 Answers

Your friends should learn to specify their problems more precisely.

int i; // i is not initialized
i = 0; // i is assigned
for( ;i<=10;i++)
{
  cout<<i;
}
like image 92
Robᵩ Avatar answered Sep 24 '22 19:09

Robᵩ