How to use auto in c language

下次还敢
Release: 2024-05-09 09:39:17
Original
757 people have browsed it

The auto keyword is used to declare local variables in C language. It has the following functions: declare local variables, which default to int type and are automatically initialized to 0 after declaration.

How to use auto in c language

Usage of auto keyword in C language

auto keyword is used to declare local in C language A storage class specifier for a variable. Local variables are declared within a function or block and are valid only within that function or block. The auto keyword can be used to declare various data types such as integers, floating point numbers, characters, and structures.

The role of auto

The auto keyword has the following functions:

  • Tell the compiler that the variable is a local variable.
  • The default is int data type.
  • Variables are automatically initialized to 0 after declaration.

Usage of auto

The usage of auto keyword is as follows:

<code class="c">auto var_name;</code>
Copy after login

where var_name is the variable name.

Comparison of auto and register keywords

auto and register are both storage class specifiers for local variables, but there are subtle differences between them:

  • The auto keyword will not be stored in a register, but the register keyword will.
  • Register keyword variables cannot be addressed by the address operator (&), but auto keyword variables can.

Example

The following example shows the use of the auto keyword:

<code class="c">#include <stdio.h>

int main() {
  auto int i;
  auto float f;

  i = 10;
  f = 20.5;

  printf("i = %d\n", i);
  printf("f = %f\n", f);

  return 0;
}</code>
Copy after login

Output:

<code>i = 10
f = 20.500000</code>
Copy after login

The above is the detailed content of How to use auto in c language. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template