C language
C++ C language
C++ file suffix name xxx.cpp
C language file suffix name xxx.c
C++ is backward compatible with C
, so generally use cpp suffix name
function
//Indicates the comment
main
return
In programming languages, the function can be regarded as a machine, and our content is encapsulated in the function . We only need to input some things, and we can output what we want.
- int indicates that the return type of the function is an integer type
- The function must first enter some values, which are defined in (), or not.
- There are some things that can be handled inside the function, which are defined in { }.
- Functions can return some value. return Returns the return value of the function, such as returning 0, 0 is an integer, which corresponds to the previously defined return value type int.
The writing method of the function:
such as: input two integers, return the addition result.
calling function (main function)
Where can the function be called? - main function
The compiler will read the code in top-to-bottom order.
If the order is reversed, the compiler will report an error because the compiler does not understand the add function.
The add function is called by the main function main
, so who calls the main function main? Does the return value type have to be an integer int?
variable
We can think of variables as empty boxes
and put any value of the same type in the box.
Note: variables in c language must be declared before use (python can be used directly)
identifier keyword
Identifiers are names we define ourselves to represent variables, functions, other entities, etc.
Identifiers must be declared or defined in order to be run by the compiler.
Identifier Naming Rules
Key words
int is not an identifier - int is not something we named arbitrarily, int is not the name of any entity, int is a keyword in C language.
All keywords in C language:
constant
Constants are invariants
. String constants should not be declared.
Variables can be modified by assignment, while constants cannot be modified, that is, constants cannot be assigned.
printf function
printf is formatted printing
. The first position in the brackets of printf() must be a string
. In addition to directly passing in a string, it can also be printed by means of placeholders.
placeholder printf
printf prints the variable result with placeholders.
Use printf to print out integers:
the output of this operation is: A=123 B=456
include command
#include can copy the code in the header file stdio.h into our code.
To call a function, you must first define the function.
For example, the printf function is a built-in function of the system. When we call the printf function, the content of the printf definition function is written in the header file.
The function definition of the printf function is written in this file, so when we refer to it, we can call it directly. No need to define it again in its own code file.