0% found this document useful (0 votes)
44 views

BCSE102L Structured and Object Oriented: Programming

C programming language was developed in 1972 by Dennis Ritchie at Bell Laboratories to be used in the UNIX operating system. It is a mid-level, structured, and procedure-oriented programming language that inherits features from previous languages like B and BPCL. The document discusses key features of C like portability, rich libraries, pointers, and recursion. It also covers basic data types, keywords, operators, input/output functions like printf() and scanf(), and data type conversion specifiers used with format strings.

Uploaded by

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

BCSE102L Structured and Object Oriented: Programming

C programming language was developed in 1972 by Dennis Ritchie at Bell Laboratories to be used in the UNIX operating system. It is a mid-level, structured, and procedure-oriented programming language that inherits features from previous languages like B and BPCL. The document discusses key features of C like portability, rich libraries, pointers, and recursion. It also covers basic data types, keywords, operators, input/output functions like printf() and scanf(), and data type conversion specifiers used with format strings.

Uploaded by

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

BCSE102L

Structured
and
Object By,
Dr. S. Vinila Jinny,
Oriented ASP/SCOPE
Programming
 C is mother language of all programming
language.
 It is a popular computer programming
language.
 It is procedure-oriented programming
language.
 It is also called mid level programming
language.
 C programming language was developed in
1972 by Dennis Ritchie at bell laboratories
of AT&T(American Telephone & Telegraph),
located in U.S.A.
 Dennis Ritchie is known as founder of c
language.
 It was developed to be used in UNIX
Operating system.
 It inherits many features of previous
languages such as B and BPCL.
Language year Developed By
ALGOL 1960 International Group
BPCL 1967 Martin Richards
B 1970 Ken Thompson
Traditional C 1972 Dennis Ritchie
K&RC 1978 Kernighan & Dennis
Ritchie
ANSI C 1989 ANSI Committee
ANSI/ISO C 1990 ISO Committee
C99 1999 Standardization
Committee
There are many features of c language are given below.

1) Machine Independent or Portable


2) Mid-level programming language
3) structured programming language
4) Rich Library
5) Memory Management
6) Fast Speed
7) Pointers
8) Recursion
9) Extensible
#include <stdio.h>
#include <conio.h>
void main(){
printf(“VIT-SCOPE”);
}
 #include <stdio.h> includes the standard input
output library functions. The printf() function is
defined in stdio.h .
 #include <conio.h> includes the console input
output library functions. The getch() function is
defined in conio.h file.
 void main() The main() function is the entry point
of every program in c language. The void keyword
specifies that it returns no value.
 printf() The printf() function is used to print data on
the console.
 getch() The getch() function asks for a single
character. Until you press any key, it blocks the
screen.
There are two I/O function of c language.
1) First is printf()
2) Second is scanf()
 printf() function is used for output.
 It prints the given statement to the console.
 Syntax of printf() is given below:
printf(“format string”,arguments_list);
 Format string can be %d(integer),
%c(character), %s(string), %f(float) etc.
 scanf() Function: is used for input.
 It reads the input data from console.
 Syntax:
scanf(“format string”,argument_list);
void main()
{
int x;
scanf(“%d”,&x);
printf(“X=%d”,x);
}
 There are four types of data types in C
language.

Types Data Types

Basic Data Type int, char, float, double

Derived Data Type array, pointer, structure, union

Enumeration Data Type enum

Void Data Type void


 A keyword is a reserved word. We cannot
use it as a variable name, constant name
etc.
 There are 32 keywords in C language as given
below:
auto break case char const contin default do
ue
double else enum extern float for goto if
int long register return short signed sizeof static
struct switch typedef union unsigned void volatile while
 An operator is simply a symbol that is used to
perform operations. There can be many
types of operations like arithmetic, logical,
bitwise, etc.
Precedence & Associativity of Operators in C
 The precedence of operator species that
which operator will be evaluated first and
next. The associativity specifies the operator
direction to be evaluated; it may be left to
right or right to left.
 E.g:
int value=10+20*10;
 Based on number of operands
 Unary
 Binary
 Ternary
 Based on type of operation
 Refer next slide
 There are following types of operators to
perform different types of operations in C
language.
1) Arithmetic Operators
2) Relational Operators
3) Logical Operators
4) Bitwise Operators
5) Assignment Operator
6) Ternary or Conditional Operators
7) Misc Operator
Operator Description Example
A=10, B=20
+ Adds two operands. A + B = 30
− Subtracts second operand A − B = -10
from the first.
* Multiplies both operands. A * B = 200
/ Divides numerator by de- B/A=2
numerator.
% Modulus Operator and B%A=0
remainder of after an integer
division.

++ Increment operator increases A++ = 11


the integer value by one.
-- Decrement operator decreases A-- = 9
the integer value by one.
Operator Description Example
A=10 B=20
== Checks if the values of two operands are (A == B) is not
equal or not. If yes, then the condition true.
becomes true.
!= Checks if the values of two operands are (A != B) is true.
equal or not. If the values are not equal,
then the condition becomes true.
> Checks if the value of left operand is (A > B) is not
greater than the value of right operand. true.
If yes, then the condition becomes true.
< Checks if the value of left operand is (A < B) is true.
less than the value of right operand. If
yes, then the condition becomes true.
>= Checks if the value of left operand is (A >= B) is not
greater than or equal to the value of true.
right operand. If yes, then the condition
becomes true.
Operator Description Example

&& Called Logical AND (A && B) is false.


operator. If both the
operands are non-zero,
then the condition
becomes true.
|| Called Logical OR (A || B) is true.
Operator. If any of the
two operands is non-
zero, then the condition
becomes true.
! Logical NOT : It is used !(A && B) is true.
to reverse the logical
state of its operand. If a
condition is true, then
Logical NOT operator will
make it false.
Operato Description Example
r
& Binary AND Operator copies a bit to the result (A & B) = 12, i.e.,
if it exists in both operands. 0000 1100

| Binary OR Operator copies a bit if it exists in (A | B) = 61, i.e.,


either operand. 0011 1101

^ Binary XOR Operator copies the bit if it is set in (A ^ B) = 49, i.e.,


one operand but not both. 0011 0001

~ (~A ) = ~(60), i.e,. -


Binary One's Complement Operator is unary 0111101
and has the effect of 'flipping' bits.

<< Binary Left Shift Operator. The left operands


value is moved left by the number of bits A << 2 = 240 i.e.,
specified by the right operand. 1111 0000

>> Binary Right Shift Operator. The left operands


value is moved right by the number of bits A >> 2 = 15 i.e.,
specified by the right operand. 0000 1111
Operator Description Example
= Simple assignment operator. Assigns values from right side operands C = A + B will
to left side operand assign the
value of A + B
to C
+= Add AND assignment operator. It adds the right operand to the left C += A is
operand and assign the result to the left operand. equivalent to
C=C+A
-= Subtract AND assignment operator. It subtracts the right operand C -= A is
from the left operand and assigns the result to the left operand. equivalent to
C=C -A
*= Multiply AND assignment operator. It multiplies the right operand C *= A is
with the left operand and assigns the result to the left operand. equivalent to
C=C*A
/= Divide AND assignment operator. It divides the left operand with the C /= A is
right operand and assigns the result to the left operand. equivalent to
C=C/A
%= Modulus AND assignment operator. It takes modulus using two C %= A is
operands and assigns the result to the left operand. equivalent to
C=C%A
Operator Description Example

<<= Left shift AND assignment operator. C <<= 2 is same as C =


C << 2
>>= Right shift AND assignment operator. C >>= 2 is same as C =
C >> 2
&= Bitwise AND assignment operator. C &= 2 is same as C = C
&2
^= Bitwise exclusive OR and assignment C ^= 2 is same as C = C
operator. ^2
|= Bitwise inclusive OR and assignment C |= 2 is same as C = C
operator. |2
 Syntax:
The conditional operator is of the form
variable = Expression1 ? Expression2 : Expression3
 Example:
#include <stdio.h>
int main()
{
int m = 5, n = 4;
(m > n) ? printf("m is greater”): printf("n is greater);
return 0;
}
Operator Description Example

sizeof() sizeof(a), where a is integer,


Returns the size of a will return 4.
variable.

& &a; returns the actual address


Returns the address of a of the variable.
variable.

* Pointer to a variable. *a;


Category Operator Associativity
Postfix () [] -> . ++ - - Left to right
Unary + - ! ~ ++ - - (type)* & sizeof Right to left

Multiplicative */% Left to right


Additive +- Left to right
Shift << >> Left to right
Relational < <= > >= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left

Comma , Left to right


 If a data type is automatically converted into
another data type at compile time is known as
type conversion.
 The conversion is performed by the compiler if both
data types are compatible with each other.
 Remember that the destination data type should not
be smaller than the source type.
 It is also known as widening conversion of the data
type.
 Eg: int -> float
 These are data types compatible with each other
because their types are numeric, and the size of int
is 2 bytes which is smaller than float data type.
Hence, the compiler automatically converts the data
types without losing or truncating the values.
 Typecasting allows us to convert one data
type into other. In C language, we use cast
operator for typecasting which is denoted by
(type).
 Syntax:
(type)value;
#include <stdio.h>
void main()
{
int sum = 17, count = 5;
double mean;
mean = (double) sum / count;
printf("Value of mean : %f\n", mean );
}
1) if-else
2) switch
3) loops
4) do-while loop
5) while loop
6) for loop
7) break
8) Continue
9) goto
 There are many ways to use if statement in C
language:
1) If statement
2) If-else statement
3) If else-if ladder
4) Nested if
 if statement is used to execute the code if
condition is true.
 syntax:-
if(expression){
//code to be execute
}
#include<stdio.h>
int main(){
int number=0;
printf("Enter a number:");
scanf("%d",&number);
if(number%2==0){
printf("%d is even number",number);
}
return 0;
}
 The if-else statement is used to execute the
code if condition is true or false.
 Syntax:
if(expression)
{
//code to be executed if condition is true
}
else
{
//code to be executed if condition is false
}
#include <stdio.h>
int main()
{
int age;
printf("Enter your age?");
scanf("%d",&age);
if(age>=18)
{
printf("You are eligible to vote...");
}
else
{
printf("Sorry ... you can't vote");
}
}
Syntax:
if(condition1)
{
//code to be executed if condition1 is true
}
else if(condition2)
{
//code to be executed if condition2 is true
}
else if(condition3){
//code to be executed if condition3 is true
}
...
else{
//code to be executed if all the conditions are false
}
#include <stdio.h>
int main() else if (marks > 40 && marks <= 60)
{ {
printf("You scored grade B ...");
int marks;
}
printf("Enter your marks?"); else if (marks > 30 && marks <= 40)
scanf("%d",&marks); {
if(marks > 85 && marks <= 100) printf("You scored grade C ...");
{ }
printf("Congrats ! you scored grade A else
..."); {
} printf("Sorry you are fail ...");
}
else if (marks > 60 && marks <= 85
}
)
{
printf("You scored grade B + ...")
;
}
 Syntax:
switch(expression)
{
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
......
default:
code to be executed if all cases are not matched;
}
#include <stdio.h>
int main()
{
int x = 10, y = 5;
switch(x>y && x+y>0)
{
case 1:
printf("hi");
break;
case 0:
printf("bye");
break;
default:
printf(" Hello bye ");
}

}
 Loops are used to execute a block of code or
a part of program of the program several
times.
Types of loops in C language:-
 There are 3 types of loops in c language.
1) do while
2) while
3) for
 It is better if you have to execute the code
at least once.
 Syntax:-
do{
//code to be executed
}while(condition);
#include<stdio.h>
int main(){
int i=1;
do{
printf("%d \n",i);
i++;
}while(i<=10);
return 0;
}
 It is better if number of iteration is not
known by the user.
 Syntax:-
while(condition){
//code to be executed
}
#include<stdio.h>
int main(){
int i=1;
while(i<=10){
printf("%d \n",i);
i++;
}
return 0;
}
 It is good if number of iteration is known by
the user.
 Syntax:-
for(initialization;condition;incr/decr){
//code to be executed
}
#include<stdio.h>
int main(){
int i=1,number=0;
printf("Enter a number: ");
scanf("%d",&number);
for(i=1;i<=10;i++){
printf("%d \n",(number*i));
}
return 0;
}
 it is used to break the execution of loop
(while, do while and for) and switch case.
 Syntax:-
jump-statement;
break;
#include<stdio.h>
void main ()
{
int i = 0;
while(1)
{
printf("%d ",i);
i++;
if(i == 10)
break;
}
printf("came out of while loop");
}
 it is used to continue the execution of loop
(while, do while and for). It is used with if
condition within the loop.
 Syntax:-
jump-statement;
continue;
#include<stdio.h>
void main ()
{
int i = 0;
while(i!=10)
{
printf("%d", i);
continue;
i++;
}
}
 The goto statement is a jump statement which is
sometimes also referred to as unconditional jump
statement. The goto statement can be used to jump
from anywhere to anywhere within a function.
 Syntax1:
goto label;
------
-------
label:
Syntax2:
label:
------
-------
goto label;
#include <stdio.h>
// C program to check if a number is
void checkEvenOrNot(int num)
// even or not using goto statement
{
if (num % 2 == 0)
goto even; int main()
else
goto odd; {
int num = 26;
even:
printf("%d is even", num); checkEvenOrNot(num);
return; return 0;
odd:
printf("%d is odd", num); }
}

You might also like