Quick learning C language 1: Hello World

Source: Internet
Author: User

Quick learning C language 1: Hello World
It is estimated that students who do not write the C language have also heard the C language. Learn it from the beginning quickly, and they will certainly be able to use it in the future. If you have used other C-like languages, such as JAVA and C #, the C syntax should be fast. First, quickly learn and practice some basic language elements, basic types, expressions, functions, loop structures, basic string operations, basic pointer operations, dynamic memory allocation, and complex data representation using structures, use function pointers to implement flexible logic. Although C is a very small language, you have to design more exercises to learn. For the basic type, I remember char and int, which are not commonly used. I should try again when I use them. Expressions are similar to JAVA and C #. You don't need to learn the basics. The meanings of Arithmetic Operators, Relational operators, logical operators, commas, and parentheses are also similar, the final result of an expression also has a type and a value. Function functions are the most basic abstraction. there is basically no language without the concept of functions. It encapsulates a series of operations, the simplest Hello world, as shown below. Static void hello_world () {printf ("hello, world \ n");} all our exercises are hand-written functions and do not need to be called externally. Therefore, we add a static, indicates that it is only visible in this file. If printf outputs a row, \ n is added at the end. Common formatting parameters include % d, % c, % s, and % p, which indicate the output of int, char, string, and pointer respectively. Branch, the loop structure is similar to other languages, but the I Declaration should be placed at the beginning of the function, c89 is like this. Static void n_hello_world (int n) {int I = 0; for (I = 0; I <n; I ++) {printf ("hello, world \ n ") ;}} string exercises. The strlen function used to obtain the length of a string is just for this purpose. However, we can write a trainer by ourselves. c has no string type, the string is represented by an array of characters ending with '\ 0', so the for loop can be rolled from the beginning to the position of' \ 0. Copy the code // string exercise to calculate the string length static int w_strlen (const char * str) {int I; // scroll the pointer backward and increment the I, until the end of the string is found for (I = 0; * str! = '\ 0'; str ++, I ++) {;} return I;} copy the code const modifier to indicate that this parameter cannot be changed in the function to prevent unexpected changes. Char * is the legendary string. To write C Programs, you must make good use of the for statement. If you use it, you can write compact programs. for example, the comma expression after the first semicolon of the for statement can increase by two variables. To understand how to store strings, the first method is to allocate memory during compilation. It is a String constant and the memory pointed to by the pointer s1 cannot be changed. The second method should be the memory allocated on the stack (uncertain), where the characters can be modified through the pointer. Copy the code static void change_str_test () {// constant cannot be modified // char * s1 = "hello"; // will core dump char s1 [10] = "hello "; * s1 = 'P'; printf ("% s \ n", s1);} You can copy the code pointer to exercise the pointer for addition and subtraction, the Type length that it points to is rolled every time it is added. For example, the char pointer is to scroll 1 byte. Copy the code // pointer exercise and reverse the string static char * reverse (char * str) {char * ret = str; // roll to char * p = str + w_strlen (str)-1; char c; // two pointers before \ 0 at the end of the character array. One is rolled back, A rolling from the back to the back until it is required to be staggered // in the rolling process, exchange the two characters pointed to by the pointer for (; p> str; -- p, ++ str) {printf ("debug [reverse]: % p % c \ n", p, str, * p, * str); c = * p; * p = * str; * str = c;} return ret;} copy the code c = * p to retrieve the character pointed to by the pointer p, assign the value to the variable c, and * to indicate the value. * P = * str is equivalent to p [I] = str [I]. The value on the right is the value, and the value on the left is the value. The value is assigned to the value, it looks a little strange, but it is written like this. P = * str is definitely incorrect, Because p is a pointer type, and * str is calculated as a character type. Dynamic Memory Allocation I remember that TCPL did not talk about memory allocation functions such as malloc and free in the previous chapters. Many tasks only need to allocate memory in the compilation phase, however, large and complex programs must dynamically manage some memory. If the C language does not have GC, You need to manually release the dynamically allocated memory, otherwise it will cause memory leakage. Therefore, you must allocate resources. If there is malloc, you must think about where it should be free. I have learned two principles: Who is allocated, who is released, who is released, and who is released. The memory from malloc must be converted to the pointer type you need, then, when you are free, the pointer will be rolled to the starting point of your dynamic memory allocation. Copy code // memory application related, connect two strings static void concat_test () {char * a = "hello"; char * B = "world "; // The result string length is two-character escape Length plus \ 0 position int len = w_strlen (a) + w_strlen (B) + 1; // dynamically allocate the memory char * p = (char *) malloc (sizeof (char) * len); char * result; // you must determine whether the memory is allocated if (p! = NULL) {// Save the start pointer of the dynamically allocated memory. When free is used, free result = p; // scroll p and a until the end of a while (*! = '\ 0') {printf ("debug [concat_test]: while a % p % c \ n", a, * ); * p ++ = * a ++;} // scroll p and B until the end of B while (* B! = '\ 0') {printf ("debug [concat_test]: while B % p % c \ n", a, * ); * p ++ = * B ++;} // The End of the entire 0 * p = '\ 0'; printf ("concat_test: % s \ n", result ); // release the dynamically allocated memory free (result);} else {printf ("malloc error") ;}} copy the code structure exercise C no class, to express complex data, the structure is required, and the structure can also be indicated by a pointer. If it is a structure variable, it is used to reference members ., if it is a pointer to the structure, it seems that the reference member uses-> nothing special. After the structure array is dynamically allocated, the boundary of pointer Scrolling should not be used. If the structure member points to the memory that is dynamically allocated, remember to be free. There is no structure. It is estimated that a large program cannot be written, and the structure should be used a lot. Copy the code // structure exercise, the statistics system struct customer {char * name; int age ;}; static void customer_manager () {// directly allocate the struct customer wawa on the stack; struct customer * p_wawa; struct customer * p_customers; int n = 2; char name [] = "wawa"; // char * name = "wawa "; // splint warning char name2 [] = "tiancai"; // access member wawa directly with the structure name. name = name; wawa. age = 30; printf ("% s is % d years old \ n", wawa. name, wawa. age); // use a pointer to access the structure member p_waw A = & wawa; p_wawa-> age = 31; printf ("% s is % d years old \ n", wawa. name, wawa. age); // dynamically allocates memory p_mers MERs = (struct customer *) malloc (sizeof (struct customer) * n) for the employee array; if (p_customers! = NULL) {// set the first entry of the array p_customers-> name = name; p_customers-> age = 10; // set the second entry of the array p_customers ++; p_customers-> name = name2; p_customers-> age = 30; // scroll out of the array, and then reverse loop to the array to start p_customers ++; while (n --> 0) {p_customers --; printf ("% s is % d years old \ n", p_customers-> name, p_customers-> age);} // release the dynamically allocated memory, p_customers is already at the starting position. // name1 In the struct. name2 is allocated on the stack and free (p_customers) is not released );}} many languages have the features of higher-order functions, For example, a function parameter or return value can also be a function, and a function pointer in C can achieve similar results for callback functions. However, the function pointer of C is quite strange and hard to remember. If it doesn't work, you can use typedef to retry the name, which is easier to write. The following uses a classic bubble sort to demonstrate the use of function pointers. Passing different comparison functions can change the behavior of the sort functions. This is a convenient way to write complex and flexible logic. Copy code // function pointer exercise, sort // comparison function of Positive Sorting static int cmp_default (int a, int B) {return a-B ;} // comparison function of reverse sorting static int cmp_reverse (int a, int B) {return B-a;} // Bubble Sorting Algorithm of int type, you can input a comparison function pointer // similar to a callback function. This function requires two int parameters and returns intstatic void sort (int * arr, int n, int (* cmp) (int, int) {int I, j, t; int * p, * q; p = arr; for (I = 0; I <n; I ++, p ++) {q = p; for (j = I; j <n; j ++, q ++) {// call the function pointed to by the function pointer is the same as using the function, it seems to be a simple way to write if (cm P (* p, * q)> 0) {t = * p; * p = * q; * q = t ;}}}} // test the sorting function static void sort_test () {int arr [] = {4, 5, 3, 1, 2}; int I, n = 5; // Positive Sorting, the address of the input cmp_default function. It seems that the & get address sort (arr, 5, cmp_default); for (I = 0; I <n; I ++) is not required) {printf ("% d % s", arr [I], I = n-1? "": ",");} Printf ("\ n"); // reverse sorting, same as sort (arr, 5, cmp_reverse); for (I = 0; I <n; I ++) {printf ("% d % s", arr [I], I = n-1? "": ",");} Printf ("\ n ");}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.