当前位置:网站首页>Basic Q & A of introductory C language
Basic Q & A of introductory C language
2022-07-06 16:04:00 【Programming fish 66】
1 What is the process of compiling and linking source programs ?
2 Write the first simple C Language program
The program has been compiled 、 link , The operation results are as follows .
3 C Language is a strongly typed language , What's the meaning of this? ?
Any program must process data , There are many types of data that computers can process . stay C Language program , Variables used to store data must be defined in advance before they can be used in programs .
The syntax for defining variables is as follows :
Variable type name Variable name table ;
for example , The following statement defines x、y、z Three variable names , Its value can only take integer value :
int x,y,z;
stay C In the program , Each variable must declare its value type . therefore ,C Language is a strongly typed programming language .
For constants used in programs 、 The type of variable must be defined in advance before it can be used , This is one of the means to ensure the reliability of the program . Some early computer programming languages did not require the definition of variable types , therefore , The type of a variable is uncertain during program operation , This will reduce the reliability of the program .
4 How to understand variables and constants ?
Variables and constants are equivalent to readable, writable and read-only data . Constants are a protection mechanism for data , There are special constants in the memory block allocated to the program ( read-only ) Storage area .
5 How to correctly understand and use assignment operators ?
Assignment operator “=” The variables on the left and the expressions on the right are connected , Assign the value of the expression to the variable on the left . Assignment operators have low priority , Only before the comma operator .
One assignment expression can contain multiple assignment expressions , The value of the assignment expression is equal to the value of the variable on the left , Without parentheses , Assignment operator press “ right to left ” Combined sequential operation . Due to the low priority of assignment operators , If it appears in other expressions, it needs to take precedence , Add parentheses .
Assignment operator “=” The variables on the left and the expressions on the right are connected , When the data types of variables and expressions are inconsistent , What kind of results will be produced ?
C Language gives the function of type conversion of assignment expression , Once the data types of variables and expressions are inconsistent , Type conversion will be performed automatically , Convert the value of the expression to the direction of the variable type . Of course , Not all mismatched types can be converted , Type conversion has a certain premise , Must be close 、 Only types that can be converted can be converted .
6 Why expressions 1/2 The value of is 0?
In the following code snippet , Variable x The value of is 0:float x;
float x;
x=1/2;
Variable x Although it is defined as single precision floating point , But after the above code is executed ,x The values for 0, instead of 0.5. This is because you are evaluating the expression 1/2 when , because 1 and 2 Are all integer constants , The result of the calculation can only take the integer part , So it's 0.
To prevent this from happening , There are two ways to do this :
(1) Use 1.0 Indicates that the divisor is a floating-point data , namely x=1.0/2.
(2) Use cast , namely (float)1/2. Cast is the use of explicit expressions to convert one data type to another , The format for :
( Type name ) expression
7 Character data and integer data 、 Why floating point data can be directly calculated ?
Character data is used to represent ASCII character . because ASCII Characters are in memory with ASCII Stored in coded form , Therefore, character data can be regarded as an integer and integer data 、 Floating point data directly performs arithmetic operations , This is not allowed in other computer languages .
For example, the following code snippet :
char y=’A’;
int z=y+x;
After the above code is executed , Variable z The value of is 97.
8 How to understand comma operators and comma expressions ?
The comma expression is C An operator peculiar to a language . stay C In all operators of the language , Comma operation has the lowest priority . Comma operator concatenates multiple expressions using commas , The values of each expression are calculated from left to right , The value of the entire comma expression is the right most, that is, the value of the last evaluated expression . Comma expressions are often used to complete multiple calculations or operations in a statement . for example ,t=x;x=y;y=t; Will be treated as three statements , and t=x,x=y,y=t; Will be treated as a statement .
Because the comma operator is C The lowest priority operator in the language , When the expression contains a comma operator , Special attention should be paid to the order of precedence of operators . for example :int x=3,y;
int x=3,y;
y=1,x++
// After execution x=4,y=1. and :
int x=3,y;
y=(1,x++);
// After execution ,y=3,x=4.
9 Overflow of integer data
Data of any data type has its definite numerical representation range in the computer , Once out of this range , There will be overflow problems .
10 Why do equality comparisons for floating-point data sometimes have problems ?
stay C In language , Only integer data and character data are accurately represented . Floating point data is expressed in exponential form , The number of significant digits of data is limited , Therefore, floating-point data is imprecise . When performing an equality comparison on floating point data , Sometimes when two equal numbers are compared, there may also be inequality .
In order to solve the error problem of equal comparison of floating-point numbers , It can be stipulated that when the absolute value of two numbers after subtraction is less than a sufficiently small number, they are considered equal .
11 What is the initial value of an uninitialized variable ?
If the variable is a static storage type variable , The system will automatically assign initial values when compiling 0( For numerical variables )、 Null character ( For character variables )、 Or null pointer ( For pointer variables ); Pointer related articles :C In language “ Dangling pointer ” and “ Wild pointer ” What does that mean? ? If the variable is a dynamic storage type , Then without assigning an initial value , There will be an uncertain value ( Garbage is worth , The historical value left after the cell is used ) As its initial value , This is very dangerous , Especially for an uncertain pointer , Modifying the value of the storage unit it points to can cause great harm . therefore , Programmers are generally required to give reasonable initial values to variables .
12 C Logical values in language 1 and 0 How to judge ?
C In language , Generally, when performing logical operations , Treat all non-zero values as 1, That is, logical truth ; And only itself is 0 The value of is regarded as 0, That is, logical false participation in judgment .
13 When solving a logical expression “ Stop when there is a solution ”( Short circuit evaluation ) What does that mean? ?
When it is necessary to judge that multiple conditions are true at the same time or at least one of them is true , You need to use logical operators && and ||. expression A && B Said when A And B When both are true , Condition is true ; expression A || B Said when A And B When at least one is true , Condition is true .
When solving A && B when , as long as A If it is false, the whole expression must be false , There is no need to solve the expression B. And for expressions A || B, Just the expression A It's true , Then the entire expression must be true , There is no need to solve the expression B.
14 switch How statements are executed ?
C In language switch Statement is used to deal with the judgment problem of multiple branches .
stay switch In the case of multiple branches in the statement , Just find one that matches the value of the expression case Branch , Then execute... Sequentially from this position , Unless you meet break Sentence or switch End of sentence .
When no one case When it matches the value of the expression , execute default Statements in branches , But that doesn't mean default Branch must be located at switch All of the statements case After branching , It can be located in switch Anywhere in the statement . Again , If in default There's no... In the branch break sentence , Then the program will still execute sequentially .
stay switch In the sentence ,case Just a statement label , It does not make conditional judgments . therefore , stay switch Statement execution time , Will be based on switch The value of the following expression finds a matching entry label , Then from this label ( That is, to the corresponding case) Start to carry out , No more conditional judgment .
15 stay C Used in program goto Is the sentence harmful but not beneficial ?
goto The sentence is C A statement in a language that controls the jump of a program , Many books say to use with caution , Because of unlimited use goto sentence , It may cause confusion in the whole program , Even programmers can't judge the running process of the program . however , This does not mean that goto It's a matter of harm but no benefit . in fact , It's just a matter of programming style ,goto It is indeed a concise and clear statement , Proper use does no harm , Of course , You can't use it too much in a program , In particular, there are too many nested uses , In that case, there will really be a situation where there are hundreds of harm but no benefit .
By jumping statements , Can better understand the essence of circular statements :
16 What is the basic idea of exhaustive method ?
The traditional mathematical problem-solving methods usually have a series of equations 、 Find a simple algorithm, etc , That's because the human brain can't do a lot of 、 High speed computing . In computer data processing , The computer can judge all possible situations of a problem through a circular program , Thus, various possible situations that meet the constraints of the problem are obtained , These possible situations are the solutions of practical problems . Due to the high speed and machinability of computers , It can automatically and continuously repeat the same processing under the control of the program , therefore “ Exhaustive method ” It is widely used in programming . for example “ Chicken and rabbit in the same cage ” The problem can be solved by exhaustive method .
17 The array is defined with initial values , Whether the size of the array can be omitted ?
Assign an initial value to the array when defining , If you assign initial values to all array elements , Then you can omit the size of the one-dimensional array , If a two-dimensional array is used, only the size of its first dimension can be omitted , The size of the second dimension must be clearly specified . If only initial values are assigned to some elements during definition , Then the size of the array cannot be omitted . For example, to define a one-dimensional integer array of three elements , Assign initial values respectively 1,2,3, Can be defined as follows :int a[]={1,2,3}; The size of the array is omitted . But if you define an array of four elements , Also assigned three initial values , It should be defined as follows :int a[4]={1,2,3}, Be careful , The length at this time cannot be omitted .
18 Whether the character array is equivalent to the string ?
Unequivalence . stay C In language , Strings are treated as character arrays , But all strings must start with ‘\0’ As a closing sign , Ordinary character arrays do not have this requirement . When a string is used to assign a value to a character array , It also takes up different storage space . Suppose there is the following definition form :char a[]={‘h’,’e’,’l’,’l’,’o’};
char a[]={‘h’,’e’,’l’,’l’,’o’};
char b[]={“hello”};
Although the character array a and b It all includes hello A few characters , however a Arrays are assigned by a single character , and b Arrays are assigned in the form of strings . that ,a The array only needs to have 5 A size of bytes , and b Arrays need 6 Bytes , Because in b Array , The system will automatically add an end flag at the end of the string ‘\0’. For the array of the above two cases , Its output forms are also different . If you want to output a The characters in the array , It can only be output character by character , And yes b In terms of arrays , It can take the form of one-time output of the whole string , And the output character will not contain ‘\0’.
19 What is a local variable 、 Global variables ?
Variables defined in a function , Its scope is limited to the functions that define it , Cannot be used in other functions , This variable is called “ local variable ”.
Variables defined outside functions , Its scope is the program location that defines the variable until the end of the program , In other functions , You can use the value of this variable , Changes to the value of a variable are also valid within the full scope of the variable , This variable is called “ Global variables ”.
20 How to understand “ Static storage category static” The variable of ?
In general , When a program calls a function , The variable definition and initialization assignment in the function will be performed first , Then execute other code . for example , For the following functions factorial Used to calculate parameters x The factorial value of :
long factorial(int x)
{
long p=1;
for(;x>=1;x--) p=p*x;
return p;
}
Every time a function is called in a program factorial when , Will define variables p, And give it an initial value 1. At the end of function execution , Through execution return p; Statement will p After the value of is passed to the system store , Variable p Will be released by the system . This type of variable is also called “auto Storage class ” or “ Dynamic storage category ”, That is, every time a function is called , All need to redefine variables , Reallocate storage space , Therefore, its storage address is “ dynamic ” Of .
If you want to... After the function exits , The values of some of these variables are still retained , For future function calls , The variable should be defined as “static Storage class ”, namely “ Static storage category ”. Usually you don't want to define global variables , But I hope that the value of the local variable inside the function will not be released , That is, you can use static storage variables .
Conclusion
Last , Thank you for reading this article . If you like these programming languages , Please share with your friends and colleagues .
If you have any questions or feedback , Or any other programming language you think should be worth learning , Also welcome to share with us in the comment area .
resources & More in-depth readings :
Open the mysterious channel to get information, click the link to join the group chat 【C Language /C++ Programming learning base 】:828339809
边栏推荐
- Essai de pénétration (1) - - outils nécessaires, navigation
- MySQL授予用户指定内容的操作权限
- socket通讯
- Opencv learning log 19 skin grinding
- Alice and Bob (2021 Niuke summer multi school training camp 1)
- 对iptables进行常规操作
- Research Report on surgical fluid treatment industry - market status analysis and development prospect prediction
- Find 3-friendly Integers
- 0-1 knapsack problem (I)
- 【高老师UML软件建模基础】20级云班课习题答案合集
猜你喜欢
渗透测试 ( 2 ) --- 渗透测试系统、靶机、GoogleHacking、kali工具
Pyside6 signal, slot
C语言必背代码大全
渗透测试 ( 4 ) --- Meterpreter 命令详解
[teacher Gao UML software modeling foundation] collection of exercises and answers for level 20 cloud class
Matlab comprehensive exercise: application in signal and system
Nodejs+vue网上鲜花店销售信息系统express+mysql
Penetration test (3) -- Metasploit framework (MSF)
mysql导入数据库报错 [Err] 1273 – Unknown collation: ‘utf8mb4_0900_ai_ci’
C语言数组的概念
随机推荐
【高老师软件需求分析】20级云班课习题答案合集
信息安全-威胁检测引擎-常见规则引擎底座性能比较
C语言学习笔记
Opencv learning log 31 -- background difference
nodejs爬虫
【练习-9】Zombie’s Treasure Chest
[exercise-3] (UVA 442) matrix chain multiplication
SSM框架常用配置文件
MySQL授予用户指定内容的操作权限
Analyse du format protobuf du rideau en temps réel et du rideau historique de la station B
Nodejs+vue网上鲜花店销售信息系统express+mysql
【练习-1】(Uva 673) Parentheses Balance/平衡的括号 (栈stack)
Frida hook so layer, protobuf data analysis
程序员的你,有哪些炫技的代码写法?
C语言数组的概念
Market trend report, technical innovation and market forecast of geosynthetic clay liner in China
Interesting drink
Information security - threat detection - Flink broadcast stream broadcaststate dual stream merging application in filtering security logs
STM32 how to use stlink download program: light LED running light (Library version)
Record of brushing questions with force deduction -- complete knapsack problem (I)