当前位置:网站首页>[C language] open the door of C

[C language] open the door of C

2022-07-04 07:24:00 InfoQ

Hello, Hello, everybody , This is my first time here InfoQ Writing on the platform , Please pay more attention to . I also wish infoQ The platform is getting better and better

C What is language

In a nutshell :C Language is a kind of computer programming language , It has the characteristics of advanced language , It also has the characteristics of assembly language .
C Language is developed by the bell Institute in the United States D.M.Ritchie On 1972 Launched in 2013 , stay 1978 Years later, C The language has been transplanted to a   Big   in   Small   On the model Microcomputer ( notes : A microcomputer is actually a computer system ). It can be used as   Writing system 、 Design language and application program, etc , It can also realize the design of some languages on the program , Write applications that do not depend on computer hardware , so to speak C In fact, the scope of application of language is very wide , Because it has a very powerful data processing ability , Not just in software development , And all kinds of research need to be done C Language , Like a microcontroller , And embedded development .

C What are the advantages of language over other languages

  • Less resources
  • Fast running speed
  • It is convenient to operate the hardware
  • If you want to learn Embedded Development , And communications , The first choice is C Language .
  • C Language is a senior big brother (ง •_•)ง

C Characteristics of language

Here I will use the shortest sentence to express
  • Efficiency :  Did it " And have both ", From here C The history of language can be known .
  • flexibility : C Language syntax can be created from the original code syntax , So as to give programmers more room to play .
  • Rich in functions :  You can use a lot of rich operators ( Arithmetic operator 、 concatenation operator 、 Relational operator 、 Assignment operator   and   Logical operators ) And custom structure types , Express any complex data type
  • Expressive power :  linguistic form , Freedom of writing , Structural specifications are complete , After light control .
  • Good portability : C Under different operating systems , Simply modify or not modify , Cross platform program development .
Print hello word
Next, I'll teach you how to C Print in language hello word, If you can succeed, it means that you have formally embarked on learning C Language process !
The compiler I use is :
vs 2013 
  This compiler can be downloaded on Baidu , Bear in mind : Official website , Baidu also has... On the key , I won't explain more here . The sample code is as follows :
#include<stdio.h>

int main(void)
{
 printf(&quot;hello word\n&quot;);

 return 0;
}
So here's why it looks like this (。・∀・)ノ゙ hi
  • The first 3 That's ok ,&nbsp; This &nbsp;
    int&nbsp;
    With a watch means shaping , So this
    main
    Is the name of the main function , It means that this is a main function , Bear in mind :
    every last C Procedures must have main function , And there is and only one in the whole C In the process ,
    Any program is from main Function executed .
  • The first 4 Xing He 6 That's ok ,
    { }
    ( The use of scope improves the locality of program logic , Enhance program reliability , Reduce code to code conflicts ).
  • The first 5 That's ok , Function call statements ,printf The functionality :
    Send the content to be output to the display screen to display ( Print ), Is a standard library function , Can be called between programs .
    Be careful :'\n' It stands for < Line break > It means .
  • The first 7 That's ok ,return return ,0 plastic , Back to main Among functions , There's nothing to say .
  • &nbsp; &nbsp; &nbsp; Be careful : When writing code, type  
    Characters must be in English , Characters must be in English , Characters must be in English   Say important things three times \(@^0^@)/
complete C Program
You are not required to be a beginner , Just know C What constitutes a language program is ok .
The sample code is as follows :
#include<stdio.h>
// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;arr It's essentially a pointer
int binary_search(int arr[],int k,int sz)
{ &nbsp; // Implementation of algorithm
&nbsp;&nbsp; &nbsp;int left = 0;// The first subscript of the left array
&nbsp;&nbsp; &nbsp;// The first subscript of the right array
&nbsp;&nbsp; &nbsp;int right = sz - 1; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* &nbsp;1 2 3 4 5 6 7 8 9 10
&nbsp; &nbsp; &nbsp; // Find the middle element subscript according to the left and right subscripts  &nbsp; 0 1 2 3 4 5 6 7 8 9 */&nbsp;&nbsp; &nbsp;
&nbsp;&nbsp; &nbsp;while (left<=right)// When left be equal to right  It means you've found k The subscript
&nbsp;&nbsp; &nbsp;{&nbsp;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;int mid = (left+right)/2;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if (arr[mid] < k) &nbsp; &nbsp; // If mid Elements (4) Than the elements I'm looking for (6) want < Words
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;left = mid + 1; &nbsp; //4+1=5 This refers to the element
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;else if (arr[mid] > k)// If mid Elements (4) Than the elements I'm looking for (6) want > Words
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;right = mid - 1; &nbsp;//4-1=3
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;else
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return mid; &nbsp; &nbsp;// return mid Go to the function
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}
&nbsp;&nbsp; &nbsp;}
&nbsp;&nbsp; &nbsp;return 1;// Not found. Return to 1
}
int main(void)
{
&nbsp;&nbsp; &nbsp;// Two points search
&nbsp;&nbsp; &nbsp;// Find a specific number in an ordered array
&nbsp;&nbsp; &nbsp;// If found, return the index of this array , No return found -1.
&nbsp;&nbsp; &nbsp;int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
&nbsp;&nbsp; &nbsp;int k = 7;
&nbsp;&nbsp; &nbsp;int sz = sizeof(arr) / sizeof(arr[0]);//sz Number of elements
&nbsp;&nbsp; &nbsp;//ret Receive return value  &nbsp; &nbsp; &nbsp; here arr Only the address of the first element of the array is passed
&nbsp;&nbsp; &nbsp;int ret = binary_search(arr,k,sz);// call ( Two points search ) function
&nbsp;&nbsp; &nbsp;if (ret == 1)
&nbsp;&nbsp; &nbsp;{
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;printf(&quot; Array subscript not found &quot;);
&nbsp;&nbsp; &nbsp;}
&nbsp;&nbsp; &nbsp;else
&nbsp;&nbsp; &nbsp;{
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;printf(&quot; Find array subscript :%d\n&quot;, ret);
&nbsp;&nbsp; &nbsp;}
&nbsp;&nbsp; &nbsp;return 0;
}
1. First write how this function should be used .
2. Then write how this function should be implemented .
This is a roughly complete c Language program , This is the realization , Write a function to realize an orderly binary search , Of course, now we are talking about the foundation , This is just an example I show you , Let everyone realize a complete and standardized c How to write language . In my submission :C  The procedure mainly includes the following parts :
Preprocessor instruction function variable statement  &  Expression comments, of course , Including one of your thoughts , Logic , So I recommend that every time we write a line of code , You can add notes , Let your line of code , Easier to understand . And the advantage of annotation is , When you want to see the code you've written before, you can look directly at your comment line , Then let's introduce the notes !
notes
 //  Single-line comments  &nbsp; With  //  The first single line comment , This comment can take up a single line .
/*  Single-line comments  */
/*  Multiline comment   Multiline comment   Multiline comment  */
/* */  Comments in this format can be one or more lines . You cannot embed comments in comments ,
Comments cannot also appear in strings or character values .

Print function and input function ( Focus on understanding )

printf: The function is : Print formatted data for printing , take  C  The string is written to the standard output in format ( Stout ). If the format includes a specifier ( from % Starting sub order ), The other parameters below the format will be formatted and inserted into the generated string , To replace their respective specifiers .
Parameters : Contains... To be written for thick text  C  character string . It can optionally contain embedded format specifiers , These specifiers are replaced by values specified in other subsequent parameters , And format as required .
To put it bluntly : Format output , printf(&quot;< Stylized strings >&quot;,< Parameter table >); Be careful : Remember to emphasize that it is English , It's not Chinese !
scanf: function : from  stdin  Read formatted data
from stdin(stdin yes C Standard input stream in language , It is generally used to get what the keyboard inputs into the buffer )  Reading data , And store it in the location indicated by other parameters according to the parameter format . Other parameters should point to the assigned object specified in the corresponding format in its format string .
Parameters : Format  ---- A character sequence containing characters that control the characters extracted from the stream  C  Character handling :
  • Blank character : This function will read and ignore any white space characters encountered before the next non white space character ( White space characters include space 、 New lines and tab characters  -  see  isspace --isspace, Computer functions , Mainly used to check parameters c Whether it is a blank character .) A single white space in the format string validates any number of white space characters extracted from the stream ( Does not include any white space characters ).
  • Nonwhite space character : Format specifier (%) With the exception of : Any character that is not white space ( blank 、 New lines or labels ) Or format specifier ( With % The beginning of a character ) A part of the character will cause the function to read the next character from the stream , Compare it with this non white space character , If the match , Throw it away , And this function continues to use the next character of the format . If the characters don't match , The function will fail , The subsequent characters that return and leave the stream are unread .
  • Format specification : By the initial percentage symbol (%)  The resulting sequence represents the format specifier , Used to specify the type and format of data to be retrieved from the stream and stored in the location indicated by other parameters .
  • Last , I'm talking about , I / O function (scanf  and  printf)  Is the most frequently used function , You must focus on , Especially format input and output ,C The key and difficult points of language , In fact, this kind of play and learn more in the back , Of course . So stick to it .
C Structural features of language programs
One C Language source programs can consist of one or more source files . Each source file can consist of one or more functions . Preprocessing commands are usually placed at the front of the program (#include). Every description , Every statement must begin with ;( A semicolon ) ending . Source files, no matter how many files you have ,C Language has and has only one main function . identifier   There must be at least... Between keywords + A space as an interval , If there is an obvious separator , Don't have to .
Programming specification
From clear writing , Easy to read , understand , From the perspective of maintenance , The following rules should be followed when writing procedures :
1. A description or statement takes up a line .
2. use {} The enclosed part , Usually represents a hierarchy of programs .{} It is usually aligned with the first letter of the structure statement , And take a single line .
3. A lower level statement or description can be indented by several boxes than a higher level statement or description . To make it look clearer , Increase the readability of the program .
C Language character set
  • First , Let's talk about : Characters are the most basic elements of a language .C Language characters consist of :
  • Letter  : Lowercase letters  a~z, Capital  A~Z, , respectively, 26 individual .
  • Numbers  : Arabic numerals  0~9  common  10  individual .
  • Space  : Let's talk about its types and forms here :
  • A key on a computer keyboard , Usually in the bottom line of the keyboard , It is the longest key on the computer keyboard . Represents an empty space , It's in GBA The simulator plays a role of acceleration in the default controller .
  • Computer knowledge , Spaces are counted as one character , So when you fill out a form on the website , You can write nothing , But don't press the spacebar in blank items , When you search in search engines , If you forget a word , Space can be used to temporarily replace . Spaces can also be used as small lattices, hollow lattices .
  • GBA expand : A simulation GBA Game software , It can be in Africa GBA Run through simulation in the platform GBA( Nintendo's second-generation portable game console ) game , When running the game, you also need to have the corresponding game ROM( read-only memory ).
  • Punctuation and identifiers : Punctuation has nothing to say , Let's focus on the next identifier
  • finger : A symbol used to identify an entity , It has different meanings in different application environments .
null

identifier

Variable name 、 Function name 、 label 、 These are called identifiers . Except that the library function name is defined by the system , The rest are defined by the user .C Language policy , The indicator can only be represented by : Letter (A~Z、a~z)、 Numbers (0~9) It's the decimal system 、 Underline (_) Composed string , And the first character must be composed of letters or underscores .
Be careful : In the identifier , Case is distinguished by . for example :Book and book Are two different identifiers . keyword : Keywords are C The vocabulary of language . They are right C It's special for , You can't use them as identification   operator ( Such as , Variable name ). Many keywords are used to specify different types , Such as  int. There are still some customs   Key word ( Such as ,if) Used to control the execution order of statements in a program . In the table  2.2  Listed in C Language keywords , Bold indicates C90 Keywords added to the standard , In italics C99 New to the standard   keyword , Bold italics indicate C11 Keywords added to the standard .
Of course C Language keywords are also divided into the following categories :
Type specifier 、 It's like data types that describe variables and functions ,int char Wait for the statement definer 、 Functions used to represent statements, such as  if else This preprocessing command word 、 Used to represent a preprocessing command, such as C Language header file include.

Last

For the first time in infoQ Writing on the platform , Please pay more attention to

​​
原网站

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202141536497940.html