answersLogoWhite

0

For static and global variables it is 0; automatic variables are not initialized by default.

in the c language there is no default value for non static local variables. The variable holds whatever was in memory before it became a variable. It's best to always initialize a non static local variable before using it in the c language (or at least before comparing it to something else). Also It's best to assume that there is no default value because this varies from language to language, and hardware to hardware.

Cheers!

User Avatar

Wiki User

16y ago

Still curious? Ask our experts.

Chat with our AI personalities

RossRoss
Every question is just a happy little opportunity.
Chat with Ross
FranFran
I've made my fair share of mistakes, and if I can help you avoid a few, I'd sure like to try.
Chat with Fran
DevinDevin
I've poured enough drinks to know that people don't always want advice—they just want to talk.
Chat with Devin
More answers

The default value is whatever happens to be present in the memory location at the point of allocation. If allocated on the stack, it will generally be zero. On the heap it could be anything, and must be initialised before being used.

User Avatar

Wiki User

12y ago
User Avatar

A default argument is a formal argument to which we have assigned a default value. If we do not explicitly supply a value for that argument, the default value will be chosen instead.

void f (int=0);

In the above example, the function f() expects an argument of type int but if we call the function without supplying a value for that argument, then the value 0 will be used by default:

f (42); // ok, argument supplied

f (); // ok, same as invoking f (0)

When a function has two or more arguments and we assign a default to any one of them, we must also assign default values to any and all of the arguments that follow it.

void f1 (int, double=0.0); // ok -- trailing argument has a default value

void f2 (int=0, double=0.0); // ok -- all arguments have default values

void f3 (int=0, double); // error -- second argument requires a default

If we cannot provide a suitable default value for the second argument in f3(), then we must swap the order of the arguments instead:

void f3 (double, int=0); // ok

Note that when we define a function that has already been declared with default values, we do not include those default values in the definition:

void f3 (double d, int i=0) { // error -- default value already given in declaration

// ...

}

For documentation purposes we can use a C-style comment to show default values in function definitions that have previously been declared:

void f3 (double d, int i /*=0*/) { // ok

// ...

}

However, it's best to avoid this as it can lead to inconsistencies if we subsequently change the default in the declaration but forget to also update the definition. The declaration alone provides all the documentation we need, repeating it in the definition only increases the maintenance burden.

When we combine default values with function overloading, we may incur an ambiguity:

void f4 ();

void f4 (int=0);

f4 (); // ambiguous: f4 () or f4 (0)?

Default values are most useful when defining constructor overloads as they allow us to combine several different constructors into one.

class date {

private:

int dd, mm, yyyy;

public:

date (int d=1, int m=1, int y=2001) dd {d}, mm {m}, yyyy {y} {}

// ...

};

date (); // invoke date (1, 1, 2001)

date (2); // invoke date (2, 1, 2001)

date (3, 3); // invoke date (3, 3, 2001)

date (4, 2, 2018); // invoke date (4, 2, 2018)

User Avatar

Wiki User

7y ago
User Avatar

Add your answer:

Earn +20 pts
Q: What is the default value of integer in c?
Write your answer...
Submit
Still have questions?
magnify glass
imp