Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
6Activity
0 of .
Results for:
No results containing your search query
P. 1
Elements of a C Program

Elements of a C Program

Ratings: (0)|Views: 275|Likes:
Published by SrihariChidura

More info:

Published by: SrihariChidura on Jun 18, 2009
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

12/10/2012

pdf

text

original

 
 
Elements of a C Program
Comments
Any text placed between the delimeters
/*
and
*/
is a
comment
.
/* a comment *//* And thisisanother comment */
Programmers insert comments to
document 
a program and improve programreadability. Comments are not part of the executable program, they are ignored by theC compiler.
A good programming practice
is to have comments in the program. Youcan choose a particular style to highlight comment, for example
/********************************************* This program generates random numbers in ** the range 0 - 100 and determines and ** prints the maximum and minimum numbers. *********************************************/int main(){...return 0;}
Constants
A constant is a value that does not change during the program execution
.Examples of constants are integer numbers such as 0, 10, 23987. These are
integer constants
. There are also
floating constants
, like 3.14159, 2.0, and
character constants
 like 'a', 'B', etc. Character constants must be included in single quotes and in Clanguage they are closely related to integers. Any sequence of characters writtenbetween
double
quotation marks is a
string constant 
. A string constant differs from acharacter constant. For example, 'A' and "A" are not the same.If you use the same constant in your program more than once, usually you will defineit using the
#define
preprocessor directive.The
#define
directive tells the preprocessor to replace every occurence of a particularcharacter string which is called a
macro name
with a specified value called a
macrobody 
.
#define
macro_name macro_body 
 
The macro body may be a string or a data item.For example, you may define the maximum number of input values as
 
#define MAXNUMBER 10
In another program you may define the course name:
#define COURSE_NAME "Computer Science"
Also you may consider to define the logical operators to make the program morereadable:
#define TRUE 1#define FALSE 0#define NULL 0#define AND &&#define OR ||#define NOT !
Then you can write a logical expression like this:
if(pass==TRUE)printf("You passed this subject");
NOTE:1. # must be the first character on line2. There is no semicolon at the end of a preprocessor directive3. Conventionally,
names of constants
are written in uppercase
Variables
A variable is used to present different values. In the statement such as
i = 1;
the value of a constant
1
is
assigned 
to the variable
. Variable names and functionnames in C are called
identifiers
. To make a valid variable name you can use all thefollowing:
Characters A => Z, and a => z.
Digits 0 => 9, which can be placed in any position except the first.
The underscore character (
 _ 
).Examples of 
valid
identifiers are max_number, var30j, firstName.A variable name
cannot
contain:
arithmetic operators (
+
,
-
,
/
,
*
,
%
)
dots (
.
)
apostrophes (
'
)
other special symbols (
#
,
?
,
^
, etc.)Examples of 
invalid
identifiers are
4th_number, last-name, quest? 
.
 
There are 32 reserved words in C (also called
keywords
),which cannot be used as identifiers.The complete list of C keywords can be found in the reference textbook.Note that all keywords are written in lowercase letters.
Before a variable can be used in a program statement, it must be
declared
, forexample:
int number;char c;float price;
To declare a variable means to specify its type, for example, integer, with a keyword
int
, (types of variables will be discussed later) and name. Variables of the same typecan be declared in one line.
int n1, n2, sum;
To assign a value to the variable, an assignment operator (
=
) is used, for example
i =1;
 
number = 100;
. A variable can be
initialised
(that is, given an initial value) at thesame time as it is declared.
int number = 0;char c = 'a';float price = 2.5;
Expressions
An expression is a combination of constants, variables, operators and functioncalls.
For example:
i + 8
is an expression of a constant plus a variable, and
exit(0)
isan expression of a function call. An expression can contain arithmetic and other typesof operators.
C arithmetic operators:
Symbol Meaning+
Addition
-
Subtraction
*
Multiplication
/
Division
%
Remainder or modulusThe last operator in this list requires some explanation. The
remainder 
operator
%
isused to obtain the remainder of the division of the first operand by the secondoperand. For example,
7 % 3
is
1
,
8 % 4
is
0
, and
5 % 7 
is
 The remainder operator can be applied only to
integer constants or variables
.

Activity (6)

You've already reviewed this. Edit your review.
1 hundred reads
Hum Naz liked this
david jack liked this
mandalashok liked this
srinivasulu143 liked this
srinivasulu143 liked this

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->