Overview of C

on Rabu, 20 Maret 2013


Problem Solving and Program Design in C

Overview of C

2.1 C Language Elements
              One advantage of C is that it lets you write programs that resemble everyday
English. Even though you do not yet know how to write your own programs, you
can probably read and understand the program.
Function main
         The two-line heading = int and main(void)
marks the beginning of the main function where program execution begins. Every
C program has a main function. The remaining lines of the program form the body
of the function which is enclosed in braces {, } .
SYNTAX: int
main(void)
{
function body
}


2.2 Variable Declarations and Data Types
Variable Declarations
            The memory cells used for storing a program’s input data and its computational
results are called variables because the values stored in variables can change
(and usually do) as the program executes. The variable declarations in a C
program communicate to the C compiler the names of all variables used in a program.
Syntax Display for Declarations
SYNTAX: int variable_list ;
double variable_list ;
char variable_list ;
EXAMPLES: int count,
large;
double x, y, z;
char first_initial;
char ans;
Data Types
            A data type is a set of values and a set of operations on those values. Knowledge
of the data type of an item (a variable or value) enables the C compiler to correctly
specify operations on that item
            Data Type int In mathematics, integers are whole numbers. The int data type
is used to represent integers in C.
            Data Type double A real number has an integral part and a fractional part that
are separated by a decimal point. In C, the data type double is used to represent
real numbers.



2.3 Executable Statements
 The executable statements follow the declarations in a function. They are the C
statements used to write or code the algorithm and its refinements. The C compiler
translates the executable statements into machine language; the computer executes
the machine language version of these statements when we run the program.
Assignment Statements
An assignment statement stores a value or a computational result in a variable,
and is used to perform most arithmetic operations in a program.

2.4 General Form of a C Program
Now that we have discussed the individual statements that can appear in C programs,
we review the rules for combining them into programs. We also discuss the
use of punctuation, spacing, and comments in a program.
Program Style Spaces in Programs
The consistent and careful use of blank spaces can improve the style of a program.
A blank space is required between consecutive words in a program line.
The compiler ignores extra blanks between words and symbols, but you may
insert space to improve the readability and style of a program
Program Style Using Comments
Each program should begin with a header section that consists of a series of comments
specifying
the programmer’s name
the date of the current version
a brief description of what the program does

2.5 Arithmetic Expressions
To solve most programming problems, you will need to write arithmetic expressions
that manipulate type int and double data. This section describes the operators
used in arithmetic expressions, and rules for writing and evaluating the expressions.
Operators / and %
When applied to two positive integers, the division operator ( / ) computes the integral
part of the result of dividing its first operand by its second.
Characters as Integers
Since characters are represented by integer codes, C permits conversion of type
char to type int and vice versa. For example, you could use the following to find
out the code your implementation uses for a question mark:
qmark_code = (int)'?';
printf("Code for ? = %d\n", qmark_code);

2.6 Formatting Numbers in Program Output
C displays all numbers in its default notation unless you instruct it to do otherwise.
This section explains how to specify the format or appearance of your output.
Formatting Values of Type int  Specifying the format of an integer value displayed by a C program is fairly easy.
Formatting Values of Type double To describe the format specification for a type double value, we must indicate both
the total field width needed and the number of decimal places desired.
2.7 Interactive Mode, Batch Mode, and Data Files
        There are two basic modes of computer operation: batch mode and interactive
mode. The programs that we have written so far run in interactive mode. In interactive
mode , the program user interacts with the program and types in data while
it is running.
Input Redirection
The miles-to-kilometers conversion program rewritten as a batch
program. We assume here that the standard input device is associated with a batch
data file instead of with the keyboard. In most systems, this association can be
accomplished relatively easily through input/output redirection using operating system
commands.
Output Redirection
You can also redirect program output to a disk file instead of to the screen. Then
you can send the output file to the printer (using an operating system command) to
obtain a hard copy of the program output.


2.8 Common Programming Errors
As you begin to program, soon you will discover that a program rarely runs correctly
the first time it executes. Murphy’s Law, “If something can go wrong, it will,”
seems to have been written with the computer program in mind. In fact, errors
are so common that they have their own special name— bugs —and the process of
correcting them is called debugging a program.
Syntax Errors
A syntax error occurs when your code violates one or more grammar rules of
C and is detected by the compiler as it attempts to translate your program. If a
statement has a syntax error, it cannot be translated and your program will not be
executed.
Run-Time Errors
Run-time errors are detected and displayed by the computer during the execution
of a program. A run-time error occurs when the program directs the computer to
perform an illegal operation, such as dividing a number by zero. When a run-time
error occurs, the computer will stop executing your program and will display a diagnostic
message that indicates the line where the error was detected.
Undetected Errors
Many execution errors may not prevent a C program from running to completion,
but they may simply lead to incorrect results. Therefore, it is essential that you
predict the results your program should produce and verify that the actual output
is correct.
Logic Errors
Logic errors occur when a program follows a faulty algorithm. Because logic
errors usually do not cause run-time errors and do not display error messages,
they are very difficult to detect. The only sign of a logic error may be incorrect
program output. You can detect logic errors by testing the program thoroughly,
comparing its output to calculated results. You can prevent logic errors by carefully
desk checking the algorithm and the program before you type it in.

Bagi yang ingin lebih mengetahui tentang :
1.Mengenal komputer dan Prmrograman (Klik)
2. Top – Down Design dalam Bahasa C (Klik)

0 komentar:

Posting Komentar