Skip to content

Commit

Permalink
..
Browse files Browse the repository at this point in the history
  • Loading branch information
kitelife committed Apr 1, 2012
1 parent 22cdb64 commit bfe7a78
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 0 deletions.
12 changes: 12 additions & 0 deletions CC++/assertSample.c
@@ -0,0 +1,12 @@
#include <assert.h>
#include <stdio.h>

int main()
{
int a = 12;
int b = 24;
assert(a > b);
printf("a is larger than b!");

return 0;
}
27 changes: 27 additions & 0 deletions CC++/fseekSample.c
@@ -0,0 +1,27 @@
#include <stdio.h>

long filesize(FILE *stream);

int main(void)
{
FILE *stream;

stream = fopen("MyFile.txt", "w+");
fprintf(stream, "This is a test");
printf("FileSize of MyFile.txt is %ld bytes\n", filesize(stream));
fclose(stream);

return 0;
}

long filesize(FILE *stream)
{
long curpos, length;

curpos = ftell(stream);
fseek(stream, 0L, SEEK_END);
length = ftell(stream);
fseek(stream, curpos, SEEK_SET);

return length;
}
20 changes: 20 additions & 0 deletions CC++/functionPointer.c
@@ -0,0 +1,20 @@
// 验证函数指针
#include <stdio.h>

int fun(int x)
{
return x + 5;
}

int main()
{
int (*f)(int x) = &fun;
int result = f(5);

printf("Result: %d\n", result);
printf("The address of fun: %d\n", fun);
printf("The address of &fun: %d\n", &fun);
printf("The address of fun + 1: %d\n", fun + 1);
printf("The address of &fun + 1: %d\n", &fun + 1);
return 0;
}
12 changes: 12 additions & 0 deletions CC++/limitsSample.c
@@ -0,0 +1,12 @@
#include <stdio.h>
#include <limits.h>

int main()
{
printf("CHAR_BIT: %d\n", CHAR_BIT);
printf("CHAR_MIN: %d\n", CHAR_MIN);
printf("CHAR_MAX: %d\n", CHAR_MAX);
printf("INT_MIN: %d\n", INT_MIN);
printf("INT_MAX: %d\n", INT_MAX);
return 0;
}
27 changes: 27 additions & 0 deletions CC++/structPointer.c
@@ -0,0 +1,27 @@
// 验证结构体指针
//
#include <stdio.h>

struct sample{
int t;
char v;
};

int main()
{
struct sample *sa =(struct sample *)malloc(sizeof(struct sample));
struct sample sb = *sa;
sa->t = 2;
sa->v = 'a';

printf("The address sa: %d\n", sa);
printf("The address sb: %d\n", &sb);
printf("%d\n", sa[0]);

int array[5] = {0,1,2,3,4};
printf("%d\n",*array);
printf("%d\n",array);
printf("%d\n",array[0]);

return 0;
}

0 comments on commit bfe7a78

Please sign in to comment.