C回顾 - XinliWang/iOS GitHub Wiki
####1.数字输出形式
%d:print as decimal integer
%6d:print as decimal integer, at least 6 characters wide
%f:print as floating point
%6f:print as floating point, at least 6 characters wide
%.2f:print as floating point, 2 characters after decimal point
%6.2f:print as floating point, at least 6 wide and 2 after decimal point
####2.File Copying
/*read a character
while(character is not **EOF(end-of-file)** indicator)
out put the character just read
read a character */
#include<stdio.h>
main(){
int c;
c = getchar();
while(c!=EOF){
putchar(c);
c = getchar();
}
}
####3.EOF
EOF = end of file
"!=EOF" 表示文件还没有结束,“=EOF”表示文件到末尾了,输入流的结束.
EOF根本不是一个字符,而是当系统读取到文件结尾,所返回的一个信号值(值为-1)
除了表示文件结尾,EOF还可以表示标准输入的结尾。
int c;
while ((c = getchar()) != EOF) {
putchar(c);
}
#####利用EOF可以统计空格''、回车'\n'、跳转下一个tab '\t'等
int c, nl, nw, nc, state;
state = OUT;
nl = nw = nc = 0;
while ((c = getchar()) != EOF) {
++nc;
if (c == '\n')
++nl;
if (c == ' ' || c == '\n' || c = '\t')
state = OUT;
else if (state == OUT) {
state = IN;
++nw; }
}
printf("%d %d %d\n", nl, nw, nc);
疑问:
如何结束EOF 的循环?
答:control + D 终止程序,即跳出。
####4.all function arguments are passed "by value"
http://www.bluefaq.com/java/75660 关于java的讨论:
比较赞同其中的两个观点:
1.Everyone here has missed the point. Some came close, but everyone is dancing around the real issue, which is this: stack vs. heap. It’s not reference vs. value. In order to understand how Java handles memory, you need to get a good grasp of stack/heap. Crash course on stack/heap before we get to the Java implementation:
Values go on and off the stack in a nice orderly fashion, like a stack of plates at a cafeteria.
Memory in the heap (also known as dynamic memory) is haphazard and disorganized. The JVM just finds space wherever it can, and frees it up as the variables that use it are no longer needed. Okay. First off, primitives go on the stack. So this code:
int x = 3;
float y = 101.1f;
boolean amIAwesome = true;
results in this: When you declare and instantiate an object. The actual object goes on the heap. What goes on the stack? The address of the object on the heap. C++ programmers would call this a pointer, but some Java developers are racist against the word “pointer”. Whatever. Just know that the address of the object goes in the stack. Like so:
int problems = 99;
String name = "Jay-Z";
An array is an object, so it goes on the heap as well. And what about the objects in the array? They get their own heap space, and the address of each object goes inside the array.
JButton[] marxBros = new JButton[3];
marxBros[0] = new JButton("Groucho");
marxBros[1] = new JButton("Zeppo");
marxBros[2] = new JButton("Harpo");
So, what gets passed in when you call a method? If you pass in an object, what you’re actually passing in is the address of the object. Some might say the “value” of the address, and some say it’s just a reference to the object. This is the genesis of the holy war between “reference” and “value” proponents. What you call it isn’t as important as that you understand that what’s getting passed in is the address to the object.
private static void shout(String name){
System.out.println("There goes " + name + "!");
}
public static void main(String[] args){
String hisName = "John J. Jingleheimerschmitz";
String myName = hisName;
shout(myName);
}
One String gets created and space for it is allocated in the heap, and the address to the string is stored on the stack and given the identifier hisName , since the address of the second String is the same as the first, no new String is created and no new heap space is allocated, but a new identifier is created on the stack. Then we call shout() : a new stack frame is created and a new identifier, name is created and assigned the address of the already-existing String. So, value, reference? You say “potato”. <此为摘录> 2.I just noticed you referenced my article ;) The Java Spec says that everything in Java is pass-by-value. There is no such thing as “pass-by-reference” in Java. The key to understanding this is that something like
Dog myDog;
is not a Dog; it’s actually a pointer to a Dog. What that means, is when you have
Dog myDog = new Dog("Rover");
foo(myDog);
you’re essentially passing the address of the created Dog object to the foo method. (I say essentially because Java pointers aren’t direct addresses, but it’s easiest to think of them that way) Suppose the Dog object resides at memory address 42. This means we pass 42 to the method. if the Method were defined as
public void foo(Dog someDog) {
someDog.setName("Max"); // AAA
someDog = new Dog("Fifi"); // BBB
someDog.setName("Rowlf"); // CCC
}
let’s look at what’s happening. the parameter someDog is set to the value 42 at line “AAA” someDog is followed to the Dog it points to (the Dog object at address 42) that Dog (the one at address 42) is asked to change his name to Max at line “BBB” a new Dog is created. Let’s say he’s at address 74 we assign the parameter someDog to 74 at line “CCC” someDog is followed to the Dog it points to (the Dog object at address 74) that Dog (the one at address 74) is asked to change his name to Rowlf then, we return Now let’s think about what happens outside the method: Did myDog change? There’s the key. Keeping in mind that myDog is a pointer , and not an actual Dog , the answer is NO. myDog still has the value 42; it’s still pointing to the original Dog . It’s perfectly valid to follow an address and change what’s at the end of it; that does not change the variable, however. Java works exactly like C. You can assign a pointer, pass the pointer to a method, follow the pointer in the method and change the data that was pointed to. However, you cannot change where that pointer points. In C++, Ada, Pascal and other languages that support pass-by-reference, you can actually change the variable that was passed. If Java had pass-by-reference semantics, the foo method we defined above would have changed where myDog was pointing when it assigned someDog on line BBB. Think of reference parameters as being aliases for the variable passed in. When that alias is assigned, so is the variable that was passed in. Does that help? (I’ll have to add this as an addendum to my article…) — Scott
<此为摘录>
####5.Bitwise Operators
& bitwise AND
| bitwise inclusive OR
^ bitwise exclusive OR
<< leftshift
'>>' right shift
~ one's complement (unary)
####6.pop()和push()
pop函数:出栈,拿出数据的操作
push: 进栈,放入数据的操作
####7.extern 主要用来声明
对变量而言,如果你想在本源文件中使用另一个源文件的变量,就需要在使用前用extern声明该变量,或者在头文件中用extern声明该变量;
对函数而言,如果你想在本源文件中使用另一个源文件的函数,就需要在使用前用声明该变量,声明函数加不加extern都没关系,所以在头文件中函数可以不用加extern。
可以参考以下链接详解:http://blog.sina.com.cn/s/blog_54a1bca70100hbf5.html
####8.Pointer和Address
int x = 1, y = 2, z[10];
int *ip; /* ip is a pointer to int */
ip = &x; /* ip now points to x */
y = *ip; /* y is now 1 */
*ip = 0; /* x is now 0 */
ip = &z[0]; /* ip now points to z[0] */
int *pa ;
pa = &a[0] ;
x = *pa;
will copy the contents of a[0] into x.
*(pa+1)
####9. -->
Both . and -> associate from left to right, so if we have
struct rect r , *rp = &r;
then these four expressions are equivalent:
r.pt1.x
rp->pt1.x
(r.pt1).x
(rp->pt1).x
The structure operators . and -> are the top of the precedence hierarchy.
struct{
int len;
char *str;
} *p;
then ++p -> len increments len, not p.
####10.malloc和free
malloc: 申请分配一块内存区域来存放指针
free: 释放申请区域
char *strdup(char *s){
char *p;
// +1 for '\0'
p = (char *) malloc(strlen(s)+1);
if(p!=NULL) strcpy(p,s);
return p;
}
####11.typedef
typedef int INTEGER; //指定用标识符INTEGER代表int类型
typedef float REAL; //指定用REAL代表float类型
这样,以下两行等价:
① int i,j; float a,b;
② INTEGER i,j; REAL a,b;
####12.Struct
struct string{
char name[8];
int age;
struct addr address;
}student;
//其中addr为另一个结构的结构名,必须要先进行说明
struct addr{
char city[20];
unsigned lon zipcode;
char tel[14];
}
嵌套式结构成员的表达:student.address.zipcode = 201111;
####13. %s
:%s: :hello, world:
:%10s: :hello, world:
:%.10s: :hello, wor:
:%-10s: :hello, world:
:%.15s: :hello, world:
:%-15s: :hello, world :
:%15.10s: : hello, wor:
:%-15.10s: :hello, wor :
####14.String Operations
strcat(s,t) concatenate t to end of s
strncat(s,t,n) concatenate n characters of t to end of s
strcmp(s,t) return -1, 0, or 1 for s < t , s == t , s > t
strncmp(s,t,n) same as strcmp but only in first n characters
strncpy(s,t,n) copy at most n characters of t to s
strlen(s) return length of s
strcpy(s,t) copy t to s
strchr(s,c) return pointer to first c in s, or NULL if not present
strrchr(s,c) return pointer to last c in s, or NULL if not present
####15.character class
isalpha(c) non-zero if c is alphabetic, 0 if not
isupper(c) non-zero if c is upper case, 0 if not
islower(c) non-zero if c is lower case, 0 if not
isdigit(c) non-zero if c is digit, 0 if not
isalnum(c) non-zero if isalpha(c) or isdigit(c), 0 if not
isspace(c) non-zero if c is blank, tab, newline, return, formfeed, vertical tab
toupper(c) return c converted to upper case
tolower(c) return c converted to lower case
####16.Mathematical functions
sin(x) sine of x, x in radians
cos(x) cosine of x, x in radians
atan2(y,x) arctangent of y/x, in radians
exp(x) exponential function ex
log(x) natural (base e) logarithm of x (x>0) log10(x) common (base 10) logarithm of x (x>0)
pow(x,y) x^y
sqrt(x) square root of x (x>0)
fabs(x) absolute value of x
####17.random()
int main(){
//randomize()
srand((unsigned)time(NULL); //用当前时间,设置种子
printf("%d",rand()%100); //生成随机数0-99
return 0;
}