answersLogoWhite

0

What is auto declaration in C?

Updated: 8/10/2023
User Avatar

Wiki User

12y ago

Best Answer

Auto is one of the four storage classes in c. This is the default storage class. The auto storage class can be used only inside functions, i.e. only to declare local variables and not to declare global variables. All the local variables are by default auto variables.

Other storage classes are:

Register - variables declared may get stored in CPU registers instead of RAM

Static - default storage class for global variables

extern - defines global variables that is visible to all object modules

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

You can use it within functions only, it means a variable which exists on the stack. (This is the default, so you don't have to actually use it, most programmers do not even know about this keyword, let alone use it.)

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

Objects and variables defined within a block (statements enclosed in braces {}), or in a function signature have auto storage unless otherwise specified using the extern or static keyword. Automatic objects have no linkage, are stored on the program stack, and persist until the end of the block in which they are declared. Although objects and variables can be specified using the auto keyword, explicit use of auto is unnecessary. Its only purpose in C++ is for clarity.

This answer is:
User Avatar

User Avatar

Wiki User

7y ago

It does exactly the same as it did in C++11; automatic type deduction through initialisation.

Any function that returns a type other than void can be used to initialise a variable of the same type. However, in order to declare that variable we must first know the function's type and if we don't know its type, then we have to look it up. But with automatic type deduction through initialisation we can simply enlist the compiler to perform the lookup for us:

auto x = foo();

If we suppose that foo() returns a double, then this would be equivalent to:

double x = foo();

Generally we only use automatic type deduction through initialisation when the return type is an implementation detail that would otherwise hinder the readability of our code. For instance, the C++ standard library contains many examples of verbose data types. Even if we import names in order to reduce the verbosity, our code can still be a little difficult to digest:

#include<iostream>

#include<chrono>

int main (void) {

using namespace std::chrono;

steady_clock::time_point start = steady_clock::now();

// perform the task...

steady_clock::time_point end = steady_clock::now();

long long duration = duration_cast<milliseconds>(end - start).count();

std::cout << "Duration: " << duration << "ms.\n";

}

In this code, the std::chrono::steady_clock::time_point data type is an implementation detail that is of no real concern to the reader. As such we can reduce the verbosity even further by using automatic type deduction through initialisation:

#include<iostream>

#include<chrono>

int main (void) {

using namespace std::chrono;

auto start = steady_clock::now();

// perform the task...

auto end = steady_clock::now();

auto duration = duration_cast<milliseconds>(end - start).count();

std::cout << "Duration: " << duration << "ms\n";

}

The duration calculation is another implementation detail that could be simplified with a little helper function, however the use of automatic type deduction alone greatly improves the readability of this otherwise trivial piece of code.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is auto declaration in C?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What was the introduction in the Declaration of Independence?

It was the Preamble, and it was in there b/c it has the reasons for writing the Declaration in the first place.


How was the Texas Declaration of Independence different to the us Declaration of Independence?

The Texas Declaration was written by George C. Childress and the other one was by the USA. Congress?


Who was george c childress?

He was the author of the Texas Declaration of Independence and a lawyer.


What is auto in turbo c?

Keyword.


What is the difference between declaration and a definition in c plus plus?

A declaration is an incomplete type whereas a definition is a complete type.


How a variable is declared and initialized during declaration in c?

During declaration, the declaration goes like this: extern &lt;type&gt; &lt;variable-name&gt; or &lt;type&gt; &lt;function-name&gt; (&lt;parameter list&gt;);


Are the three declaration char a char a and char c same?

Yes.


What is meant by multiple declaration in c?

int i_dont_know; double i_dont_know;


How you get auto click on boombang?

On youtube search boombang auto clicker mediafire or have this auto Auto Click 3.0 by shocker Hope i helped c:


What are the functions of a storage class?

There are four storage class specifiers in C and C++. These are - 1. auto : The storage specifier auto refers to automatic variable declaration. The life of an automatic variable is the time during which its parent function is running. The scope of an auto variable is the function scope. They can be accessed only from their parent functions. Syntax : auto int a; 2. register : A register variable has all the characteristics of an auto variable. The only difference is that auto variable uses the main memory to store data and register uses the CPU registers. 3. extern : This storage specifier is used to declare a global variable. The life of these variables is the time during which the program runs.


What is meant by function declaration in C language?

The name of the function is established by what is called function declaration. It also establishes the number and the types of parameters.


Where the declaration of c variable can be done?

at orchard road on the shop where the variable can be done.