博文
[置顶] C语言 新手必看(2006-11-27 13:25:00)
摘要:
本人也是刚学C不久算是老菜鸟。把我遇到的问题写出来,希望对初学者有点帮助。一、分号,大括号 分号。分号是表达式结束的标志,一般表达式后面都要有。写“;”的时候一定要注意:是否要结束前面的语句。注意:if,switch,for,while语句,数据类型定义,函数定义。if(a>b) /*if语句,没有分号*/{...}while(i+=10,i!=100); /*while语句循环体为空,依靠逗号语句实现循环*/struct a /*结构体数据定义,没有分号*/{...}b; /*结构体变量,要有分号*/int sum(...) /*函数定义,没有分号*/{....}大括号。大括号是一个语句段的标志。写大括号时最好一次写一对,然后把光标移到大括号里写。避免遗漏。二、运算符号“=”与“==”“=”用于赋值,注意“=”左边必须是变量,右边可以是常量也可以是变量。“==”用于判断两个量的大小,左右两边可以是常量也可以是变量(尽量把常量写在左边,这样“==”误写成“=”时编译器就会报错)。运算表达式有它本身的值。“=”语句值为“=”右边的量的大小;“==”语句值为判断结果,无外乎两种,0(表示“==”两边不等)1(“==”两边相等)。(i=1)==(3==4); /*i值为1,整个表达式值为0*/ “,”逗号语句是从左到右计算,整个逗号语句的值为最后一个语句的值i=(j=2,j+8,j++); /*先计算j=2,值为2,j+8不是运算表达式,跳过,计算j++得到整个括号内的值3*/“/”“/”两边都是整型变量的时候结果也是整型,而且结果遵循向0靠拢的原则。运算符的优先级及结合方向不多说了,表格一定要背牢。三、变量 命名不要吝惜,能说明白性质尽量在变量的名字上说明。避免老是使用一个字母,或者难以理解的变量名。对于较长的变量命名可以使用宏定义、类型定义。#deine UP 0x4800 /*键码的宏定义*/typedef struct{....}MEM; /*结构体变量的类型定义*/ 类型注意变量的使用范围。当不确定变量的范围的时候尽量选用值域宽的类型。双目运算符两边的变量一般类型要一致。不一致的时候需要使用类型转换。上回说的是一些小问题,真正编东西,学东西还有许多......
指针部分(2007-08-21 21:20:00)
摘要:题目要求: 数组1234567 ,倒过来输出。
# include <stdio.h>void main () { int a[7]={1,2,3,4,5,6,7}, i, *p; for (p=a+6;p>=a;p--) printf("%d,",*p); getch();}
这里应该注意2个问题。 数组的第一位置是a[0] 而不是a[1].
......
ping的源程序 (2006-12-05 12:42:00)
摘要:
// Module Name: Ping.c//// Description:// This sample illustrates how an ICMP ping app can be written// using the SOCK_RAW socket type and IPPROTO_ICMP protocol.// By creating a raw socket, the underlying layer does not change// the protocol header so that when we submit the ICMP header// nothing is changed so that the receiving end will see an // ICMP packet. Additionally, we use the record route IP option// to get a round trip path to the endpoint. Note that the size// of the IP option header that records the route is limited to// nine IP addresses.//// Compile:// cl -o Ping Ping.c ws2_32.lib /Zp1//// Command Line Options/Parameters:// Ping [host] [packet-size]// // host String name of host to ping// packet-size Integer size of packet to send // (smaller than 1024 bytes)////#pragma pack(1)#define WIN32_LEAN_AND_MEAN#include #include #include #include #define IP_RECORD_ROUTE 0x7// // IP header structure//typedef struct _iphdr {unsigned int h_len:4; // Length of the heade......
C语言printf格式笔记(2006-12-04 10:17:00)
摘要:1.转换说明符 %a(%A) 浮点数、十六进制数字和p-(P-)记数法(C99) %c 字符 %d 有符号十进制整数 %f 浮点数(包括float和doulbe) %e(%E) 浮点数指数输出[e-(E-)记数法] %g(%G) 浮点数不显无意义的零"0" %i 有符号十进制整数(与%d相同) %u 无符号十进制整数 %o 八进制整数 e.g. 0123 %x(%X) 十六进制整数0f(0F) e.g. 0x1234 %p ......
关于C语言中return的一些总结 (2006-12-03 22:10:00)
摘要:关于C语言中return的一些总结 return是C++预定义的语句,它提供了种植函数执行的一种放大。当return语句提供了一个值时,这个值就成为函数的返回值. 说到return,有必要提及主函数的定义,下面是从网络上找到的资料,好好消化吧,对了解主函数中返回值的理解有很大的帮助. 很多人甚至市面上的一些书籍,都使用了void main( ) ,其实这是错误的。C/C++ 中从来没有定义过void main( ) 。C++ 之父 Bjarne Stroustrup 在他的主页上的 FAQ 中明确地写着 The definition void main( ) { /* ... */ } is not and never has been C++, nor has it even been C.( void main( ) 从来就不存在于 C++ 或者 C )。下面我分别说一下 C 和 C++ 标准中对 main 函数的定义。 1. C 在 C89 中,main( ) 是可以接受的。Brian W. Kernighan 和 Dennis M. Ritchie 的经典巨著 The C programming Language 2e(《C 程序设计语言第二版》)用的就是 main( )。不过在最新的 C99 标准中,只有以下两种定义方式是正确的: int main( void ......
C语言的34种运算符(2006-11-29 19:21:00)
摘要:
34种运算符:
算术运算符:+ - * / % ++ --
关系运算符:< <= == > >= !=
逻辑运算符:! && ||
位运算符 :<< >> ~ | ^ &
赋值运算符:= 及其扩展
条件运算符:?:
逗号运算符:,
指针运算符:* &
求字节数 :sizeof
强制类型转换:(类型)
分量运算符:. ->
下标运算符:[]
其它 :( ) -
......
C语言程序设计技巧(2006-11-27 21:01:00)
摘要:
作者: 出处:天极论坛责任编辑: [ 2003-04-03 15:31 ]
在许多应用软件运行时都带有命令行参数,其实这些命令行参数在C语言编写的程序中也可以实现
在许多应用软件运行时都带有命令行参数,其实这些命令行参数在C语言编写的程序中也可以实现,灵活地运用命令行参数进行处理可以有效地提高程序的运行效率,收到事半功倍的效果。 C语言中有关命令行参数涉及到程序的主函数main(int argc,char *argv[]这样两个参数,其中,int argc表示命令行参数的个数(包括可执行程序名本身),char *argv[]表示每个参数的具体内容,argv[0]为命令行中可执行程序名本身,argv[1]为命令行中第二个参数的内容,依次类推。如下例输出命令行参数的个数及参数的内容:
main (int argc,char *argv[],{int I; printf("\n命令行中可执行文件名为:%s",argv[0]); printf("\n总共有%d个参数:",argc); I=0; while(argc>=1) {printf(″%s ",argv[I++]); argc--;}} 命令行参数用的最多还是在诸如DIR A:等之类带有盘符、路径或文件名这样的命令行中,所以说灵活处理这一类参数才能有效地提高程序的运行效果。譬如DIR命令,其后可以是盘符,可以是路径,也可以是文件名,如何区分这一参数呢?请看下例(此程序模拟DIR命令,程序要求在命令行输入一个参数:盘符或路径或文件名,若无参数或参数多于一个都将取默认的参数“*.*”)。
\*--------------------功能:模拟DIR命令进行处理命令行参数--------------------*/#include#include#include#inchludeint j,num=0;char ss[20],path[50],path2[50];void main (int argc,char *argv[]){ struct ffblk f; int done; if(argc==2) /*取命令行参数到数组中*/ strcpy(ss,argv[1]); else strcpy(ss,″*.*″);......
函数大全(w开头)(2006-11-27 20:09:00)
摘要:
函数名: wherex 功 能: 返回窗口内水平光标位置 用 法: int wherex(void); 程序例:
#include
int main(void) { clrscr(); gotoxy(10,10); cprintf("Current location is X: %d Y: %d\r\n", wherex(), wherey()); getch();
return 0; }
函数名: wherey 功 能: 返回窗口内垂直光标位置 用 法: int wherey(void); 程序例:
#include
int main(void) { clrscr(); gotoxy(10,10); cprintf("Current location is X: %d Y: %d\r\n", wherex(), wherey()); getch();
return 0; }
函数名: window 功 能: 定义活动文本模式窗口 用 法: void window(int left, int top, int right, int bottom); 程序例:
#include
int main(void) {
window(10,10,40,11); textcolor(BLACK); textbackground(WHITE); cprintf("This is a test\r\n");
return 0; }
函数名: write 功 能: 写到一文件中 用 法: int write(int handel, void *buf, int nbyte); 程序例:
#include #include #include #include #include #include
int main(void) { int handle; char string[40]; int length, res;
/* Create a file named "TEST.$$$" in the current directory and write a string to it. If "TEST.$$$" already exists, it will be overwritten. */
......
函数大全(v开头)(2006-11-27 20:08:00)
摘要:
函数名: vfprintf 功 能: 送格式化输出到一流中 用 法: int vfprintf(FILE *stream, char *format, va_list param); 程序例:
#include #include #include
FILE *fp;
int vfpf(char *fmt, ...) { va_list argptr; int cnt;
va_start(argptr, fmt); cnt = vfprintf(fp, fmt, argptr); va_end(argptr);
return(cnt); }
int main(void) { int inumber = 30; float fnumber = 90.0; char string[4] = "abc";
fp = tmpfile(); if (fp == NULL) { perror("tmpfile() call"); exit(1); }
vfpf("%d %f %s", inumber, fnumber, string); rewind(fp); fscanf(fp,"%d %f %s", &inumber, &fnumber, string); printf("%d %f %s\n", inumber, fnumber, string); fclose(fp);
return 0; }
函数名: vfscanf 功 能: 从流中执行格式化输入 用 法: int vfscanf(FILE *stream, char *format, va_list param); 程序例:
#include #include #include
FILE *fp;
int vfsf(char *fmt, ...) { va_list argptr; int cnt;
va_start(argptr, fmt); cnt = vfscanf(fp, fmt, argptr); va_end(argptr);
return(cnt); }
int main(void) { int inumber = 30; float fnumber = 90.0; char string[4] = ......
函数大全(u开头)(2006-11-27 20:07:00)
摘要:
函数名: ultoa 功 能: 转换一个无符号长整型数为字符串 用 法: char *ultoa(unsigned long value, char *string, int radix); 程序例:
#include #include
int main( void ) { unsigned long lnumber = 3123456789L; char string[25];
ultoa(lnumber,string,10); printf("string = %s unsigned long = %lu\n",string,lnumber);
return 0; }
函数名: ungetc 功 能: 把一个字符退回到输入流中 用 法: int ungetc(char c, FILE *stream); 程序例:
#include #include
int main( void ) { int i=0; char ch;
puts("Input an integer followed by a char:");
/* read chars until non digit or EOF */ while((ch = getchar()) != EOF && isdigit(ch)) i = 10 * i + ch - 48; /* convert ASCII into int value */
/* if non digit char was read, push it back into input buffer */ if (ch != EOF) ungetc(ch, stdin);
printf("i = %d, next char in buffer = %c\n", i, getchar()); return 0; }
函数名: ungetch 功 能: 把一个字符退回到键盘缓冲区中 用 法: int ungetch(int c); 程序例:
#include #include #include
int main( void ) { int i=0; char ch;
puts("Input an integer followed by a char:")......
