c Programing 1
c Programing 1
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
Contents
• Introduction
• Structure of C program
• Data types
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
Introduction
• A computer is a machine that manipulates
information.
• A computer performs the task commanded by
the user.
• The command is given through a sequence of
lines of code that are understood by the
computer.
• So the art of writing such code is called
programming.
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
Steps to Learn a Programming Language
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
Contd...
• Every Programming Language has 2 important
pillars
– Syntax
– Semantic
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
Different generations of P.L
• First generation (machine level)
• Second generation (Assembly level)
• Third generation (High level): c,c++,java etc
• Fourth generation (Non-Procedural )
• Fifth generation (NLP)
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
Classification of High Level P.L
• Functional-LISP
• Object Oriented- c++,Java,Python
• Procedure – FORTAN,ALGOL,BASIC,PASCAL,C
• Declarative- PROLOG
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
Evolution of C Programming
Boolean introduced
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
Contd..
• ALGOL- Algorithmic Lang
• BCPL- Basic Combined
• ANSI- American National Standards Institute
• ISO-International Organization for
Standardization
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
Contd..
• ‘C’ was developed in the year 1972 by Dennis
Ritchie at Bell Laboratories to be used by the
UNIX Operating System.
• It was named ‘C’ because many of features are
derived from an earlier language called B.
• ‘C’ was designed for implementing system
software, later it was widely used for
developing portable application.
• C is a procedure oriented language.
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
Structure of C programming
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
Input and Output statements in c
printf(“control string”,arg1,arg2,arg3....);
Control string may also contain text to be
printed like instructions to the user or any text
to make output readable.
scanf(“control string”,arg1,arg2,arg3..);
Control string specifies the type and format of
the data that has to be obtained from
keyboard and stored in the memory locations
pointed by the arguments arg1,arg2..
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
Printf(“hello sai\bhi”);
hello sahi
/r- it moves cursor to the beginning of the
current line
Printf(“hello sai\rhi”);
hillo sai
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
Data types
• It is a kind of data to be stored in a variable.
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
Contents
• Variables
• Tokens
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
Variables
• A variable is a name of the memory location. It is used to
store data. Its value can be changed, and it can be reused
many times.
• It is a way to represent memory location through symbol so
that it can be easily identified.
• Syntax: type variable_list;
int a;
float b;
char c;
• Here, a, b, c are variables. The int, float, char are the data
types.
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
Rules for defining variables
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
• We can also provide values while declaring the variables as
given below:
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
Tokens
• Tokens in C is the most important element to be used in
creating a program in C.
• We can define the token as the smallest individual element in
C.
• For `example, we cannot create a sentence without using
words; similarly, we cannot create a program in C without
using tokens in C.
• Therefore, we can say that tokens in C is the building block or
the basic component for creating a program in C language.
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
1. keywords
• Keywords in C can be defined as the pre-defined or
the reserved words having its own importance, and each
keyword has its own functionality.
• Since keywords are the pre-defined words used by the
compiler, so they cannot be used as the variable names.
• If the keywords are used as the variable names, it means that
we are assigning a different meaning to the keyword, which is
not allowed.
• C language supports 32 keywords.
• All keywords are in lower case.
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
2.Identifiers
• An identifier is a name used to identify a variable,function or any
other user-defined item.
Rules
• The first character of an identifier should be either an alphabet or
an underscore, and then it can be followed by any of the character,
digit, or underscore.
• It should not begin with any numerical digit.
• In identifiers, both uppercase and lowercase letters are distinct.
Therefore, we can say that identifiers are case sensitive.
• Commas or blank spaces cannot be specified within an identifier.
• Keywords cannot be represented as an identifier.
• The length of the identifiers should not be more than 31 characters.
• Identifiers should be written in such a way that it is meaningful,
short, and easy to read
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
Contd...
• Example of valid identifiers
total, sum, average, _m _, sum_1, etc.
• Example of invalid identifiers
2sum (starts with a numerical digit)
int (reserved word)
char (reserved word)
m+n (special character, i.e., '+')
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
Differences between Keyword and Identifier
Keyword Identifier
Keyword is a pre-defined word. The identifier is a user-defined word
It must be written in a lowercase letter. It can be written in both lowercase and
uppercase letters.
Its meaning is pre-defined in the c Its meaning is not defined in the c
compiler. compiler.
It is a combination of alphabetical It is a combination of alphanumeric
characters. characters.
It does not contain the underscore It can contain the underscore character.
character.
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
3.Constants
• A constant is a value or variable that can't be
changed in the program, for example: 10, 20,
'a', 3.4, "c programming" etc.
• constant variable has to be declared and
defined at the same time
Example: const int a=4;
• There are different types of constants in C
programming.
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
2 ways to define constant in C
• There are two ways to define constant in C
programming.
1. const keyword
2. #define pre-processor
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
Way 1: using C const keyword
#include<stdio.h>
int main(){
const float PI=3.14;
printf("The value of PI is: %f",PI);
return 0;
}
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
Way-2 : Using #define
#include<stdio.h>
#define PI 3.14
int main()
{
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
4. Strings
• String is a group of characters
• char s[6] = “hello";
• char ch*6+=,‘h', ‘e', ‘l', ‘l', 'o’, '\0'};
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
5. Special symbols
• C language special characters are used to
define different tasks that acan be carried out
by compiler.
• Example: : { } [ ] etc.
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
6.Operators
• An operator is simply a symbol that is used to
perform operations.
• There can be many types of operations like
arithmetic, logical, bitwise, etc.
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
Arithmetic Operator
• It can be applied to any integer or floating
point number
• The modulus operator(%) finds the remainder
of an integer division.
• % operator can be applied only to integer
operands
• While performing modulo division, the sign of
the result is always the sign of first operand.
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
Relational Operator
• It is also known as comparison operator.
• It compares two values and returns either true
or false.
• In c programming 0 is treated as false, 1 is
treated as true.
True-1
False-0
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
Logical Operator
• Logical operators are useful when we want to
test multiple conditions.
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
Bitwise operator
• C supports some special operators known as
bitwise operators for manipulation data at bit
level.
• A bitwise operator operates on each bit of
data.
• Bit wise operators may not be applied to a
float or double.
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
15 14 13 1 11 1 9 8 7 6 5 4 3 2 1 0
2 0
A=-4
1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
Bit wise AND (&)
• It compares each bit of its first operand with
the corresponding bit of its second operand.
• If both bits are 1, the corresponding bit in the
result is 1 otherwise 0.
Example:
1. a=5 ,b=7
2. A=-5,b=7
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
A=5 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1
B=7 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1
A&B=5 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
A=-5 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1
2’s complement of
A 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1
B=7 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1
A&B=3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
Bitwise OR (|)
• It compares each bit of its first operand with
the corresponding bit of its second operand.
• If one or both bits are1, corresponding bit in
the result is 1 otherwise 0.
• Example:
1. a=5,b=7
2. A=-5,b=7
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
A=5 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1
B=7 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1
A|B=7 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
A=-5 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1
2’s complement of
A 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1
B=7 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
A|B=-1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
A=-4 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
B=-6 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0
2’s of A
2’s of B 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 0
1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
Bitwise NOT(~)
• It performs logical negation of each bit, it
actually produces 1’s complement of the given
binary value.
Example:
A=5 ~A=?
A=-5, ~A=?
Formula : -(n+1) where n is a given number
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
A=5 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1
~A 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 0
2’s
1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0
complem
ent of ~A
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
A=-5 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1
2’s complement of
A 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1
~A 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
Bitwise XOR (^)
• It performs operation on individual bits of the
operands.
• If one of bit is 1, the corresponding bit in the
result is 1 otherwise 0.
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
A=5 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1
B=7 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1
A^B=2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
Bitwise Right shift(>>)
• It is responsible for shifting bits towards right.
Syntax:
Operand>>number
Example:
A=7
A>>1
Every bit in the A is shifted to right by one place,
so LSB of A is lost,the MSB of A is set to 0.
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
A=7 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1
A>>1
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
A=6 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0
A>>1
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
A=-7 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1
2’s
comple
ment
of A
A>>1
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
A=-6 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0
2’s
comple
ment
of A
A>>1
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
Formula for Right shift(>>)
If n is positive number, then
n>>1 = n/2
n>>2=n/4
n>>x=n/pow(2,x)
If n is negative number, then
if n is odd
n>>1= -(n/2+1)
If n is even
n>>1=-n/2
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
Bitwise Left shift(<<)
• It is responsible for shifting bits towards left.
Syntax:
Operand<<number;
Example:
A=7
A<<1
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
A=7 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1
A<<1
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
A=6 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0
A<<1
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
A=-7 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1
2’s
comple
ment
of A
A<<1
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
A=-6 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0
2’s
comple
ment
of A
A<<1
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
Formula for Left shift (<<)
• If n is any integer then
n<<1 = 2*n
n<<2= 4*n
n<<x=pow(2,x)*n
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
Increment Operator(++)
• It increases the value of operand by one.
• It has two variants
i) pre-increment
ii) Post-increment
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
Pre-increment(++operand)
• In this, initially the value of operand is
incremented by 1 and then the changed value
is assigned to the left hand side variable. The
‘++’ symbol is used before the variable name.
• Example:
A=10
B=++A;//A=A+1 , B=A
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
Post-increment (operand++)
• In this, initially the value of operand is
assigned to the variable on the left side and
then it increases the value of operand by 1.
The symbol ‘++’ is used after the variable
name.
• Example:
A=10
B=A++//B=A, A=A+1
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
Decrement Operator(--)
• It decreases the value of operand by one.
• It has two variants
i) pre-decrement
ii) Post-decrement
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
Pre-decrement(--operand)
• In this, initially the value of operand is
decremented by 1 and then the changed value
is assigned to the left hand side variable. The
‘--’ symbol is used before the variable name.
• Example:
A=10
B=--A;//A=A-1 , B=A
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
Post-decrement (operand--)
• In this, initially the value of operand is
assigned to the variable on the left side and
then it decreases the value of operand by 1.
The symbol ‘--’ is used after the variable
name.
• Example:
A=10
B=A--//B=A, A=A-1
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
Assignment Operator (=)
• It is used to assign a value to the variable.
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
Ternary Operator
• It is also known as conditional operator.
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
A=5 B= 7 C= 18
A=12 B=14 C=13
A=9 B=8 C=10
A=20 B=18 C=10
Result= (A>B&&A<C)?(B>C?B:C):(B>C?(A>C:A):C)
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
Expression
• An expression is a combination of constants,
variables, operators and functions.
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
Comma operator
• int a=1,2,3;//1
• int a=(1,2,3);//3
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
Type Conversion
• The type conversion process in C is basically
converting one type of data type to other to
perform some operation.
• The conversion is done only between those
data types wherein the conversion is possible
ex – char to int and vice versa.
i) Implicit Type Conversion or automatic type
conversion
ii) Explicit Type Conversion
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
Implicit type conversion (Low to High)
• This type of conversion is usually performed by the
compiler when necessary without any commands by
the user.
• Thus it is also called "Automatic Type Conversion".
• The compiler usually performs this type of
conversion when a particular expression contains
more than one data type.
• All the data types of the variables are
upgraded to the data type of the variable with
largest data type.
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
Example:
#include<stdio.h>
int main()
{
int x = 10; // integer x
char y = 'a'; // character c
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
Explicit Type Conversion (high to low)
• This process is also called type casting and it is
user defined. Here the user can type cast the
result to make it of a particular data type.
syntax:
• (type) expression
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
Example
#include<stdio.h>
int main()
{
double x = 1.2;
return 0;
}
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati
Difference b/n Implicit and explicit
Implicit Explicit
• Low to high • High to low
• Compiler • Done by user
• No data loss • Data loss
• Automatic type conversion • Type casting
• int x= 3+’a’ • float s=34.5
• int x=(int)s+5;
SAIMEDHA - GATE,ECET,PSUs-
Vijayawada,Hyderabad,Tirupati