Book: C by Discovery 4th Edition by Foster and Foster
Chapter 1: Getting Started
- What to Expect from C
- Language Level
Programming languages are low, intermediate or high. C is intermediate to high, closer to what humans think. Assembler is how computer thinks (1s and 0s) and is considered a low level language. - Programming environment
A programing language can either assembly, interpret or compile. C is traditionally compiled and takes several steps. After that is done it is translated to the assembler language for that computer. - Portability and Efficiency of C Code
If a C program is written properly it is portable, meaning it can be run on other computer processors with no modifications. This comes at the expense of non-portable code like assembler being smaller and faster. - Ease of maintenance
Because C is a structure language and can use functions, these functions can be written in several files before the compile. Now if there is a major change that will affect one item of the program, the file that contains all references to it can be changed, and not the whole program. This makes maintaining the program much easier.
- Language Level
- Fundamentals - Reserved Words, Identifiers, the Character Set
- Reserved words
Words that C uses that cannot be used for other purposes are reserved words. - Identifiers or Names in C
Identifiers are words that we choose to represent variables, subprograms and other elements. They are made up of letters, digits and underscores though they cannot start with a number. They are also case sensitive. Depending on the compiler there may be limitations as to how long they are. Lastly they cannot be identical to any keyword in C. - The Character Set
Because computers use different character sets, it is possible that extensive use of one set will make a program that is not portable. - Format of C Programs
C is a free from language that basically ignores white space put into a program
- Reserved words
- Subprograms or Functions in C
In C a subprogram is called a function. It consist of the following:
type function_name(formal parameter declarations)
{
variable declarations;
code;
}
Type is what type of value it will return. The rest is fairly self-explanatory.- Calling a function
If you write a separate routine to be called upon later, this is a function. In order to use it you must type its name in the calling routine and pass whatever parameters it might need.
- Calling a function
- An Introduction to Output in C
In C language, input and output are not part of the language but are external functions that come with the C compiler. To use the input and output in C, the line #include <stdio.h> must be included at the top of the program. For printing, the command printf("text to print\n"); will be used in the program. The \n represents a line feed to be done. There are other escape lines like that. \t is for tab, \b is for backspace, \" for double quotes in a string and \' for single quotes. \\ allows for a backslash to be done and lastly \0 is the null character. Also, any character that is supported in the computers character set can be done with the number of it. In \'0141' would be the letter a for example. - Input and Output with Variables
Variables are used to hold data while a program runs. In C these must be declared as to what type they are. Both of these are legal ways of declaring them in C
int counter;
int counter = 1;
It is also legal to use the first one and then declare its value later on in the program. Several variable can be put on the same line if desired.- Input with scanf()
scanf() is used to input a variable from the input device. It uses two parameters. %d is the input, in this case decimal digits, and the variable name with a & in front of it, which tells the computer what memory location to put it in to correspond with the variable. It can return strange values if the input is not what it is expecting (characters in place of numbers).
scanf("%d", &intcar);
- Input with scanf()
- Arithmetic Operations
Symbol | Action |
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | remainder |
= | Assignment |
1.6.1 Precedence of Arithmetic Operations
Unary, multiply/divide, add, subtract, assign. It is possible to make a compound assignment, meaning while assigning a variable you do one action on it. A += 1 would be the same as A = A + 1.
- Introduction to Functions and Structured Programming in C
- Designing a program
1. Specify what the program is to do
2. Analyze the program
3. Design the software solution
4. Translate design into code
5. Test and debug the code
6. Maintain an modify code as need arises. - Preprocessor constants
In defining variables in a program, constants can be used for values that will not change. These should be placed in such a way that it would take changing a value in one place to resolve any future need of changing the value. - Function Parameters
A function may have values passed to it. By definition, those values will take on the type of variable it is upon return. Any return statement stops execution of the program and returns to the calling routine. - Function Calls
A call of a function is done by its name and in ( ) next to it, any parameters it needs to pass.
- Designing a program
No comments:
Post a Comment