Computer Programming
(FCCS002 )
Semester 1, Section 2
UNIT-I Basics of C
Introduction
• C is a procedural programming language.
• It was initially developed by Dennis Ritchie as a system programming
language to write operating system.
• The main features of C language include low-level access to memory,
simple set of keywords, and clean style.
• These features make C language suitable for system programming like
operating system or compiler development.
• C was invented to write an operating system called UNIX.
• C is a successor of B language which was introduced around the early
1970s.
• The language was formalized in 1988 by the American National
Standard Institute (ANSI).
• The UNIX OS was totally written in C.
• Today C is the most widely used and popular System Programming
Language.
• Most of the state-of-the-art software have been implemented using C.
• Difference between Object Oriented and Procedure Oriented
Programming Language
• Difference between High Level and Low Level Languages
Basics of C
C Character Set
Constants, Variables and Keywords
• A constant is an entity that doesn’t change whereas a variable is an
entity that may change.
• Types of C Constants :
• C constants can be divided into two major categories: (a) Primary
Constants , (b) Secondary Constants.
Rules for Constructing Variable Names
• A variable name is any combination of 1 to 31 alphabets, digits or
underscores.
• The first character in the variable name must be an alphabet or
underscore.
• No commas or blanks are allowed within a variable name.
• No special symbol other than an underscore (as in gross_sal) can be
used in a variable name.
• Ex.: si_int
m_hra
pop_e_89
• C compiler is able to distinguish between the variable names by
making it compulsory for you to declare the type of any variable name
that you wish to use in a program.
• This type declaration is done at the beginning of the program.
• Following are the examples of type declaration statements:
• Ex.:
int si, m_hra ;
float bassal ;
char code ;
C Keywords
• Keywords are the words whose meaning has already been explained to
the C compiler (or in a broad sense to the computer).
• 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.
• Some C compilers allow you to construct variable names that exactly
resemble the keywords.
• However, it would be safer not to mix up the variable names and the
keywords.
• The keywords are also called ‘Reserved words’.
• There are only 32 keywords available in C.
The First C Program
• Before we begin with our first C program do remember the following
rules that are applicable to all C programs:
(a) Each instruction in a C program is written as a separate
statement. Therefore a complete C program would comprise of a
series of statements.
(b) The statements in a program must appear in the same order in
which we wish them to be executed; unless of course the logic of
the problem demands a deliberate ‘jump’ or transfer of control to
a statement, which is out of sequence.
(c) Blank spaces may be inserted between two words to improve the
readability of the statement. However, no blank spaces are allowed
within a variable, constant or keyword.
(d) All statements are entered in small case letters.
(e) C has no specific rules for the position at which a statement is to be
written. That’s why it is often called a free-form language.
(f) Every C statement must end with a ;. Thus ; acts as a statement
terminator.
• /* Calculation of simple interest */
/* Author gekay Date: 25/05/2004 */
main( )
{
int p, n ;
float r, si ;
p = 1000 ;
n=3;
r = 8.5 ;
/* formula for simple interest */
si = p * n * r / 100 ;
printf ( "%f" , si ) ;
}
• Any number of comments can be written at any place in the program.
• For example, a comment can be written before the statement, after the
statement or within the statement as shown below:
/* formula */ si = p * n * r / 100 ;
si = p * n * r / 100 ; /* formula */
si = p * n * r / /* formula */ 100 ;
• Comments cannot be nested.
For example,
/* Cal of SI /* Author sam date 01/01/2002 */ */
is invalid.
• A comment can be split over more than one line, as in,
/* This is
a jazzy
comment */
Such a comment is often called a multi-line comment.
• main( ) is a collective name given to a set of statements.
• This name has to be main( ), it cannot be anything else.
• All statements that belong to main( ) are enclosed within a pair of
braces { } as shown below.
main( )
{
statement 1 ;
statement 2 ;
statement 3 ;
}
• The general form of printf( ) function is,
printf ( "", ) ;
can contain,
%f for printing real values
%d for printing integer values
%c for printing character values
• /* Just for fun. Author: Bozo */
main( )
{
int num ;
printf ( "Enter a number" ) ;
scanf ( "%d", &num ) ;
printf ( "Now I am letting you on a secret..." ) ;
printf ( "You have just entered the number %d", num ) ;
}
The Basic Data Types
• C defines five foundational data types: character, integer, floating-
point, double floating-point, and valueless.
• These are declared using char, int, float, double, and void, respectively.
• an object of type char is 1 byte
• an int is 32 bits
• The range of float and double will depend upon the method used to
represent the floating-point numbers. Standard C specifies that the
minimum range for a floating-point value is 1E–37 to 1E+37
• The type void either explicitly declares a function as returning no
value or creates generic pointers.
Modifying the Basic Types
• Except type void, the basic data types may have various modifiers
preceding them.
• A type modifier alters the meaning of the base type to more precisely
fit a specific need.
• The list of modifiers is shown here:
signed
unsigned
long
short
Backslash Character Constants
Operators
• There are four main classes of operators: arithmetic, relational,
logical, and bitwise.
• In addition, there are some special operators, such as the assignment
operator, for particular tasks.
• The Assignment Operator
• Multiple Assignments
• Compound Assignments
Arithmetic Operators
• The operators +, –, *, and / work as they do in most other computer
languages.
• You can apply them to almost any built-in data type.
• When you apply / to an integer or character, any remainder will be
truncated.
• The modulus operator % also works in C as it does in other languages,
yielding the remainder of an integer division.
The Increment and Decrement Operators
• C includes two useful operators that simplify two common
operations.
• These are the increment and decrement operators, ++ and – –.
• The operator ++ adds 1 to its operand, and – –subtracts 1.
Relational and Logical Operators
• In the term relational operator, relational refers to the relationships
that values can have with one another.
• In the term logical operator, logical refers to the ways these
relationships can be connected.
Bitwise Operators
• Bitwise operation refers to testing, setting, or shifting the actual bits
in a byte or word, which correspond to the standard char and int data
types and variants.
The ? Operator
C Instructions
• There are basically three types of instructions in C:
(a) Type Declaration Instruction
(b)Arithmetic Instruction
(c)Control Instruction
• Type declaration instruction − To declare the type of variables used in
a C program.
• Arithmetic instruction − To perform arithmetic operations between
constants and variables.
• Control instruction − To control the sequence of execution of various
statements in a C program.
Type Declaration Instruction
• The type declaration statement is written at the beginning of main( )
function.
• Ex.:
int bas ;
float rs, grosssal ;
char name, code ;
a) .
b) .
Arithmetic Instruction
a) z = k * 1 is legal, whereas k * 1 = z is illegal.
b) The expression 10 / 2 yields 5, whereas, 10 % 2 yields 0.
c) char a, b, d ;
a = 'F’ ;
b = 'G’ ;
d = '+’ ;
d) char x, y ;
int z ;
x = 'a’ ;
y = 'b’ ;
z=x+y;
Integer and Float Conversions
a) An arithmetic operation between an integer and integer always
yields an integer result.
b) An operation between a real and real always yields a real result.
c) An operation between an integer and real always yields a real
result.
In this operation the integer is first promoted to a real and then
the operation is performed.
Hence the result is real.
Type Conversion in Assignments
• int i ;
float b ;
i = 3.5 ;
b = 30 ;
what gets stored in i is 3
30 is promoted to 30.000000 and then stored in b
Hierarchy of Operations
Associativity of Operators
Control Instructions in C
• There are four types of control instructions in C.
• They are:
(a) Sequence Control Instruction
(b) Selection or Decision Control Instruction
(c) Repetition or Loop Control Instruction
(d) Case Control Instruction