1. Learning Objectives

  1. Understand basic data types of C language
  2. Understand the basic concepts of variables
  3. Understand how variables are used
  4. You learned how to name variables
  5. Understand format placeholders
  6. Understand the output of variables

directory

Is C really hard? If you don’t see this picture, you can easily learn C by breaking it into parts. The first article: (one) from the learning error The second: (2) not be so hard to do a simple C language development you understand the process Third: (3) easily understand the first C language program Article 5 (five) C language variables, constants, and operating Article 6 (six) easily understand the logic of the C language arithmetic Article 7: (11) C language pointer is originally like this: (11) C language custom function is really very simple twelfth chapter: (12) the original structure is such a thing :(13) socket server prepared

C language novice 100 error solutions

recommended

I am participating in 1024 activity, welcome everyone to like, favorites, comments on my dry goods articlesAn article to guide you from 0 to 1 to know how to build a website and complete the COMPILATION of CMS system

Welcome everyone to pay attention to the public account, the public account every 1024 and 1024 times will be lucky draw a mechanical keyboard + 2 IT books oh ~

2. Understand the basic data types and concepts of C language programs

In C programming language, the system defines a variety of data types. This section will explain the classification of basic data types. The basic data types are integer, character, real (floating point), and enumeration. Enumeration data types are not covered in this section, but the concept and use of enumeration data types will be explained in more detail in later lessons.

A system-defined data type can be used to describe a variable. In programming, a variable is a container that can store values. It is an abstract concept. If the variable is described concretely, it can be called a container marked with a storage type description. For example, a box marked with moon cake on the outside indicates that the box is a container for the moon cake type and the stored value can be changed (changes in the stored value will be explained in the next chapter).

2.1 Understanding basic Data Types Basic data types refer to types that cannot be decomposable. I’m sure readers will find this definition as unintelligible as usual.

Why do primitive data types refer to non-decomposable types? Because in C programming language, some data types actually have a combination of multiple data types inside, a combination of multiple data types is called a constructed data type, which can be decomposed into several or one data type. Basic data cannot be factored, such as an integer, which represents the type of an integer that cannot be factored.

2.2 Understanding integer types and Using Integer Type Description Variables Integer types are represented by INT in C language, which is also called type specifier. As mentioned earlier, type specifiers can be used to describe a variable; Using int to describe a variable means that the value stored in the variable must be an integer. The description in C language is as follows:

int a;
Copy the code

Where a is the name of the variable, or the container we’re going to call a, and we’re going to use int to describe the container.

There are certain rules for naming variables, not arbitrary naming rules, which must comply with the following standards (excerpted from the encyclopedia) :

  1. The identifier must start with a letter or underscore, and other identifiers can be numbers, letters, and underscores.
  2. Letters in identifiers are case-insensitive, but they have different meanings.
  3. The valid length of an identifier is 31 characters
  4. You cannot use keywords. The keyword refers to a special flag used in C language. For example, int indicates an integer. Int cannot be used as a variable name.

The best way to name a variable is to represent the value it stores. For example, if I need a variable to store the age, I can name the variable age. The name of the variable is the best way to express the content of the stored value. In some cases, however, more than one English word may be required to express the stored value of the variable. In cases where no other naming standard is specified, several classic naming conventions can be used: Hungarian, Locke, underline, and PASCAL.

Hungarian nomenclature: Hungarian nomenclature adds a descriptive name to an attribute. For example, if you need to create a variable whose age is an integer, you can write it as isex, which means sex of type int. The C language is as follows:

int iage;
Copy the code

Hump nomenclature: The hump nomenclature is a combination of multiple English words. The first letter of the English word is lowercase and the first letter of the following words is uppercase. So when I want to create a variable for myAge I can say myAge, my for my, Age for myAge. The C language is as follows:

int myAge;
Copy the code

PASCAL nomenclature: The PASCAL nomenclature is similar to the hump nomenclature in that all English words are capitalized, such as MyAge. The C language is as follows:

int MyAge;
Copy the code

Underscore naming: The underscore naming convention uses the underscore spacing when multiple English words are combined, such as my_age. The C language is as follows:

int my_ag;
Copy the code

All of the above variable creation needs to follow variable initialization. Variable initialization refers to the need to assign a value to a variable when creating it to prevent uncontrollable bugs in the subsequent programming. Variable initialization can make unknown variable values known, reducing program errors. Such as:

int my_ag=0;
Copy the code

Use the equals sign (=) to store values to the right of the equals sign in variables to the left of the equals sign; Note here that in non-special cases, the value to be stored must match the type of the variable. A complete C variable initialization code is as follows:

void main(a){
	int myAge;
}
Copy the code

2.3 Understanding character types and Using Character variable Description After understanding the creation and initialization of integer variables, then understand character types and the initialization of character variables. The character type is represented by char. Similarly, char can be used to describe variables. C language code is described as follows:

char my_name='T';
Copy the code

The preceding description is the same as the integer description. A variable is described by adding a descriptor to the name of the variable, and it also indicates that a new variable of the type is created. This creates a variable my_name and gives it a value of ‘T’.

Some students may ask, is the value of this character ‘T’? Why single quotes? This is because in C, it is a syntactic requirement to use single quotation marks when assigning or passing character values. For example, if I create a variable called T and I give a separate T to the variable my_name, will I give the variable T or the character T? So in C, characters that are enclosed by single quotes are represented as themselves.

It is also important to understand the concept of a character, which is a single symbol, letter or number, not a series of symbols. For example, “ABCDefg.; The string d[]213 “is called a string, and a character refers to a single symbol. When I pass a value of character type variable “ABCDefg,. D []213 “is incorrect. There may be a warning in the new C language standard. Programs under warning can run, but will appear uncontrollable state. When assigning characters, single quotes are required for character values.

Floating point can be understood as a decimal or a real number, divided into single precision and double precision. Single precision is represented by float and double by double. The difference between single precision and double precision is that the number is different and the precision is different. Since this series is based on a quick start tutorial, I’ll cover single-precision floats and not much else. We just need to understand that for things like decimals, we need to use float to describe variables. Such as:

float fval=1.1;
Copy the code

This uses float to describe the variable fval, which uses Hungarian nomenclature. F stands for float, and val is short for value, meaning the value of float. The stored value is 1.1.

Output of variable value

In the previous chapters, we learned about using printf to make a program display the specified content at runtime, so how do you directly print the value of a variable?

Now suppose we have a variable named myAge that stores a value of 25. Using printf to print the value of myAge to the screen, we can write:

#include<stdio.h>
void main(a){
	int myAge=25;
	printf("%d",myAge);
}
Copy the code

In the code, the variable myAge is described by an int, indicating that the value stored in the variable is an integer; To the right of the myAge variable is an equals sign, which means that the content to the right of the equals sign stores a value equal to the variable myAge to the left of the equals sign. Then print it using printf.

As mentioned in the previous section, printf will display the contents of the double quotation marks inside the parentheses on the output value screen as is. It should display as %d.

The running result of the program is:



The program does not output %d because %d is a special flag called a format placeholder. %d indicates that a value of integer type will be printed at this position. This value will be the first value after the double quotation mark; To separate double quotes from values, use commas, as inprintf("%d",myAge);Is written in the same way. %d displays the value of myAgew.

If I have multiple variables, one of which is myAge and the other is myName, then the output of the two values can be written as:

#include<stdio.h>
void main(a){
	int myAge=25;
	char myName='T';
	printf("%c:%d",myName,myAge);
}
Copy the code

The above code creates two variables, one named myAge and one named myName, which store 25 and T characters, respectively. The output code is:printf("%c:%d",myName,myAge);. In printf, we found an unknown flag %c, %c is a format placeholder like %d; %c indicates that the position will be replaced by a character value, %d indicates that the position will be replaced by an integer value; The exact output values between them will be determined by the order of the variables separated by commas after double quotes. The first value %c is replaced with the value stored in myName and %d is replaced with the value stored in myAge when the output is displayed. Colons between %c and %d are printed as is, because colons are not format placeholders. The following output is displayed:

The above example may cause some students to think that %c represents a character and automatically look for the first replacement of the character. That’s not the case. All values are replaced in order. The first placeholder will print the value of the first variable behind it, and so on.

Here’s another example code to see the output order:

#include<stdio.h>
void main(a){
	int myAge=25;
	char myName='T';
	char myName1='b';
	printf("%c:%d:%c",myName,myAge,myName1);
}
Copy the code

The above example adds a variable, myName1, that stores the value of character B. In printf all placeholders are written in the numeric order corresponding to the variable type; The first myName is %c, the second myAge is %d, and the third myName1 is %c. The results are as follows:



The format placeholder for floating point numbers is %f, as shown in the following example:

#include<stdio.h>
void main(a){
	float myAge=25.5;
	printf("My age this year is: % F",myAge);
}
Copy the code

The results are as follows:

Four,

Through the above description and explanation, we know the following contents:

  1. Understand the basic data types of C language have integer type, character type, floating point type
  2. Learned that variables are containers for storing values
  3. See how variables are created that need to be described using type symbols and need to be initialized
  4. Learned about variable hump nomenclature, Hungarian nomenclature, underscore nomenclature, PASCAL nomenclature
  5. Understand the function and usage of format placeholders %d and %c
  6. Learned about variables using printf and format placeholders for output display

IT original animation, learning materials, original tutorials, please pay attention to the public number.