C language keyword auto

1. In the c language, the keyword auto is used to declare a variable as an automatic variable

  Automatic variables are also called local variables. Treat variables not defined in any classes, structures, enumerations, unions, and functions as global variables, and variables defined in functions as local variables. All local variables are auto by default, and are generally omitted.
  When auto declares global variables, a compilation error occurs:

auto int i;
void main(void)
{
    
    
}

  When auto declares local variables, the compilation is normal:

void fun(auto int i)
{
    
    
	auto int j = i;
}
void main(void)
{
    
    
	auto int i = 1;
	fun(i);
}

2. In the c language, only auto is used to modify variables, and the variable type is integer by default

void main(void)
{
    
    
	double a = 1.2, b = 2.7;
	auto c = a + b;//c语言中,c = 3
}

3. In c++, the keyword auto is a type specifier

  The type of the variable is inferred from the initial value of the variable or the data type involved in the expression. When programming, it is usually necessary to assign the value of an expression to a variable, which requires that the type of the expression is clearly known when declaring the variable. The new C++11 standard introduces the auto type specifier, allowing the compiler to analyze the type of expression . Since the compiler needs to infer the type of the variable or expression, the variable defined by auto must be initialized. In the C language, variables defined by auto can not be initialized, because the variables are of integer type by default. This part refers to the blog: Analysis of the auto keyword in C language and the auto keyword in C++

void main(void)
{
    
    
	double a = 1.2, b = 2.7;
	auto c = a + b; // c++中, c = 3.9
}

Topic: Write a program, read in a string containing punctuation marks and spaces, and output the remaining part of the string after removing the punctuation marks and spaces

  C++ source code:

//编写一个程序,读入一个包含标点符号和空格的字符串,将标点符号和空格去除后输出字符串剩余的部分
#include <iostream>
#include <string>

using namespace std;
int main(void)
{
    
    
	/*src为源字符串即要操作的字符串,dest为目标字符串,存放操作结果*/
	string src, dest;
	getline(cin, src);            /*从输入中读取一行赋值给str*/
	for (auto c : src)            /*对str中的每个字符*/
	{
    
    
		if (!ispunct(c) && c != ' ')           /*如果该字符不是标点符号和空格*/
		{
    
    
			dest.push_back(c);     /*把该字符尾插到dest中*/
		}
	}
	cout << dest << endl;          /*输出dest中的内容*/
	system("pause");
	return 0;
}

  Example:
Insert picture description here

Guess you like

Origin blog.csdn.net/maple_2014/article/details/108478405