Overview of C
2.1 C Language Elements
Preprocessor Directives
preprocessor directive a C program line beginning
with # that provides an instruction to the preprocessor.
preprocessor a system program that modifies a C program prior to its
compilation library a collection of useful functions and symbols that
may be accessed by a program
/*
* Converts distances from miles
to kilometers.
*/
#include <stdio.h>
/* printf, scanf
definitions */
#define KMS_PER_MILE 1.609
/* conversion constant */
int
main(void)
{
double miles,
/* distance in miles kms;
/* equivalent distance in
kilometers */
/* Get the distance in miles. */
printf("Enter the distance
in miles> ");
scanf("%lf",
&miles);
/* Convert the distance to
kilometers. */
kms = KMS_PER_MILE * miles;
/* Display the distance in
kilometers. */
printf("That equals %f
kilometers.\n", kms);
return (0);
constant macro a name that is replaced by a
particular constant value before the program is sent to the compiler comment
text beginning with /* and ending with */ that provides supplementary
information but is ignored by the preprocessor and compiler.
Syntax Displays for
Preprocessor Directives
#include Directive for Defining
Identifiers from
Standard Libraries
SYNTAX: #include <standard
header file>
EXAPLES: #include <stdio.h>
#include <math.h>
INTERPRETATION: #include directives
tell the preprocessor where to find the meanings of standard identifiers used
in the program. These meanings are collected in files called standard header
files. ThMe header file stdio.h contains information about standard input
and output functions such as scanf and printf. Descriptions of common
mathematical functions are found in the header file math.h . We will
investigate header files associated with other standard libraries in later
chapters.
#define Directive for Creating
Constant Macros
SYNTAX: #define NAME value
EXAMPLES: #define MILES_PER_KM
0.62137
#define PI 3.141593
#define MAX_LENGTH 100
INTERPRETATION: The C
preprocessor is notified that it is to replace each use of the identifier
NAME by value . C program
statements cannot change the value associated with NAME .
Function main
declarations the part of a
program that tells the compiler the names of memory cells in a program executable
statements program executable statements program lines that are
converted to machine language instructions and executed by
the computer
EXAMPLE: int
main(void)
{
printf("Hello
world\n");
return (0);
}
Reserved Words
reserved word a word that has special meaning
in C
Standard Identifiers
standard identifier a word having special meaning
but one that a programmer may redefine (but redefinition
is not recommended!)
2.2 Variable Declarations and
Data Types
Variable Declarations
variable a name associated with a memory
cell whose value can change variable declarations statements that
communicate to the compiler the names of variables in the program and the kind
of information stored in each variable.
Data Types
data type a set of values and operations
that can be performed on those values.
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 (for example, 3.14159
, 0.0005 , 150.0 ). You can store a real number
in a type double variable,
perform the common arithmetic operations (add,
subtract, multiply, and
divide), and compare them.
The ASCII Code
ASCII code a particular code that
specifies the integer representing each char value.
Assignment Statements
assignment statement an instruction that stores
a value or a computational result in a variable
Input/Output Operations and
Functions
input operation an instruction that copies data
from an input device into memory output operation an instruction that
displays information stored in memory input/output function a C
function that performs an input or output operation. function call calling
or activating a function. function argument enclosed in parentheses following
the function name; provides information needed by the
function format string in a call to printf, a string of
characters enclosed in quotes ("), which specifies the form
of the output line print list in a call to printf, the
variables or expressions whose values are displayed
placeholder a symbol beginning with % in a format string that
indicates where to display the output value newline escape
sequence the character sequence \n, which is used in a
format string to terminate an output line.
Syntax Display for printf
Function Call
SYNTAX: printf( format
string, print list );
printf( format string );
EXAMPLES: printf("I am %d
years old, and my gpa is %f\n",
age, gpa);
printf("Enter the object
mass in grams> ");
1. Every C program has
preprocessor directives and a main function. The main
function contains variable declarations
and executable statements.
2. Variable names must begin
with a letter or an underscore (the latter not recommended)
and consist of letters, digits,
and underscore symbols. A reserved
word cannot be used as an
identifier.
3. C’s data types enable the
compiler to determine how to store a particular
value in memory and what
operations can be performed on that value.
Three standard data types are int
, d ouble , and char . The data type of each
variable must be declared.
4. The executable statements are
derived from the algorithm and are translated
into machine language.
Assignment statements are used to perform
computations and store results
in memory. Function calls are used to get
data (function scanf ) and to
display values stored in memory (function
printf ).
Tidak ada komentar:
Posting Komentar