没有合适的资源?快使用搜索试试~ 我知道了~
首页从零开始学习C语言编程:掌握世界主流语言
从零开始学习C语言编程:掌握世界主流语言
需积分: 7 2 下载量 34 浏览量
更新于2024-07-18
收藏 3.04MB PDF 举报
"《从零开始学习C编程》是一本专为初学者设计的计算机编程教程,作者是诺埃尔·卡尔查兰教授,他在大学里活跃地教授各种编程语言。本书的核心目标是通过世界上最常用的语言之一——C语言,传授基础编程原理。C语言因其高效性和广泛应用性,在软件开发领域占据着重要地位。 在《Learn to Program with C》中,作者以深入浅出的方式引导读者步入编程世界,确保即使没有编程经验的人也能逐步掌握编程概念。书中涵盖了C语言的基础语法、数据类型、控制结构(如循环和条件语句)、函数、数组、指针以及文件操作等内容,这些都是构建任何程序的基本构件。 诺埃尔·卡尔查兰作为专家的声音,他的教学风格注重实践与理论相结合,使读者不仅了解理论知识,还能通过编写实际代码来巩固理解。全书包括了大量的实例和习题,帮助读者通过解决实际问题来提升编程技能。 版权方面,所有内容受法律保护,除非在特定情况下,如用于学术评论或教学目的的简短摘录,或者经出版商所在国家版权法允许的复制,否则未经许可不得复制或重印。此外,电子适应、计算机软件或类似技术的使用也需遵守相应版权规定。 对于想要学习C语言并踏入编程领域的读者来说,《Learn to Program with C》是一本不可或缺的入门指南,它将带领你在探索编程世界的过程中建立坚实的基础,为今后在IT行业中取得成功打下坚实的基础。无论是为了个人兴趣还是职业发展,这本书都是一个理想的起点。"
资源详情
资源推荐
xxi
Preface
This book attempts to teach computer programming to the complete beginner using the C
language. As such, it assumes you have no knowledge whatsoever about programming. And if
you are worried that you are not good at high-school mathematics, don’t be. It is a myth that you
must be good at mathematics to learn programming. In this book, knowledge of primary school
mathematics is all that is required—basic addition, subtraction, multiplication, division, finding
the percentage of some quantity, finding an average or the larger of two quantities.
Some of our most outstanding students over the last thirty years have been people with
little mathematics background from all walks of life—politicians, civil servants, sports people,
housewives, secretaries, clerical assistants, artists, musicians and teachers. On the other hand,
we’ve had mathematical folks who didn’t do as well as might be expected.
What will be an asset is the ability to think logically or to follow a logical argument. If you are
good at presenting convincing arguments, you will probably be a good programmer. Even if you
aren’t, programming is the perfect vehicle for teaching logical thinking skills. You should learn
programming for these skills even if you never intend to become a serious programmer.
The main goal of this book is to teach fundamental programming principles using C, one
of the most widely used programming languages in the world today. C is considered a ‘modern’
language even though its roots date back to the 1970s. Originally, C was designed for writing
‘systems’ programs—things like operating systems, editors, compilers, assemblers and input/
output utility programs. But, today, C is used for writing all kinds of applications programs as
well—word processing programs, spreadsheet programs, database management programs,
accounting programs, games, educational software—the list is endless.
However, this book is more about teaching programming basics than it is about teaching C.
We discuss only those features and statements in C that are necessary to achieve our goal.
Once you learn the principles well, they can be applied to any language.
Chapter 1 gives an overview of the programming process. Chapter 2 describes the basic
building blocks needed to write programs. Chapter 3 explains how to write programs with the
simplest kind of logic—sequence logic. Chapter 4 shows how to write programs which can make
decisions. Chapter 5 explains the notion of ‘looping’ and how to use this powerful programming
idea to solve more interesting problems. Chapter 6 deals with the oft-neglected, but important,
topic of working with characters. Chapter 7 introduces functions—the key concept needed for
writing large programs. Chapter 8 tackles the nemesis of many would-be programmers—array
processing. Chapter 9 explains how lists of items stored in arrays can be searched, sorted and
merged. And Chapter 10 deals with structures—the collection of one or more items, possibly of
different types, grouped together under a single name for convenient handling.
The first step in becoming a good programmer is learning the syntax rules of the
programming language. This is the easy part and many people mistakenly believe that this
makes them a programmer. They get carried away by the cosmetics—they learn the features of a
language without learning how to use them to solve problems.
xxii
■ PrefaCe
Of course, you must learn some features. But it is far better to learn a few features and be
able to use them to solve many problems rather than learn many features but can’t use them to
solve anything. For this reason, this book introduces a feature (like an if statement, say) and then
discusses many examples to illustrate how the feature can be used to solve different problems.
This book is intended for anyone who is learning programming for the first time, regardless of
age or institution. The material has been taught successfully to students preparing for high-school
examinations in Computer Studies or Information Technology, students at college, university and
other tertiary-level institutions.
The presentation is based on the experience that many people have difficulty in learning
programming. To try and overcome this, we use an approach which provides clear examples,
detailed explanations of very basic concepts and numerous interesting problems (not just
artificial exercises whose only use is to illustrate some language feature).
While computer programming is essentially a mental activity and you can learn a fair amount
of programming from just reading the book, it is important that you “get your hands dirty” by
writing and running programs. One of life’s thrills is to write your first program and get it to run
successfully on a computer. Don’t miss out on it.
But do not stop there. The only way to learn programming well is to write programs to solve
new problems. The end-of-chapter exercises are a very rich source of problems, a result of the
author’s forty-odd years in the teaching of programming.
Thank you for taking the time to read this book. I hope your venture into programming is a
successful and enjoyable one.
—Noel Kalicharan
1
Chapter 1
Elementary Programming
Concepts
In this chapter, we will explain the following:
• How a computer solves a problem
• The various stages in the development of a computer program: from
problem definition to finished program
• How a computer executes a program
• What is a “data type” and its fundamental role in writing a program
• The role of characters—the basic building blocks of all programs
• The concepts of constants and variables
• The distinction between syntax and logic errors
• How to produce basic output in C using the printf statement
• What is an escape sequence
• How descriptive or explanatory comments can be included in your program
• What is an assignment statement and how to write one in C
1.1 Programs, Languages, and Compilers
We are all familiar with the computer’s ability to perform a wide variety of tasks. For instance, we
can use it to play games, write a letter or a book, perform accounting functions for a company,
learn a foreign language, listen to music on a CD, send a fax, or search for information on the
Internet. How is this possible, all on the same machine? The answer lies with programming—
the creation of a sequence of instructions that the computer can perform (we say “execute”) to
accomplish each task. This sequence of instructions is called a program. Each task requires a
different program:
• To play a game, we need a game-playing program.
• To write a letter or a book, we need a word processing program.
Chapter 1 ■ elementary programming ConCepts
2
• To do accounts, we need an accounting program.
• To learn Spanish, we need a program that teaches Spanish.
• To listen to a CD, we need a music-playing program.
• To send a fax, we need a fax-sending program.
• To use the Internet, we need a program called a “Web browser.”
For every task we want to perform, we need an appropriate program. And in order for the
computer to run a program, the program must be stored (we sometimes say loaded) in the
computer’s memory.
But what is the nature of a program? First, we need to know that computers are built to
execute instructions written in what is called machine language. In machine language, everything
is expressed in terms of the binary number system—1s and 0s. Each computer has its own
machine language and the computer can execute instructions written in that language only.
The instructions themselves are very simple: for example, add or subtract two numbers,
compare one number with another, or copy a number from one place to another. How, then, can
the computer perform such a wide variety of tasks, solving such a wide variety of problems, with
such simple instructions?
The answer is that no matter how complex an activity may seem, it can usually be broken
down into a series of simple steps. It is the ability to analyze a complex problem and express
its solution in terms of simple computer instructions that is one of the hallmarks of a good
programmer.
Machine language is considered a low-level programming language. In the early days of
computing (1940s and ‘50s) programmers had to write programs in machine language, that is,
express all their instructions using 1s and 0s.
To make life a little easier for them, assembly language was developed. This was closely
related to machine language but it allowed the programmer to use mnemonic instruction codes
(such as ADD and names for storage locations (such as sum) rather than strings of binary digits
(bits). For instance, a programmer could refer to a number by sum rather than have to remember
that the number was stored in memory location 1000011101101011.
A program called an assembler is used to convert an assembly language program into
machine language. Still, programming this way had several drawbacks:
• It was very tedious and error prone.
• It forced the programmer to think in terms of the machine rather than in
terms of his problem.
• A program written in the machine language of one computer could not
be run on a computer with a different machine language. Changing your
computer could mean having to rewrite all your programs.
To overcome these problems, high-level or problem-oriented languages were developed in
the late 1950s and ‘60s. The most popular of these were FORTRAN (FORmula TRANslation) and
COBOL (COmmon Business-Oriented Language). FORTRAN was designed for solving scientific
and engineering problems that involved a great deal of numerical computation. COBOL was
designed to solve the data-processing problems of the business community.
Chapter 1 ■ elementary programming ConCepts
3
The idea was to allow the programmer to think about a problem in terms familiar to him
and relevant to the problem rather than have to worry about the machine. So, for instance, if he
wanted to know the larger of two quantities, A and B, he could write
IF A IS GREATER THAN B THEN BIGGER = A ELSE BIGGER = B
rather than have to fiddle with several machine or assembly language instructions to get the same
result. Thus high-level languages enabled the programmer to concentrate on solving the problem
at hand, without the added burden of worrying about the idiosyncrasies of a particular machine.
However, the computer still could only execute instructions written in machine language.
A program called a compiler is used to translate a program written in a high-level language to
machine language.
Thus we speak of a FORTRAN compiler or a COBOL compiler for translating FORTRAN and
COBOL programs, respectively. But that’s not the whole story. Since each computer has its own
machine language, we must have, say, a FORTRAN compiler for a Lenovo ThinkPad computer
and a FORTRAN compiler for a MacBook computer.
1.2 How a Computer Solves a Problem
Solving a problem on a computer involves the following activities:
1. Define the problem.
2. Analyze the problem.
3. Develop an algorithm (a method) for solving the problem.
4. Write the computer program that implements the algorithm.
5. Test and debug (find the errors in) the program.
6. Document the program. (Explain how the program works and
how to use it.)
7. Maintain the program.
There is normally some overlap of these activities. For example, with a large program, a
portion may be written and tested before another portion is written. Also, documentation should
be done at the same time as all the other activities; each activity produces its own items of
documentation that will be part of the final program documentation.
1.2.1 Define the Problem
Suppose we want to help a child work out the areas of squares. This defines a problem to be
solved. However, a brief analysis reveals that the definition is not complete or specific enough
to proceed with developing a program. Talking with the child might reveal that she needs a
program that requests her to enter the length of a side of the square; the program then prints the
area of the square.
www.allitebooks.com
剩余322页未读,继续阅读
yadunhaha
- 粉丝: 5
- 资源: 25
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 新型矿用本安直流稳压电源设计:双重保护电路
- 煤矿掘进工作面安全因素研究:结构方程模型
- 利用同位素位移探测原子内部新型力
- 钻锚机钻臂动力学仿真分析与优化
- 钻孔成像技术在巷道松动圈检测与支护设计中的应用
- 极化与非极化ep碰撞中J/ψ的Sivers与cos2φ效应:理论分析与COMPASS验证
- 新疆矿区1200m深孔钻探关键技术与实践
- 建筑行业事故预防:综合动态事故致因理论的应用
- 北斗卫星监测系统在电网塔形实时监控中的应用
- 煤层气羽状水平井数值模拟:交替隐式算法的应用
- 开放字符串T对偶与双空间坐标变换
- 煤矿瓦斯抽采半径测定新方法——瓦斯储量法
- 大倾角大采高工作面设备稳定与安全控制关键技术
- 超标违规背景下的热波动影响分析
- 中国煤矿选煤设计进展与挑战:历史、现状与未来发展
- 反演技术与RBF神经网络在移动机器人控制中的应用
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功