C language difficulty 1 input output, link, string manipulation
Recently found to write a period of C, there are some points and not very good get, I would like to summarize, write a series of blog, oneself also good to tidy up the idea. This series is mainly from three books: "C Expert Programming", "C Traps and defects", "C and pointers". Interested students can take a good look at these books, the basis for a good C language is a very good number of books.
1 formats for some input and outputThe corresponding printf () and scanf () functions; Here I give the usual scanf format code, and the printf () function is the same.
2 The use of the GetChar () function;The GetChar () function reads a character from the standard input and returns its value. If no more characters are present in the input, the function returns the constant EOF (defined in stdio.h), which is used to prompt the end of the file. GetChar () is a library function in Stdio.h, which is to read a character from the stdin stream, that is, if the stdin has data, it can be read directly without input, the first time GetChar (), it does require manual input, but if you lose more than one character, Subsequent getchar () are read directly from the buffer when they are executed. In fact, the program GetChar, memory buffer, input device. You press the key is put in the buffer, and then for the program GetChar you have not tried to hold a lot of keys and then wait a moment will drip drops, that is, the buffer is full, you press the key is not stored in the buffer. Keyboard input characters are stored in the buffer, once you type Enter, GetChar into the buffer to read the characters, only return the first character as the value of the GetChar function at a time, if there is a loop or enough GetChar statement, it will read out all the characters in the buffer until ' \ n '. To understand this, the sequence of characters you enter is read sequentially because the function of the loop is to reuse GetChar to read the characters in the buffer, instead of GetChar to read multiple characters, in fact GetChar can only read one character at a time. If you need to cancel the ' \ n ' effect , can be used GetChar (), to clear, here GetChar (); just got ' \ n ' but not assigned to any character variable, so there's no effect, This is the equivalent of erasing this character. Also note that here you enter SSSS on the keyboard to see the echo is from the role of GetChar, if you use getch can not see what you entered.
1#include <stdio.h>2 3 intMain ()4 {5 inttest;6 while((Test=getchar ())!=eof &&test!='\ n')7 {8printf"%d\n", test);9 }Ten return 0; One}
3 Declarations of some pointers I've been wondering what's going on with int *a and int* a? In fact, the two declarations have the same meaning, which is to declare a pointer variable a that points to an integral type. But the latter is not a good approach because it is prone to error when declaring several variables. such as int* a,b,c; in fact, you just want to declare a as a pointer variable.
4 link Properties in the scope of the variable, we should pay attention to the scope, and link properties are closely related.
5 2-dimensional array as a parameter 2-dimensional arrays are used as parameters in many places, but the problem is very error-prone. Note the size of the second dimension must be specified as a parameter. For example A[][10] This can be used as a parameter, but if it is: a[][]. You cannot be a formal parameter. 6 string length There is a strlen function in the library function to get the length of the string, but pay attention when using it, otherwise it is easy to make mistakes, for example:
7 character classification is a function that includes some characters in the library function, it is convenient to do some basic operation on the characters.
Associated with some memory operations, similar in functionality, but more powerful.
8 & &&,| and | | The difference between & is the bitwise operator; && is a logical operator; | is a bitwise operator; is a logical OR operator;
9 The details of the symbol. When using the multi-character symbol, pay attention to the existence of the space, can not be more than a few characters in the character symbol, will cause the meaning to completely change
C language Difficulty 1 input output, link, string operation