Documente Academic
Documente Profesional
Documente Cultură
Topics to Be Covered
Ca high-level programming language developed in
1972 by Dennis Ritchie at AT&T Bell Laboratories.
We will discuss:
the elements of a C program
the types of data that can be processed by C.
C statements for
performing computations,
for entering data, and
for displaying results.
An algorithm
An algorithm that is
implemented in C language
1. C language elements
What is the
program about?
There are two
parts in the code:
Preprocessor
Function main
Preprocessor directives
Are commands that give instructions to C preprocessor , whose job is to
modify the text of a C program before it is compiled.
Begins with a number symbol (#)
Two most common directives :
#include
#define
#include
#include directive gives a program access to a library.
This directive causes the preprocessor to insert definitions
from a standard header file into a program before compilation.
The directive
#define
This directive instructs the preprocessor to replace each
occurrence of KMS_PER_MILE by 1.609 before compilation
begins.
Associates the constant macro KMS_PER_MILE with the meaning
1.609.
constant macro a name that is replaced by a particular constant value
before the program is sent to the compiler.
Comments
The text on the right of each preprocessor directive, starting
with /* and ending with */ , is a comment .
Comments provide supplementary information making it
easier for us to understand the program, but comments are
ignored by the C preprocessor and compiler.
Syntax: #include
Syntax: #define
Function main
Every C program has a main function.
marks the
beginning of the
main function
where program
execution begins.
The remaining
lines of the
program
Function main
Reserved Words
Some example of reserved words in C:
Notice that main is not a reserved word. However, this is a fairly technical
distinction, and for practical purposes you are advised to treat , main as if it were
reserved as well.
Identifiers
Other words in Fig. 2.1 are identifiers that come in two varieties:
standard
user-defined.
User-defined identifiers
We choose our own identifiers to name memory cells that will hold data
and program results and to name operations.
Example:
KMS_PER_MILE
Avoid two names that are different only in their use of uppercase and
lowercase letters.
Example: LARGE and large
Not to use two names that differ only in the presence or absence of
an underscore
Example: xcoord and x_coord.
Data type
A data type of an item specifies operations on that item.
Example: C uses standard data types
int as integers.
'z' '2'
'9'
3. Executable statements
Assignment statement
Examples:
sum = sum + item;
new_x = x;
copies the value of variable x into variable new_x
new_x = -x;
get the value of x , negate that value, and store the result in new_x.
next_letter = 'A';
3. Executable statements
A function call consists of two parts: the function name and the
function arguments , enclosed in parentheses.
The arguments for printf consist of a format string (in quotes) and a print
list (the variable kms ).
Displaying Prompts
prompt (prompting message) a message displayed to
indicate what data to enter and in what form.
Note, variable
declarations are
inside the function
main
intmain()
{
doubleradius,area;
printf("Radius:");
scanf("%lf",&radius);
area=PI*radius*radius;
printf(Areaofthecircleis%f\n",area);
return(0);
}
Program Style
Spaces in Programs
Comments in Programs, describe
purpose of the program,
use of identifiers,
purpose of each program step.
Self check