Movatterモバイル変換


[0]ホーム

URL:


Menu
×
See More 
Sign In
+1 Get Certified Upgrade Teachers Spaces Get Certified Upgrade Teachers Spaces
   ❮     
     ❯   

C++ Tutorial

C++ HOMEC++ IntroC++ Get StartedC++ SyntaxC++ OutputC++ CommentsC++ VariablesC++ User InputC++ Data TypesC++ OperatorsC++ StringsC++ MathC++ BooleansC++ If...ElseC++ SwitchC++ While LoopC++ For LoopC++ Break/ContinueC++ ArraysC++ StructuresC++ EnumsC++ ReferencesC++ PointersC++ MemoryManagement

C++ Functions

C++ FunctionsC++ Function ParametersC++ Function OverloadingC++ ScopeC++ RecursionC++ Lambda

C++ Classes

C++ OOPC++ Classes/ObjectsC++ Class MethodsC++ ConstructorsC++ Access SpecifiersC++ EncapsulationC++ Friend FunctionsC++ InheritanceC++ PolymorphismC++ TemplatesC++ FilesC++ Date

C++ Errors

C++ ErrorsC++ DebuggingC++ ExceptionsC++ Input Validation

C++ DataStructures

C++ Data Structures & STLC++ VectorsC++ ListC++ StacksC++ QueuesC++ DequeC++ SetsC++ MapsC++ IteratorsC++ Algorithms

C++ Namespaces

C++ Namespaces

C++ Projects

C++ Projects

C++ How To

C++ Add Two NumbersC++ Random Numbers

C++ Reference

C++ ReferenceC++ KeywordsC++ <iostream>C++ <fstream>C++ <cmath>C++ <string>C++ <cstring>C++ <ctime>C++ <vector>C++ <algorithm>

C++ Examples

C++ ExamplesC++ Real-Life ExamplesC++ CompilerC++ ExercisesC++ QuizC++ SyllabusC++ Study PlanC++ Certificate


C++auto


The auto Keyword

Theauto keyword automatically detects the type of a variable based on the value you assign to it.

It helps you write cleaner code and avoid repeating types, especially for long or complex types.

For example: Instead of writingint x = 5;, you can write:

Example

auto x = 5; // x is automatically treated asint
Try it Yourself »

Starting inC++11,auto became a powerful way to let the compilerfigure out the type based on the value you assign.


Example with Different Types

Here's an example showing howauto can be used to create variables of different types, based on the values you assign:

Example

// Creating auto variables
auto myNum = 5; // int
auto myFloatNum = 5.99f; // float
auto myDoubleNum = 9.98; // double
auto myLetter = 'D'; // char
auto myBoolean = true; // bool
auto myString = string("Hello"); // std::string
Try it Yourself »

Important Notes

  • auto only works when you assign a value at the same time (You can't declareauto x; without assigning a value)
  • Once the type is chosen, it stays the same. See example below:
auto x = 5; // x is now an int
x = 10;     // OK - still an int
x = 9.99;   // Error - can't assign a double to an int

Note: In this tutorial, we usually useint,double, and other basic types when the type is simple and easy to see.

But for more complex types - likeiterators andlambdas, which you will learn more about in a later chapter, we useauto to keep the code cleaner and easier to understand.





×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted ourterms of use,cookies andprivacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved.W3Schools is Powered by W3.CSS.


[8]ページ先頭

©2009-2025 Movatter.jp