Learning Summary
1, String str= "Hello world!"; (Java), char[20]= "Hello world!"; (C). In fact, the implementation of Java string, is also a character array.
2. The trailing end of the string ends with a null character, so "Hello world! "The length of this character array is 13. The <string.h> Library has a strlen () function that can be the length of a character array of a computer string (excluding the null character, \).
3. The difference between scanf ("%s", name) and gets:
1#include <stdio.h>2 intMainvoid){3 Charname[ +];4printf"What ' s your name?\n");5scanf"%s", name);6printf"Hello%s!\n", name);7 return 0;8}
Operation Result:
What ' s your name?
Tom Green
Hello tom!
1#include <stdio.h>2 intMainvoid){3 Charname[ +];4printf"What ' s your name?\n");5 gets (name);6printf"Hello%s!\n", name);7 return 0;8}
Operation Result:
What ' s your name?
Tom Green
Hello Tom green!
4, #define可用于常量定义, format: #define Name value, the compiler will replace all the name with value after compilation, the equivalent of a simple "face value" substitution. If value is a combination of elements, the best brackets, such as # define SUM (X+y), should be in case of confusion caused by this "word game".
5. Another representation of a C constant can use the Const keyword, such as the const int months=12. Then months is a constant. In short, the const -Modified object is constant and cannot be modified.
6, Programming exercises (title 7):
1#include <stdio.h>2 #defineJ2s 3.7853 #defineY2G 1.6094 intMain () {5 DoubleMile,gallon;6printf"Enter your mile:");7scanf"%LF",&mile);8printf"Enter your gallon:");9scanf"%LF",&gallon);Tenprintf"your oil wear is%.1f\n", mile/gallon); Oneprintf"your CHN oil wear is%.1f\n", mile*y2g/(gallon*j2s)); A}
Operation Result:
Enter your mile:100
Enter your gallon:50
Your oil wear is 2.0
Your CHN oil wear is 0.9
"C Language Learning", C Primer Plus, chapter 4th, strings and formatted input/output