PPS Notes Unit 1
PPS Notes Unit 1
SESSION 2024-25
UNIT-1 NOTES
Subject Teacher: Shashi Bhushan Singh Yadav
What is C?
C is a programming language developed at AT & T’s Bell Laboratories of USA in 1972. It was designed and
written by Dennis Ritchie. In the late seventies C began to replace the more familiar languages of that time like
PL/I, ALGOL, etc.
Features of C:
C is a high level language having lesser number of keywords than Java or Pascal.
C is a native language of UNIX, Linux & many of the Windows packages, database programs, graphics
libraries are written using C programs.
C is portable since it provides standard set of libraries that work on the same way with all machines.
C is modular, as it supports functions to divide the program into sub-programs.
Constants /
Alphabets / Digits /
Variables / Instructions Program
Special symbols
Keywords
Character Sets in C:
A character denotes any alphabet, digit or special symbol used to represent information. Following
figure shows the valid alphabets, numbers and special symbols allowed in C.
Digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
The alphabets, numbers and special symbols when properly combined form constants, variables and
keywords.
6
Structure of a C program:
Comments: The comments are usually ignored by the compiler so this part is used only to inform about
the necessary descriptions of the program.
1. Preprocessor Directives
2. Function Prototypes
It is used to inform the compiler that the following functions are defined in this program.
It specifies the name of the function, arguments or parameters and return type and it ends with a
semicolon.
Example: int sum(int, int);
3. Global Variables
4. Main Function
5. User-defined Functions
User defined functions are defined after the main() function as per the function prototype declared in the
program.
These functions may also be defined above (before) the main function. In that case, there is no need for
function prototype. For example:
Basic Data Types in C: There are five atomic data types in C: character, integer,
floating-point, double floating-point and valueless (char, int, float, double, and void
respectively).
All other data types in C are based upon one of these types. The size and range of these
data types may vary between processor types and compilers. However, in all cases a character is
1 byte. The size of an integer is usually the same as the word length of the execution
environment of the program. For most 16-bit environments, such as DOS or Windows 3.1, an
integer is 16 bits. For most 32-bit environments, such as Windows NT, an integer is 32 bits.
Following table shows all data types defined by the ANSI/ISO C Standard:
NOTE: The sizes & ranges of int, short & long are compiler dependent. Sizes in this table
are for 16-bit compiler.
Keywords in C:
Keywords are the words whose meaning has already been explained to the C compiler. The
keywords cannot be used as variable names because if we do so we are trying to assign a
new meaning to the keyword, which is not allowed by the computer.
The keywords are also called ‘Reserved words’. There are only 32 keywords available in C.
following table lists all the keywords used in C:
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
Identifiers: Identifiers are the variable names declared inside the program.
Definition: A variable is an entity that may change whereas a constant is an entity that doesn’t
change.
Variables in C:
An entity that may vary during program execution is called a variable. Variable names
are names given to locations in memory. These locations can contain integer, real or character
constants. In any language, the types of variables that it can support depend on the types of
constants that it can handle. This is because a particular type of variable can hold only the same
type of constant.
For example, an integer variable can hold only an integer constant, a real variable can
hold only a real constant and a character variable can hold only a character constant.
NOTE:
1. The rules for constructing different types of constants are different. However, for
constructing variable names of all types the same set of rules apply.
2. These rules remain same for all the types of primary and secondary variables. The
‘type’ preceding the ‘variable names’ distinguishes them from each other.
Truly speaking, the range of an Integer constant depends upon the compiler. For a 16-bit compiler
like Turbo C or Turbo C++, this range is –32768 to +32767.
Examples of valid real constants (in fractional form): +325.34, 426.0, -32.76, -48.5792 etc.
The exponential form of representation of real constants is usually used if the value of the
constant is either too small or too large. In exponential form of representation, the real constant is
represented in two parts. The part appearing before ‘e’ is called mantissa, whereas the part following
‘e’ is called exponent.
Following rules must be observed while constructing real constants expressed in exponential
form:
The mantissa part and the exponential part should be separated by a letter e.
The mantissa part may have a positive or negative sign.
Default sign of mantissa part is positive.
The exponent must have at least one digit, which must be a positive or negative integer.
Default sign is positive.
Range of real constants expressed in exponential form is -3.4e38 to 3.4e38.
Examples of valid real constants (in exponential form): +3.2e-5, 4.1e8, -0.2e+3, -3.2e-5 etc.
Examples of valid character constants: 'A', 'I', '5' '=', '+' etc.
Special Symbols: Special symbols are the characters used other than alphabets &
digits such as:
− Subtraction == Equal to
+ Addition != Not equal to
* Multiplication > Greater than
/ Division < Less than
% Modulus >= Greater than or equal to
<= Less than or equal to
Logical Operator Action Bitwise Operator Action
- Unary minus
++ Increment
–– Decrement
= “Assignment” of value on the right side of it to the variable on the left side.
Before we begin with our first C program, do remember the following rules that are applicable to
all C programs:
Let us now write down our first C program. It would simply calculate simple interest for
a set of values representing principle, number of years and rate of interest.
/* formula */ si = p * n * r / 100 ;
si = p * n * r / 100 ; /* formula */
si = p * n * r / /* formula */ 100 ;
4. Sometimes it is worthwhile mentioning the purpose of the statement (or a set of
statements) using a comment. e.g.
main( )
{
statement 1 ;
statement 2 ;
statement 3 ;
}
6. Any variable used in the program must be declared before using it. e.g.,
int p, n ;
float r, si ;
float r, si;
r = 8.5;
8. In the statement,
si = p * n * r / 100 ;
* and / are the arithmetic operators. (The arithmetic operators available in C are +, -, *, /
and %).
9. Once the value of si is calculated it needs to be displayed on the screen. Unlike other
languages, C does not contain any instruction to display output on the screen. All output
to screen is achieved using readymade library functions such as printf( ).
Where format strings can contain format specifiers and/or other strings.
Format Specifiers:
Format specifiers are used to input & output data types.
The scanf() & printf() functions accepts a wide variety of format specifiers, some of
them are shown in the following table:
Solution-
/* Program to reverse the 5-digit number*/
#include<stdio.h>
#include<conio.h>
void main( )
{
int num,a;
long int rev=0; /*Initialised to zero otherwise it will contain garbage value*/
clrscr( );
printf(“Enter a five digit Number: ”);
scanf(“%d”,&num);