Unit 1
Unit 1
GEMS ASC:[email protected]
Sample Program o Predefined means that it is a function that
o has already been written and compiled, and
#include<stdio.h> linked together with our program at the time
#include<conio.h> of linking.
void main( ) o The printf function causes the everything
{ between the starting and the ending
printf(“WELCOME”); quotation marks to be printed out.
getch( ); o Every statement in C should end with a
} semicolon (;) mark.
The #include directive
o C programs are divided into modules or
functions. Some functions are written by
users, like us, and many others are stored in
the C library. Basic Structure of C Program
o Library functions are grouped category-wise
and stored in the library, it is necessary to Documentation section
tell the compiler about the files to be Link Section
accessed. Definition Section
o This is achieved using the pre-processor Global Declaration section
directive #include as follows: main( ) function section
#include<filename> {
Filename is the name of the library file that Declaration part
contains the required function definition Executable part
o Pre-processor directives are placed at the
}
beginning of a program.
The main( ) function Subprogram section
o The main( ) is a special function used by the Function 1
C system to tell the computer where the Function 2
program starts.
..
o Every program must have exactly one
main() function. Function n
o If we use more than one main() function, the
compiler cannot understand which one Fig: an overview of a C program
marks the beginning of the program.
o The documentation section consists of a set of
o The empty pair of parenthesis immediately
comment lines giving the name of the
follow the main indicates that the function
program, the author and other details, which
main has no arguments(or parameters).
would like to use to later.
o The opening brace marks the beginning of
o The link section provides instructions to the
the function main and the closing brace
compiler to link functions from the system
indicates the end of the function.
library.
o All the statements between these two braces
o The definition section defines all symbolic
form the function body. The function body
constants.
contains a set of instructions to perform the
o There are some variables that are used in
given task.
more than one function. Such variables are
printf( ) function
called global variables and are declared in the
o printf is a standard C function for printing
global declaration section that is outside of all
output.
the functions.
GEMS ASC:[email protected]
o Every C program must have one main() C TOKENS
function section. o The smallest individual elements, which are
This section contains two parts, declaration identified by the compiler, are known as
part and executable part. tokens.
The declaration part declares all the o Tokens supported in C can be categorized as:
variables used in the executable part. Identifiers
There is at least one statement in the Keywords
executable part. Constants
These two parts must appear between the Special symbols
opening and the closing braces. Operators
The program execution begins at the
opening brace and ends at the closing brace. IDENTIFIERS
The closing brace of the main function o Identifiers are the names that are given to
section is the logical end of the program. various program elements, such as variables,
All statements in the declaration and functions, and arrays.
executable parts ends with a semicolon(;). o The rules for forming an identifier are:
o The subprogram section contains all the user- First character must be an alphabet.
defined functions that are called in the main Must consist of only letters, digits, or
function. underscore.
User-defined functions are generally placed Cannot use a keyword.
immediately after the main function, Must not contain a white space.
although they may appear in any order. KEYWORDS
o Keywords are the reserved words that convey
CHARACTER SET a special meaning to the compiler.
o The characters that can be used to form o All keywords have fixed meanings and these
words, numbers and expressions depend upon meaning cannot be changed.
the computer on which the program is run is o Example:
known as characters set. int double Short
o The characters in C are grouped into long Char Else
following categories: float while If
Letters
Digits
Special characters
White spaces.
o The compiler ignores white space unless they
are a part of a string constant.
White spaces may be used to separate
words, but are prohibited between the
characters of keywords and identifiers.
GEMS ASC:[email protected]
CONSTANTS These numbers are shown in decimal
o Constants in C refer to fixed values that do notation, having a whole number
not change during the execution of a program. followed by a decimal point and the
o C supports several types of constants: fractional part.
It is possible to omit digits before the
Constants
decimal point, or digits after the decimal
point.
Example:
Numeric Character 1.20 .95 215. +.5
Constants Constants Exponent form
General from
mantissa e exponent
Integer Real Single String The mantissa is either a real number
Constants Constants character Constants expressed in decimal notation or an
Constants
integer.
Integer Constants The exponent is an integer number with
o Integer constant refers to a sequence of digits. an optional plus or minus sign.
o These are whole numbers without any The letter e separating the mantissa and
fractional part. the exponent can be written in either
o There are three types of integers: lower case or upper case.
Decimal integer Example: 215.65 : 2.1565e2
It consist of a set of digits, 0 through 9, Single Character Constants
preceded by an optional – or + sign. o A single character constant contains a single
Embedded spaces, commas, and non- character enclosed within a pair of single
digit characters are not permitted quote marks.
between digits. o Example:
Example: „5‟ „X‟ „;‟
Valid: 123 -321 0 +78 o The character constant „5‟ is not the same as
Octal Integer the number 5.
It consists of any combination of digits o Character constants have integer values
from the set 0 through 7, with a leading known as ASCII values.
0. Backslash Character constants
Hexadecimal Integer o These are used in output functions
A sequence of digits preceded by 0x or „\n‟ New line
0X is considered as hexadecimal „\t‟ Horizontal tab
integer. „\‟‟ Single quote
„\”‟ Double quote
They may also include alphabets A
„\\‟ Back slash
through F or a through.
The letter A through F represent the String Constants
numbers 10 through 15. o A string constant is a sequence of characters
Example: 0x2 0x9F enclosed in double quotes.
Real Constants o The characters may be letters, numbers,
o Real constants are numbers having fractional special characters and blank space.
parts. o Example:
o These may be written in one of the two forms: “Hello” “1987” “5+3”
Fractional form
GEMS ASC:[email protected]
o A single character string constant does not DATA TYPES
have an equivalent integer value while a o Data types are means to identify the types of
character constant has an integer value. and associated operations of handling it.
Special Symbols o C supports three classes of data types:
o Special symbols are mainly used to inform Primary data types
the compiler, how statements are grouped Derived data types
together in the code. User-defined data types
o The following ASCII characters are
separators (punctuators): PRIMARY DATA TYPES
() [] {} ; , . Integral Type
GEMS ASC:[email protected]
SYMBOLIC CONSTANT DEFINITION
o A symbolic constant is a name that substitutes
for a sequence of characters. The characters
may represent a numeric constant, a character
constant, or a string.
o Symbolic constants are usually defined at the
beginning of the program.
o General form:
#define identifier text
Where
- identifier represents a symbolic name,
typically written in upper-case letters.
- text represents the sequence of characters
that is associated with the symbolic name.
- the text does not end with semicolon.
o Example:
#define TAXRATE 0.23
#define PI 3.141593
#define FALSE 0
o Advantages:
Symbolic constants are more readily
identified than the information they
represent.
It is easier to change the value of single
symbolic constant than to change every
occurrence of some numerical constant
that may appear in several places within
the program.
GEMS ASC:[email protected]