C language / PDF
C language / PDF
In this chapter we will learn how to
• write simple computer programs using the C programming language;
• perform basic mathematical calculations;
• manage data stored in the computer memory and disk;
• generate meaningful output on the screen or into a computer file.
Sample of the pdf document :
The C programming language was developed in the early 1970’s by Ken Thomp- son and Dennis Ritchie at the Bell Telephone Laboratories. It was designed and implemented in parallel with the operating system Unix and was aimed mostly as a system implementation language[1].
It, nevertheless, evolved into one of the most flexible and widely used computer programming languages today.
In 1989, the American National Standards Institute (ANSI) adopted a document that standardized the C language. The particular version of the language that it describes is widely referred to as ANSI C or C89. This document was adopted in 1990 by the International Organization for Standardization[1] (ISO) as C90 and was later expanded to the current standard, which is often referred to as C99.
The C language consists of a small set of core commands and a large number of library functions that can be incorporated in a program, if necessary. There exist many excellent books that discuss the C language in detail, starting from the first book The C Programming Language by B.W. Kernighan and D.M. Ritchie[2].
This book describes ANSI C and remains today one of the easiest texts on the subject. In this chapter, we will cover the most basic of the core commands of the language, as well as those library functions that are useful in developing computational physics programs.
1.1 The first program:
It is a tradition in the culture of C programming that the first programcompiled and executed by a new student of the language is one that prints on the screen the happy message “Hello World!”[2]. This program, which is shown bellow, demonstrates the
1.1. THE FIRST PROGRAM 1-3
in C. For now, it suffices to say that the main program is the set of commands
that appear between two braces (symbols ‘{’ and ’}’) immediately following this
identification.
The first command of the main program,
printf(‘‘Hello World!\n’’);
prints on the screen the message
Hello World!
The command name is printf and stands for print formatted. The command
printf takes a number of arguments, which are enclosed in parentheses. In
this particular example, the only argument is the string of characters “Hello
World \n”, which is printed on the screen. All character strings in C are enclosed
in quotes, which are not part of the string themselves. They denote its beginning
and end and are not printed on the screen. The last part of the character string
is the control character \n, which stands for newline. It also does not appear
on the screen, but is there to instruct the program to begin the next output in the
following line on the screen.
The final command on the program,
return 0;
identifies the point where the program reaches its normal end and control is returned
to the operating system. The number 0 signifies the fact that the program is
finishing normally, without any error messages.
Most lines in this example end with text in plain English that is preceded by the
symbols //. These are comments to help the programmer understand the structure
of the program and are not commands of the language. In fact, the compiler ignores
anything from the symbols // to the end of the line. This construction, which is
actually borrowed from C++, is one of the ways that allows a C programmer to
insert explanatory comments in the program. In a different construction, comments
are inserted between the symbols \* and *\, as in the following example:
\* This is a comment ...
1.2 Managing Simple Data with C
One of the most important operations performed by a computer is the storage and manipulation of data. For archival purposes, data are stored in external devices, such as magnetic disks and laser disks, which retain the information even after the power of the computer has been turned off. However, in order for the computer to manipulate the data, they need to be stored in its random-access memory (RAM),
which the microprocessor has a direct access of.
Data Types Within the C language, different types of data are stored and manipulated in different ways, so that the minimum amount of memory is utilized with the maxi- mum efficiency. Of all the data types recognized by the compiler, we will consider here only the four that are the most useful for mathematical computations. They are.......
Click here for Download PDF / FREE
C language / PDF
0 commentaires: