bootgames 发表于 2013-5-4 13:49:16

C语言编程能力如何提升?

同样实现一个功能,别人的代码让我感觉简洁精练,而我自己写的长、繁琐,请问如何提升自己的编程水平?能够写出简洁高效的代码?

monkerman 发表于 2013-5-4 13:55:02

菜鸟路过.
我以前在 CSDN 上得到的答案是: 多看书, 多看优秀源码, 多敲代码. 一般还要加上多思考.

monkerman 发表于 2013-5-4 13:57:38

看了楼主的帖子: 我...........可以说你基础不扎实么? 建议先把基础知识轮一遍吧.{:sad:}

ywhbn 发表于 2013-5-4 22:50:09

代码格式可以参照 Linux Kernel Coding Style ,逻辑结构可以先画流程图、伪代码、优化结构,然后再写代码,也可以先写代码再完善:合适的变量名,短小的子程序,简单的结构
推荐看《代码大全》

1148729990 发表于 2013-5-4 23:05:44

也是很纠结的事..........优秀源码不好找啊,,求资源

LDMega 发表于 2013-5-4 23:13:49

1148729990 发表于 2013-5-4 23:05
也是很纠结的事..........优秀源码不好找啊,,求资源

很多优秀的RTOS就是很好的源码啊!

1148729990 发表于 2013-5-4 23:23:02

LDMega 发表于 2013-5-4 23:13 static/image/common/back.gif
很多优秀的RTOS就是很好的源码啊!

嗯...谢谢...但同时还想好好学习别人的好的编程框架...不喜欢以前那种脚踩西瓜皮滑到那就是那...

jimmyfan 发表于 2013-5-5 09:24:58

同样啊,自己写的程序真是没法和那些简洁的比啊

wugang_1213 发表于 2013-5-5 09:39:11

悟道靠自己, 要多思考,多总结。

dzmcs 发表于 2013-5-5 09:40:08

lua nginx linux kernel代码

小点的axel

zuu0 发表于 2013-5-5 09:46:31

这东西,没有最好,只有更好

每次改都能发现改进的空间

lans0625 发表于 2013-5-5 12:45:19

代码要简洁,多参考优秀的源码。
代码要高效,只能学习算法。算法才是程序的灵魂。

god-father 发表于 2013-5-5 12:56:35

写代码的目的是做产品,产品很重要一点是要快速推向市场,写代码最重要一点是达到功能,尽量少BUG。
至于自己的代码比人家的啰嗦,这个我认为不重要。如果你觉得你的代码有改进的必要,可以在后续的升级中完善。
编程是熟能生巧的,多做做产品,自己就会比较顺手。
总之,一步一步来,一口是无法吃成胖子的。

3htech 发表于 2013-5-6 17:02:31

不引进先人的东西,只闭门造车,制造的东西不太可靠。尤其是一个人搞一个项目的时候。

122402902 发表于 2013-5-6 17:04:37

师傅领进门,修行靠个人

BigWolf 发表于 2013-5-6 17:25:55

都是靠第一直觉想的算法,基本上不知道怎么修改。

ITOP 发表于 2013-5-6 17:29:22

算法真的很重要呀!多动手多参考优秀代码!

millwood0 发表于 2013-5-6 18:26:39

C语言编程能力如何提升?

1) write more code;
2) write smarter code;

别人的代码让我感觉简洁精练,

Don't write short code. Write code that's easy to understand, easy to maintain and easy to port.

For example, this would be a bad piece of code to set a pin to output:DDRB |= (1<<2);If you in the future need to change the port or pin assignment, you will need to manually change all the code.

Instead, use this:
#define LED_DDRDDRB //led on portb
#define LED          (1<<2) //led on pin.2

#define IO_OUT(ddr, pins)ddr |= (pins) //set pins as output on ddr

...
IO_OUT(LED_DDR, LED); //set led as output
You only need to change LED_DDR or LED if you wish to change port / pin assignment; you only need to change IO_OUT() if you wish to port the code to a different mcu with a different way to set the output - on a PIC, for example, IO_OUT should be defined as such:
#define IO_OUT(ddr, pins) ddr &=~(pins) //set pins as output on ddrEssentially,you try to code to a logic machine as much as you can.

wind2100 发表于 2013-5-6 19:26:45

这个和思维 方式有关系练思维 可以下下棋 之类的

xslff 发表于 2013-5-6 19:45:08

吃饭想,睡觉想,走路仍然想,不出一年,必成高手!

monkerman 发表于 2013-5-6 20:00:11

millwood0 发表于 2013-5-6 18:26 static/image/common/back.gif
1) write more code;
2) write smarter code;



+++++++1
Don't write short code. Write code that's easy to understand, easy to maintain and easy to port.
还有俺的签名. {:lol:}

monkerman 发表于 2013-5-6 20:03:34

本帖最后由 monkerman 于 2013-5-6 20:06 编辑

推荐楼主看一篇文章, 里面有些原则不错, 我也是领悟不多, 介绍给你吧. 共勉.
作者: RobPike
Notes on Programming in C
网上也许有中译版.

绿茶山人 发表于 2013-5-6 20:08:21

看了18楼的回复,真是学习了!

luckly2008can 发表于 2013-5-7 08:58:07

基础还是最重要的

椒盐时代 发表于 2013-5-7 10:18:48

点滴的积累就会有一个质变

guowanling8061 发表于 2013-5-7 10:27:39

monkerman 发表于 2013-5-4 13:55 static/image/common/back.gif
菜鸟路过.
我以前在 CSDN 上得到的答案是: 多看书, 多看优秀源码, 多敲代码. 一般还要加上多思考.
...

赞成此君建议!不是一蹴而就的?经验加积累,前提基础一定要夯实!

ZSHDZ 发表于 2013-5-7 19:05:06

18楼很惊艳

millwood0 发表于 2013-5-8 04:47:33

I was asked if I could provide a few examples of utilizing the approach I laid out earlier.

I typically use a set of macros to operate the ports / pins. I put those and other often-used macros (like NOP(), ei(), di(), etc.) into a file called gpio.h.

gpio.h always contains the same macros:

IO_SET(port, pins): set pins on port
IO_CLR(port, pins): clear pins on port
IO_FLP(port, pins): flip pins on port

IO_OUT(ddr, pins): pins set as otuput
IO_IN(ddr, pins): pins set as input

when I need to operate on a pin/port, I always use those macros. When I port the code from one mcu to another, all I need to do is to include the gpio.h file for that platform and I can recompile my code, with confidence that it will always work and no debugging is required.

millwood0 发表于 2013-5-8 04:54:39

Another example: i2c devices (or spi devices for that matter).

Typical implementation is to write i2c driver into the device driver file and compile them.

I do it over two layers: i2c layer and device layer.

In the device layer, I would call i2c_start(), i2c_stop(), i2c_write() and i2c_read() to perform i2c specific operations, without any knowledge of if the operations are done via software or hardware i2c.

So my device layer would look like this:
==========myi2c.c/.h=========
#include "i2c_hw.h"//use hardware i2c
//#include "i2c_sw.h" //use software i2c

void myi2c_write(unsigned char addr, unsigned char data) {
i2c_start(); //send the start condition
i2c_write(addr, data); //write data to addr
i2c_stop();
}
In i2c_hw or i2c_sw, I would implement the actual i2c_start(), i2c_stop(), i2c_write() .. routines - they have the same names!

So if I want to run my code using hardware i2c from a given mcu, I just need to include the i2c_hw.c/.h files for that platform; for software i2c, I just need to include i2c_sw.c/.h files.

At a higher level, I would then write myi2c.c/myi2c.h files so that the same drivers can be ported to different mcus.

millwood0 发表于 2013-5-8 04:56:55

If you follow this approach over a period of time, you will find that you start to accumulate a library of device drivers that are highly portable and fully debugged. I for example have a driver for a lot of different lcds, each with the same calling profile and can be used interchangeably from one lcd to another - to switch to a different lcd, I just need to include the right drivers and my code will compile right away, with me knowing that it will work.

Over time, this becomes a huge competitive advantage.

lookatu 发表于 2013-5-8 08:52:33

办法很简单,写个一万行代码,不行就写个十万行,自然就好了!

huaweishan 发表于 2013-5-8 14:36:30

多写,多学好的代码,看看这些书:C和指针、C陷阱与缺陷、C专家编程。

huaweishan 发表于 2013-5-8 14:38:36

多写,多分析好的代码。多看看这些书:C和指针、C陷阱与缺陷、C专家编程、算法与结构。

lsz0628 发表于 2013-5-8 16:05:38

基础很重要,算法是程序的灵魂,多吸收好的代码,多写。

windboy 发表于 2013-5-8 16:20:31

光看不行,还要写,多写

看优秀的代码,写优秀的程序

这个是练出来的

clarkewayne 发表于 2013-5-8 16:21:44

lans0625 发表于 2013-5-5 12:45 static/image/common/back.gif
代码要简洁,多参考优秀的源码。
代码要高效,只能学习算法。算法才是程序的灵魂。 ...

非常同意!

clarkewayne 发表于 2013-5-8 16:29:13

millwood0 发表于 2013-5-6 18:26 static/image/common/back.gif
1) write more code;
2) write smarter code;



碉堡了!!

snail593 发表于 2013-5-15 12:05:35

学习了 我也要多写 在此mark

snail593 发表于 2013-5-15 12:06:57

加油 好好学习 天天向上

zhcj66 发表于 2013-5-15 15:45:24

windboy 发表于 2013-5-8 16:20 static/image/common/back.gif
光看不行,还要写,多写

看优秀的代码,写优秀的程序


同意这个观点,我一开始的时候写的很差劲,后来看了高人的代码后,慢慢改正自己的不足,多写写,思路清晰多了,错误也慢慢减少了些

lonecat 发表于 2013-5-15 16:22:45

受教了 谢谢

ljbskx 发表于 2013-8-13 20:50:03

十分钟写程序 三十分钟思考

WERWER 发表于 2013-8-14 10:22:14

多写多看,看看那些规范啊 什么的 都是有好处的。

华仔 发表于 2013-8-14 10:30:27

多看成熟的代码,如操作系统。前提是基本功要扎实,语法和数据结构是基础,算法是灵魂。

ronic 发表于 2013-8-14 10:31:56

Mark有空来读

liweigang20 发表于 2013-8-14 22:32:22

其实多交流,进步很快!

Lin_abc 发表于 2013-8-15 00:03:47

millwood0 发表于 2013-5-6 18:26 static/image/common/back.gif
1) write more code;
2) write smarter code;



说的很好,楼主可参照举一反三。敲代码是集思维、基础、实践、经验于一体的, 所以实践、积累以及开拓思维能力均很重要。

Lin_abc 发表于 2013-8-15 00:05:31

补充一点, 正如上面很多同学说的, 操作系统是良好的范例,多看看操作系统,既可以开拓视野,又可以感受操作系统的魅力。

duanguozhao 发表于 2013-8-16 00:46:28

学习了!!

jzffzj 发表于 2013-8-16 08:45:49

多看看别人写的代码 ,尤其找一下游戏的源码看看

qinshiysb 发表于 2013-8-16 09:05:08

推荐两本书给你《C语言设计》《C语言深度解剖》先看完《C语言设计》并在按照上面的题目自己编一下程序,然后再去看《C语言深度解剖》看完之后你会发现收货很大的{:smile:}

假如我是真的 发表于 2013-8-16 10:20:27

一口吃不成胖子,楼主心急了

东海傲虾 发表于 2013-8-16 11:27:31

这个 以我个人观点来看,就是多看些内核的源码,不如ucos,linux,ecos等等,这些操作系统的源码都是
大师来编写的,无论是语言风格还是思维逻辑都是世界顶尖级的,值得我们借鉴,当你把一个操作系统的
内核看个差不多的时候你就会发现,能力不知不觉提高了一大截。

huhulixin 发表于 2013-8-16 12:14:48

多看好代码   很多东西都是可以移植的    很多程序都是固定的   可以用在很多地方你发现好的就吸收了积累多了就行了

rifjft 发表于 2013-8-16 13:50:21

欲练神功,必先自宫{:lol:}

maimaige 发表于 2013-8-16 14:55:12

mark 一下

Rainfieldwood 发表于 2013-8-18 09:41:46

mark!学习了

yh1036164041 发表于 2013-8-19 10:39:08

本人正在摸索,通过大神指点,有所领悟,我来回答望采纳(急需莫元):1,自己对编译语言要理解深刻到位2,多看别人的优秀代码,遇到好的代码一定要收藏起来 3,看一些优秀的模块与教程 4,多加练习,也就是在项目中多运用收集的代码模块与模版      很快你就可以吸收并有余力自己写出优秀的代码了

龙缘天下 发表于 2013-8-20 13:47:05

就是代码的累积,做多了就熟悉了,建议你看看ucos的代码写法,这个是很有代表意义的{:smile:}

yelvAVR 发表于 2013-8-20 14:08:04

一步一步来吧!~否则会从头再来!~~·

爱电子1122 发表于 2013-8-20 17:43:31

建议看看C的经典书籍吧,《C陷阱与缺陷》,还有陈正冲的书籍等等,看看别人代码的思路,主要自己要通过项目去写代码,这样提高比较快,多练习,反复调试,这只是我的建议吧。

wangxuedong 发表于 2013-8-22 10:16:28

给你推荐一本书,效果嘎嘎的《C语言程序设计第二版》谭浩强

xofun 发表于 2013-8-24 14:56:31

自学单片机快俩月了自己总结了些经验:
1.多看其他人写好的程序取其精华;
2.自己写好的程序多修改去掉冗杂部分,做到精简程序;
3.模块化,把常用原件程序都做成子函数,直接调用

shendade 发表于 2013-8-24 16:17:53

完全没有必要焦虑,都是一个过程。
刚接触编程的时候啥都不会的时候是啥样还记得吗
慢慢来,注意有个好编程习惯就是了。逻辑思维慢慢培养

lusolzyy 发表于 2013-8-24 18:36:18

自己一开始都埋着头写,写着写着发现别人写的算法更好更简洁就拿别人来看,分析,自己按照别人的方法结合自己的实际重新改进,再总结,慢慢就好了~

lianyu125 发表于 2013-8-24 18:48:49

书籍推荐Cprimer   plus   然后找些例程编程训练自己的编程思想,遇到不懂的问题翻那本C书籍,我是这样过来的   

hyh19890917 发表于 2013-8-25 12:03:33

师傅领进门休息靠个人

11lhlie 发表于 2013-8-30 00:22:54

看看这个,希望对你有用。。。

成就与价值 发表于 2013-8-30 00:40:20

多思考,多看优秀的源代码,多敲代码,有一定的代码量之后,就有感觉了!

1501697860 发表于 2013-8-30 07:54:28

學習了,多謝 大家

lmt50211 发表于 2013-8-30 08:30:37

继续关注......

zhongsandaoren 发表于 2013-8-30 10:07:35

提高编程能力,无非就是多看编程技巧与方法的书籍,多看高质量的例程代码,编程的时候多思考、有时候不妨变换下实现的思路。至于编程技巧与方法的书籍我推荐如图。

BCE312 发表于 2013-8-30 10:35:57

我这有几本关于C语言的书 分享了

Emmadxw 发表于 2013-8-31 15:42:06

个人觉得想要锻炼自己这方面的能力,想必肯定是有看到过别人写的代码,发现了差距,建议学习别人有的而自己没有的,为啥别人能想到,而自己却没有好的应用,多去写写,与别人做做对比,当然功夫不是一时半会就能掌握的,这也是个积累的过程,到最后你会发现,其实就是经验,还有重要的是编程习惯,非常重要!!!

lpdliumyc 发表于 2013-9-2 14:58:58

看自已的每一句C代码和对应翻译成的汇编代码,以汇编代码最少为宜

qq325600 发表于 2013-9-2 15:55:20

哇 好多东西 留个爪

18814888577 发表于 2013-9-5 09:52:42

这个首先当然是要系统学习C语言基础,同时实践,没有时间积累,就没有经验。

znzn2007 发表于 2013-9-12 15:23:43

大家说得都有道理,总是需要多看多写多实验,没有什么捷径可以走,我个人是这么认为

zxc2769 发表于 2013-9-12 17:19:22

勤能补拙是良训啊

ark_hd_hero 发表于 2013-9-12 17:42:12

多学习别人的程序架构,规划。架构规划的好坏决定你的程序所能达到的高度.
然后才是优秀的编程技巧让你锦上添花而已.

maimaige 发表于 2013-9-12 17:45:25

我先mark一下

xingyuezh 发表于 2013-9-12 18:05:59

学习一下哈

opa 发表于 2013-9-12 18:59:52

多番书吧,再找几个开源慢慢看。

只是开源的大都不是很复杂不利于初学者,
要不就是文挡不齐全不利学习,
感觉就像从hello world一下子到OS开发...。
所以开源也是一样多看。

若说有没有由简入难的开源专案,
也是只能多找多看,
像是hello world到mp3 player之类的,
难度应该比较低,应该会比较有到位的感受...。

ttoto 发表于 2013-9-13 08:47:26

练习,只有练习。如果手头没有项目建议刷ACM题库。刷30题就可以明显提高语言感觉。

petermxw 发表于 2013-9-13 20:27:55

推荐一本书 看完再给分哈 c和指针最好买来看 电脑看不靠谱

黄瓜 发表于 2013-9-14 15:03:04

多写写{:lol:}

闷鱼 发表于 2013-9-14 16:55:36

{:huffy:}怒赞啊 主要是思维方式没变 总是走不出菜鸟的圈子 唉

stdio 发表于 2013-9-14 17:20:14

ttoto 发表于 2013-9-13 08:47 static/image/common/back.gif
练习,只有练习。如果手头没有项目建议刷ACM题库。刷30题就可以明显提高语言感觉。 ...

就这!cool !! 楼主,把莫元给这位老兄吧!
其实,写大半个编译器,就有C语感了;写大半个CPU,就有VERILOG语感了。

hcf83098 发表于 2013-9-14 22:22:16

做个记号,回头再看

szmini2006 发表于 2013-9-14 22:35:02

多看,多练

马宏坤 发表于 2013-9-17 18:07:32

多谢程序,时间长了,你自然就是高手了。同时也要多看一些好的程序的!

Lboyve 发表于 2013-9-18 08:56:43

看了16楼。震精了

m32112 发表于 2013-9-18 09:48:51

参照代码规范,代码规则进行编写!具体可参照周立功网站的一些资料!

xf331785508 发表于 2013-9-18 10:07:37

millwood0 发表于 2013-5-8 04:56 static/image/common/back.gif
If you follow this approach over a period of time, you will find that you start to accumulate a libr ...

好方法,你这样的积累,最后形成了大量硬件接口库了。教会了C语言,教教怎么学英语吧。呵呵{:lol:}

error1314 发表于 2013-9-18 10:16:48

要提高全靠天分

chenshichao541 发表于 2013-9-18 12:52:02

mark.....   

lofky 发表于 2013-10-13 16:29:48

多看视频和看C语言的书籍

二进制 发表于 2013-10-13 16:41:35

持续关注               

Super_C 发表于 2013-10-13 17:47:24

思维方式,还有就是多写,多看。

kilyh 发表于 2013-10-13 23:00:14

提高智商是根本啊,银杏叶提取物据说可以改善脑血液循环你可以试试,我不是做广告啊。呵呵
页: [1] 2 3
查看完整版本: C语言编程能力如何提升?