SlideShare a Scribd company logo
1 of 66
Unit-1
INTRODUCTION TO ‘C’ LANGUAGE
• ‘C’ is a high–level programming language developed in 1972
by Dennis Ritchie
• at AT & T Bell Laboratories.
• It was primarily developed for system programming i.e. for
designing operating
• systems, compilers etc.
Importanceof’C’Language
• It is a robust language, whose rich set of built-in functions and
operations can be used to write any complex program.
• It is a middle level language because the ‘C’ compiler combines the
capabilities of anassembly language with the features of a high-level
language and therefore it is well
• suited for writing both system software and business packages.
• ‘C’ Programs are efficient and fast.
• C is highly portable, that C’ programs written on one computer can be run
on another with little (or) no modification.
• ‘C’ Language is well suited for structured programming, thus requiring the
user to think of a problem in terms of function modules (or) blocks.
• ‘C’ program has the ability to extend itself. It was named ‘C’ because it is
an offspring of BCPL(BasicCombinedProgrammingLanguage) which was
popularly called ‘B’ language.
1.Basic structure of C programs:
• Almost everything has a definite structure. Likewise, in the case of
programming languages, all of them have a definite structure. These structures
have to be followed while writing the code.
• The structure of a C program can be mainly divided into six parts, each
having its purpose
• It makes the program easy to read, easy to modify, easy to document, and
makes it consistent in format.
Section Description
Documentatio
n
Consists of the description of the program, programmer's name, and
creation date. These are generally written in the form of comments.
Link
All header files are included in this section which contains different
functions from the libraries. A copy of these header files is inserted into
your code before compilation.
Definition
Includes preprocessor directive, which contains symbolic constants.
E.g.: #defineallows us to use constants in our code. It replaces all the
constants with its value in the code.
Global
Declaration
Includes declaration of global variables, function declarations, static
global variables, and functions.
Main()
Function
For every C program, the execution starts from the main() function. It is
mandatory to include a main() function in every C program.
Subprograms
Includes all user-defined functions (functions the user provides). They can
contain the inbuilt functions and the function definitions declared in the
Global Declaration section. These are called in the main() function.
Basic Structure of the C Program
EX: Write a C program to find circumference of circle.
/* Author : Ramu
Aim : Program for finding circumference of circle*/
#include<stdio.h>
#include<conio.h>
#define PI 3.1415
int main ( )
{
float c, r;
printf (“enter radius of circle”);
scanf (“%f”, &r);
c = 2 * PI * r;
printf (“Circumference = %f”, c);
return 0;
}
Documentation
• In a C program, single-line comments can be written using two
forward slashes i.e., //.
• and we can create multi-line comments using /* */. Here,
we've used multi-line comments.
/**
* file: area.c
* author: you
* description: program to find circumference of circle.
*/
Link
• All header files are included in this section.
• A header file is a file that consists of C declarations that can be
used between different files. It helps us in using others' code in
our files. A copy of these header files is inserted into your code
before compilation.
ex: #include <stdio.h>
Definition
• A preprocessor directive in C is any statement that begins with
the "#" symbol.
• The #define is a preprocessor compiler directive used to create
constants
• #define is typically used to make a source program easy to
modify and compile in different execution environments.
• The define statement does not ends with a semicolon.
EX: #define PI 3.1415
Global Declaration
• This section includes all global variables, function
declarations, and static variables.
• The variables declared in this section can be used anywhere in
the program.
• They're accessible to all the functions of the program. Hence,
they are called global variables.
• There is no global declaration in the above program.
main() Function
• In the structure of a C program, this section contains the main
function of the code.
• The compiler starts execution from the main() function. It can
use global variables, static variables, inbuilt functions, and
user-defined functions.
• The return type of the main() function can be void and also not
necessarily int.
Subprograms
• This includes the user-defined functions called in the main()
function. User-defined functions are generally
• written after the main() function irrespective of their order.
2. Programming Style:
It is designed to provide information on how to effectively use
indentation, comments, and other elements that will make your
C code more readable.
Line Breaks :
• Rather than putting everything on one line, it is much more
readable to break up long lines so that each statement and
declaration goes on its own line.
For example
#include<stdio.h>
int main(void)
{
int revenue = 80;
int cost = 50;
int roi;
roi = (100 * (revenue - cost)) / cost;
if (roi > 0)
{
printf (“%dn”, roi);
}
return 0;
}
Indentation
• improves the readability of the code.
• It is mainly used for code inside looping statements, control
structures, functions etc.
• as good intended code is easy to maintain and is good
looking.
• It makes the code more readable and easy to understand.
Comments:
• Comments in code can be useful for a variety of purposes.
• Having good comments throughout your code will make it
much easier to remember what specific parts of your code do.
Execution Process of a C Program
• When we execute a C program it undergoes with the following
process…
Program consists of set of instructions written in a
programming language The job of a programmer is to write
and test the program.
There are 4 steps for converting a ‘C’ program into machine
language.
1) Writing and editing the program
2) Compiling the program
3) Linking the program
4) Executing the program
The Character set of ‘C’
C language consist of some characters set, numbers and some
special symbols. The character set of C consist of all the
alphabets of English language. C consist of
 Alphabets a to z, A to Z
 Numeric 0,1 to 9
 Special Symbols {,},[,],?,+,-,*,/,%,!,;,and more
The words formed from the character set are building
blocks of C and are sometimes known as tokens. These
tokens represent the individual entity of language.
The following different types of token are used in C
1) Identifiers 2)Keywords 3)Constants
4) Operators 5)Punctuation Symbols
C Tokens:
TOKEN is the smallest unit in a ‘C’ program. It is each and every word and
punctuation that you come across in your C program. The compiler breaks
a program into the smallest possible units (Tokens) and proceeds to the
various stages of the compilation.
• C Token is divided into six different types,.
• Keywords, Operators, Strings, Constants, Special Characters, and
Identifiers.
Keywords and Identifiers
• In ‘C’ every word can be either a keyword or an identifier.
• Keywords have fixed meanings, and the meaning cannot be changed. They
act as a building block of a ‘C’ program. There are a total of 32 keywords
in ‘C’. Keywords are written in lowercase letters.
• Following table represents the keywords in ‘C’
Keywords in C Programming Language
Auto double int struct
Break else long switch
Case enum register typedef
Char extern return union
Const short float unsigned
Continue for signed void
Default goto sizeof volatile
Do if static while
Identifier
• Identifiers in C are short and informative names that uniquely identify
variables or function names. These are user-defined words used for naming
of functions, variables, structures, unions, arrays etc.
• These can be composed of lowercase letters, uppercase letters, underscore
or digits but the first character should be either an underscore or an
alphabet.
 There are some defined rules in C language for declaring identifiers:
1. Identifiers shouldn't begin with any numerical digit and hence, the first
character must be either an underscore or an alphabet.
2. Identifiers are case-sensitive and hence, both lowercase and uppercase
letters are distinct.
3. The length of identifiers shouldn't be more than 31 characters.
4. Commas and blank spaces are not allowed while declaring an identifier.
 Also, the most important rule is that we can't use keywords as identifiers
because keywords in C language are reserved words for special purposes
only.
Some Valid Identifiers:
Ex: count1, _count, count123, count_123 etc.
Constants
• Constants are the variables whose values are fixed and
cannot be modified during the execution of a program, once
they are defined. They are also known as literals.
• The constant variables in C can be initialized only once and
their default value is zero. If we try to re-initialize or re-
define any constant variable, then we will get a compilation
error.
• We can declare constants in C language using
const keyword Here, we are using the const keyword to
declare a variable and assigning a value to it that cannot be
modified later.
• const var name = value;
Types of Constants in C Language
• Constant Example
• Integer constant 10, 20, 30 etc.
• Floating-point constant 10.2, 20.5, 30.6 etc.
• Octal constant 011, 022, 088 etc.
• Hexadecimal constant 0x1a, 0x4b, 0x6b, etc.
• Character constant 'x', 'y', 'z' etc.
• String constant "Java", "C++", "Python"
etc.
Variables
• Variables in a c programming language are the named
memory locations where the user can store different values
of the same datatype during the program execution.
• Variable is a name given to a memory location where we can
store different values of the same datatype during the
program execution.
• A variable name may contain letters, digits and underscore
symbol. The following are the rules to specify a variable
name...
• Variable name should not start with a digit.
• Keywords should not be used as variable names.
• A variable name should not contain any special symbols
except underscore(_).
• A variable name can be of any length but compiler considers
only the first 31 characters of the variable name.
Declaration of Variable
• Declaration of a variable tells the compiler to allocate the
required amount of memory with the specified variable name
and allows only specified data type values into that memory
location
Syntax of declaration:
Datatype variable name;
Ex: int num
 The above declaration tells to the compiler that allocates 2
bytes of memory with the
 name number and allows only integer values into that
memory location.
Initialization of a variable:
Datatype variable name= value;
int num=10;
Datatypes:
• The C language has a rich set of data types to handle different kind of
data entered by programmer. Basically a data type informs compiler
about how much memory a declared variable required to store input.
• Data types are used extensively for declaring variables and functions of
different types.
• There are three classes of data types:
 Primary Data types
 Derived Data types
 User Defined Data types
• Primary data types are those which are already defined in programming
also known in-built data types. These data types are basic building
blocks of any programming language and numerous composite data
types are constructed from this basic type.
Primary data Types include:
• Characters
• Integers
• Floating point
• Void
• Note: All memory size mentioned are of 32 bit system.
Character Types:
 A single character in C is of “char” type data. Characters stored in 8
bits or 1 byte of memory and the character can also be signed and
unsigned. Syntax.
 There’s no dedicated “character type” in C language. char is same as
integer type. char is just a smallest integer type. So, Like any other
integer type, it can be signed or unsigned.
 char 1 byte −128 to 127 %c
 signed char 1 byte −128 to 127 %c
 unsigned char 1 byte 0 to 255 %c
 Signed and unsigned char both used to store single character as per
their ASCII values. For example, ‘A’ will be stored as 65. we don’t need
to specify keyword ‘signed’ for using signed char.
 but keyword ‘unsigned’ for using unsigned char must be used.
 unsigned char name = ‘a’;
 ASCII table contains 128 character values from 0 to 127 and the
remaining 127 characters is known as extended ASCII character set.
Integer Types:
• As the name suggests used to hold only integer values. Generally,
an integer stores values in range limited from -2147483648 to
2147483647. A signed integer uses one bit for sign out of 16 bits.
• The integers classified into three classes of storage with both
signed and unsigned qualifier based on the range of values held
by them.
Datatype
size
Range
Format
specifier
short int
2
-32,768 to 32,767 %hd
unsigned short
int
2
0 to 65,535 %hu
unsigned int
4
0 to 4,294,967,295 %u
int
4
-2,147,483,648 to 2,147,483,647 %d
long int
4
-2,147,483,648 to 2,147,483,647 %ld
unsigned long int
4
0 to 4,294,967,295 %lu
Floating Point Types:
• Data type ‘float’ used to stores real numbers in 32 bits with up to 6 decimal
points precision. The double type also similar to float stores in 64 bits with more
accurate precision up to 14 decimals and Long double further extends the
precision using 80 bits and 19 decimal places.
Datatypes Size Range Limit Format Specifier
float 4 1.2E-38 to 3.4E+38 %f
double 8 2.3E-308 to 1.7E+308 %lf
long double 12 3.4E-4932 to 1.1E+4932 %Lf
Void Types
• Void is considered a data type (for organizational purposes) and usually specifies
function return type. Basically a ‘void’ keyword used as a place holder where
you would put a data type to represent “NO DATA”.
• Function with no return value and no arguments.
EX
void function(void);
1. Input – output statements
– Storing a value into memory is called ‘ input operation’.
– After executing the computations, the results are stored in memory and the
results can be displayed to the user by ‘output operation’
– All input / output operations in ‘C’ are performed using input / output functions
– The most common I/o functions are supplied as part of the ‘C’ standard I/O
library through the preprocessor directive # include<stdio.h>
– Most commonly used I/o functions are
(a) printf ( )
(b) scanf ( )
a) printf ( ) function:
Syntax:
printf(“format string”, print list);
e.g.: printf (“average of 3 numbers = %f”,avg);
* The printf ( ) function displays the value of its format string after
substituting the values of the expressions in the print list.
* It also replaces the escape sequences such as ‘n’ by their meanings.
b) scanf ( ) function
Syntax:
scanf (“format string”, input list);
• The scanf ( ) function copies into memory data typed at the keyboard by the
program user during program execution.
• The input list must be preceded by ampersand ( &)
2) Assignment statements
The assignment statements stores a value (or) a computational result in a variable
and is used to perform most arithmetic operations in a program.
Syntax: variable=expression
e.g.:
c = a+b;
avg = sum/3;
r1 = (b*b – 4 * a*c);
• The variable before the assignment operator is assigned the value of the
expression after it.
• The previous value of variable is destroyed
Example:
# include<stdio.h>
# include<conio.h>
# include<math.h>
main ( )
{
float a,b,c,r1,r2,D;
clrscr ( );
printf (“enter a,b,c values”);
scanf (“ %f %f %f”, &a, &b, &c);
D= b*b – a*b*c;
if (D>0)
{
r1 = -b+sqrt (D) / (2*a);
r2 = -b-sqrt (D) / (2*a);
printf (“Roots are real and are %f %f”, r1, r2);
}
else if (D= =0)
{
r1 = -b/(2*a);
r2 = -b/(2*a);
printf (“Roots are equal and are %f %f”, r1, r2);
}
else
printf(“Roots are imaginary”);
getch ( );
}
5) Testing
Case 1: Enter a,b,c values : 1 4 3
r1 = -1
r2 = -3
Case 2: Enter a,b,c values : 1 2 1
r1 = -1
r2 = -1
Case 3: Enter a,b,c values : 1 4 1
Roots are imaginary
Opeartors:
• C operators are one of the features in C which has symbols that can be
used to perform mathematical, relational, bitwise, conditional, or logical
manipulations. The ‘C’ programming language has a lot of built-in
operators to perform various tasks as per the need of the program.
(or)
• In other words, we can also say that an operator is a symbol that tells the
compiler to perform specific mathematical, conditional, or
logical functions.
Arithmetic Operator With Example
• Arithmetic Operators are the operators which are used to perform
mathematical calculations like addition (+), subtraction (-),
multiplication (*), division (/), and modulus (%). It performs all the
operations on numerical values (constants and variables).
Operator Description
+ Addition It adds two operands
-n subtraction It subtracts second operand from the first
*Multiplication It multiplies both operands
/Division It is responsible for dividing numerator by
the denomerator
%Modulo Division This operator gives the remainder of an
integer after division
Example:
#include <stdio.h>
int main()
{
int a = 7,b = 5, c;
c = a+b;
printf("a+b = %d n",c);
c = a-b;
printf("a-b = %d n",c);
c = a*b;
printf("a*b = %d n",c);
c = a/b;
printf("a/b = %d n",c);
c = a%b;
printf("Remainder when a is divided by b = %d n",c);
return 0;
}
Output:
a+b = 12
a-b = 2
a*b = 35
a/b = 1
Remainder when a divided by b = 2
Relational Operator With Example
• Relational operators are specifically used to compare two quantities or values in a
program. It checks the relationship between two operands. If the given relation is
true, it will return 1 and if the relation is false, then it will return 0. Relational
operators are heavily used in decision-making and performing loop operations
• Here, we assume that the variable A holds 15 and the variable B holds the 25.
Operator Description Example
== It is used to check if the values of the two
operands are equal or not. If the values of the
two operands are equal, then the condition
becomes true.
(A == B) is not true.
!= It is used to check if the values of the two
operands are equal or not. If the values are
not equal, then the condition becomes true.
(A != B) is true.
> It is used to check if the value of left operand
is greater than the value of right operand. If
the left operand is greater, then the condition
becomes true.
(A > B) is not true.
< It is used to check if the value of left operand
is less than the value of right operand. If the
left operand is lesser, then the condition
becomes true.
(A < B) is true.
>= It is used to check if the value of left operand
is greater than or equal to the value of right
operand. If the value of the left operand is
greater than or equal to the value, then the
condition becomes true.
(A >= B) is not true.
<= It is used to check if the value of left operand
is less than or equal to the value of right
operand. If the value of the left operand is
less than or equal to the value, then the
condition becomes true.
(A <= B) is true.
Example:
#include <stdio.h>
int main()
{
int x = 8, y = 10;
printf("%d == %d is : (%d) n", x, y, x == y);
printf("%d != %d is : (%d) n ", x, y, x != y);
printf("%d > %d is: (%d)n ", x, y, x > y);
printf("%d < %d is : (%d) n", x, y, x < y);
printf("%d >= %d is : (%d) n", x, y, x >= y);
printf("%d <= %d is : (%d) n", x, y, x <= y);
return 0;
}
Output:
8 == 10 is: (0)
8 != 10 is: (1)
8 > 10 is: (0)
8 < 10 is : (1)
8 >= 10 is :(0)
8 <=10 is :(1)
Logical operators
• In the C programming language, we have three logical operators when
we need to test more than one condition to make decisions.
These logical operators are:
 && (meaning logical AND)
 || (meaning logical OR)
 ! (meaning logical NOT)
• An expression containing a logical operator in C language returns either
0 or 1 depending upon the condition whether the expression results in
true or false.
Example:
The variable A holds 7 and variable B holds 3.
Operator Description Example
&&
This is the AND operator in C
programming language. It performs
logical conjunction of two
expressions. (If both expressions
evaluate to True, then the result is
True. If either of the expression
evaluates to False, then the result is
False)
((A==7) && (B>7)) equals to 0
||
It is the NOT operator in C
programming language. It performs a
logical disjunction on two
expressions. (If either or both of the
expressions evaluate to True, then the
result is True)
((A==7) || (B>7)) equals to 1
!=
It is the Logical NOT Operator in C
programming language. It is used to
reverse the logical state of its operand.
If a condition is true, then the Logical
NOT operator will make it false and
vice versa.
!(A && B) is true
Example:
#include <stdio.h>
int main()
{
int a = 15, b = 15, c = 20, results;
results = (a == b) && (c > b);
printf("(a == b) && (c > b) is %d n", results);
results = (a == b) && (c < b);
printf("(a == b) && (c < b) is %d n", results);
results = (a == b) || (c < b);
printf("(a == b) || (c < b) is %d n", results);
results = (a != b) || (c < b);
printf("(a != b) || (c < b) is %d n", results);
results = !(a != b);
printf("!(a != b) is %d n", results);
results = !(a == b);
printf("!(a == b) is %d n", results);
return 0;
}
Output:
(a == b) && (c > b) is 1
(a == b) && (c < b) is 0
(a == b) || (c < b) is 1
(a != b) || (c < b) is 0
!(a != b) is 1
!(a == b) is 0
1. Logical AND Operator 2. Logical OR Operator
X Y X&&Y
1 1 1
1 0 0
0 1 0
0 0 0
X Y X||Y
1 1 1
1 0 1
0 1 1
0 0 0
3. Logical NOT Operator
X !X
0 1
1 0
Increment/Decrement Operator With Example
• C programming has basically two operators which can increment ++ and decrement --
the value of a variable. It can change the value of an operand (constant or variable) by 1.
Increment and Decrement Operators are very useful operators that are generally used to
minimize the calculation. These two operators are unary operators, which means they can
only operate on a single operand. For example, ++x and x++ means x=x+1 or --x and
x−− means x=x-1.
• If we use the operator as a pre-fix, it adds 1 to the operand, and the result is assigned to
the variable on the left. Whereas, when it is used as a post-fix, it first assigns the value to
the variable on the left i.e., it first returns the original value, and then the operand is
incremented by 1.
Operator Description
++
This increment operator increases the
integer value by 1.
--
This decrement operator decreases the
integer value by 1.
Example:
#include <stdio.h>
int main()
{
int a = 11, b = 90;
float c = 100.5, d = 10.5;
printf("++a = %d n", ++a);
printf("--b = %d n", --b);
printf("++c = %f n", ++c);
printf("--d = %f n", --d);
return 0;
}
Output:
++a = 12
--b = 89
++c = 101.500000
--d = 9.500000
Assignment Operator
• An assignment operator is mainly responsible for assigning a value
to a variable in a program. Assignment operators are applied to
assign the result of an expression to a variable. This operator plays
a crucial role in assigning the values to any variable. The most
common assignment operator is =.
• C language has a collection of shorthand assignment operators that
can be used for C programming.
Operator Description Example
= Used to assign the values from right side of the
operands to left side of the operand
C = A + B will assign the
value of A + B to C.
+= Adds the value of the right operand to the value of
the left operand and assigns the result to the left
operand.
C += A is same as C = C + A
-= Subtracts the value of the right operand from the
value of the left operand and assigns the result to
the left operand.
C -= A is same as C = C - A
*= Multiplies the value of the right operand with the
value of the left operand and assigns the result to
the left operand.
C *= A is same as C = C * A
/= Divides the value of the left operand with the value
of the right operand and assigns the result to the left
operand.
C /= A is same as C = C / A
%= Takes modulus using the values of the two operands
and assigns the result to the left operand.
C %= A is same as
C = C % A
Example:
#include <stdio.h>
int main()
{
int a = 7, b;
b = a; // b is 7
printf("b = %dn", b);
b += a; // b is 14
printf("b = %dn", b);
b -= a; // b is 7
printf("b = %dn", b);
b *= a; // b is 49
printf("b = %dn", b);
b /= a; // b is 7
printf("c = %dn", c);
b %= a; // b = 0
printf("b = %dn", b);
return 0;
}
Output:
b = 7
b = 14
b = 7
b = 49
b = 7
b = 0
Bitwise Operators:
• Bitwise operators are the operators which work on bits and perform the bit-by-bit
operation. Mathematical operations like addition, subtraction, multiplication,
division, etc are converted to bit-level which makes processing faster and easier
to implement during computation and compiling of the program.
• Bitwise operators are especially used in C programming for performing bit-level
operations. C programming language supports a special operator for bit operation
between two variables.
• The truth tables for &, |, and ^
p q p & q p | q p ^ q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
Here, we will assume that A = 50 and B = 25 in binary format as
follows.
A = 00110010
B = 00011001
-----------------
A&B = 00010000
A|B = 00111011
A^B = 00101011
~A = 11001101
Assume variable 'A' holds 50 and variable 'B' holds 25.
Operator Description Example
& Binary AND Operator.
It copies a bit to the result if it exists in both the
operands.
(A & B) = 16, i.e. 00010000
| Binary OR Operator.
It copies a bit if and only if it exists in either operand.
(A | B) = 59, i.e. 00111011
^
Binary XOR Operator.
It copies the bit only if it is set in one operand but not
both.
(A ^ B) = 43, i.e. 00101011
~ Binary One's Complement Operator.
It is unary and has the effect of 'flipping' bits.
(~A ) = ~(50), i.e,. -0111101
<< Binary Left Shift Operator.
The value of the left operands is moved left by the
number of bits specified by the right operand.
A << 2 = 200 i.e. 11001000
>> Binary Right Shift Operator.
The value of the left operands is moved right by the
number of bits specified by the right operand.
A >> 2 = 12 i.e., 00001100
Conditional operators
• It is also known as shorthand or ternary operator or condensed if-
then-else. 2 It is called ternary because it uses three expressions. The
two symbols that are used to denote this operator are the question mark
(?) and colon (;) & Three expressions are placed as given below,
i. The first expression is placed before.
ii. The second expression is placed between? and: Here the value of
expression2 may be a constant value or variable.
• iii. The third and final expression is placed after the symbol: Here the
value of expression2 may be a constant value or variable.
Syntax :
Condition or Expression1_to_evaluate ? Expression2: Expression3;
• If the expression being evaluated is true then expression 2 will be
performed otherwise expression 3 will be performed.
Ex. 1) To find the largest of two numbers
Large a > b? a : b;
 If the value of variable a; is greater than b then value of a; is assigned to the
variable large otherwise the value of b will be assigned.
2) To check the number of days in the month of February
 days = ( year % 40) ? 29: 28;
3) Ternary operator can be nested (One within another). For example, consider the
following example to find the largest of the three numbers
 Large = (( a > b && a>c) ? a :((b>c) ? b : c)); Here, expression 3 itself evaluate
another condition.
Conditional operators:
It is also known as shorthand or ternary operator or condensed if-then-else. 2 It
is called ternary because it uses three expressions. The two symbols that are used to
denote this operator are the question mark (?) and colon (;) & Three expressions are
placed as given below,
i. The first expression is placed before.
ii. The second expression is placed between? and: Here the value of expression2
may be a constant value or variable.
iii. The third and final expression is placed after the symbol: Here the value of
expression2 may be a constant value or variable.
Syntax : Condition or Expression1_to_evaluate ? Expression2: Expression3;
If the expression being evaluated is true then expression 2 will be performed otherwise
expression 3 will be performed.
Ex. 1) To find the largest of two numbers
 Large a > b? a : b;
• If the value of variable a; is greater than b then value of a; is assigned to the variable
large otherwise the value of b will be assigned.
2) Ternary operator can be nested (One within another). For example, consider the following
example to find the largest of the three numbers
 Large = (( a > b && a>c) ? a :((b>c) ? b : c)); Here, expression 3 itself evaluate another
condition.
 special operations
Some of the special operations are coma, ampersand(&amp;), size of operators.
a) coma: ( , )
It is used as separator for variables
eg; a=10, b=20
b) Address:(&amp;)
It is used to get the address of a variables.
C) Sizeof ( ) :
It is used to get the size of a data type of a variable in bytes.
sizeof Operator :
• Unary operator sizeof is used to find the number of bytes occupied by the operand. It is
generally used in structure and union to know the number of bytes occupied by structure
and union instead of calculating the length manually. It will help during the portability
of code when data types vary in length from compiles to compiles.
Syntax : sizeof(operand)
• Where operand may be basic or user-defined data type or constant value.
Ex:
1) c=sizeof(int);
On execution, the value of c contains 2, an integer data type required two bytes to
store the value.
2) c=sizeof(float);
On execution, the value of c contains 4, as float data type required two bytes to store value.
3) c = sizeof(67);
On execution, the value contains 2, an integer constant that required two bytes to store
value.
4) c = sizeof(6.7);
On execution the value of c contains 8, as value 6.7 by default is considered as double
rather than float data type and hence it occupies eight bytes to store value.
5) c = sizeof(“abcde”)
O/p : 5 (It returns the number of characters enclosed within).
Program:
main ( )
{
int a=10;
float b=20 ;
printf (“ a= %d b=%f’, a,b );
printf (“ Adderss of a =%u “ &a ) ;
printf (“ Adderss of b =%u” &b ) ;
printf (“Size of a = %d ” , sizeof (a) ) ;
printf ( “ Size of b = %d ” . sizeof (b) ) ;
}
Expression Evaluation
• Expressions are evaluated using an assignment statement of the form:
• In the above syntax, variable is any valid C variable name. When the statement
like the above form is encountered, the expression is evaluated first and then
the value is assigned to the variable on the left hand side.
Arithmetic Expressions:
• The arithmetic expression is evaluated in specific order considering the
operator's precedence, and the result of an expression will be based on the type
of variable.
Example:
 From the given above arithmetic expressions example, let's consider the
value
 operands to be a=2,b=3, and c=4.
 After substitution of values into the given expression, we get:
 Z = 2 + 3 - (2 * 4)
Below given steps are in order of precedence in which the operators of an
expression must be evaluated.
Step1: First, Priority is given to the parenthesis, so whatever is inside the
parenthesis will be evaluated first, a*c (2*4 = 8).
Step 2: The next level of precedence is given to + and - operator, so next a+b (2+3
= 5) is evaluated
Step 3: a+b will be subtracted from a*c i.e; 5 - 8 = -3.
Step 4: The final value after evaluation of an arithmetic expression will be stored
into the variable Z, according to the example value three is stored into the variable
Z that is z = -3
Look at the below-given figure to understand the
arithmetic expressions evaluation in a better way.
Example 1 Program Using Arithmetic Expressions:
#include <stdio.h>
void main()
{
int a=2,b=3,c=4,z;
z=a+b-(a*c);
printf("Result= %d",z);
}
Output:
Example 2:
Operator Precedence and Associativity
• Every C operator has a precedence (priority) associated with it.
This precedence is used to determine how an expression
involving more than one operator is evaluated.
• There are different levels of operator precedence and an operator
may belong to one of these levels. The operators at the higher
level of precedence are evaluated first. The operators in the same
level of precedence are evaluated from left to right or from right
to left, based on the associativity property of an operator.
• The precedence and associativity of various operators in C are as
given below:
• The precedence and associativity of various operators in C are
as given below:
 In a c programming language, the data conversion is performed in two
different methods as follows...
1. Type Conversion
2. Type Casting
Type Conversion(Implicit Typecasting)
 The type conversion is the process of converting a data value from one data
type to another data type automatically by the compiler. Sometimes type
conversion is also called implicit type conversion. The implicit type
conversion is automatically performed by the compiler.
 For example, in c programming language, when we assign an integer value
to a float variable the integer value automatically gets converted to float
value by adding decimal value 0. And when a float value is assigned to an
integer variable the float value automatically gets converted to an integer
value by removing the decimal value.
int i=10;
float x=15.5;
Char ch=’A’;
#include<stdio.h>
#include<conio.h>
void main(){
int i = 95 ;
float x = 90.99 ;
char ch = 'A' ;
i = x ;
printf("i value is %dn",i);
x = i ;
printf("x value is %fn",x);
i = ch ;
printf("i value is %dn",i);
}
Typecasting
• When you convert the data type of a variable to another data type then
this technique is known as typecasting.
• Let’s say that you want to store a value of int data type into a variable of
float data type.
• Then you can easily do this with the help of typecasting. It is one of the
important concepts of the C programming language.
Explicit Type Casting
• It is not like implicit type casting where the data type is converted
automatically. In Explicit type casting, you have to force the conversion
between data types.
Syntax:-
(data_type_name) expression
• data_type_name is the name of the data type to which you want to
convert to.
• The expression can be a variable, an actual expression or a constant.
Example:
#include<stdio.h>
int main()
{
float val = 56.3;
int a = (int)val + 50; // explicit type casting
printf("TechVidvan Tutorial: Explicit Type
Casting!n");
printf("Value of val: %fn", val);
printf("Value of a: %dn",a);
return 0;
}

More Related Content

Similar to Unit-1 (introduction to c language).pptx

67404923-C-Programming-Tutorials-Doc.pdf
67404923-C-Programming-Tutorials-Doc.pdf67404923-C-Programming-Tutorials-Doc.pdf
67404923-C-Programming-Tutorials-Doc.pdf
Rajb54
 
Introduction to the c programming language (amazing and easy book for beginners)
Introduction to the c programming language (amazing and easy book for beginners)Introduction to the c programming language (amazing and easy book for beginners)
Introduction to the c programming language (amazing and easy book for beginners)
mujeeb memon
 

Similar to Unit-1 (introduction to c language).pptx (20)

Unit-2.pptx
Unit-2.pptxUnit-2.pptx
Unit-2.pptx
 
Structure
StructureStructure
Structure
 
Learning the C Language
Learning the C LanguageLearning the C Language
Learning the C Language
 
C programming notes BATRACOMPUTER CENTRE IN Ambala CANTT
C programming notes BATRACOMPUTER CENTRE IN Ambala CANTTC programming notes BATRACOMPUTER CENTRE IN Ambala CANTT
C programming notes BATRACOMPUTER CENTRE IN Ambala CANTT
 
Copy of UNIT 2 -- Basics Of Programming.pptx
Copy of UNIT 2 -- Basics Of Programming.pptxCopy of UNIT 2 -- Basics Of Programming.pptx
Copy of UNIT 2 -- Basics Of Programming.pptx
 
Unit-2_Getting Started With ‘C’ Language (3).pptx
Unit-2_Getting Started With ‘C’ Language (3).pptxUnit-2_Getting Started With ‘C’ Language (3).pptx
Unit-2_Getting Started With ‘C’ Language (3).pptx
 
358 33 powerpoint-slides_1-introduction-c_chapter-1
358 33 powerpoint-slides_1-introduction-c_chapter-1358 33 powerpoint-slides_1-introduction-c_chapter-1
358 33 powerpoint-slides_1-introduction-c_chapter-1
 
C Language
C LanguageC Language
C Language
 
67404923-C-Programming-Tutorials-Doc.pdf
67404923-C-Programming-Tutorials-Doc.pdf67404923-C-Programming-Tutorials-Doc.pdf
67404923-C-Programming-Tutorials-Doc.pdf
 
C programming
C programmingC programming
C programming
 
Unit 2 introduction to c programming
Unit 2   introduction to c programmingUnit 2   introduction to c programming
Unit 2 introduction to c programming
 
C programming
C programmingC programming
C programming
 
C language ppt
C language pptC language ppt
C language ppt
 
Introduction of c programming unit-ii ppt
Introduction of  c programming unit-ii pptIntroduction of  c programming unit-ii ppt
Introduction of c programming unit-ii ppt
 
C programming slide day 01 uploadd by md abdullah al shakil
C programming slide day 01 uploadd by md abdullah al shakilC programming slide day 01 uploadd by md abdullah al shakil
C programming slide day 01 uploadd by md abdullah al shakil
 
C programming
C programmingC programming
C programming
 
Rr
RrRr
Rr
 
Introduction to the c programming language (amazing and easy book for beginners)
Introduction to the c programming language (amazing and easy book for beginners)Introduction to the c programming language (amazing and easy book for beginners)
Introduction to the c programming language (amazing and easy book for beginners)
 
C pdf
C pdfC pdf
C pdf
 
UNIT 1 NOTES.docx
UNIT 1 NOTES.docxUNIT 1 NOTES.docx
UNIT 1 NOTES.docx
 

Recently uploaded

Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
VictoriaMetrics
 

Recently uploaded (20)

Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
 
WSO2CON 2024 - Lessons from the Field: Legacy Platforms – It's Time to Let Go...
WSO2CON 2024 - Lessons from the Field: Legacy Platforms – It's Time to Let Go...WSO2CON 2024 - Lessons from the Field: Legacy Platforms – It's Time to Let Go...
WSO2CON 2024 - Lessons from the Field: Legacy Platforms – It's Time to Let Go...
 
WSO2Con2024 - Organization Management: The Revolution in B2B CIAM
WSO2Con2024 - Organization Management: The Revolution in B2B CIAMWSO2Con2024 - Organization Management: The Revolution in B2B CIAM
WSO2Con2024 - Organization Management: The Revolution in B2B CIAM
 
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public AdministrationWSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
 
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...
 
WSO2CON 2024 - Unlocking the Identity: Embracing CIAM 2.0 for a Competitive A...
WSO2CON 2024 - Unlocking the Identity: Embracing CIAM 2.0 for a Competitive A...WSO2CON 2024 - Unlocking the Identity: Embracing CIAM 2.0 for a Competitive A...
WSO2CON 2024 - Unlocking the Identity: Embracing CIAM 2.0 for a Competitive A...
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
WSO2Con2024 - Software Delivery in Hybrid Environments
WSO2Con2024 - Software Delivery in Hybrid EnvironmentsWSO2Con2024 - Software Delivery in Hybrid Environments
WSO2Con2024 - Software Delivery in Hybrid Environments
 
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public AdministrationWSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
WSO2CON 2024 - How CSI Piemonte Is Apifying the Public Administration
 
AzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdf
AzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdfAzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdf
AzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdf
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!
WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!
WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 

Unit-1 (introduction to c language).pptx

  • 1. Unit-1 INTRODUCTION TO ‘C’ LANGUAGE • ‘C’ is a high–level programming language developed in 1972 by Dennis Ritchie • at AT &amp; T Bell Laboratories. • It was primarily developed for system programming i.e. for designing operating • systems, compilers etc.
  • 2. Importanceof’C’Language • It is a robust language, whose rich set of built-in functions and operations can be used to write any complex program. • It is a middle level language because the ‘C’ compiler combines the capabilities of anassembly language with the features of a high-level language and therefore it is well • suited for writing both system software and business packages. • ‘C’ Programs are efficient and fast. • C is highly portable, that C’ programs written on one computer can be run on another with little (or) no modification. • ‘C’ Language is well suited for structured programming, thus requiring the user to think of a problem in terms of function modules (or) blocks. • ‘C’ program has the ability to extend itself. It was named ‘C’ because it is an offspring of BCPL(BasicCombinedProgrammingLanguage) which was popularly called ‘B’ language.
  • 3. 1.Basic structure of C programs: • Almost everything has a definite structure. Likewise, in the case of programming languages, all of them have a definite structure. These structures have to be followed while writing the code. • The structure of a C program can be mainly divided into six parts, each having its purpose • It makes the program easy to read, easy to modify, easy to document, and makes it consistent in format.
  • 4. Section Description Documentatio n Consists of the description of the program, programmer's name, and creation date. These are generally written in the form of comments. Link All header files are included in this section which contains different functions from the libraries. A copy of these header files is inserted into your code before compilation. Definition Includes preprocessor directive, which contains symbolic constants. E.g.: #defineallows us to use constants in our code. It replaces all the constants with its value in the code. Global Declaration Includes declaration of global variables, function declarations, static global variables, and functions. Main() Function For every C program, the execution starts from the main() function. It is mandatory to include a main() function in every C program. Subprograms Includes all user-defined functions (functions the user provides). They can contain the inbuilt functions and the function definitions declared in the Global Declaration section. These are called in the main() function. Basic Structure of the C Program
  • 5. EX: Write a C program to find circumference of circle. /* Author : Ramu Aim : Program for finding circumference of circle*/ #include<stdio.h> #include<conio.h> #define PI 3.1415 int main ( ) { float c, r; printf (“enter radius of circle”); scanf (“%f”, &r); c = 2 * PI * r; printf (“Circumference = %f”, c); return 0; }
  • 6. Documentation • In a C program, single-line comments can be written using two forward slashes i.e., //. • and we can create multi-line comments using /* */. Here, we've used multi-line comments. /** * file: area.c * author: you * description: program to find circumference of circle. */
  • 7. Link • All header files are included in this section. • A header file is a file that consists of C declarations that can be used between different files. It helps us in using others' code in our files. A copy of these header files is inserted into your code before compilation. ex: #include <stdio.h>
  • 8. Definition • A preprocessor directive in C is any statement that begins with the "#" symbol. • The #define is a preprocessor compiler directive used to create constants • #define is typically used to make a source program easy to modify and compile in different execution environments. • The define statement does not ends with a semicolon. EX: #define PI 3.1415
  • 9. Global Declaration • This section includes all global variables, function declarations, and static variables. • The variables declared in this section can be used anywhere in the program. • They're accessible to all the functions of the program. Hence, they are called global variables. • There is no global declaration in the above program.
  • 10. main() Function • In the structure of a C program, this section contains the main function of the code. • The compiler starts execution from the main() function. It can use global variables, static variables, inbuilt functions, and user-defined functions. • The return type of the main() function can be void and also not necessarily int.
  • 11. Subprograms • This includes the user-defined functions called in the main() function. User-defined functions are generally • written after the main() function irrespective of their order. 2. Programming Style: It is designed to provide information on how to effectively use indentation, comments, and other elements that will make your C code more readable. Line Breaks : • Rather than putting everything on one line, it is much more readable to break up long lines so that each statement and declaration goes on its own line.
  • 12. For example #include<stdio.h> int main(void) { int revenue = 80; int cost = 50; int roi; roi = (100 * (revenue - cost)) / cost; if (roi > 0) { printf (“%dn”, roi); } return 0; }
  • 13. Indentation • improves the readability of the code. • It is mainly used for code inside looping statements, control structures, functions etc. • as good intended code is easy to maintain and is good looking. • It makes the code more readable and easy to understand. Comments: • Comments in code can be useful for a variety of purposes. • Having good comments throughout your code will make it much easier to remember what specific parts of your code do.
  • 14. Execution Process of a C Program • When we execute a C program it undergoes with the following process…
  • 15. Program consists of set of instructions written in a programming language The job of a programmer is to write and test the program. There are 4 steps for converting a ‘C’ program into machine language. 1) Writing and editing the program 2) Compiling the program 3) Linking the program 4) Executing the program
  • 16. The Character set of ‘C’ C language consist of some characters set, numbers and some special symbols. The character set of C consist of all the alphabets of English language. C consist of  Alphabets a to z, A to Z  Numeric 0,1 to 9  Special Symbols {,},[,],?,+,-,*,/,%,!,;,and more The words formed from the character set are building blocks of C and are sometimes known as tokens. These tokens represent the individual entity of language. The following different types of token are used in C 1) Identifiers 2)Keywords 3)Constants 4) Operators 5)Punctuation Symbols
  • 17. C Tokens: TOKEN is the smallest unit in a ‘C’ program. It is each and every word and punctuation that you come across in your C program. The compiler breaks a program into the smallest possible units (Tokens) and proceeds to the various stages of the compilation. • C Token is divided into six different types,. • Keywords, Operators, Strings, Constants, Special Characters, and Identifiers. Keywords and Identifiers • In ‘C’ every word can be either a keyword or an identifier. • Keywords have fixed meanings, and the meaning cannot be changed. They act as a building block of a ‘C’ program. There are a total of 32 keywords in ‘C’. Keywords are written in lowercase letters. • Following table represents the keywords in ‘C’
  • 18. Keywords in C Programming Language Auto double int struct Break else long switch Case enum register typedef Char extern return union Const short float unsigned Continue for signed void Default goto sizeof volatile Do if static while
  • 19. Identifier • Identifiers in C are short and informative names that uniquely identify variables or function names. These are user-defined words used for naming of functions, variables, structures, unions, arrays etc. • These can be composed of lowercase letters, uppercase letters, underscore or digits but the first character should be either an underscore or an alphabet.  There are some defined rules in C language for declaring identifiers: 1. Identifiers shouldn't begin with any numerical digit and hence, the first character must be either an underscore or an alphabet. 2. Identifiers are case-sensitive and hence, both lowercase and uppercase letters are distinct. 3. The length of identifiers shouldn't be more than 31 characters. 4. Commas and blank spaces are not allowed while declaring an identifier.  Also, the most important rule is that we can't use keywords as identifiers because keywords in C language are reserved words for special purposes only. Some Valid Identifiers: Ex: count1, _count, count123, count_123 etc.
  • 20. Constants • Constants are the variables whose values are fixed and cannot be modified during the execution of a program, once they are defined. They are also known as literals. • The constant variables in C can be initialized only once and their default value is zero. If we try to re-initialize or re- define any constant variable, then we will get a compilation error. • We can declare constants in C language using const keyword Here, we are using the const keyword to declare a variable and assigning a value to it that cannot be modified later. • const var name = value;
  • 21. Types of Constants in C Language • Constant Example • Integer constant 10, 20, 30 etc. • Floating-point constant 10.2, 20.5, 30.6 etc. • Octal constant 011, 022, 088 etc. • Hexadecimal constant 0x1a, 0x4b, 0x6b, etc. • Character constant 'x', 'y', 'z' etc. • String constant "Java", "C++", "Python" etc.
  • 22. Variables • Variables in a c programming language are the named memory locations where the user can store different values of the same datatype during the program execution. • Variable is a name given to a memory location where we can store different values of the same datatype during the program execution. • A variable name may contain letters, digits and underscore symbol. The following are the rules to specify a variable name... • Variable name should not start with a digit. • Keywords should not be used as variable names. • A variable name should not contain any special symbols except underscore(_). • A variable name can be of any length but compiler considers only the first 31 characters of the variable name.
  • 23. Declaration of Variable • Declaration of a variable tells the compiler to allocate the required amount of memory with the specified variable name and allows only specified data type values into that memory location Syntax of declaration: Datatype variable name; Ex: int num  The above declaration tells to the compiler that allocates 2 bytes of memory with the  name number and allows only integer values into that memory location. Initialization of a variable: Datatype variable name= value; int num=10;
  • 24. Datatypes: • The C language has a rich set of data types to handle different kind of data entered by programmer. Basically a data type informs compiler about how much memory a declared variable required to store input. • Data types are used extensively for declaring variables and functions of different types. • There are three classes of data types:  Primary Data types  Derived Data types  User Defined Data types • Primary data types are those which are already defined in programming also known in-built data types. These data types are basic building blocks of any programming language and numerous composite data types are constructed from this basic type.
  • 25. Primary data Types include: • Characters • Integers • Floating point • Void • Note: All memory size mentioned are of 32 bit system.
  • 26. Character Types:  A single character in C is of “char” type data. Characters stored in 8 bits or 1 byte of memory and the character can also be signed and unsigned. Syntax.  There’s no dedicated “character type” in C language. char is same as integer type. char is just a smallest integer type. So, Like any other integer type, it can be signed or unsigned.  char 1 byte −128 to 127 %c  signed char 1 byte −128 to 127 %c  unsigned char 1 byte 0 to 255 %c  Signed and unsigned char both used to store single character as per their ASCII values. For example, ‘A’ will be stored as 65. we don’t need to specify keyword ‘signed’ for using signed char.  but keyword ‘unsigned’ for using unsigned char must be used.  unsigned char name = ‘a’;  ASCII table contains 128 character values from 0 to 127 and the remaining 127 characters is known as extended ASCII character set.
  • 27. Integer Types: • As the name suggests used to hold only integer values. Generally, an integer stores values in range limited from -2147483648 to 2147483647. A signed integer uses one bit for sign out of 16 bits. • The integers classified into three classes of storage with both signed and unsigned qualifier based on the range of values held by them. Datatype size Range Format specifier short int 2 -32,768 to 32,767 %hd unsigned short int 2 0 to 65,535 %hu unsigned int 4 0 to 4,294,967,295 %u int 4 -2,147,483,648 to 2,147,483,647 %d long int 4 -2,147,483,648 to 2,147,483,647 %ld unsigned long int 4 0 to 4,294,967,295 %lu
  • 28. Floating Point Types: • Data type ‘float’ used to stores real numbers in 32 bits with up to 6 decimal points precision. The double type also similar to float stores in 64 bits with more accurate precision up to 14 decimals and Long double further extends the precision using 80 bits and 19 decimal places. Datatypes Size Range Limit Format Specifier float 4 1.2E-38 to 3.4E+38 %f double 8 2.3E-308 to 1.7E+308 %lf long double 12 3.4E-4932 to 1.1E+4932 %Lf Void Types • Void is considered a data type (for organizational purposes) and usually specifies function return type. Basically a ‘void’ keyword used as a place holder where you would put a data type to represent “NO DATA”. • Function with no return value and no arguments. EX void function(void);
  • 29. 1. Input – output statements – Storing a value into memory is called ‘ input operation’. – After executing the computations, the results are stored in memory and the results can be displayed to the user by ‘output operation’ – All input / output operations in ‘C’ are performed using input / output functions – The most common I/o functions are supplied as part of the ‘C’ standard I/O library through the preprocessor directive # include<stdio.h> – Most commonly used I/o functions are (a) printf ( ) (b) scanf ( ) a) printf ( ) function: Syntax: printf(“format string”, print list); e.g.: printf (“average of 3 numbers = %f”,avg); * The printf ( ) function displays the value of its format string after substituting the values of the expressions in the print list. * It also replaces the escape sequences such as ‘n’ by their meanings.
  • 30. b) scanf ( ) function Syntax: scanf (“format string”, input list); • The scanf ( ) function copies into memory data typed at the keyboard by the program user during program execution. • The input list must be preceded by ampersand ( &) 2) Assignment statements The assignment statements stores a value (or) a computational result in a variable and is used to perform most arithmetic operations in a program. Syntax: variable=expression e.g.: c = a+b; avg = sum/3; r1 = (b*b – 4 * a*c); • The variable before the assignment operator is assigned the value of the expression after it. • The previous value of variable is destroyed
  • 31. Example: # include<stdio.h> # include<conio.h> # include<math.h> main ( ) { float a,b,c,r1,r2,D; clrscr ( ); printf (“enter a,b,c values”); scanf (“ %f %f %f”, &a, &b, &c); D= b*b – a*b*c; if (D>0) { r1 = -b+sqrt (D) / (2*a); r2 = -b-sqrt (D) / (2*a); printf (“Roots are real and are %f %f”, r1, r2); }
  • 32. else if (D= =0) { r1 = -b/(2*a); r2 = -b/(2*a); printf (“Roots are equal and are %f %f”, r1, r2); } else printf(“Roots are imaginary”); getch ( ); } 5) Testing Case 1: Enter a,b,c values : 1 4 3 r1 = -1 r2 = -3 Case 2: Enter a,b,c values : 1 2 1 r1 = -1 r2 = -1 Case 3: Enter a,b,c values : 1 4 1 Roots are imaginary
  • 33. Opeartors: • C operators are one of the features in C which has symbols that can be used to perform mathematical, relational, bitwise, conditional, or logical manipulations. The ‘C’ programming language has a lot of built-in operators to perform various tasks as per the need of the program. (or) • In other words, we can also say that an operator is a symbol that tells the compiler to perform specific mathematical, conditional, or logical functions. Arithmetic Operator With Example • Arithmetic Operators are the operators which are used to perform mathematical calculations like addition (+), subtraction (-), multiplication (*), division (/), and modulus (%). It performs all the operations on numerical values (constants and variables).
  • 34. Operator Description + Addition It adds two operands -n subtraction It subtracts second operand from the first *Multiplication It multiplies both operands /Division It is responsible for dividing numerator by the denomerator %Modulo Division This operator gives the remainder of an integer after division
  • 35. Example: #include <stdio.h> int main() { int a = 7,b = 5, c; c = a+b; printf("a+b = %d n",c); c = a-b; printf("a-b = %d n",c); c = a*b; printf("a*b = %d n",c); c = a/b; printf("a/b = %d n",c); c = a%b; printf("Remainder when a is divided by b = %d n",c); return 0; }
  • 36. Output: a+b = 12 a-b = 2 a*b = 35 a/b = 1 Remainder when a divided by b = 2 Relational Operator With Example • Relational operators are specifically used to compare two quantities or values in a program. It checks the relationship between two operands. If the given relation is true, it will return 1 and if the relation is false, then it will return 0. Relational operators are heavily used in decision-making and performing loop operations • Here, we assume that the variable A holds 15 and the variable B holds the 25.
  • 37. Operator Description Example == It is used to check if the values of the two operands are equal or not. If the values of the two operands are equal, then the condition becomes true. (A == B) is not true. != It is used to check if the values of the two operands are equal or not. If the values are not equal, then the condition becomes true. (A != B) is true. > It is used to check if the value of left operand is greater than the value of right operand. If the left operand is greater, then the condition becomes true. (A > B) is not true. < It is used to check if the value of left operand is less than the value of right operand. If the left operand is lesser, then the condition becomes true. (A < B) is true. >= It is used to check if the value of left operand is greater than or equal to the value of right operand. If the value of the left operand is greater than or equal to the value, then the condition becomes true. (A >= B) is not true. <= It is used to check if the value of left operand is less than or equal to the value of right operand. If the value of the left operand is less than or equal to the value, then the condition becomes true. (A <= B) is true.
  • 38. Example: #include <stdio.h> int main() { int x = 8, y = 10; printf("%d == %d is : (%d) n", x, y, x == y); printf("%d != %d is : (%d) n ", x, y, x != y); printf("%d > %d is: (%d)n ", x, y, x > y); printf("%d < %d is : (%d) n", x, y, x < y); printf("%d >= %d is : (%d) n", x, y, x >= y); printf("%d <= %d is : (%d) n", x, y, x <= y); return 0; } Output: 8 == 10 is: (0) 8 != 10 is: (1) 8 > 10 is: (0) 8 < 10 is : (1) 8 >= 10 is :(0) 8 <=10 is :(1)
  • 39. Logical operators • In the C programming language, we have three logical operators when we need to test more than one condition to make decisions. These logical operators are:  && (meaning logical AND)  || (meaning logical OR)  ! (meaning logical NOT) • An expression containing a logical operator in C language returns either 0 or 1 depending upon the condition whether the expression results in true or false. Example: The variable A holds 7 and variable B holds 3.
  • 40. Operator Description Example && This is the AND operator in C programming language. It performs logical conjunction of two expressions. (If both expressions evaluate to True, then the result is True. If either of the expression evaluates to False, then the result is False) ((A==7) && (B>7)) equals to 0 || It is the NOT operator in C programming language. It performs a logical disjunction on two expressions. (If either or both of the expressions evaluate to True, then the result is True) ((A==7) || (B>7)) equals to 1 != It is the Logical NOT Operator in C programming language. It is used to reverse the logical state of its operand. If a condition is true, then the Logical NOT operator will make it false and vice versa. !(A && B) is true
  • 41. Example: #include <stdio.h> int main() { int a = 15, b = 15, c = 20, results; results = (a == b) && (c > b); printf("(a == b) && (c > b) is %d n", results); results = (a == b) && (c < b); printf("(a == b) && (c < b) is %d n", results); results = (a == b) || (c < b); printf("(a == b) || (c < b) is %d n", results); results = (a != b) || (c < b); printf("(a != b) || (c < b) is %d n", results); results = !(a != b); printf("!(a != b) is %d n", results); results = !(a == b); printf("!(a == b) is %d n", results); return 0; } Output: (a == b) && (c > b) is 1 (a == b) && (c < b) is 0 (a == b) || (c < b) is 1 (a != b) || (c < b) is 0 !(a != b) is 1 !(a == b) is 0
  • 42. 1. Logical AND Operator 2. Logical OR Operator X Y X&&Y 1 1 1 1 0 0 0 1 0 0 0 0 X Y X||Y 1 1 1 1 0 1 0 1 1 0 0 0 3. Logical NOT Operator X !X 0 1 1 0
  • 43. Increment/Decrement Operator With Example • C programming has basically two operators which can increment ++ and decrement -- the value of a variable. It can change the value of an operand (constant or variable) by 1. Increment and Decrement Operators are very useful operators that are generally used to minimize the calculation. These two operators are unary operators, which means they can only operate on a single operand. For example, ++x and x++ means x=x+1 or --x and x−− means x=x-1. • If we use the operator as a pre-fix, it adds 1 to the operand, and the result is assigned to the variable on the left. Whereas, when it is used as a post-fix, it first assigns the value to the variable on the left i.e., it first returns the original value, and then the operand is incremented by 1. Operator Description ++ This increment operator increases the integer value by 1. -- This decrement operator decreases the integer value by 1.
  • 44. Example: #include <stdio.h> int main() { int a = 11, b = 90; float c = 100.5, d = 10.5; printf("++a = %d n", ++a); printf("--b = %d n", --b); printf("++c = %f n", ++c); printf("--d = %f n", --d); return 0; } Output: ++a = 12 --b = 89 ++c = 101.500000 --d = 9.500000
  • 45. Assignment Operator • An assignment operator is mainly responsible for assigning a value to a variable in a program. Assignment operators are applied to assign the result of an expression to a variable. This operator plays a crucial role in assigning the values to any variable. The most common assignment operator is =. • C language has a collection of shorthand assignment operators that can be used for C programming.
  • 46. Operator Description Example = Used to assign the values from right side of the operands to left side of the operand C = A + B will assign the value of A + B to C. += Adds the value of the right operand to the value of the left operand and assigns the result to the left operand. C += A is same as C = C + A -= Subtracts the value of the right operand from the value of the left operand and assigns the result to the left operand. C -= A is same as C = C - A *= Multiplies the value of the right operand with the value of the left operand and assigns the result to the left operand. C *= A is same as C = C * A /= Divides the value of the left operand with the value of the right operand and assigns the result to the left operand. C /= A is same as C = C / A %= Takes modulus using the values of the two operands and assigns the result to the left operand. C %= A is same as C = C % A
  • 47. Example: #include <stdio.h> int main() { int a = 7, b; b = a; // b is 7 printf("b = %dn", b); b += a; // b is 14 printf("b = %dn", b); b -= a; // b is 7 printf("b = %dn", b); b *= a; // b is 49 printf("b = %dn", b); b /= a; // b is 7 printf("c = %dn", c); b %= a; // b = 0 printf("b = %dn", b); return 0; }
  • 48. Output: b = 7 b = 14 b = 7 b = 49 b = 7 b = 0
  • 49. Bitwise Operators: • Bitwise operators are the operators which work on bits and perform the bit-by-bit operation. Mathematical operations like addition, subtraction, multiplication, division, etc are converted to bit-level which makes processing faster and easier to implement during computation and compiling of the program. • Bitwise operators are especially used in C programming for performing bit-level operations. C programming language supports a special operator for bit operation between two variables. • The truth tables for &, |, and ^ p q p & q p | q p ^ q 0 0 0 0 0 0 1 0 1 1 1 1 1 1 0 1 0 0 1 1
  • 50. Here, we will assume that A = 50 and B = 25 in binary format as follows. A = 00110010 B = 00011001 ----------------- A&B = 00010000 A|B = 00111011 A^B = 00101011 ~A = 11001101
  • 51. Assume variable 'A' holds 50 and variable 'B' holds 25. Operator Description Example & Binary AND Operator. It copies a bit to the result if it exists in both the operands. (A & B) = 16, i.e. 00010000 | Binary OR Operator. It copies a bit if and only if it exists in either operand. (A | B) = 59, i.e. 00111011 ^ Binary XOR Operator. It copies the bit only if it is set in one operand but not both. (A ^ B) = 43, i.e. 00101011 ~ Binary One's Complement Operator. It is unary and has the effect of 'flipping' bits. (~A ) = ~(50), i.e,. -0111101 << Binary Left Shift Operator. The value of the left operands is moved left by the number of bits specified by the right operand. A << 2 = 200 i.e. 11001000 >> Binary Right Shift Operator. The value of the left operands is moved right by the number of bits specified by the right operand. A >> 2 = 12 i.e., 00001100
  • 52. Conditional operators • It is also known as shorthand or ternary operator or condensed if- then-else. 2 It is called ternary because it uses three expressions. The two symbols that are used to denote this operator are the question mark (?) and colon (;) & Three expressions are placed as given below, i. The first expression is placed before. ii. The second expression is placed between? and: Here the value of expression2 may be a constant value or variable. • iii. The third and final expression is placed after the symbol: Here the value of expression2 may be a constant value or variable. Syntax : Condition or Expression1_to_evaluate ? Expression2: Expression3; • If the expression being evaluated is true then expression 2 will be performed otherwise expression 3 will be performed.
  • 53. Ex. 1) To find the largest of two numbers Large a > b? a : b;  If the value of variable a; is greater than b then value of a; is assigned to the variable large otherwise the value of b will be assigned. 2) To check the number of days in the month of February  days = ( year % 40) ? 29: 28; 3) Ternary operator can be nested (One within another). For example, consider the following example to find the largest of the three numbers  Large = (( a > b && a>c) ? a :((b>c) ? b : c)); Here, expression 3 itself evaluate another condition. Conditional operators: It is also known as shorthand or ternary operator or condensed if-then-else. 2 It is called ternary because it uses three expressions. The two symbols that are used to denote this operator are the question mark (?) and colon (;) & Three expressions are placed as given below, i. The first expression is placed before. ii. The second expression is placed between? and: Here the value of expression2 may be a constant value or variable. iii. The third and final expression is placed after the symbol: Here the value of expression2 may be a constant value or variable.
  • 54. Syntax : Condition or Expression1_to_evaluate ? Expression2: Expression3; If the expression being evaluated is true then expression 2 will be performed otherwise expression 3 will be performed. Ex. 1) To find the largest of two numbers  Large a > b? a : b; • If the value of variable a; is greater than b then value of a; is assigned to the variable large otherwise the value of b will be assigned. 2) Ternary operator can be nested (One within another). For example, consider the following example to find the largest of the three numbers  Large = (( a > b && a>c) ? a :((b>c) ? b : c)); Here, expression 3 itself evaluate another condition.  special operations Some of the special operations are coma, ampersand(&amp;), size of operators. a) coma: ( , ) It is used as separator for variables eg; a=10, b=20 b) Address:(&amp;) It is used to get the address of a variables. C) Sizeof ( ) : It is used to get the size of a data type of a variable in bytes.
  • 55. sizeof Operator : • Unary operator sizeof is used to find the number of bytes occupied by the operand. It is generally used in structure and union to know the number of bytes occupied by structure and union instead of calculating the length manually. It will help during the portability of code when data types vary in length from compiles to compiles. Syntax : sizeof(operand) • Where operand may be basic or user-defined data type or constant value. Ex: 1) c=sizeof(int); On execution, the value of c contains 2, an integer data type required two bytes to store the value. 2) c=sizeof(float); On execution, the value of c contains 4, as float data type required two bytes to store value. 3) c = sizeof(67); On execution, the value contains 2, an integer constant that required two bytes to store value. 4) c = sizeof(6.7); On execution the value of c contains 8, as value 6.7 by default is considered as double rather than float data type and hence it occupies eight bytes to store value. 5) c = sizeof(“abcde”) O/p : 5 (It returns the number of characters enclosed within).
  • 56. Program: main ( ) { int a=10; float b=20 ; printf (“ a= %d b=%f’, a,b ); printf (“ Adderss of a =%u “ &a ) ; printf (“ Adderss of b =%u” &b ) ; printf (“Size of a = %d ” , sizeof (a) ) ; printf ( “ Size of b = %d ” . sizeof (b) ) ; }
  • 57. Expression Evaluation • Expressions are evaluated using an assignment statement of the form: • In the above syntax, variable is any valid C variable name. When the statement like the above form is encountered, the expression is evaluated first and then the value is assigned to the variable on the left hand side. Arithmetic Expressions: • The arithmetic expression is evaluated in specific order considering the operator's precedence, and the result of an expression will be based on the type of variable. Example:  From the given above arithmetic expressions example, let's consider the value  operands to be a=2,b=3, and c=4.  After substitution of values into the given expression, we get:  Z = 2 + 3 - (2 * 4)
  • 58. Below given steps are in order of precedence in which the operators of an expression must be evaluated. Step1: First, Priority is given to the parenthesis, so whatever is inside the parenthesis will be evaluated first, a*c (2*4 = 8). Step 2: The next level of precedence is given to + and - operator, so next a+b (2+3 = 5) is evaluated Step 3: a+b will be subtracted from a*c i.e; 5 - 8 = -3. Step 4: The final value after evaluation of an arithmetic expression will be stored into the variable Z, according to the example value three is stored into the variable Z that is z = -3
  • 59. Look at the below-given figure to understand the arithmetic expressions evaluation in a better way. Example 1 Program Using Arithmetic Expressions: #include <stdio.h> void main() { int a=2,b=3,c=4,z; z=a+b-(a*c); printf("Result= %d",z); } Output:
  • 61. Operator Precedence and Associativity • Every C operator has a precedence (priority) associated with it. This precedence is used to determine how an expression involving more than one operator is evaluated. • There are different levels of operator precedence and an operator may belong to one of these levels. The operators at the higher level of precedence are evaluated first. The operators in the same level of precedence are evaluated from left to right or from right to left, based on the associativity property of an operator. • The precedence and associativity of various operators in C are as given below: • The precedence and associativity of various operators in C are as given below:
  • 62.
  • 63.  In a c programming language, the data conversion is performed in two different methods as follows... 1. Type Conversion 2. Type Casting Type Conversion(Implicit Typecasting)  The type conversion is the process of converting a data value from one data type to another data type automatically by the compiler. Sometimes type conversion is also called implicit type conversion. The implicit type conversion is automatically performed by the compiler.  For example, in c programming language, when we assign an integer value to a float variable the integer value automatically gets converted to float value by adding decimal value 0. And when a float value is assigned to an integer variable the float value automatically gets converted to an integer value by removing the decimal value. int i=10; float x=15.5; Char ch=’A’;
  • 64. #include<stdio.h> #include<conio.h> void main(){ int i = 95 ; float x = 90.99 ; char ch = 'A' ; i = x ; printf("i value is %dn",i); x = i ; printf("x value is %fn",x); i = ch ; printf("i value is %dn",i); }
  • 65. Typecasting • When you convert the data type of a variable to another data type then this technique is known as typecasting. • Let’s say that you want to store a value of int data type into a variable of float data type. • Then you can easily do this with the help of typecasting. It is one of the important concepts of the C programming language. Explicit Type Casting • It is not like implicit type casting where the data type is converted automatically. In Explicit type casting, you have to force the conversion between data types. Syntax:- (data_type_name) expression • data_type_name is the name of the data type to which you want to convert to. • The expression can be a variable, an actual expression or a constant.
  • 66. Example: #include<stdio.h> int main() { float val = 56.3; int a = (int)val + 50; // explicit type casting printf("TechVidvan Tutorial: Explicit Type Casting!n"); printf("Value of val: %fn", val); printf("Value of a: %dn",a); return 0; }