You are on page 1of 44

UNIT I - BASICS OF C PROGRAMMING

INTRODUCTION

What is programming paradigm ?

•A programming paradigm is a fundamental


style of computer programming.

•Paradigms differ in the concepts and


methods used to represent the elements of a
program (such as objects, functions, variables,
constants).
HISTORY

•Different approaches to programming have developed over


time.
•The concept of a "programming paradigm" as such dates at
least to 1978.
•The lowest-level programming paradigms are machine code.
•In the 1960s, assembly languages were developed followed by
development of procedural languages.
•Following the widespread use of procedural languages, object-
oriented programming (OOP) languages were created and
followed by many more…
IMPERATIVE PROGRAMMING PARADIGM

 Imperative programming is a programming paradigm that


uses statements that change a program's state.
 In much the same way that the imperative mood in natural
languages expresses commands.
 An imperative program consists of commands for the
computer to perform.
 Imperative programs describe the details of HOW the results
are to be obtained.
 HOW means describing the Inputs and describing how the
Outputs are produced.
Examples are: C, C++, Java, PHP, Python, Ruby etc
DECLARATIVE PROGRAMMING
PARADIGM
 Declarative programming is a programming paradigm—a style
of building the structure and elements of computer programs—
that expresses the logic of a computation without describing its
control flow.
 Declarative programming focuses on what the program
should accomplish.
 Declarative programming often considers programs as
theories of a formal logic, and computations as deductions in
that logic space.
 Examples are: SQL, XSQL (XMLSQL) etc.
FUNCTIONAL PROGRAMMING PARADIGM

 Functional programming is a subset of declarative


programming.
 Programs written using this paradigm use functions, blocks of
code intended to behave like mathematical functions.
 Functional languages discourage changes in the value of
variables through assignment, making a great deal of use of
recursion instead.
 Examples are : F#, Haskell, Lisp, Python, Ruby, JavaScript etc.
OBJECT ORIENTED PROGRAMMING PARADIGM

 Object-oriented programming (OOP) is a programming


paradigm based on the concept of "objects", which may contain
data, in the form of fields, often known as attributes; and code, in
the form of procedures, often known as methods.
 There is significant diversity of OOP languages, but the most
popular ones are class-based, meaning that objects are instances
of classes, which typically also determine their type.
 In OOP, computer programs are designed by making them out of
objects.
 Examples are: C++, C#, Java, PHP, Python
MULTI PARADIGM

 A multi-paradigm programming language is a programming language


that supports more than one programming paradigm.

 The design goal of such languages is to allow programmers to use the


most suitable programming style and associated language constructs for a
given job.

 Languages such as C++, Java, Python are multiparadigm programming


languages that support object-oriented programming to a greater or lesser
degree, typically in combination with imperative, procedural programming.
A Glance of different paradigms

Paradigm Description Examples


Imperative Programs as statements that C, C++, Java, PHP, Python, Ruby.
directly change computed state
(datafields).

Functional Treats computation as the C++, Lisp, Python, JavaS cript


evaluation of mathematical
functions avoiding state.

Object-oriented Treats datafields as object s C++, C#., Java, PHP, Python .


manipulated through predefined
methods only

Declarative Defines program logic, but not SQL, CSS.


detailed control flow
Structure of a C program
Document
Include files
Define part
Global declaration
Main ( )
{
Variable declaration;
Executable statement;
--------------------------;
--------------------------;
--------------------------;
}

sub program ( )
{
--------------------------;
--------------------------;
---------------------------;
}
 
Document part:-

 It’s writing comments in the program


This is to understand the programming
 /*------------------------*/ single line commenting
 /*----------------------
----------------------
----------------------*/ multiple line commenting
At compile time compiler ignores all the comments and generates
exe file these are only from source file
They are only for the user
Exe file does not contain the comments
Include files:-
C language provides key words and predefined functions.Only key words
are a part of c compiler.Predefined functions are not from the compiler they
are from the c library files (header files). When the program wants to use
any predefined function then corresponding header file should be included
to the program
Ex:-

# include<stdio.h>
# include <string.h>
Header file is a collection of related functions
Define part:-

This is to define the global constant


Ex:-
# define N 20
# define SIZE 1024
Global constant can be used any where in the program
# define N =20  is invalid
# define n = 20  invalid
# define n 20  is invalid
# define N 20  valid
As per coding standards name of the constant must be in upper case only

Main ( ):-
It’s a function and starting point of execution of the program
C program compiles from top to bottom. Execution starts from the main function
only
To compile C program main function is not a compulsion but to generate an exe
file and execution main function is required
Variable declaration:-
Syntax:-
data type variable
Ex:-
int eno;
float salary;
Variable:-
It’s a name given to some location in memory
Its value changes at run time
It’s a temporary storage location
It vanishes automatically when the control out of the program
 
Data types

It’s a storage representation of data


There are 4 types of data types
1. System defined data types
2. User defined data types
3. Derived data types
4. Empty data types
system defined data types :-
 
They are provided by the compiler it self
They are also called as predefined or fundamental or basic data types

System defined data types


character type(Char)
2. Numeric type
 
Character type
 1. Signed type character(signed char)
  2. Unsigned type character(unsigned char)
 
Numeric type
1. integer type
2.Floating point type
Integer type

1. Signed type
 Signed short int
 Signed int
Signed long int
 
2. Unsigned type
Unsigned short int
 Unsigned int
Unsigned long int

Floating point type


1. Float
2. Double
3. Long double
Size of data types:-

Data type 16bit(turbo.c) 32bit(turbo.c) 32bit(ANSI.C) 64bit(ANSI.C)


Short int 1byte 2bytes 2bytes 2bytes
int 2bytes 2bytes 2bytes 2 bytes

Long int 2bytes 4bytes 4bytes 8bytes

pointer 2bytes 2bytes 4bytes 8bytes


FORMATTED CODES
DATA FORMATTED
TYPE CODE
SIGNED CHAR %c
UNSIGNED CHAR %c
SIGNED SHORT INT %d
UNSIGNED SHORT INT %u
SIGNED INT %d
UNSIGNED INT %d
SIGNED LONG INT %ld
UNSIGNED LONG INT %lu
FLOAT %f
DOUBLE FLOAT %lf
LONG DOUBLE %Lf
Constants
Constants are the fixed values that never change during the execution of a program.

1. Integer constants
2. Character constants
3. String constants
4. Real Constants
Enumeration Constants
It is used to assign names to the integral constants which makes a program easy to
read and maintain. The keyword “enum” is used to declare an enumeration.

syntax 
enum enum_name{const1, const2, ....... };

Example
enum week
{sunday, monday, tuesday, wednesday, thursday, friday, Saturday
}; enum week day;
Keywords

auto break case char

const continue default do

double else enum extern

float for goto if

int long register return

short signed sizeof static

struct switch typedef union

unsigned void volatile while


Operators
Type of Operators Description
Arithmetic Operators Perform mathematical calculations like addition, subtraction,
multiplication, division and modulus
Assignment Operators Assign the values for the variables in C programs
Relational operators Compare the value of two variables
Logical operators Perform logical operations on the given two variables.

Bit wise operators Perform bit operations on given two variables

Conditional (ternary) Conditional operators return one value if condition is true


operators and returns another value is condition is fals

Increment/decrement Either increase or decrease the value of the variable by one.


operators
Special operators &, *, sizeof( ) and ternary operators.
ARITHMETIC OPERATORS

Arithmetic Operators/Operation Example

+ (Addition) A+B

– (Subtraction) A-B

* (multiplication) A*B

/ (Division) A/B

% (Modulus) A%B
Arithmetic Operator

#include <stdio.h>
int main()
{
int a=40,b=20, add,sub,mul,div,mod;
add = a+b;
sub = a-b;
mul = a*b;
div = a/b;
mod = a%b;
printf("Addition of a, b is : %d\n", add);
printf("Subtraction of a, b is : %d\n", sub);
printf("Multiplication of a, b is : %d\n", mul);
printf("Division of a, b is : %d\n", div);
printf("Modulus of a, b is : %d\n", mod);
return 0;
}
RELATIONAL OPERATORS
Relational Operators/Operation Example

> A>B

< A<B

>= A>=B

<= A<=B

== A==B

!= A!=B
Example Assignment Operator

#include <stdio.h>
int main()
{
int m=40,n=20;
if (m == n)
{
printf("m and n are equal");
}
else
{
printf("m and n are not equal");
}
return 0;
}
Logical Operators
Assignment Example
Operators/Operation

&& (a>5)&&(b<5) It returns true when both conditions are


(logical AND) true

|| (a>=10)||(b>=10)
(logical OR) It returns true when at-least one of the condition is
true
! !((a>5)&&(b<5))
(logical NOT) It reverses the state of the operand “((a>5) && (b<5))”
If “((a>5) && (b<5))” is true, logical NOT operator
makes it false
Example Logical Operator

#include <stdio.h>
int main()
{
int m=40,n=20;
int a=20,p=30;
if (m>n && m !=0)
{ printf("&& Operator : Both conditions are true\n"); }
if (a>p || p!=20)
{ printf("|| Operator : Only one condition is true\n"); }
if (!(m>n && m !=0))
{ printf("! Operator : Both conditions are true\n"); }
else
{ printf("! Operator : Both conditions are true. " \
"But, status is inverted as false\n");
}
return 0;
}
BIT WISE OPERATORS

These operators are used to perform bit operations. Decimal values are converted into
binary values which are the sequence of bits and bit wise operators work on these bits.
Following are bitwise operator

1)& Bitwise AND


2)| Bitwise OR
3)~ Bitwise NOT
4)^ XOR
5)<< Left Shift
6)>> Right Shift
CONDITIONAL OR TERNARY OPERATORS

Conditional operators return one value if condition is true and returns another value is
condition is false. This operator is also called as ternary operator.

Syntax : (Condition? true_value: false_value);


Example : (A > 100 ? 0 : 1);

Increment / Decrement Operators


Increment operators are used to increase the value of the variable by one
and decrement operators are used to decrease the value of the variable by
one in C programs.
Increment operator
++var_name; (or) var_name++;
Decrement operator
- -var_name; (or) var_name – -;
Example for increment operators Example for Decrement operators

#include <stdio.h> #include <stdio.h>


int main() int main()
{ {
int i=1; int i=1;
while(i<10) while(i<10)
{ {
printf("%d ",i); printf("%d ",i);
i++; i--;
} }
return 0; return 0;
} }
Input/Output statements

getchar()
Represented as getchar ()
To read a character from the input buffer
char ch;
ch=getchar ();
getchar() and scanf() both starts the reading processes when the process enters key
Both functions will read spaces, tabs and new line characters
 
putchar( ):-
putchar( )
tis function is used to display the character
Ex:- char ch=’a’;
putchar (ch);
scanf() and printf() 
stdio.h contains the definition of the functions printf() and scanf()
which are used to display output on screen and to take input from user respectively.

#include<stdio.h>
void main()
{ // defining a variable int i; /* displaying message on the screen asking the user
to input a value */
printf("Please enter a value..."); /* reading the value entered by the user */
scanf("%d", &i); /* displaying the number as output */
printf( "\nYou entered: %d", i); }

Format String
Decision Making Statements

1.simple if
2.else if
3.nested if
4.ladder if

1.Simple if
Syntax:-
if<condition>
{
----------------;
-----------------;
-----------------;
}
Program to find the greater of two numbers using if statement
#include<stdio.h>
main( )
{
int a,b;
printf(“\n enter the two numbers a,b);
scanf(“%d %d “,&a,&b);
if(a>b)
{
printf(“\n greater =%d”,a);
}
else
{
printf(“\n greater =%d”,b);
}
}
1.Simple if Program to find the greater of three numbers
#include<stdio.h>
main( )
else if:- {
int a,b,c;
 
printf(“\n enter the numbers”);
If<condition> scanf(“%d %d %d”,&a,&b,&c);
{ if(a>b)
… {
if(a>c)
… {
… printf(“\n greate=%d”,a);
} }
else
Else
{
{ printf(“\n greater =%d”,c);
… }
… }
else if(b>c)
… {
} printf(“\n greater =%d”,b);
}
else
{
printf(“\n greater =%d”,c);
}
}
Program to find the grade of the students
Nested if #include<stdio.h>
main()
Nested if:-
{
if<cond1> nt m1,m2,m3,avg;
{ printf("\nenter three subjects marks");
---------------; scanf("%d %d %d",&m1,&m2,&m3);
if( m1>=40 && m2>=40 && m3>=40 )
-----------------; {
if<cond2> avg=( m1 + m2 + m3 )/3;
{ if( avg >= 70 )
-------------; {
printf("\ngrade A with distinction");
--------------; }
else else if( avg >= 60 )
{ {
printf("\ngrade A");
----------; }
-----------; else if( avg >= 50 )
} {
--------------; printf("\ngrade B");
}
--------------; else
} {
else printf("\ngrade C");
}
{ }
-------------; else
------------; {
} printf("\nfail and no grade");
}
} }
Switch Case Statement
Switch case statements:-
 
syntax for switch case statement:-
switch(value)
{
case constant1:
……….
……….
……….
break;
case constant2:
……..
……..
…….
break;
default:
…………
…………
…………
break;
}
#include<stdio.h>
main()
{
int num;
printf("\n enetr a number");
scanf("%d",&num);
switch(num)
{
case 1 :
printf("\n ONE");
break;
case 2 :
printf("\n TWO");
break;
case 3 :
printf("\n THREE");
break;
case 4 :
printf("\m FOUR");
break;
default :
printf("\n DEFAULT");
break; /* optional */
}
printf("\n END");
}
 
Looping statements
1. While loop
2. Do while loop
3. for loop

While loop
program to display numbers from 1 to 10
 
while<cond> #include<stdio.h>
{ main()
…….. {
……. int i=1;
…….  
} while( i <= 10)
  {
printf("%d ",i);
i++;
}
 
}_
Do - while loop #include<stdio.h>  
Syntax int main(){    
do int i=1;      
{ do{    
…….. printf("%d \n",i);    
…….. i++;    
…….. }while(i<=10);   
} return 0;  
while<cond>; } 

Out put

1 2 3 4 5 6 7 8 9 10
programe to find the factorial of a given number
For loop  
Syntax #include<stdio.h>
for (initialization ; condition ; increment or main()
decrement) {
{ int n;
………. long int fact;
……….. printf("\nEnter a number");
………. scanf("%d",&n);
} for( fact=1 ; n>=1 ; n-- )
{
fact = fact * n;
}
printf("\nfactorial = %ld ", fact);
}
program to display fibonacci series
0 1 1 2 3 5 8 13 21 34 55 ……………………….n

You might also like