C Programming
C Programming
INTRODUCTION:
A programming language is a set of instructions for the system to perform a specific task.
Every programming language needs an expert to correct and evaluate the code before execution.
This expert is called a Compiler.
Since all of these instructions are to be executed by the processor, we use an assembler to create
an“.exe” file for execution.
PROGRAM STRUCTURE:
Every programming language for precision follows two important criteria,
SYNTAX
And they follow a set of vocabulary words called,
KEYWORDS
The first line of C starts with importing or including pre-defined library files that have definitions
and instructions of certain methods and functions that are used in the source code.
Since every programming needs an input and output we typically start with importing library files
that has details about the input and output function.
This section is called the header section, and it ends when we start the main function. All the
instructions that are to be executed before giving to the compiler are given in this section.
#include<stdio.h>
# - Pre-Processor Directive (This tells that this is to be executed before giving to the compiler).
include- Keyword (Which means to attach or insert).
<>- Syntax (This tells what is to be attached or inserted).
stdio- The file or library file name, which has all the definitions of standard input and output.
.h- This tells the extension type of the header file.
The angular tag in the header file works as a grouping symbol that groups the library file name.
It follows simple balancing using stack.
Stack Mechanism:
Stack starts pushing the data into the stack when it finds an open symbol.It starts popping the data
until it finds the open symbol when it finds the closed symbol.So it is mandatory to havea balanced
open and close symbol when working with grouping.
#include<stdio.h>
int main()
{
return 0;
}
int- Written type
main- Function name
{ } -Function block
return - Keyword(Return is a keyword which tells the compiler to exit the current function and
return to where it was called. A function can have any number of written statement but at any given
point of time, only one return gets executed.)
Themain function indicates the start of the program and the completion of the main function
indicates the end of the program.
A function is a set of instruction that is given to the system to execute a specific task.
If the function after the completion needs a certain value to give back, then we will configure the
function with the respective written type.
When a function is configured with a specific written type then it is conventional to have a written
statement within the function block.
Return is the keyword to exit the current function and return to where it was called.A function can
have many no. of return statements.
DATA:
Data are any information that we pass into the system.
On defining a data, we should always declare the space required to store the data and the name of
the place.
A data type helps in allocating the space required to store the data.
A variable or identifier a name we give to the allocated memory space to identify the data.
Data of type single characters is always represented by a single quote. Eg: char gender = ‘M’.
A string data is always enclosed with double quote.Ex: char[ ] designation = “Manager”.
There are two diff. type of data type
Int – numbers
Float – Decimal Numbers
Char – Single Character
Every datatype allocates the memory depending upon the memory that is assigned to it.
It is necessary for the programmer to allocate the data in an efficient manner without causing
memory insufficiency or memory deficiency. Ex: char = ‘AB’ isillegal since only one bit is allocated
which can store one character.
Program:
#include <stdio.h>
int main()
{
int a = 10;
float b = 78.89;
char ch = 'A';
char role[] = "Manager";
printf("\n%d",a);
printf("\n%d",sizeof(a));
printf("\n%d",&a);
printf("\n%f",b);
printf("\n%d",sizeof(b));
printf("\n%d",&b);
return 0;
}
Output:
10
4
-2027419288
78.889999
4
-2027419284
Program:
#include <stdio.h>
int main()
{
int a=10;
printf("\n%d",a);
a=40;//no memory allocation only assignment so no error!
printf("\n%d",a);
return 0;
}
Output:
10
40
Program:
#include <stdio.h>
int main()
{
int a=10;
printf("\n%d",a);
int a=40;//memory allocation takes place for second time
printf("\n%d",a);
return 0;
}
Output:
error
//memory allocation takes place for second time
Program:
#include <stdio.h>
#define a 20
int main()
{
printf("\n%d",a);
return 0;
}
Output:
20
MODIFIERS:
Modifiers usually modify the existing data either by expanding the memory or shortening the
memory or maintaining a special information like sign.
In C we have modifiers like short, long, long long, signed and unsigned.
CONSTANT VARIABLE:
A constant variables a variable for which we restrict assignment of new values.
When declaring a variable with const keyword, we usually block only the direct assignment using
equals, however it can be changed by indirect assignment.
We can define a strict constant variable by using pre-processing macro called define.
Program:
#include <stdio.h>
int main()
{
const int a=10;
printf("\n%d",a);
a=40;//error because a is a constant variable
printf("\n%d",a);
return 0;
}
Output:
error
//assignment of read-only variable ‘a’
INPUT FORMATS:
Scanf:
It is the function that is predominantly used for accepting the input from the user.
Output:
10 20
10
20
Printf:
One of the common and most predominant method for output in C is “printf” function.
Program:
#include <stdio.h>
int main(){
char greetings[]="Hello";
char name []="Jack";
int a=10;
int b=20;
printf("hai");
printf("%s",greetings);
printf("\nThe value of a %d and value of b %d",a,b);
printf("\nHello \" %s ",name);
printf("\ngood\bmorning");
printf("\nhow are \ru");
return 0;
}
Output:
haiHello
The value of a 10 and value of b 20
Hello " Jack
goomorning
uow are
Program:
#include <stdio.h>
int main(){
int a;
int sr=scanf("%d",&a);
printf("\n%d\n",sr);
int pr=printf("%d",a);
printf("\n%d",pr);
return 0;
}
Output:
2004
1
2004
4
Program:
#include <stdio.h>
int main(){
int a;
double b = 34.67;
//int sr=scanf("%d",&a);
//printf("\n%d\n",sr);
printf("\n%d\n",scanf("%d",&a));
//int pr=printf("%d",a);
//printf("\n%d",pr);
printf("\n%d",printf("%d",a));
return 0;
}
Output:
25000
1
25000
5
Program:
#include<stdio.h>
int main(){
int a;
printf("%d",printf("%d",printf("%d",12)));
return 0;
}
Output: 1221
TYPECASTING:
Converting data from one data type to another is called typecasting.
Typecasting usually happens in two types
Implicit Cast
Implicit casting refers to the casting that is done by the system itself.
Explicit Cast
Explicit typecasting refers to convertions made by the programmer
in the program by coding.
When type casting of data happens such that the higher range value is forcefully assigned to or
casted to the lower data type then there is a round off happening such that it corresponds to the
values that a shorter data can accommodate.
For example:
int - 4 bytes – 32 bits – 4294967296 - (-2147483648 – 2147483647)
Signed short int – 2 bytes – 16 bits – 65536 – (-32768- 32767)
unsigned short – 2 bytes – 16 bits- (0-65536)
Program:
#include <stdio.h>
int main()
{
int val=32769;
short int b=(short int)val;
printf("\n%d",b);
int val2=32769;
unsigned short int b2=(unsigned short int)val;
printf("\n%d",b2);
return 0;
}
Output:
-32767
32769
In the above code the value b gives a negative range value since the number exceeds the positive
limit of the short int. Hence the counting after the highest positive value is taken from the negative
scale.
The value b2 hold the exact value because unsigned int holding capacity is much greater than the
casted value.
RULES FOR WRITING A VARIABLE:
A variable should not start with a number.
A variable should never be a keyword.
A variable should not have any special character including space.
The only allowed special character is underscore.
OPERATORS:
Operators are symbols that helps you to perform specific operation on the operands.
An operator works on operands. Depending upon the number of operands, operators are broadly
classified to,
Unary operator:
Operator with a single opearand. Ex: i++, ++i , ~C
Binary operator:
Operator with two operands. Ex: a+b, a-b, a*b
Ternary operator:
Operator with three operands. Ex:?:
Types Of Operators:
Arithmetic operator
+,-,*,/,%
Program:
#include <stdio.h>
int main()
{
int val=32769;
short int b=(short int)val;
printf("\n%d",b);
int val2=32769;
unsigned short int b2=(unsigned short int)val;
printf("\n%d",b2);
return 0;
}
Output:
7
3
10
2
1
Assignment Operator
=,+=,-=,/=,%=,*=
Program:
#include <stdio.h>
int main()
{
int a;
scanf("%d",&a);
printf("%d",a);
printf("\n%d",a+=5);
printf("\n%d",a);
printf("\n%d",a-=5);
printf("\n%d",a);
printf("\n%d",a*=2);
printf("\n%d",a);
printf("\n%d",a/=2);
printf("\n%d",a);
printf("\n%d",a%=3);
printf("\n%d",a);
return 0;
}
Output:
20
20
25
25
20
20
40
40
20
20
2
2
Relational Operator:
<,>,<=,>=,==,!= (gives Boolean output)
Program:
#include <stdio.h>
int main()
{
char x,y;
int a,b;
scanf("%c\n%c",&x,&y);
scanf("%d\n%d",&a,&b);
printf("\n%d",x>y);
printf("\n%d",a>b);
printf("\n%d",a>x);
printf("\n%d",y>b);
printf("\n%d",a!=b);
return 0;
}
Output:
A
B
10
20
0
0
0
1
1
Logical Operator:
&&,||,!
Bitwise Operator:
&,\,^,~,<<,>>
These operators works just like logical gates but works on bit level.
Example:
5&4 – 4
(101 & 100) – (100)
5\4 – 5
(101\100) – (101)
5^4 – 1
(101^100) – (001)
The left shift and right shift operators, shifts the data towards left or right depending on the shift
value.
~ = -(Value + 1)
~5 = -(5+1) = -6
Program:
#include <stdio.h>
int main(){
int a = 5, b = 4;
printf("\n%d",a&b);
printf("\n%d",a||b);
printf("\n%d",a^b);
printf("\n%d",10<<2);
printf("\n%d",10<<3);
printf("\n%d",10>>2);
printf("\n%d",~a);
printf("\n%d",~-3);
return 0;
}
Output:
4
1
1
40
80
2
-6
2
Incement operators increases the operand by 1 and assign to the value to the operand.
Example:i++ =>i=i+1
Program:
#include <stdio.h>
int main()
{
int a = 10;
int b = 30;
int c=a++;
int d=++b;
printf("\n%d",a);
printf("\n%d",b);
printf("\n%d",c);
printf("\n%d",d);
return 0;
}
Output:
11
31
10
31
Program:
#include <stdio.h>
int main()
{
int a = 10;
int b = 30;
int c=a++;
int d=++a;
printf("\n%d",a);
printf("\n%d",b);
printf("\n%d",c);
printf("\n%d",d);
return 0;
}
Output:
12
30
10
12
In some cases we may need to repeat the same set of lines again and again instead of executing the
next sequence. This also helps in reducing the lines of code without repeating the same instructions
again and again.
if() TRUE
If() TRUE
else if()
if()
else FALSE
elseif()
if()
else if()
else if() FALSE
else
else if()
if()
else if()
else if() TRUE
else
if()
if() TRUE
else
if elavuation of true:
Any number any character any string, will always evaluate to true expect 0
if(‘a’)
if(‘david’)
if(‘-6’)
id(‘5’)
Any variable in if considers the value inside the variable to evaluate the if block.
int a=7;
if(a)
int a=1;
if( - - a) FALSE
int a=1;
if(a - -) TRUE
Program:
#include <stdio.h>
int main()
{
int a;
scanf("%d",&a);
if (a>0)
{
printf("Positive");
}
else if (a==0)
{
printf("Neutral");
}
else{
printf("Negative");
}
return 0;
}
Output:
10
Positive
-10
Negative
0
Neutral
Program:
#include <stdio.h>
int main()
{
int a;
scanf("%d",&a);
if (a>=1000 & a<=4999)
{
printf("Discount = $50");
printf("\nTotal Amount=%d",a-50);
}
else if (a>=5000 & a<=9999)
{
printf("Discount = $100");
printf("\nTotal Amount=%d",a-100);
}
else if (a>=10000)
{
printf("Discount = $500");
printf("\nTotal Amount=%d",a-500);
}
else{
printf("No discount available");
printf("\nTotal Amount=%d",a);
}
return 0;
}
Output:
10200
Discount = $500
Total Amount=9700
Program:
#include <stdio.h>
int main()
{
int amt,No2000=0,No500=0,No200=0,No100=0,No50=0,No20=0,No10=0,No5=0,No2=0,No1=0;
scanf("%d",&amt);
if (amt>=2000)
{
No2000=amt/2000;
amt=amt-(No2000*2000);
//amt=amt%2000;
}
if (amt>=500)
{
No500=amt/500;
amt=amt-(No500*500);
}
if (amt>=200)
{
No200=amt/200;
amt=amt-(No200*200);
}
if (amt>=100)
{
No100=amt/100;
amt=amt-(No100*100);
}
if (amt>=50)
{
No50=amt/50;
amt=amt-(No50*50);
}
if (amt>=20)
{
No20=amt/20;
amt=amt-(No20*20);
}
if (amt>=10)
{
No10=amt/10;
amt=amt-(No10*10);
}
if (amt>=5)
{
No5=amt/5;
amt=amt-(No5*5);
}
if (amt>=2)
{
No2=amt/2;
amt=amt-(No2*2);
}
if (amt>=1)
{
No1=amt/1;
amt=amt-(No1*1);
}
printf("\nNo2000*%d=%d",No2000,No2000*2000);
printf("\nNo500*%d=%d",No500,No500*500);
printf("\nNo200*%d=%d",No200,No200*200);
printf("\nNo100*%d=%d",No100,No100*100);
printf("\nNo50*%d=%d",No50,No50*50);
printf("\nNo20*%d=%d",No20,No20*20);
printf("\nNo10*%d=%d",No10,No10*10);
printf("\nNo5*%d=%d",No5,No5*5);
printf("\nNo2*%d=%d",No2,No2*2);
printf("\nNo1*%d=%d",No1,No1*1);
printf("\nTotal notes:%d",No2000+No500+No200+No100+No50+No20+No10+No5+No2+No1);
return 0;
}
Output:
9999
No2000*4=8000
No500*3=1500
No200*2=400
No100*0=0
No50*1=50
No20*2=40
No10*0=0
No5*1=5
No2*2=4
No1*0=0
Total notes:15
Program:
#include <stdio.h>
int main()
{
int amt=0, x=0, y=0, no5=0, no1=0;
scanf("%d%d%d",&amt,&x,&y);
if (amt>=5)
{
no5=amt/5;
if (no5>x)
{
no5=x;
amt=amt-(no5*5);
}
else{
amt=amt%5;
}
no1=amt/1;
if (no1>y)
{
printf("\n-1");
}
else{
amt=amt%1;
printf("\nNo5*%d=%d",no5,no5*5);
printf("\nNo1*%d=%d",no1,no1*1);
}
}
else
{
no1=amt/1;
if (no1>y)
{
printf("\n-1");
}
else{
amt=amt%1;
printf("\nNo5*%d=%d",no5,no5*5);
printf("\nNo1*%d=%d",no1,no1*1);
}
}
return 0;
}
Output:
27
5
10
No5*5=25
No1*2=2
int main(){
int avail5=0,avail1=0,amt=0,no5=0,no1=0;
scanf("%d%d%d",&amt,&avail5,&avail1);
if(amt>=5){
no5=amt/5;
if(no5>avail5){
no5=avail5;
}
amt=amt-(no5*5);
}
if(amt>=1){
no1=amt/1;
if(no1>avail1){
no1=avail1;
}
amt=amt-(no1*1);
}
if(amt==0){
printf("\n%d\n%d",no5,no1);
}
else{
printf("-1");
}
return 0;
}
Program:
#include <stdio.h>
int main()
{
int num;
scanf("%d",&num);
if (num%3==0 && num%5==0)
{
printf("Zoom");
}
else if (num%3==0)
{
printf("Zip");
}
else if (num%5==0)
{
printf("Zap");
}
else
{
printf("Invalid");
}
return 0;
}
Output:
1008
Zip
Program:
#include <stdio.h>
int main()
{
int a,b;
scanf("%d%d",&a,&b);
if (a==b)
{
printf("%d",a*b);
}
else
{
printf("%d",2*(a+b));
}
return 0;
}
Output:
2
3
10
Program:
#include <stdio.h>
int main()
{
int num;
scanf("%d",&num);
(num%2==0)?(num%10==0)?printf("Yes"):printf("No"):printf("Odd");
return 0;
}
Output:
20
Yes
Program:
#include <stdio.h>
int main()
{
int a,b,c;
scanf("%d%d",&a,&b);
c=(a==b)?a+b:2*(a+b);
printf("%d",c);
return 0;
}
Output:
6
8
28
SWITCH CASE:
Switch case helps in eliminating multiple options of execution. This allows in executing only the
desired path of execution with respect to the choice that is given to pick from the multiple option. A
switch case should be configured with all the expected path of execution depending upon the inputs
we make process. Case labels in switch should always be an integer constant representation.
Program:
#include <stdio.h>
int main()
{
int num;
printf("1.Enquiry\n2.Just Browsing\n3.Support\n");
scanf("%d",&num);
switch(num)
{
case 1:printf("\nPlease drop your email");
case 2:printf("\nPoke around I'm here if you need");
case 3:printf("\nPlease wait our rep will join you");
}
return 0;
}
Output:
1.Enquiry
2.Just Browsing
3.Support
2
Poke around I'm here if you need
Please wait our rep will join you]
In the above code the switch block works fine but only exhibits only the partial elimination since we
have no break. To ensure a complete elimination and obtain desired output, we have to use break in
the switch.
Program:
#include <stdio.h>
int main()
{
int num;
printf("1.Enquiry\n2.Just Browsing\n3.Support\n\n");
scanf("%d",&num);
switch(num)
{
case 1:printf("\nPlease drop your email");
break;
case 2:printf("\nPoke around I'm here if you need");
break;
case 3:printf("\nPlease wait our rep will join you");
}
return 0;
}
Output:
1.Enquiry
2.Just Browsing
3.Support
2
Poke around I'm here if you need
Program:
#include <stdio.h>
int main()
{
int num;
printf("1.Enquiry\n2.Just Browsing\n3.Support\n\n");
scanf("%d",&num);
switch(num)
{
case 1:printf("\nPlease drop your email");
break;
case 2:printf("\nPoke around I'm here if you need");
break;
case 3:printf("\nPlease wait our rep will join you");
default:printf("\nEnter a valid data");
}
return 0;
}
Output:
1.Enquiry
2.Just Browsing
3.Support
4
Enter a valid data
Program:
#include <stdio.h>
int main()
{
char num;
printf("a.Enquiry\nb.Just Browsing\nc.Support\n\n");
scanf("%c",&num);
switch(num)
{
case 'a':printf("\nPlease drop your email");
break;
case 'b':printf("\nPoke around I'm here if you need");
break;
case 'c':printf("\nPlease wait our rep will join you");
default:printf("\nEnter a valid data");
}
return 0;
}
Output:
a.Enquiry
b.Just Browsing
c.Support
b
Poke around I'm here if you need
Case label:
A case label can also be a constant variable declared using #define and when decoded will result to
an integer constant.
Program:
#include <stdio.h>
#define ch 'a'
#define ch2 'b'
#define ch3 'c'
int main()
{
char num;
printf("a.Enquiry\nb.Just Browsing\nc.Support\n\n");
scanf("%c",&num);
// char ch='a',ch2='b',ch3='c'; leads to error
switch(num)
{
case ch:printf("\nPlease drop your email");
break;
case ch2:printf("\nPoke around I'm here if you need");
break;
case ch3:printf("\nPlease wait our rep will join you");
default:printf("\nEnter a valid data");
}
return 0;
}
Output:
a.Enquiry
b.Just Browsing
c.Support
a
Please drop your email
Program:
#include <stdio.h>
int main()
{
char ch;
scanf("%c",&ch);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
printf("Vowel");
else
printf("Consonants");
return 0;
}
Output:
f
Consonants
LOOPING:
When same set of instructions is repeated again and again and it follows a pattern and we put a set
of code into a loop.
C supports 3 different looping techniques,
for
while
do while
for and while loop are entry check loop whereas do while is an exit check loop. for and while are
very strict loop and it executes the loop only if the condition is satisfied. A do while loop is an exit
check loop and thus executes the loop atleast once before checking the condition.
Program:
#include <stdio.h>
int main()
{
int i;
for(i=1;i<=5;i++)
{
printf("\n%d",i);
}
printf("\n%d",i);//6
return 0;
}
Output:
1
2
3
4
5
6
Program:
#include <stdio.h>
int main()
{
int i;
i=1;
while(i<=5)
{
printf("\n%d",i);
i++;
}
return 0;
}
Output:
1
2
3
4
5
Program:
#include <stdio.h>
int main()
{
int i;
i=1;
do
{
printf("\n%d",i);
i++;
}
while(i<=5);
return 0;
}
Output:
1
2
3
4
5
Program:
#include <stdio.h>
int main()
{
int i,num,sum=0;
scanf("%d",&num);
for(i=1;i<=num;i++)
{
sum=sum+i;
}
printf("%d",sum);
return 0;
}
Output:
5
15
Program:
#include <stdio.h>
int main()
{
int i,num,sum=0;
scanf("%d",&num);
for(i=2;i<=num;i=i+2)
{
sum=sum+i;
}
printf("%d",sum);
return 0;
}
Output:
8
20
Program:
#include <stdio.h>
int main()
{
int i,num,sum=0;
scanf("%d",&num);
for(i=1;i<=num;i=i+2)
{
sum=sum+i;
}
printf("%d",sum);
return 0;
}
Output:
9
25
Program:
#include <stdio.h>
int main(){
int mul=0,i=0,table=0,start=0,end=0;
printf("Table:");
scanf("%d",&table);
printf("Start:");
scanf("%d",&start);
printf("End:");
scanf("%d",&end);
for(i=start;i<=end;i++)
{
mul=i*table;
printf("\n%dx%d=%d",table,i,mul);
}
return 0;
}
Output:
Table:2
Start:1
End:5
2x1=2
2x2=4
2x3=6
2x4=8
2x5=10
Program:
#include <stdio.h>
int main(){
long int i,num,fac=1;
scanf("%ld",&num);
for(i=1;i<=num;i++){
fac=fac*i;
}
printf("%ld",fac);
return 0;
}
Output:
5
120
Program:
#include <stdio.h>
#include <string.h>
int main()
{
int i,start,end;
char choice[10];
scanf("%d%d",&start,&end);
printf("Enter even or odd: ");
scanf("%s",choice[10]);
if(strcmp(choice,"odd")==0)
{
if(start%2==0)
{
start=start+1;
}
for(i=start;i<=end;i=i+2)
{
printf("%d",i);
}
}
else
{
if(start%2!=0)
{
start=start+1;
}
for(i=start;i<=end;i=i+2)
{
printf("%d",i);
}
}
return 0;
}
Output:
1
5
Enter even or odd: even
2
4
Program:
#include <stdio.h>
int main()
{
int i;
for(i=1;i<=5;i++);
{
printf("%d",i);
}
return 0;
}
Output:
6
Program:
#include <stdio.h>
int main() {
int a;
char ch;
for(a=1;a<=3;a++)
{
for (ch= 'a';ch<='c';ch++)
{
printf("\n %d %c",a,ch);
}
}
return 0;
}
Output:
1a
1b
1c
2a
2b
2c
3a
3b
3c
Program:
#include <stdio.h>
int main()
{
int i;
for(i=1;i<=5;i++)
{
printf("\n%d",i);
}
printf("\nBreak");
for(i=1;i<=5;i++)
{
if(i==3)
{
continue;
}
}
return 0;
}
Output:
1
2
3
4
5
Break
Program:
#include <stdio.h>
int main(){
inti,a,start,end;
printf("Start: ");
scanf("%d",&start);
printf("End: ");
scanf("%d",&end);
for(i=start;i<=end;i++)
{
for(a=1;a<=10;a++)
{
printf("\n%dx%d=%d",i,a,i*a);
}
}
return 0;
}
Output:
Start: 1
End: 2
1x1=1
1x2=2
1x3=3
1x4=4
1x5=5
1x6=6
1x7=7
1x8=8
1x9=9
1x10=10
2x1=2
2x2=4
2x3=6
2x4=8
2x5=10
2x6=12
2x7=14
2x8=16
2x9=18
2x10=20
Program:
#include <stdio.h>
int main()
{
int i, cpin=1234, pin;
printf("Welcome to Bank of Kishaanth :)");
printf("\nEnter your pin number: ");
scanf("%d",&pin);
if (pin==cpin)
{
printf("Logged In...");
}
else
{
for(i=1;i<3;i++)
{
printf("Wrong attempt!");
printf("\nEnter your pin number: ");
scanf("%d",&pin);
if (pin==cpin){
printf("Logged In...");
break;
}
else if (i==2)
{
printf("\nYour Card has been blocked!\nVisit your bank!");
}
}
}
return 0;
}
Output:
Welcome to Bank of Baroda:)
Enter your pin number: 123
Wrong attempt!
Enter your pin number: 234
Wrong attempt!
Enter your pin number: 456
NUMBER PROGRAMS:
Every number is always split from the last
Every number divided by 10 will give the number without the last digit
Ex: 36/10=3
343/10=34
1/10=0
Ex: 36%10=6
273%10=3
3%10=3
Ex: 3*10=30
Program:
#include <stdio.h>
int main()
{
int num,d,sum=0;
scanf("%d",&num);
while(num!=0)
{
d=num%10;
sum=sum+d;
num=num/10;
}
printf("%d",sum);
return 0;
}
Output:
5692
22
Program:
#include <stdio.h>
int main()
{
int i,num;
scanf("%d",&num);
i=0;
while(num!=0)
{
num=num/10;
i++;
}
printf("%d",i);
return 0;
}
Output:
123456789
9
Program:
#include <stdio.h>
int main()
{
int num,d,mul=1;
scanf("%d",&num);
while(num!=0)
{
d=num%10;
mul=mul*d;
num=num/10;
}
printf("%d",mul);
return 0;
}
Output:
12345
120
Program:
#include <stdio.h>
int main()
{
int num,i,sum=0;
scanf("%d",&num);
for(i=1;i<num;i++)
{
if(num%i==0)
{
sum=sum+i;
}
}
if(sum==num)
{
printf("It is a perfect number");
}
else{
printf("It is not a perfect number");
}
return 0;
}
Output:
496
It is a perfect number
Program:
#include <stdio.h>
int main()
{
int i,num,flag=0;
scanf("%d",&num);
for(i=2;i<num;i++)
{
if(num%i==0)
{
printf("It is not a prime number");
flag=1;
break;
}
}
if (flag==0)
printf("\nPrime Number");
return 0;
}
Output:
45
It is not a prime number
Program:
#include <stdio.h>
#include <math.h>
int main()
{
int i,num,num1,a,j,arm,sum=0;
scanf("%d",&num);
num1=num;
for(i=0;num>0;i++)
{
num=num/10;
}
num=num1;
for(j=1;j<=i;j++)
{
a=num%10;
arm= pow(a,i);
sum= sum+arm;
num=num/10;
}
if(sum==num1)
{
printf("It is an armstrong numner!");
}
else{
printf("It is not an armstrong number!");
}
return 0;
}
Output:
45
It is not an armstrong number!
Program:
#include <stdio.h>
#include <math.h>
int main()
{
int i,num,num1,squ,ope;
scanf("%d",&num);
num1=num;
squ=pow(num,2);
for(i=0;num>0;i++)
{
num=num/10;
}
num=num1;;
if (squ%((int)pow(10,i))==num)
{
printf("\nAtomorphic Number");
}
else{
printf("\nIt is not an Atomorphic number");
}
return 0;
}
Output:
625
Atomorphic Number
Program:
#include <stdio.h>
#include <math.h>
int main()
{
int i,num,num1,a,j,dis,sum=0;
scanf("%d",&num);
num1=num;
for(i=0;num>0;i++)
{
num=num/10;
}
num=num1;
for(j=i;j<=i && j>0;j--)
{
a=num%10;
dis= pow(a,j);
sum= sum+dis;
num=num/10;
}
if(sum==num1)
{
printf("It is an disarium numnber!");
}
else{
printf("It is not an disarium number!");
}
return 0;
}
Output:
135
It is an disarium number!
Program:
#include <stdio.h>
int main()
{
int num,digit,i=0;
scanf("%d",&num);
while(num!=0){
digit=num%10;
if(digit==0){
printf("duck no\n");
i++;
break;
}
num/=10;
}
if(i==0){
printf("not a duck no");
}
return 0;
}
Output:
1010
duck no
Program:
#include <stdio.h>
int main()
{
int num,rev=0,digit,num1;
scanf("%d",&num);
num1=num;
while(num!=0){
digit=num%10;
rev=rev*10+digit;
num/=10;
}
//printf("num=%d\nrev=%d",num1,rev);
if(rev==num1){
printf("palindrome");
}
else
printf("not palindrome");
return 0;
}
Output:
111
Palindrome
Program:
//strong number
#include <stdio.h>
int main()
{
int i,j,k,initial,final,fac,digit,sum;
scanf("%d",&initial);
scanf("%d",&final);
for(i=initial;i<=final;i++)
{
j=i;
sum=0;
while(j!=0)
{
fac=1;
digit=j%10;
for(k=digit;k>0;k--)
{
fac=fac*k;
}
j=j/10;
sum=sum+fac;
}
if(sum==i)
{
printf("\n%d",i);
}
}
return 0;
}
Output:
1
200
1
2
145
PATTERN PROGRAMS:
Program:
#include <stdio.h>
int main() {
int row,col,norow;
scanf("%d",&norow);
for(row=1;row<=norow;row++){
for(col=1;col<=row;col++){
printf("*");
}
printf("\n");
}
return 0;
}
Output:
5
*
**
***
****
*****
Program:
#include <stdio.h>
int main(){
int row,col,norow;
scanf("%d",&norow);
for(row=1;row<=norow;row++){
for(col=1;col<=row;col++){
printf("%d",row);
}
printf("\n");
}
return 0;
}
Output:
5
1
22
333
4444
55555
Program:
#include <stdio.h>
int main()
{
int row,col,norow;
scanf("%d",&norow);
for(row=1;row<=norow;row++){
for(col=1;col<=row;col++){
printf("%d",col);
}
printf("\n");
}
return 0;
}
Output:
5
1
12
123
1234
12345
Program:
#include <stdio.h>
int main()
{
int row,col,norow,count=1;
scanf("%d",&norow);
for(row=1;row<=norow;row++){
for(col=1;col<=row;col++){
printf("%d ",count);
count++;
}
printf("\n");
}
return 0;
}
Output:
5
1
23
456
7 8 9 10
11 12 13 14 15
Program:
#include <stdio.h>
int main()
{
int row,col,norow,count=1;
scanf("%d",&norow);
for(row=1;row<=norow;row++){
for(col=1;col<=row;col++){
printf("%d ",count*2);
count++;
}
printf("\n");
}
return 0;
}
Output:
5
2
46
8 10 12
14 16 18 20
22 24 26 28 30
Program:
#include <stdio.h>
int main()
{
int row,col,norow,oddcount=1,evencount=2;
scanf("%d",&norow);
for(row=1;row<=norow;row++){
for(col=1;col<=row;col++){
if(row%2==0){
printf("%d ",evencount);
evencount+=2;
}
else{
printf("%d ",oddcount);
oddcount+=2;
}
}
printf("\n");
}
return 0;
}
Output:
5
1
24
357
6 8 10 12
9 11 13 15 17
Program:
#include <stdio.h>
int main()
{
int row,col,norow,oddcount=1,evencount=2;
scanf("%d",&norow);
for(row=norow;row>=1;row--){
for(col=1;col<=norow;col++){
if(col<row){
printf(" ");
}
else{
printf("*");
}
}
printf("\n");
}
return 0;
}
Output:
5
*
**
***
****
*****
Program:
#include <stdio.h>
int main()
{
int i,row,col;
scanf("%d",&row);
for(i=row;i<=row && i>0;i--)
{
for(col=1;col<=i;col++)
{
printf("*");
}
printf("\n");
}
return 0;
}
Output:
5
*****
****
***
**
*
Program:
#include <stdio.h>
int main() {
int row,col,i,count=1;
scanf("%d",&row);
for(i=1;i<=row;i++)
{
count=i;
for(col=1;col<=row;col++)
{
if(count==row)
count=0;
printf("%d",count);
count++;
}
printf("\n");
}
return 0;
}
Output:
5
12340
23401
34012
40123
01234
Program:
#include <stdio.h>
int main()
{
int row,col,norow,count=1;
char ch='a';
scanf("%d",&norow);
for(row=1;row<=norow;row++){
for(col=1;col<=norow;col++){
if(row==1||row==norow){
printf("%c",ch);
ch++;
}
else{
if(col==1||col==norow){
printf(" ");
}
else{
printf("%d",count);
count++;
}
}
}
printf("\n");
}
return 0;
}
Output:
5
abcde
123
456
789
Fghij
Program:
#include <stdio.h>
int main()
{
int nrow,col,row,evencount=2,oddcount=1;
scanf("%d",&nrow);
for(row=1;row<=nrow;row++)
{
if(row%2!=0)
{
for(col=1;col<nrow;col++)
{
printf("\t%d",evencount);
evencount=evencount+2;
}
}
if(row%2==0)
{
for(col=1;col<nrow-1;col++)
{
printf("\t%d",oddcount);
oddcount=oddcount+2;
}
}
printf("\n");
}
return 0;
}
Output:
5
2 4 6 8
1 3 5
10 12 14 16
7 9 11
18 20 22 24
Program:
#include <stdio.h>
int main()
{
int row,nrow,mrow,count=1,col;
scanf("%d",&nrow);
for(row=1;row<=nrow;row++)
{
mrow=nrow*row;
for(col=1;col<=nrow;col++)
{
if(row%2!=0)
{
printf("\t%d",count);
count++;
}
if(row%2==0)
{
printf("\t%d",mrow);
mrow--;
count++;
}
}
printf("\n");
}
return 0;
}
Output:
4
1 2 3 4
8 7 6 5
9 10 11 12
16 15 14 13
ARRAYS:
Array data structures stores the data of similar data type. The array allocates continuous memory
allocation for the datas. Arrays are static memory allocation, hence cannot be resized.
Each element of the array are accessed using index or subscript value. Every element of the array
has equal memory allocation depending upon the datatype of the array.
Array index always starts with zero and array index ends at arraylength-1.
Array throws index out of bound exception, when trying to access the index that is not allocated to
the index.
70 80 90
0 1 2
4 4 4
12
Program:
#include <stdio.h>
int main()
{
long int a=10;
long int marks[]={70,80,90};
printf("\na:%d",a);
printf("\n&a:%d",&a);
printf("\nmarks:%d",marks);
printf("\nAddress marks:%d",&marks);
printf("\nmarks[0]:%d",marks[0]);
printf("\nAddress of marks[0]:%d",&marks[0]);
printf("\nmarks[1]:%d",marks[1]);
printf("\nAddress of marks[1]:%d",&marks[1]);
printf("\nmarks[2]:%d",marks[2]);
printf("\nAddress of marks[2]:%d",&marks[2]);
return 0;
}
Output:
a:10
&a:-2143926968
marks:-2143926992
Address marks:-2143926992
marks[0]:70
Address of marks[0]:-2143926992
marks[1]:80
Address of marks[1]:-2143926984
marks[2]:90
Address of marks[2]:-2143926976
Program:
#include <stdio.h>
int main()
{
int n;
scanf("%d",&n);
int marks[n];
for(int i=0;i<n;i++)
{
scanf("%d",&marks[i]);
}
for(int i=0;i<n;i++)
{
printf("%d\t",marks[i]);
}
return 0;
}
Output:
3
78 66 89
78 66 89
Program:
#include <stdio.h>
int main()
{
int sum;
int num[]={30,11,15,10,20};
sum= num[0]+num[1]+num[2]+num[3]+num[4];
printf("%d",sum);
return 0;
}
Output:
86
Program:
#include <stdio.h>
int main()
{
int n,sum=0;
scanf("%d",&n);
int marks[n];
for(int i=0;i<n;i++)
{
scanf("%d",&marks[i]);
if(marks[i]%2==0)
{
sum=sum+marks[i];
}
}
printf("%d",sum);
return 0;
}
Output:
3
78 66 58
202
Program:
#include <stdio.h>
int main() {
int n,sum=0;
scanf("%d",&n);
int marks[n];
for(int i=0;i<n;i++)
{
scanf("%d",&marks[i]);
if(i%2==0)
{
sum=sum+marks[i];
}
}
printf("%d",sum);
return 0;
}
Output:
5
22 44 66 34 78
166
Program:
#include <stdio.h>
int main()
{
int noelem,felem,pnum,i;
printf("Enter the no of elements:");
scanf("%d",&noelem);
printf("Enter the first element:");
scanf("%d",&felem);
int n=felem;
int series[noelem];
series[0]=felem;
for(i=1;i<noelem;i++)
{
series[i]=(2*(n-1));
n=series[i];
}
printf("Enter the element to be printed:");
scanf("%d",&pnum);
if(pnum>noelem||pnum<1)
{
printf("Invalid Element");
}
else
{
printf("%d",series[pnum-1]);
}
return 0;
}
Output:
Enter the no of elements:5
Enter the first element:5
Enter the element to be printed:4
26
Program:
#include <stdio.h>
int main()
{
int size,i,j,max;
scanf("%d",&size);
int arr[size];
for(i=0;i<size;i++)
{
scanf("%d",&arr[i]);
}
max=arr[0];
for(j=1;j<size;j++)
{
if(max<arr[j])
{
max=arr[j];
}
}
printf("%d",max);
return 0;
}
Output:
5
45 66 35 89 66
89
Program:
#include <stdio.h>
int main()
{
int size,i,j,min;
scanf("%d",&size);
int arr[size];
for(i=0;i<size;i++)
{
scanf("%d",&arr[i]);
}
min=arr[0];
for(j=1;j<size;j++)
{
if(min>arr[j])
{
min=arr[j];
}
}
printf("%d",min);
return 0;
}
Output:
4
33 62 70 54
33
Program:
#include <stdio.h>
int main()
{
int size,i,ele1,ele2;
scanf("%d",&size);
int arr[size];
for(i=0;i<size;i++)
{
scanf("%d",&arr[i]);
}
printf("Elements to be multiplied:");
scanf("%d %d",&ele1,&ele2);
printf("Product:%d",arr[ele1-1]*arr[ele2-1]);
return 0;
}
Output:
5
25847
Elements to be multiplied:4 2
Product:20
Program:
#include <stdio.h>
int main()
{
int n,i,j,k,dummy=0;
scanf("%d",&n);
int arr[n];
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(arr[i]>arr[j])
{
dummy=arr[i];
arr[i]=arr[j];
arr[j]=dummy;
}
}
}
for(k=0;k<n;k++)
{
printf("\t%d",arr[k]);
}
return 0;
}
Output:
5
33 64 38 61 26
26 33 38 61 64
Program:
#include <stdio.h>
int main()
{
int n,i,j,k,dummy=0;
scanf("%d",&n);
int arr[n];
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(arr[i]<arr[j])
{
dummy=arr[i];
arr[i]=arr[j];
arr[j]=dummy;
}
}
}
for(k=0;k<n;k++)
{
printf("\t%d",arr[k]);
}
return 0;
}
Output:
5
62 93 30 41 33
93 62 41 33 30
Program:
#include <stdio.h>
int main()
{
int size,i,elem,dummy=0;
scanf("%d",&size);
int arr[size];
for(i=0;i<size;i++)
{
scanf("%d",&arr[i]);
}
printf("Element to be searched: ");
scanf("%d",&elem);
for(i=0;i<size;i++)
{
if(elem==arr[i])
{
printf("Element Fetched!");
printf("\nIndex of the element: %d",i);
dummy++;
}
}
if(dummy==0)
{
printf("Invalid Element");
}
return 0;
}
Output:
5
23 56 84 33 90
Element to be searched: 56
Element Fetched!
Index of the element: 1
1D, 2D, 3D ARRAY:
1D Array: int marks[]={70,80,90};
Marks 70 80 90
2D Array:int cls[][]={{70,80,90},{71,81,91}};
Marks 70 80 90 71 81 91
Student 0 1
Marks 70 80 90 71 81 91 72 82 92 73 83 93
Student 0 1 0 1
Class 0 1
Program:
#include <stdio.h>
int main()
{
int sub,nsub;
scanf("%d",&nsub);
int marks[nsub];
for(sub=0;sub<nsub;sub++)
{
scanf("%d",&marks[sub]);
}
for(sub=0;sub<nsub;sub++)
{
printf("%d\t",marks[sub]);
}
return 0;
}
Output:
3
12 34 65
12 34 65
Program:
#include <stdio.h>
int main()
{
int nsub,sub,nstu,stu;
scanf("%d %d",&nstu,&nsub);
int cls[nstu][nsub];
for(stu=0;stu<nstu;stu++)
{
printf("\n Student%d",stu);
for(sub=0;sub<nsub;sub++)
{
scanf("%d",&cls[stu][sub]);
}
}
for(stu=0;stu<nstu;stu++)
{
printf("\n Student%d",stu);
for(sub=0;sub<nsub;sub++)
{
printf("%d",cls[stu][sub]);
}
}
return 0;
}
Output:
2
3
Student070 80 90
Student171 81 91
Student0708090
Student1718191
Program:
#include<stdio.h>
int main()
{
int cls,stu,sub,ncls,nstu,nsub;
scanf("%d%d%d",&ncls,&nstu,&nsub);
int dep[ncls][nstu][nsub];
for(cls=0;cls<ncls;cls++)
{
printf("\nclass%d",cls);
for(stu=0;stu<nstu;stu++)
{
printf("\nStudent%d",stu);
for(sub=0;sub<nsub;sub++)
{
scanf("%d",&dep[cls][stu][sub]);
}
}
}
for(cls=0;cls<ncls;cls++)
{
printf("\nclass%d",cls);
for(stu=0;stu<nstu;stu++)
{
printf("\nStudent%d",stu);
for(sub=0;sub<nsub;sub++)
{
printf("\t%d\t",dep[cls][stu][sub]);
}
}
}
return 0;
}
Output:
1
2
3
class0
Student0 70 80 90
Student1 71 81 91
class0
Student0 70 80 90
Student1 71 81 91
C POINTERS:
Pointers are variables that stores address of another variable. These pointer variables are always
represented with a * symbol when it is declared.
A pointer variable has a data type that is same as the data type of the variable that it is assigned to.
Every pointer variable is always declared with data type of the variable it holds pointed to. To enable
seamless operation with the data that it holds.
However the memory allocation of any pointer variable will always be the same.
This is done because all the pointer variable irrespective of the data type will always hold only the
address of another variable.
Program:
#include <stdio.h>
int main()
{
int a=10;
int *p;
p=&a;
int **b;
b=&p;
int ***c;
c=&b;
printf("\nValue of a %d",a);
printf("\nValue of Adress of a %d",&a);
printf("\nValue of p %d",p);
printf("\nValue of Adress of p %d",&p);
printf("\nValue of *p %d",*p);
printf("\nValue of **b %d",**b);
printf("\nValue of ***c %d",***c);
return 0;
}
Output:
Value of a 10
Value of Adress of a 229475436
Value of p 229475436
Value of Adress of p 229475440
Value of *p 10
Value of **b 10
Value of ***c 10
Program:
#include <stdio.h>
int main()
{
int a=10;
char ch ='a';
int *p=&a;
char *c=&ch;
double t=780.90;
double *d=&t;
printf("\nSize of a:%d",sizeof(a));
printf("\nSize of p:%d",sizeof(p));
printf("\nSize of ch:%d",sizeof(ch));
printf("\nSize of t:%d",sizeof(t));
printf("\nSize of d:%d",sizeof(d));
printf("\nSize of c:%d",sizeof(c));
return 0;
}
Output:
Size of a:4
Size of p:8
Size of ch:1
Size of t:8
Size of d:8
Size of c:8
Program:
#include <stdio.h>
int main()
{
int a=10;
int *p=&a;
int arr[]={10,20,30};
int *c=arr;
int *d=arr;
printf("*p+1:%d",*p+1);
printf("\n*(p+1):%d",*(p+1));
for(int i=0;i<3;i++)
{
printf("\n*(c+i)->arr[%d]:%d",i,*(c+i));
printf("\n*c+i->arr[%d]:%d",i,*c+i);
printf("\n*d++->arr[%d]:%d",i,*d++);
}
return 0;
}
Output:
*p+1:11
*(p+1):0
*(c+i)->arr[0]:10
*c+i->arr[0]:10
*d++->arr[0]:10
*(c+i)->arr[1]:20
*c+i->arr[1]:11
*d++->arr[1]:20
*(c+i)->arr[2]:30
*c+i->arr[2]:12
*d++->arr[2]:30
CALL BY VALUE & CALL BY REFERENCE:
A function call usually passes a copy of value of the variable that is to be sent for another function to
perform an operation.
These kinds of operations operates only in the copied space in the function incrementation and does
not reflect in the original data space.
If the changes is to be held in the main copy then the value should be returned after the function
call.
However the return in C works for only one value. Hence for multiple value changes the variable
should be passed as a reference.
Program:Call by Value
#include <stdio.h>
int increment (int sal)
{
sal = sal+500;
printf("\n sal:%d",sal);
}
int main()
{
int sal;
scanf("%d",&sal);
increment(sal);
printf("\n After increment: %d",sal);
return 0;
}
Output:
Call By Value Call By Reference
Passes only the duplicate copy. Passes the reference of address
variable.
The calling function will always have its It affects the data of calling function.
data unaffected.
Adviced when the changes need not to Best option when we need the data
be reflected in the original data or only change in calling function to be changes
one data needs to be changed using based on the operation of function call.
return
1000
sal:1500
After increment: 1000
Program:
#include <stdio.h>
int increment (int *sal)
{
*sal = *sal+500;
printf("\n sal:%d",*sal);
}
int main()
{
int sal;
scanf("%d",&sal);
increment(&sal);
printf("\n After increment:%d",sal);
return 0;
}
Output:
1000
sal:1500
After increment:1500
STRINGS:
C does not support a special data type called strings. It usually treats strings as character array and
every string always ends with a null character (‘\0).
While accepting a string input the scanf reads the data only untill it reads a namespace.
Hence while reading strings it is always advisable to read datas using "fgets" and “fputs”.
Program:
#include <stdio.h>
int main()
{
char name[67]="John"; //Direct Declaration
char name2[100];
scanf("%s",name2);
printf("\n%s",name);
printf("\n%s",name2);
return 0;
}
Output:
John Derry
John
John
Program:
#include <stdio.h>
int main()
{
char name[67]="John";
char series[100];
fgets(series, sizeof(series), stdin);
printf("\n%s",name);
printf("\n%s",series);
fputs(series,stdout);
return 0;
}
Output:
John Derry
John
John Derry
John Derry
Program:
#include <stdio.h>
#include <string.h>
int main()
{
char name[67]="John";
char series[100];
fgets(series, sizeof(series), stdin);
printf("\n%s",name);
printf("\n%s",series);
fputs(series,stdout);
printf("\n%ld",strlen(name));
strcat(name,"snow");
printf("\n%s",name);
strcpy(name,"Danerys");
printf("\n%s",name);
return 0;
}
Output:
John Derry
John
John Derry
John Derry
4
Johnsnow
Danerys
STRUCTURES:
Program:
#include <stdio.h>
#include <string.h>
struct Employee{
int empid;
char name[20];
double salary;
};
int main()
{
int a=10;
struct Employee emp;
emp.empid=2204000;
strcpy(emp.name,"John");
emp.salary=25000;
printf("\nEmpid:%d",emp.empid);
printf("\nName:%s",emp.name);
printf("\nSalary:%lf",emp.salary);
return 0;
}
Output:
Empid:2204000
Name:John
Salary:25000.000000
Program:
#include <stdio.h>
#include <string.h>
struct Employe{
int empid;
char name [59];
float salary;
};
int display(struct Employe e)//Passing structure Employee to the function
{
printf("\n Empid:%d",e.empid);
printf("\n Name:%s",e.name);
printf("\n salary:%f", e.salary);
}
int main() {
int a=10;
struct Employe empl;
struct Employe emp={5642, "John", 500000};
empl.empid=5241;
strcpy(empl.name, "Derry");
empl.salary=250000;
display(emp);
display(empl);
return 0;
}
Output:
Empid:5642
Name:John
salary:500000.000000
Empid:5241
Name:Derry
salary:250000.000000
Program:
#include <stdio.h>
#include<string.h>
typedef struct Employe{
int empid;
char name [58];
float salary;
}Emp; //Emp means struct Employee
int display (Emp e);
int display (Emp e)
{
printf("\n Empid:%d",e.empid);
printf("\n Name:%s",e.name);
printf("\n salary:%f", e.salary);
}
int main() {
Emp emp1;
Emp emp2={5642,"John",500000};
emp1.empid=5241;
strcpy(emp1.name,"Derry");
emp1.salary=250000;
display(emp1);
display(emp2);
return 0;
}// typedef is a keyword that is used to name our data type as per our convenience. This typedef is
very essential when the data type name is too long.
Output:
Empid:5241
Name:Derry
salary:250000.000000
Empid:5642
Name:John
salary:500000.000000
UNION:
Unions are just like structures and the difference lies in memory space allocation. In structures there
is a separate memory available for all the members of the structure.
Hence the size of the structure will be always equal to the sum of size of all the elements in the
structure.
Union does not allocates separate memory space for each of its members. Union picks the largest
data type of the member and allocate only that space.
When data is stored in union since it has one storage location it keeps on overwriting the existing
data with the latest update through the union.
Hence while accessing union it is very important to use the data before it is being overwritten by any
other member.
Program:
#include <stdio.h>
#include <string.h>
struct Employee{
int empid;
char name[20];
double salary;
};
union EmployeeExample{
int empid;
char name[20];
double salary;
};
int main()
{
struct Employee emp1;
union EmployeeExample emp2;
emp1.empid=101;
strcpy(emp1.name,"John");
emp1.salary=150000;
printf("\nEmpid:%d",emp1.empid);
printf("\nName:%s",emp1.name);
printf("\nSalary:%lf",emp1.salary);
emp2.empid=102;
strcpy(emp1.name,"Jack");
emp2.salary=250000;
printf("\nEmpid:%d",emp2.empid);
printf("\nName:%s",emp2.name);
printf("\nSalary:%lf",emp2.salary);
printf("\nsize of %ld",sizeof(emp1));
printf("\nsize of %ld",sizeof(emp2));
return 0;
}
Output:
Empid:101
Name:John
Salary:150000.000000
Empid:0
Name:
Salary:250000.000000
size of 32
size of 24
Program:
#include <stdio.h>
#include<string.h>
struct Employe{
int empid;
char name [50];
float salary;
} typedef Emp;
int main()
{
Emp emp1[3];
emp1[0].empid=101;
strcpy(emp1[0].name, "will");
emp1[0].salary=15000;
emp1[1].empid=102;
strcpy(emp1[1].name, "jack");
emp1[1].salary=15000;
emp1[2].empid=103;
strcpy(emp1[2].name, "rohit");
emp1[2].salary-15000;
for(int i=0;i<3;i++)
{
printf("\nEmploye Id:%d", emp1[i].empid);
printf("\nEmploye Name: %s", emp1[i].name);
printf("\nEmploye salary:%.2f",emp1[i].salary);
}
return 0;
}
Output:
Employe Id:101
Employe Name: will
Employe salary:15000.00
Employe Id:102
Employe Name: jack
Employe salary:15000.00
Employe Id:103
Employe Name: rohit
Employe salary:21278425088.00
Program:
#include <stdio.h>
#include <string.h>
struct Employee{
int empid;
char name[20];
double salary;
};
int main()
{
int a=10;
int *b;
b=&a;
printf("%d",*b);
struct Employee emp1;
struct Employee *emp2;
emp2=&emp1;
emp1.empid=101;
strcpy(emp1.name,"John");
emp1.salary=150000;
printf("\nEmpid:%d",emp2->empid);
printf("\nName:%s",emp2->name);
printf("\nSalary:%lf",emp2->salary);
return 0;
}
// -> is used to represent pointer access of structures
Output:
10
Empid:101
Name:John
Salary:150000.000000