Unit I - Basics of C Programming
Unit I - Basics of C Programming
INTRODUCTION
sub program ( )
{
--------------------------;
--------------------------;
---------------------------;
}
Document part:-
# include<stdio.h>
# include <string.h>
Header file is a collection of related functions
Define part:-
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
1. Signed type
Signed short int
Signed int
Signed long int
2. Unsigned type
Unsigned short int
Unsigned int
Unsigned long int
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
+ (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>=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
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.
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