Structures
USER DEFINED DATA TYPE
struct employee {
int emp_id;
char name ;
float salary;
};
#include<stdio.h>
struct pokemon{
int attack;
int speed;
int hp;
char class; //A B C
};
int main(){
struct pokemon pika;
pika.attack=60;
pika.speed=40;
pika.hp=30;
pika.class= 'A';
printf("attack speed hp class %d %d %d %c\n",pika.attack,pika.speed,pika.hp,pika.class);
struct pokemon ash;
ash.attack=30;
ash.speed= 50;
ash.hp=90;
ash.class= 'B';
printf("attack speed hp class %d %d %d %c",ash.attack,pika.speed,ash.hp, ash.class);
}
//AISE HI AAP SIRF STRUCT POKEMON AUR USKE AAGE EK PECIFIER LAGA KE INFINITY INFORMATION STORE KAR SAKTE HAI
YE JUB HUME INPUT LEA HO VALUES KO note %c likh rehe ho tho ek space dedena kyu ki koi white space name ki chiz hoti hai NOTE =%C BUS EK CHARACTER PRINT KARE GA BUT TUMEHE PURA NAME PRINT KAR NA HO THO EK CHAR ARRAY BA NAO USKA SIZE RAKHO AUR %S KE SAATH PRINT KARAO.
printf("enter attack speed hp and name ");
scanf("%d%d%d %c", &pika.attack,&pika.speed,&pika.hp,&pika.class);
printf("attack speed hp class %d %d %d %c\n",pika.attack,pika.speed,pika.hp,pika.class);
SAME PROGRAME HAI BUS PIKA AUR ASH UPPAR HI DEFIE KAR DIYA
#include<stdio.h>
struct pokemon{
int attack;
int speed;
int hp;
char class; //A B C
} pika, ash; // ye TYPEDEF nhi hai samajh bhai niche dekh compare kar ye thoda alag hai
int main(){
pika.attack=60;
pika.speed=40;
pika.hp=30;
pika.class= 'A';
printf("attack speed hp class %d %d %d %c\n",pika.attack,pika.speed,pika.hp,pika.class);
;
ash.attack=30;
ash.speed= 50;
ash.hp=90;
ash.class= 'B';
printf("attack speed hp class %d %d %d %c",ash.attack,pika.speed,ash.hp, ash.class);
}
FOR PRINTING NAME
strcpy(a1.name,"habits ");
NICHE WALE CODE MAI WITHOUT STRCPY USE KAR KE NAME INPUT LIYE AUR PRINT KARWAYA
#include<stdio.h>
#include<string.h>
struct Person{
char name[100];
int salary;
int age;
}a1;
int main(){
printf("enter employee details 1)name 2)salary 3)age \n");
scanf("%s %d %d", a1.name, &a1.salary, &a1.age); //a1.name ke pehele '&' nhi use hoga
printf("your input is \n %s\n %d\n %d\n ",a1.name, a1.salary, a1.age);
}
TYPEDEF KEYWORD
typedef is used for renaming something
NOTE :look apna collage notes.
#include<stdio.h>
typedef float realNumber;
int main(){
int x;
realNumber y = 3.14; //simpally humne float ko ek naya name de diya realNumber ye jo realNumber hai vo float ka kam kare ga
//basically typedef is used for renaming something.
printf("%f",y);
}
TYPEDEF KEYWORD apna collage
used to create a alias for data types
in simple word jo bhi hum structure ana rahe hai uske liye ek nick name dena for example agar hum ne koi bade nam ka structure banaya par hum uss bade name ko bar bar likh na nhi chahate so uske liye ek shortform bana denge
#include <stdio.h>
#include <string.h>
typedef struct student { //bhai yaha type def likh na mat bhul na bus
int roll;
float cgpa;
char name[100];
} stu;
int main(){
stu a1; //ya struct student a1 ki jage bus stu a1 likha bus
a1.roll = 34;
a1.cgpa = 8.1;
strcpy(a1.name, "ashish");
printf(" roll no is : %d", a1.roll);
return 0;
}
thoda galat hai janae se pehele kar fir se
#include <stdio.h>
#include<string.h>
struct adress{
int hoouseno;
int blockno;
char city[100];
char state[100];
};
void print(struct address a1);
int main() {
struct address a1[100];
printf("enter info here for person 1");
scanf("%d", &a1[0].houseno);
scanf("%d",&a1[0].blockno);
scanf("%d", a1.state);
scanf("%d", a1.city);
printf("enter info here for person 2");
scanf("%d", &a1[1].houseno);
scanf("%d",&a1[1].blockno);
scanf("%d", a1.state);
scanf("%d", a1.city);
printf("enter info here for person 3");
scanf("%d", &a1[2].houseno);
scanf("%d",&a1[2].blockno);
scanf("%d", a1.state);
scanf("%d", a1.city);
print(a1.[0]);
print(a1.[1]);
print(a1.[2]);
return 0;
}
void print(struct address a1){
printf("address is : %d, %d, %s, %s\n", a1.houseno, a1.blockno, a1.state, a1.city);
//make a structure to store bank account information of a customer of abc bank. also, make an alias for it
#include<stdio.h>
#include<string.h>
typedef struct bankaccount{
int accountno;
char name[100];
} acc;
int main(){
acc a1 = {124,"ashish"};
acc a2 = {234, "shradha"};
acc a3 = {102, "shiv"};
printf("acc no = %d \n",a1.accountno);
printf("acc name = %s",a1.name);
return 0;
}
ARRAY OF STRUCTURES
#include<stdio.h>
#include<string.h>
int main(){
typedef struct pokemon{
int attack;
int hp;
int speed;
char class;
char name [15];
}pokemon;
pokemon arr[3]; // dekh yaHha 3n alag box bun gai 0,1,2
arr[0].attack = 40; //ab ye arr[0] ke box ke andar 4 type distribute hue hai
arr[0].hp = 30;
arr[0].speed = 50;
arr[0].class = 'A';
// arr[0].name = "poki"; aise nhi likh sakte hai
strcpy(arr[0].name, "poki");
arr[1].attack = 35;
arr[1].hp = 64;
arr[1].speed = 44;
arr[1].class = 'B';
strcpy(arr[1].name, "loki");
arr[2].attack = 450;
arr[2].hp = 305;
arr[2].speed = 350;
arr[2].class = 'C';
strcpy(arr[2].name, "moki");
for(int i=0;i<3;i++){ //ye ek loop se sari chiz print ho jaegi pehele 0 position pe check hoga aur print fir 1 fir 2
printf("Name = %s\n", arr[i].name );
printf("Attack = %d\n", arr[i].attack);
printf("Hp = %d\n", arr[i].hp);
printf("Speed = %d\n", arr[i].speed);
printf("Class = %c\n \n", arr[i].class);
}
}
dekh humne uppar value declare kiya tha matlab attack =30 aise ab input lene hai tho bus printf ki jage scanf kar do LOOP LAGA KE
DECLARATION : hum aise ek structure array ko declare kar te hai
struct pokemon arr[size]; or
agar typedef declare kiya hia tho pokemon arr[s];
ACCESS : aur print kar na ho tho aise access kar te hai
arr[i].attribute;
Q ) //A record contains name of cricketer his age number of test that he played and avarage reun . Creat an array and then write a program to read these records
#include<stdio.h>
#include<string.h>
int main(){
typedef struct cricketer {
char name [20];
int age;
int nft;
int avg;
}cricketer;
cricketer arr[10];
printf("Enter player details 1) name 2) Age 3) No of test he played 4) His avarage \n ");
for(int i=0;i<10;i++){
scanf("%s %d %d %d \n", arr[i].name, &arr[i].age,&arr[i].nft, &arr[i].avg);
}
printf("Your player details are ");
for(int i=0;i<10;i++){
printf("Name = %s\n Age= %d\n No of test he played= %d\n His avarage= %d\n\n",arr[i].name,arr[i].age,arr[i].nft,arr[i].avg);
}
}
// PREFECTLLY RUN HO RAHA HAI EKDAM MUST CODE LIKHA HAI TUNE
#include<stdio.h>
int main(){
typedef struct date{
int day;
int month;
int year;
}date;
date a,b;
a.day = 12;
a.month = 12;
a.year = 2002;
b.day = 12;
b.month = 4;
b.year = 2042;
//if(a==b){
// printf("equal");
// } a == b COMPAR NHI HO SAKTA HAI BUT USKE ANDAR KI CHIZ COMPARE HO SAKTI HAI JAISE HUMM NE NICHE KIYA HAI.
// else{
// printf("not equal ");
// }
if(a.day == b.day ){
printf("equal");
}
else{
printf("not equal")
}
// hum ye bhi kar sakte hai
date c;
c == a;
}
#include<stdio.h>
#include<string.h>
int main(){
typedef struct pokemon{
int hp;
int speed;
int attack;
char name[20];
}pokemon; // ye hamare normal structure
typedef struct legendarypokemon{
pokemon normal; //yaha pokemon likh ne se hum upparwale pokemon ki //sari ciz iss nested structure mai use kar sakte hai
char ability[10]; // aur extra chiz bhi add kar sakte hai
}legendarypokemon; // ye first nested structure banaya humne
typedef struct godpokemon{
pokemon legend; //ye pokemon likhne se upar wala main structure pokemon ki sari chiz issme aagai
int specialattack; //ye like ke maine is nested mai kuch extra add kiya
} godpokemon;
godpokemon az;
az.specialattack = 300;
strcpy(az.legend.ability, "turn anyone in stone"); // .legend likh ne se hune jo nseted loop banayaa tha legendary pokemon ka uske andar ki chiz use kar sak te hai jaise hum ne ussme ability banaya tha vo hum issme bhi use kar sakt te hai
az.legend.normal.attack = 500;
legendarypokemon am;
am.normal.hp = 150; //aise hum access kar sakte hai upar wale pokemon ke // attributes
strcpy(am.ability, "fire ball");
am.normal.speed = 60;
am.normal.attack = 50;
strcpy(am.normal.name,"ash");
}
STRUCTURE AS FUNCTION
No comments:
Post a Comment