Elements of ‘C’ programming
language for SEE and NEB
Every language has some basic elements and rules to
write program. These basic elements are character set, variable, data types,
constants, keywords (reserved words), variable declaration, expressions,
statements, etc.
Character
Set
The character set consists of alphabet digit or
special symbol used to represent information. Character set used in C are as
follows:
Alphabets |
Uppercase (i.e A,B, ……….Y,Z) or Lowercase (i.e. a,b,…………y,z) |
Digits |
0,1,2,………….8,9 |
Special symbol |
+, -, *, /, =, ( ), [ ], < >, ‘, “, !, #, %,
&, _, |, ^, ~, \, ., ;, “, ? |
White space characters |
Blank space, new line, tab etc.. |
Token
In C program the smallest individual units are known
as C tokens. Each and every alphabet and punctuation symbols are called tokens.
Compiler collects the valid character
set to make sensible tokens. so, it be said that tokens are collection of
different characters, symbols operator and punctuators.
C tokens are of six types. They are,
Keywords (eg: int, while),
Identifiers (eg: main, total),
Constants (eg: 10, 20),
Strings (eg: “total”, “hello”),
Special symbols (eg: (), {}),
Operators (eg: +, /,-,*)
Reserved
Words/Keywords
Keywords are command or predefined words that have specific
meanings i.e. all keywords have fixed meanings and these meanings cannot be
changed. C program is case sensitive program. So the keyword should in small
case. They are:
Auto |
break |
case |
char |
const |
continue |
default |
do |
double |
else |
enum |
extern |
Float |
for |
goto |
If |
Int |
long |
register |
return |
Short |
signed |
sizeof |
static |
struct |
switch |
typedef |
union |
unsigned |
void |
volatile |
white |
Identifiers:
Identifiers can be defined as the name of the
variables, functions, array, structures etc.. created by the programmer. Keywords can’t be used as identifiers because
they have some special meanings and functions in the program.
Example:
length, area, a, b, sum, etc.
Rules
for naming identifiers:
- Identifiers are the combination of digits, alphabets and underscore.
- Keywords can’t used as identifier
- It is not case sensitive i.e. lower case (small letter) or upper case (capital letter) can be used as identifier. Both lower case and upper case has distinct meanings such as num and NUM are two different identifiers.
- Any characters, digits (except some special symbols such as #, . etc) can be used, but the first letter should be a character.
- The length of identifiers based on the compiler. In earlier compilers it was 8 characters, ANSI C allows 31 characters but in modern compilers it may be of any length.
- It must be unique in a program.
- Blank spaces are not allowed.
The identifiers are generally given meaningful
names. Some examples of valid identifiers name-
Value a
net_pay rec1 _data MARKS
Some example of invalid identifiers name are-
5bc: First
character of identifiers should be an alphabet or underscore
int : int
is a keyword
rec#: # is a
special character
avg no: blank
space is not allowed.
Comments
Comments are arbitrary text written by a programmer,
which are placed between the character sets /* and */. Comments are helpful for what purpose
programming is? Comment lines are not executable statements and therefore any
text between /* and */ is ignored by the compiler.
In C program, a programmer can introduce comments in
two ways: single line comment and multiple line comments. Single line comment
is indicated by // (double slashes) where as multiple line comments is enclosed
within /* and */
Example
// Single line comment
./* This program calculates sum of two different
numbers*/
Variable:
Variable is an
identifier or name given to any memory location that is stored the data while
the execution of the program. This data
storage place is identified by the variable name.
In C language the
variable must be declared with its data type before using in the program.
Types of variable:
i. Numeric variable:
The variable that stores numeric data only called numeric variable. The numeric
data may be whole number or fractional number.
ii. String variable:
The variable that store characters data only are called string variable. The
string data may be single character or string.
Rules For Declaring
variable:
Variable name |
Example |
1. It cannot start
with number |
2num |
2. It can contain
number else where |
num2 |
3. It cannot contain
any arithmetic operator |
a+b*c |
4. It cannot contain
any punctuation marks |
@#%! |
5. It may contain or
begin with an underscore |
_height |
6. It cannot contain
a C keyword |
While |
7. It cannot contain
a space |
num ber |
8. It can be of mixed
case |
NumBer |
Syntax: data_type variable1,
variable2……..;
Example1:
int a,b,c sum;
Example 2:
int a=20;
char choice;
choice=’A’;
Constant:
Constant are the values
that remain unchanged during program execution. C constant can be classified
into two components.
a) Primary
constant: Eg. Integer, real, character, and string
b) Secondary Constant:
Eg. Array, pointer, union, structure, etc..
Primary constant:
Integer constant: An
integer constant refers to a sequence of a positive or negative nondecimal
number. Example: int a=10, int b=-5 etc.
Real constant: Numeric
constant that contains decimal points are called real constant or floating point
constant. Example: float a=10.7, float b= -5.3 etc.
The character constant: A
constant that contains a single alphabet, a single digit, or a single special
symbol enclosed within a single inverted comma is called a character constant. Example: char 'n'
String Constant: A string constant has zero, one, or more than
one character. A string constant is enclosed within double quotes (" "). At the end of the string, \0 is
automatically placed by the compiler. Example:
char add ="dhangadhi"
No comments:
Post a Comment