Unit 1 (First)
Unit 1 (First)
(First)
Harjender Singh
Asstt. Professor
Mob : 9718933400
INTRODUCTION
• C’ is a powerful, efficient, general purpose structured
programming and most popular high level and system
programming language
• It can also be used for writing numerical, text and
database processing application.
• It was developed by Dennis Ritchie at AT&T Bell
laboratories of USA in 1972.
• Initially ‘C’ was written originally for programming under
UNIX operating system. Today ‘C’ is running on a number
of operating system including MS-DOS.
• C combines the structure of high level language and
efficiency of assembly language.
• It also supports the features of low level language, so it is
sometimes called as middle level language.
• ‘C’ also contains some additional features that allow it
to be used at lower level.
• It is generally used for writing the operating systems as
well as for application programming.
• There is no single compiler of ‘C’. Many different
organizations have written and implemented ‘C’
compiler.
• Even for the same computer, there may be several
different compilers, each with its own individual
requirements.
• ‘C’ is also called as structured programming language. It
means to divide the program into small modules, where
each module performs a specified task.
• The structured programming also helps to provide an
error free, accurate and maintainable code.
• C’ is a case sensitive language.
• The variable used in upper case has different
meaning as declared in lowercase letters.
• ‘C’ is a programmer oriented.
• It provides you to manipulate individual bits
in memory and allowing the user to access the
hardware.
Year Language Developed Remarks
UNIX
Adding new data and functions is not easy. Adding new data and function is easy.
Procedural programming does not have any Object-oriented programming provides data
proper way of hiding data so it is less secure. hiding so it is more secure.
Examples: C, FORTRAN, Pascal, Basic, etc. Examples: C++, Java, Python, C#, etc.
FEATURES OF ‘C’
• ‘C’ has become a popular, flexible and general purpose programming
language because of its versatile features. Some of its important features
are
• C is a general purpose programming language. It is also used for writing
commercial as well as scientific application.
• C is structured programming language i.e. to develop well defined,
portable and easy to maintain program.
• It has rich set of operators, inbuilt functions, keywords and data-types.
• It provides compact representation for expression.
• Portability: C program written for one computer can be executed on
another with little or no modifications.
• C is a case sensitive language. Identifiers written in lowercase letter are
different from upper case identifiers.
• Ability to extend itself: A C program is a collection of functions that are
supported by the ‘C’ library. We can add our own functions to the ‘C’
library.
• Limited number of keywords: There are only 32 keywords in ‘C’. All other
words except keywords can be used as a variable name.
• Code Reusability: A programmer can easily create his own
functions and use them again and again in different application.
• In ‘C’ syntax errors are easily detected by the compiler and can be
easily removed.
• /* Modularity: A ‘C’ program code can be break into small
modules and can be reused in different program.
• ‘C’ supports rich set of data types.
• It supports for recursive call functions.
• Flexibility: It can be used in a variety of applications i.e. embedded
systems to commercial data processing techniques.
• Large set of Inbuilt functions: ‘C’ supports number of inbuilt
functions like input/output, string handling, library, mathematical
and graphical designing.
• Documentation: ‘C’ allows user to add comments in program. Its
improve the readability of the program. Comments are written
in /* and */
• ‘C’ supports pointer arithmetic and pointer manipulation.
• Memory Management: These functions help to save the memory
‘C’ PROGRAM STRUCTURE
A ‘C’ program consists of following sections.
Comments Section
Preprocessor Directives Section
Global Declaration Section
Main Program Section
Functions
• /* comments */
• #define MAX 200 // Preprocessor Directive
• #include<stdio.h> // Header file
• int a=20; // Global Declaration
• int b=40;
• // main() // function Name
• { // Start of Program
• ……………………….. // Program Statement
• …………………………
• } // End of Function
•
•
• Comment: A comment start with /* and end with */. A
comment improves the readability of program.
• Preprocessor Directives: These are instruction for the
compiler which perform various manipulations on the source
file before it is actually compiled. The main function of
preprocessor is to include other files in source file. All
preprocessor directive start with # sign. The most commonly
directive are #define directive and #include<stdio.h>.
• Global Declaration Section: There are two places where
variable are declared, inside the function and outside the
function.
• The variable declared outside the function is known as global
variable and they can be accessed by any other function in the
program. These variable remain entire time when program is
executing.
• Executing the Code: To execute a compiled source code we use F9 key for
compiling and CTRL+F9 key to run the program. The errors also be
generated at compile and run time.
• Debugging the Code: Debugging is the process
of correcting syntax and logical errors. A syntax
error occur when the compiler encounter code
that violates ‘C’ language rules. Syntax errors are
also called as compile time errors. Logical errors
are run time errors that provides and erroneous
output from the program.
• .
CHARACTER SET USED IN ‘C’
• Letters or Alphabets: Both upper case and lower case letters of English.
A,B,C …………….X,Y,Z.
a,b,c ………………x,y,z
• Special Symbols:
! * + \ < ‘ _ @ % {} [ ] ; : “ “ ? / & ^ = etc.
TOKENS :
• The smallest individual unit in a program is called as token or a
lexical unit.
• A token is a group of characters that is understood as a unit. They
are the basic building block of a ‘C’ program. i.e. a programmer
writes a program using tokens. There are six types of token in ‘C’
they are
• Keywords
• Identifiers
• Constants
• Strings
• Operators
• Special Symbols/characters (:,#,;,””?
• Note : When tokens are not arranged in a organized sequence,
the compiler generate an error message.
IDENTIFIERS
• An identifier is a names given to the program, elements such as array,
variables and functions. It is a sequence of character taken from C
character set. The number of character in identifier is not fixed, but most of
C compilers allow 31 character. The first character of identifiers must begin
with a letter or an underscore but not with a digit.
• Rules:
• It refers to a fixed value that does not change during the execution of a C program. any attempt to change
the value of a constant provide an error message. C supports several types of constants.
• Constants available in C
• Numeric Constants
– Integer constants
• Decimal Integer Constants
• Octal integer constants
• Hexadecimal integer constants
• Unsigned integer constants
• Long integer constants
– Real constants/floating point constants
• Character constants
– Single character constants
– String constants
• Example:
• 123456U
• 07654u
• 0X123456U
• Long integers constant are those integers which are non negative. It can be identified by appending the
letter l or L after the constant value.
•
• Example:
• 123456l
• 0987654L
• 0X123456L
• Const long int a=12345L
• Unsigned long Integers constant can be identified by appending the letter UL to the end of the constant.
The letter may be either upper or lower case.
• Example:
• 123456ul
• 0987654UL
• 0X123456UL
• Real Constant:
• Number containing fraction parts are called as
real or floating numbers. The real can also be
expressed in exponential or scientific notation.
Example:
• Const float Pi = 3.141
• 0.0097867, 23.0e0, 56.E0, +.99, -.66,
+1245.00, 1.9E+8 etc.
• CHARACTER CONSTANT:
•
• It contains a single character enclosed within a pair of single quotes. Data such as ‘A’ or
‘a’ is known as character constant. It might be a digit, a alphabet or a special symbol. A
character constant have integer values that has determined by computer known as ASCII
values.
• Example:
•
• ‘8’,’e’,’R’,’s’, ‘ ‘ ,’$’ etc.
•
• String Constants or Multiple Character Constant:
•
• A sequence of characters enclosed within pair of double quotes is known as string
constant. The characters may be letters, numbers, special characters or blank spaces.
• Example
•
• “Hello”, “MSI”, “Maharaja Surajmal Institute”, “Good Luck”, “8+0” etc.
•
• Note: A string constant does not have an ASCII values.
•
BACKSLASH CHARACTER CONSTANTS (ESCAPE
SEQUENCE)
‘C’ has special character constants called backslash character constants. These
are unprintable ASCII character which performs special action in the output
statements. All escape sequences begin with a single backslash( \ ) followed by
character.
Some common escape sequences are:
CHAR
•Derived data types :It is made up of one or more simple data items
like Array, Function and Pointer
•User-defined data types: These data types are created by users using
one or more basic types and other derived and user defined types like
Structure, Union and enum.
Type Size(byt Range
es in
memory
)
Short or signed short 1 -32768 to +32768
int
unsigned short int 1 0 to 65535
Int or signed int 2 -32768 to +32768
Unsigned int 2 0 to 65535
Long int or signed long 4 -2,147483648 to
int +2,147483648
Unsigned long int 4 0 to 4294967295
• Example:
• #define PI 3.141
• #define COUNT 20
• #define AGE 22
• In ‘C’ symbolic constants can be defined Using #define preprocessor directive
• Using const qualifier
• Using enum keyword
•
• Advantages:
•
• The value of constant can be changed in the program by doing only one modification.
• The chance of error is reduced
• There is no need to repeat the value of same constant in every expression.
• The readability of program is also increased.
• Example: To calculate the Area of a Circle.
• #include < stdio.h >
• #define PI 3.141
• Main()
• {
• Double radius = 2.5;
• Area = PI * radius * radius;
• Printf (“Area of Circle is = %lf “, area);
• }
• Example :
#include <stdio.h>
#define MIN(a,b) ((a)<(b)?(a):(b))
void main()
{
printf("The minimum value between 50 and 20 is: %d\n", MIN(50,20));
}
• Example :
#include <stdio.h>
#include <conio.h>
#define NAME "TuotiralsAndExamples.com"
#define AGE 10
int main()
{
printf("%s is over %d years old.\n", NAME, AGE);
return 0;
}
Example :
• #include <stdio.h>
• #include <conio.h>
• #define AGE (20 / 2)
• int main()
• {
• printf("TuotiralsAndExamples is over %d years old.\n", AGE);
• return 0;
• }
• EXPRESSION
•
• It is a combination of constant, variable and operator to form an
executable statements. It may consists of combination of entities
such as constant, a value, a variable, an array and a reference to
function interconnected by one or more operator.
•
• Example:
•
• C+D
• X>Y
• C=a+b
• Explicit type conversion is done by the user by using (type) operator.
• A cast operator is (a unary operator) used to temporarily convert constant, variable or
expression to a particular type. The syntax of cast operator is:
• Syntax: (datatype)expression
• #include<stdio.h>
• int main(){
• short a=10; //initializing variable of short data type
• int b; //declaring int variable
• b=a; //implicit type casting
• printf("%d\n",a);
• printf("%d\n",b);
• }