0% found this document useful (0 votes)
44 views38 pages

Unit 1 (First)

The document provides an introduction to the C programming language, highlighting its development by Dennis Ritchie in 1972 and its features such as being a structured, general-purpose language that combines high-level and low-level programming capabilities. It outlines the structure of a C program, the steps for executing a program, and details on identifiers, keywords, constants, and tokens used in C. Additionally, it compares procedural and object-oriented programming, emphasizing the advantages of C in various applications.

Uploaded by

shreyas70792006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views38 pages

Unit 1 (First)

The document provides an introduction to the C programming language, highlighting its development by Dennis Ritchie in 1972 and its features such as being a structured, general-purpose language that combines high-level and low-level programming capabilities. It outlines the structure of a C program, the steps for executing a program, and details on identifiers, keywords, constants, and tokens used in C. Additionally, it compares procedural and object-oriented programming, emphasizing the advantages of C in various applications.

Uploaded by

shreyas70792006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

Unit – I

(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

1960 ALGOL International Committee Too general, too abstract

1963 CPL Cambridge University Hard to learn, difficult to implement


(Combined Programming
Languag)
1967 BCPL Martin Richards Concerned only with specific program

1970 B Ken Thompson Concerned only with specific program

1972 C Dennis Ritchie Originally programming under UNIX

UNIX

1978 K&RC Brain Kernigham


(Kent Recursive Calculator)

1989 ANSI C ANSI committee

1990 ANSI/ISO C ISO committee

1999 C99 Standardization Committee


Procedural Oriented Programming Object-Oriented Programming

In procedural programming, the program is In object-oriented programming, the program


divided into small parts called functions. is divided into small parts called objects.

Procedural programming follows a top-down Object-oriented programming follows


approach. a bottom-up approach.

There is no access specifier in procedural Object-oriented programming has access


programming. specifiers like private, public, protected, etc.

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.

In procedural programming, overloading is not Overloading is possible in object-oriented


possible. programming.

In procedural programming, there is no In object-oriented programming, the concept


concept of data hiding and inheritance. of data hiding and inheritance is used.
in procedural programming, the function In object-oriented programming, data is
is more important than the data. more important than function.

Procedural programming is based on Object-oriented programming is based on


the unreal world. the real world.

Procedural programming is used for Object-oriented programming is used for


designing medium-sized programs. designing large and complex programs.

Code reusability absent in procedural Code reusability present in object-


programming, oriented programming.

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.

• What is the difference b/w # and ##?


• Functions: A function is a set of instructions aimed to achieve a
predefined objective. Every ‘C’ program begin with function
name main( ). The body of function is enclosed in curly braces.
A function body consists of two parts..
• Local variable declaration
• Executable statements
• All statements in the declaration and executable parts end with
semicolon ‘;’ called as statement terminator
• ‘C’ allows different form of main () like
• Main() abc(int x,int y);
• int main()
• void main()
• char main(void)
• void main(void)
• Return()
STEPS FOR EXECUTING A ‘C’ PROGRAM

• A ‘C’ program involves following steps


• Editing/creating your Program
• Save the program
• Compiling the program Code
• Linking the program with various modules,
functions from the ‘C’ library
• Debugging the Code or to find errors
• Loader (Executing the Code)
• Specifying another name for an executable
file.(save the file with any name)
• Editing a Program: We can create ‘C’ program using the various rules
available in ‘C’. A file name consists of letters, digits and special characters
followed by ‘.’ and the letter ‘c’. The program entered into the file is called
as source code.

• Compiling the program code: To compile a ‘C’ program a compiler will


be required to execute the statements. A ‘C’ compiler converts the source
code into machine code which computer can understand and producing
errors. After compilation an object code file is created which usually has
the extension .obj.

• Linking the program: Linking is the process of putting libraries, inbuilt


functions and programs that are required by the object code. The task of
linking is performed by the linker. A linker combines several object modules
into single executable code and stored automatically into another file.

• 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.

• Specifying another name for an executable file: if


we want to save the existing file with another
name, then rename the file or save as.

• .
CHARACTER SET USED IN ‘C’

• The set of character consists of alphabets, digits and special symbols


which is used to represent information.

• Letters or Alphabets: Both upper case and lower case letters of English.
A,B,C …………….X,Y,Z.
a,b,c ………………x,y,z

• Decimal and Digits:


0,1,2,3………………7,8,9

• White spaces characters:


Blank space, new line, carriage return, vertical and horizontal tab
etc.

• 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:

• Identifier consists of alphabets (lowercase and uppercase), digits and


underscore.
• It must not start with digit.
• They cannot be a keyword or reserve word.
• Uppercase and lowercase identifiers are different in C.
• No special characters such as blank space, comma, semicolon, colon,
period, slash, punctuation are not allowed.
• No two successive underscore are allowed.
• Name of identifier is simple so that it can be easily understood
Keyword
• A key word is reserve word in C. It cannot be used as an identifiers in the program. They have
predefined meaning and cannot be changed. There are 32 keywords in C. all keyword are written in
lowercase. So INT, WHILE, GOTO, SWITCH are identifiers not keyword.

• Standard Keyword

• Auto for typedef
• Continue goto union
• Break if unsigned
• Case int void
• Char long volatile
• Const register while
• Default return switch
• Do short Float
• Double signed Extern
• Else sizeof struct
• Enum static
CONSTANTS

• 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

• const. qualifier is used to declare a constant i.e.


• Const<type> <name> = <val>
• Where
• const: is a reserve word.
• <type>: is any valid datatype.
• <name>: is an identifier.
• <val>: is the value assigned to the constant
• NUMERIC CONSTANT:
• It is made up of digits that do not change their values during program execution. It can be classified into 2 categories.
• Integer Constants: An integer is a whole number without decimal point. An integer constants is sequence of digits.
• Eg. const int rate = 100;
• There are 3 types of integer constants in C.
• Decimal Integer constant: consists of set of digits 0 to 9 preceded by an optional – or + sign. Commas, spaces and non digits
characters are not allowed between digits of integer constants.
• Example
• 456
• 017
• +124567
• -876

• Octal Integer Constant: It consists of any combination of digits between 0 to 7 with leading 0.

• Example

• 01
• 078- wrong
• 0465
• 07564

• Hexadecimal Integer Constant: It consists of any combination of digits between 0 and 9 and A to F or a to f. It must begin with 0x
or 0X.

• Example

• 0x5
• 0X9A
• 0x7FF
• 0Xabc8

• Unsigned Integer, Long Integer and Unsigned Long Integer Constant:

• Unsigned integers constant are those integers which are non negative. It can be identified by appending
the letter U or u to the end of the constant value.

• 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:

Sr. No Escape Sequences Character Description

1. \n New line Position the cursor to the beginning of


next line

2 \t Horizontal tab Move the cursor to the next tab


3. \r Carriage return Position the cursor to the beginning of
current line

4. \a Beep System beep


5. \b Backspace Move the cursor one space to the left
6 \\ Backslash Print a backslash character
7. \’ Single quotes To print single quotes
8. \” Double quotes To print double quotes
9. \xhh Hexadecimal To display some symbols using
hexadecimal number
• VARIABLES:

• It is a location in the memory in which we can store data. Every variable is identified by
a name and a data type for easy reference. The value of a variable is not fixed, it can
change as many times during program execution.

• Variable Naming conventions

• The name can be up to 31 characters long.
• It must begin with letter or an underscore.
• Can be made up of a-z, A-Z, 0-9 and underscore.
• It cannot a duplicate or keyword.
• All variables names are case sensitive. i.e. Total, TOTAL, total and tOTAL are all different.
• The first character cannot be numeric.

• Example:

• int x;
• float y;
• double z;
• char p;
Datatype :

Variable in a program occupies some space in computer’s memory


where a value is stored. A value may be an integer,
float, character or a string. C supports different type of data type.
These are :
2.1
•Primary / simple / primitive / atomic data types:
It supports five data types. Int, char, float, double and void.
INTEGER :
signed integer( int, short int, long int)
unsigned integer( unsigned int, unsigned short int, unsigned long int)
character( char, signed char, unsigned char)
floating point( float, double, long double)
FLOAT:

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

Type Size(byt Range


es in
memory
)
float 4 bytes -3.4x1038 to +3.4x1038
double 8 bytes -1.79x 10308 to +1.79x10308
Long double 16 bytes -1.1x 104932to +1.1x104932

Type Size(byt Range


es in
memory
)
Char or signed char 1 -128 to +127
Unsigned char 1 0 to 255
• 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.

Conversion Character Meaning

%c Data item is a single character

%d Data item is a single decimal integer

%e Data item is a floating point value with an exponent

%f Data item is a floating point value w/o exponent

%g Data item is a floating point value

%h Data item is a short integer

%i Data item is a decimal, hexadecimal or octal integer

%o Data item is a an octal integer w/o a leading 0

%s Data item is a string

%u Data item is a an unsigned decimal integer

%x Data item is a hexadecimal integer w/o the leading 0x


ASSIGNMENT STATEMENTS
• The assignment statement is used to assign values to a variable. The ‘=’ sign is used as an assignment operator.
The syntax is
• Variable –name=expression;



Example:

• Count = 1;
• Flag = ‘c’;
• Length = 200;
• Sum = 5*9;
• Y = x +1;
• Area = length * Breadth


• MULTIPLE ASSIGNMENTS

• If two or more variable are to assigned for the same value then the assignments can be combined. The syntax
is:
• Variable = variable = variable = expression;


Example:
• A=B=C = 10;

SYMBOLIC CONSTANTS
• Symbolic constants allow the programmer to define a name for a constants and used that name throughout the
program. The syntax is
– # define symbolic names constant values

• 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

• Write a Program for explicit conversion



• include<stdio.h>
• #include<conio.h>
• void main()
• {
• int m,n ;
• float c,f;
• m=s,n=9;
• printf("enter faranite \n");
• scanf("%f",&f);
• c=(float)m/n*(f-32);
• printf("temp in celcius \n");
• printf("%f",c); getch();
• }
• Implicit type casting means conversion of data types without losing its original
meaning.
• Implicit type conversion in C happens automatically, when a value is copied to its
compatible data type.
• If the operands are of two different data types, then an operand having lower data
type is automatically converted into a higher data type.

• Write a Program for implicit Conversion



• #include<stdio.h>
• #include<conio.h>
• void main()
• {
• int a;
• float b,d;
• printf("entr the value of a ");
• scanf("%d",&a);
• printf("enter the value of b ");
• scanf("%f",&b);
• d=a*b;
• printf("the sum is %f",d); getch();
• }
• #include<stdio.h>
#include<conio.h>
void main()
{
float i=3.54;
int p;
p = (int) i;
printf("Explicit value is %d",p);
getch();
}
• #include <stdio.h>
• main()
• {
• int number = 1;
• char character = 'k'; /*ASCII value is 107 */
• int sum;
• sum = number + character;
• printf("Value of sum : %d\n", sum );
• }

• #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);
• }

You might also like