0% found this document useful (0 votes)
128 views45 pages

C Language Fy Bca

The document provides an overview of the history and development of the C programming language. It discusses that C evolved from earlier languages like ALGOL (1960s), BCPL (1967), and B (1970). C was developed by Dennis Ritchie at Bell Labs in 1972 and combined concepts from these earlier languages. It also coded the Unix operating system. Today, C remains widely used for systems programming and is found in operating systems and applications. The document also covers basic C programming concepts like tokens, program structure, data types, and operators.

Uploaded by

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

C Language Fy Bca

The document provides an overview of the history and development of the C programming language. It discusses that C evolved from earlier languages like ALGOL (1960s), BCPL (1967), and B (1970). C was developed by Dennis Ritchie at Bell Labs in 1972 and combined concepts from these earlier languages. It also coded the Unix operating system. Today, C remains widely used for systems programming and is found in operating systems and applications. The document also covers basic C programming concepts like tokens, program structure, data types, and operators.

Uploaded by

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

SSSDIIT - JUNAGADH

HISTORY OF C
ALGOL
The root of all modern languages is ALGOL. ALGOL
is introduced in the early 1960s. ALGOL was the first
computer language to use a block structure. ALOGL
was widely used in Europe. ALGOL language gave the
concept of structured programming to the computer
science community.
HISTROY OF C
BCPL
In 1967, Martin Richards developed a language called
BCPL. The full form of BCPL is Basic Combined
Programming Language. BCPL was developed
primarily for writing System software.

HISTROY OF C
B
In 1970, Ken Thompson developed a language called
B at Bell Laboratory. Language B has been developed
using many features of BCPL.

C
C was evolved from ALGOL, BCPL and B. Dennis
Ritchie has developed C Language at Bell Laboratory
in 1972. C uses many concepts of ALGOL, BCPL and B
languages and added the concept of data types and
other powerful features. Unix operating system,
which was also developed at Bell Laboratory, was
coded almost entirely in C.

Today, C is running under a variety of operating
system and different hardware platforms.
HISTORY OF C IN SORT
Year Language Developed By
1960 ALGOL International Group
1967 BCPL Martin Richards
1970 B Ken Thompson
1972 C Dennis Ritchie
1978 K&R C Kerningham and
Ritchie
1989 ANSI C ANSI Committee
Importance of C
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.
Today's most popular Linux OS and RDBMS MySQL have
been written in C.
Advantages of C or
Characteristics of C
C is general-purpose structured programming language
that is powerful, efficient and compact.
C is a structured, high level and machine independent
language.
C allows software developers to develop programs
without worrying about the hardware platforms.
C is a strong language whose rich set of built-in functions
and operators can be used to write any complex
programs.
The C is well suited for writing both System Software and
Application Software.
Advantages of C or
Characteristics of C
C programs are efficient (Well-Organized or
Disciplined) and fast due to its variety of data types
and powerful operators.
C is highly portable. This means that C programs are
written for one computer can be run on another
computer.
Another important feature of C is its ability to extend
itself. A C program is basically a collection of
functions that are supported by the C library. We can
continuously add our own functions to C library.
Character set
The characters that can be used to form words, numbers
and expressions depend upon the computer on which
the program is run. However, a subset of characters is
available that can be used on most personal, micro, mini
and mainframe computers. The characters in C are
grouped into the following categories:
Letters
Digits
Special characters
White spaces
Character set
Letters White Spaces Special Characters
Uppercase A...Z Blank space , Comma & Ampersand
Lowercase a...z Horizontal tab . Period ^ Caret
Carriage return ; Semicolon * Asterisk
New line : Colon - Minus Sign
Form feed ? Question Mark + Plus Sign
’ Apostrophe < Less Than Sign
” Quotation Mark > Greater Than Sign
! Exclamation Mark ( Left Parenthesis
| Pipe ) Right Parenthesis
/ Slash [ Left Bracket
Digits \ Backslash ] Right Bracket
~ Tilde { Left Brace
All decimal digits 0...9 _ Under Score } Right Brace
$ Dollar Sign # Number Sign
% Percent Sign @ At the rate Sign
Basic structure of C programm
The C Program can be viewed as a group of building
blocks of called functions. A function is a subroutine
that may include one or more statements designed to
perform a specific task.
A C Program may contain one or more sections which
are shown in figure below :

Basic structure of C programm
Documentation Section

Linkage Section

Definition Section

Global Declaration Section

main ( ) Function Section


{
Declaration Part;
Executable Part;
}

Subprogram Section

Function 1

(User Defined Function)


Function 2

---

---

Function N
Basic structure of C programm
 DOCUMENTATION SECTION
 The documentation section consists of a set of comment lines giving the name of the
program, name of the author, Program definition and other details, which the programmer
would like to user later.
 LINKAGE SECTION
 The linkage section provides instructions to the compiler to link functions from the system
library.

 DEFINITION SECTION
 The definition section defines all symbolic constants.

 GLOBAL DECLARATION SECTION
 There are some variables those are used in more than one function. Such variables are called
global variables and are declared in the global declaration section that is outside of all the
functions. Global Declaration Section also declares all the user-defined functions.

Basic structure of C programm
main ( ) FUNCTION SECTION
Every C program must have one main( ) function section.
This section contains two parts, declaration part and
executable part. The declaration part declares all the
variables used in the executable part. There is at least one
statement in the executable part. These two parts must
appear between the opening and the closing braces. The
program execution begins at the opening brace and ends at
the closing brace. The closing brace of the main function
section is the logical end of the program. All statements in
the declaration and executable parts end with a semicolon.
Basic structure of C programm
SUBPROGRAM SECTION
The subprogram section contains all the user-defined
functions that are called in the main function. User-
defined functions are generally placed immediately
after the main function, although they may appear in
any order.
C TOKENS
 In a paragraph of text, individual words and punctuation marks are called
tokens. Similarly, in a program the smallest individual units are known as C
tokens.
 C programs are written using these tokens and the syntax of the language. C
has following six types of tokens:

C TOKENS

Keywords Identifiers Constant Operators Strings Special


void price s + – * / “Sum” Symbols
if main 2.50 > < >= “Rate” { }
switch name -14 && = ++ Odd” ( )
‘A’ [ ]
C TOKENS
 KEYWORDS
 Every C word is classified as either a keyword. All keywords have fixed
meanings and these meanings cannot be changed. All keywords must be
written in lowercase. The following table shows the C keywords:

Int if break struct


float else continue union
double switch void typedef
char case return const
unsigned default auto volatile
signed for register enum
short while static goto
long do extern sizeof
C TOKENS
IDENTIFIER
 Identifiers refer to the names of variables, functions and arrays. These
are user-defined names and consist of a sequence of letters and digits.
 Both uppercase and lowercase letters are permitted.
 Underscore character is usually used as a link between two words in
long identifiers.
 Rules for Identifiers
 First character must be an alphabet (or Underscore).
 Must consist letters, digits or underscore.
 Only first 31 characters are significant.
 Cannot use a keyword.
 Must not contain white space.
C TOKENS
CONSTANT
Constants in C refer to fixed values that do not
change during the execution of the program. C
supports following types of constants:
CONSTANTS

Numeric Constants Character Constants

Integer Real Single Character String

Constants Constants Constants Constants


C TOKENS
 Integer Constants
 123 -321 0 654321 +78
 Real Constants
 0.0083 –0.75 435.36 +247.0
 Single Character Constants
 ‘5’ ‘X’ ‘:’ ‘\n‘ ‘\t’
 String Constants
 “Hello!” “1987” “WELL DONE” “5+3” “X” “$===>”
C TOKENS
 OPERATORS
 C supports a rich set of operators. An operator is a symbol that tells the
computer to perform certain mathematical or logical manipulations.
Operators are used in programs to manipulate data and variables.
 C operators can be classified into following eight categories:
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Assignment operators
5. Increment and decrement operators
6. Conditional operators
7. Bitwise operators
8. Special operators
VARIABLES
 A variable is a data name that may be used to store a data value. A variable
may take different values at different times during execution. A variable name
can be chosen by the programmer. Some examples of such variables names
are:
 Average_age
 dollar
 counter
 standard
 As mentioned earlier, variable names may consist of letters, digits, and the
underscore (_) character.
VARIABLES
 Followings are rules for variable.
1. Variables must begin with a letter.
2. ANSI standard recognizes a length of 31 characters.
3. Uppercase and lowercase are significant. That is, the variable Total is not the
same as total or TOTAL.
4. It should not be a keyword.
5. White space is not allowed.
Declaration of variable:
 After designing suitable variable names, we must declare them to the compiler.
Declaration does two things:
1. It tells the compiler what the variable name is.
2. It specifies what type of data, the variable will hold.
 The declaration of variables must be done before they are used in the progarm.
 The syntax for declaring a variable is as follows:
 data-type v1,v2,.....vn ;
OPERATORS
 C supports a rich set of operators. An operator is a symbol that tells
the computer to perform certain mathematical or logical
manipulations. Operators are used in programs to manipulate data
and variables.
 C operators can be classified into following eight categories:
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Assignment operators
5. Increment and decrement operators
6. Conditional operators
7. Bitwise operators
8. Special operators
OPERATORS
ARITHMETIC OPERATORS
C provides all the basic arithmetic operators
Arithmetic Operators
Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo division
OPERATORS
RELATIONAL OPERATORS
 We often compare two numbers and depending on their relation, take
certain decisions. For example, we may compare the salary of two
employees, or the marks of two subjects, and so on.
 These comparisons can be done with the help of relational operators.
An expression such as
a>b
 For example: 10 < 20 is true but 20 < 10 is false
Relational Operators
Operator Meaning
< is less than
<= is less than or equal to
> is greater than
>= is greater than or equal to
== is equal to
!= is not equal to
OPERATORS
LOGICAL OPERATORS
C has the following three logical operators:
Logical Operators
Operator Meaning

&& Logical AND


|| Logical OR
! Logical NOT

 The logical operators && and || are used when we want to test more than one
condition (relational expression) and make decisions. An example is :
 a > b && a > c
 The logical expression given above is true only if a>b is true and a > c is true. If
either (or both) of them is false, the expression is false.
OPERATORS
ASSIGNMENT OPERATORS

 Assignment operators are used to assign the result of an expression to a


variable. We have seen the usual assignment operator, ‘=’. In addition, C has a
set of ‘shorthand’ assignment operators of the form:
 V op=exp;
 Where v is a variable, exp is an expression and op is a C binary arithmetic
operator. The operator op= is knows as shorthand assignment operator.
 For example , x=x+1 can be written as x+=5.
Shorthand Assignment Operators
Statement with simple Assignment operator Statement with shorthand operator
a=a+1 a += 1
a=a–1 a -= 1
a = a * (n+1) a *= n+1
a = a / (n+1) a /= n+1
a=a%b a %= b
OPERATORS
INCREMENT AND DECREMENT OPERATORS
 C allows two very useful operators not generally found in other languages.
These are the increment and decrement operators:
 ++ and – –
 The operator ++ add 1 to operand, while – – subtracts 1 from operand. Both are
unary operators and takes the following form:
 ++m; or m++;
 --m; or m--;
 ++m; is equivalent to m = m+1; (or m += 1;)
 --m; is equivalent to m = m-1; (or m-=1;)
 A prefix operator first adds 1 to the operand and then the result is assigned to the
variable on left. On the other hand, a postfix operator first assigns the value to the
variable on left and then increment the operand
OPERATORS
CONDITIONAL OPERATOR (or TERNORY
OPERATOR)
 A ternary operator pair “ ? : “ is available in C to construct conditional expressions of the
form : exp1 ? exp2 : exp3;
 where exp1, exp2, and exp3 are expressions.
 The ternary operator ? : works as follows:
 For example, consider the following statements.
 a = 10;
 b = 15;
 x = (a > b) ? a : b;
 In this example, x will be assignment the value of b. This can be achieved using the if…
else statements as follows:
 if (a > b)
 x = a;
 else
 x = b;
OPERATORS
BITWISE OPERATORS
 These operators are used for resting (inactive) the bits, or shifting them right
or left. Bitwise operators may not be applied to float or double.
Bitwise Operators
Operator Meaning
<< bitwise shift LEFT
>> bitwise shift RIGHT

SPECIAL OPERATORS
 The sizeof operator is normally used to determine the lengths (size) of
operand. The operand may be a variable, a constant or a data type qualifier
 It returns the number of bytes the operand occupies.
 Examples:
 m = sizeof(sum);
 n = sizeof(long int);
OPERATOR PRECEDENCE AND ASSOCIATIVITY
Operator Description Associativity Rank

() Function call Left to right 1


[] Array element reference
+ Unary plus
- Unary minus Right to left 2
++ Increment
-- Decrement
! Logical negation
~ Once complement
* Pointer reference (indirection)

& Address
Sizeof Size of an object
(type) Type cast (conversion)
* Multiplication Left to right 3
/ Division
% Modulus
+ Addition Left to right 4
- Subtraction
<< Left shift Left to right 5
>> Right shift
< Less than Left to right 6
<= Less than or equal to
> Greater than
>= Greater than of equal to
== Equality Left to right 7
|= Inequality
& Bitwise AND Left to right 8
^ Bitwise XOR Left to right 9
| Bitwise OR Left to right 10
&& Logical AND Left to right 11
|| Logical AND Left to right 12
?: Conditional expression Right to left 13
= Assignment operators Right to left 14
* = /=%=
+= -=
^= |= &=
<<= >>=
, Comma operator Left to right 15
OPERATOR PRECEDENCE AND ASSOCIATIVITY

 Each operator in C has a precedence associated with it. This precedence is


used to determine how an expression involving more than one operator is
evaluated. . The operators at the higher level or precedence evaluated first.
 The operators of the same precedence are evaluated either from ‘left or right’
or from ‘right to left’, depending on the level. This is known as the
associativity properly of an operator.
 Table below provides a complete list of operators, . Rank 1 indicates the
highest precedence level and rank 15 the lowest.

 Rules of Precedence and Associativity


 Precedence rules decides the order in which different operators are applied.
 Associativity rule decides the order in which multiple occurrences of the same
level operator are applied
DATA TYPES
 C language is rich in its data types. The variety of data types allows the
programmer to select the type appropriate to the needs of the application as
well as the machine.
ANSI C supports three categories of data types:
1. Primary (or Fundamental) Data Types (int, char, float, double)
2. Derived Data Types (Array, Function, Structure, Pointer)
3. User Defined Data Types (typedef, enum)
Primary Data Types
 C compiler supports four fundamental data types, namely integer (int),
character (char), floating point (float) and double-precision floating point
(double).
 Primary data types, size they hold in memory, range of the data they can store
and format specifiers used for the data type (on 16-bit machines) are given in
table below:
Data Type Size in Memory Range Format
Specifier
char or signed char 1 Byte or 8 Bits -128 to 127 %c
unsigned char 1 Byte or 8 Bits 0 to 255 %c
int or signed int 2 Bytes or 16 Bits -32767 to 32768 %d
unsigned int 2 Bytes or 16 Bits 0 to 65535 %u
short int or 2 Bytes or 16 Bits -32767 to 32768 %d
signed short int
long int or 4 Bytes or 32 Bits -2147483648 to 2147483647 %ld
signed long int
Unsigned long int 4 Bytes or 32 Bits 0 to 4294967295 %lu
float 4 Bytes or 32 Bits 3.4E-38 to 3.4E+38 %f
double 8 Bytes or 64 Bits 1.7E-308 to 1.7E+308 %lf
long double 10 Bytes or 80 Bits 3.4E-4932 to 1.1E+4932 %Lf
User-Defined Type Declaration
 C compiler supports two user defined data types like : typedef and enum.
typedef data type:
 C supports a feature known as “type definition” that allows users to define an
identifier that would represent an existing data type. The user-defined data type
identifier can later be used to declare variables. It takes the general form:
 typedef type identifier;
 Where type refers to an existing data type and “identifier” refers to the “new”
name given to the data type.
 Remember that the new type is “new” only in name, but not the data type.
typedef cannot create a new type. Some examples of type definition are:
 typedef int units;
 typedef float marks;
 They can be later used to declare variables as follows:
 units batch1, batch2;
 marks mark1[50], mark2[50];
User-Defined Type Declaration
enum data type:
 Another user-defined data type is enumerated data type provided by
ANSI standard. It is defined as follows:
 enum identifier {value1, value2,..... valueN};
 The “identifier” is a user-defined enumerated data type, which can be
used to declare variables that can have one of the values enclosed
within the braces (known as enumeration constants). After this
definition, we can declare variables to be of this “new” type as below:
 enum identifier v1,v2, .... vN;
 The enumerated variables v1, v2, .... vN can only have one of the values value1,
value2, .... valueN. The assignments of the following types are valid:
 v1 = value3;
 v5 = value1;
User-Defined Type Declaration
An example:
 enum day { Monday, Tuesday, .... Sunday};
 enum day week_start, week_end;
 week_start = Monday;
 week_end = Friday;
 The compiler automatically assigns integer digits beginning with 0 to all the
enumeration constants.
 However, the automatic assignments can be overridden by assigning values
explicitly to the enumeration constants. For example:
 enum day {Monday = 1, Tuesday, ... Sunday};
 Here, the constant Monday is assigned the value of 1. The remaining
constants are assigned values that increase successively by 1.
TYPE CONVERSIONS
 Type conversion means, to convert data type of a variable or constant
to another type.
 There are two types of type conversion:
 (1) implicit type conversion
 (2) explicit type conversion
 (1) implicit type conversion
 When we use constants or Variables of different types in an
expression, C automatically converts any values to the proper type so
that the expression can be evaluated accurately. This automatic
conversion is known as implicit type conversion.
 If the operands are of different types, the ‘lower’ type is automatically
converted to the ‘higher’ type before the operation proceeds. The
result is of the higher type.
TYPE CONVERSIONS
 (2) Explicit Conversion
 Type conversion performed by the programmer forcefully by writing
the data type of the expression of specific type is known as explicit
type conversion.
 This type conversion is also known as “type casting”.
 Syntax : (type-name) expression;
 Where type-name is one of the standard C data types. The expression
may be a constant, variable or an expression.
Example Action
x = (int) 7.5 7.5 is converted to integer by truncation.
a = (int ) 21.3/(int)4.5 Evaluated as 21/4 and the result would be 5.
b = (double) sum/n Division is done in floating point mode.
y = (int) (a + b) The result of a+b is converted to integer.
z = (int) a+b a is converted to integer and then added to b.
p = cos ((double)x) Converts x to double before using it.
Pre - processor
 The pre processor is just a text that instruct the compiler to do
required pre processing before the actual processing.
 It is not a part of compiler, but a separate step in the compilation
process.
 All pre processor commands begin with hash symbol (#).
 Following 2 are most important pre processors that we use in our
program.
 (1) # include : to include particular header file in our program.
 Syntax : # include <header file name>
 (2) # define: to define symbolic constant. Its value can not be
changed during the execution of program.
 Syntax: #define symbolic_name value.
Defining symbolic constant
Symbolic constant is a name of value. We can use this
name instead of that value. Because it is a constant,
its value can not be changed during the execution of
program.
#define is used to define symbolic constant.
Syntax: #define symbolic_name value.
Example : #define pi 3.14
Symbolic names are sometimes called constant
identifiers.
Defining symbolic constant
Since the symbolic names are constants (not variables),
they do not appear in declarations.
The following rules apply to a # define statement which
define a symbolic constant.
 Symbolic names are written in CAPITALS.
 No blank space between the pound sign ‘#’ and the word
define is permitted.
 ‘#’ must be the first character in the line.
 #define statements must not end with a semicolon.
 Symbolic names are NOT declared for data types. Its data
type depends on the type of constant.
ASSIGNMENT 1
Q.NO QUESTIONS MARKS
1 Explain different languages used before c. 3
2 Write down history & importance of c. 3
3 Explain characteristics of c. 5
4 Explain character set used in c. 3
5 Explain basic structure of c program in detail. 5
6 Write a short note on main() function. 3
7 Explain c tokens in detail. 5
8 What is variable? Explain how to define a variable. 3
9 What is operator? List out & explain type of operator. 5
10 Explain ternary operator. 3
11 Explain precedence and associativity of an operator. 3
12 Explain data types available in c. 5
13 What is type conversion? Explain implicit & explicit type 5
conversion.
14 Write a short note on pre-processor 3
15 Explain how to define symbolic constant? 3

You might also like