Struct 1
Struct 1
Structures (also called structs) are a way to group several related variables
into one place. Each variable in the structure is known as a member of the
structure.
Unlike an array, a structure can contain many different data types (int, float,
char, etc.).
Syntax
struct structure_name {
data_type member_name1;
data_type member_name1;
....
....
};
Example:-
struct myStructure {
int myNum;
char myLetter[20];
};
structure varivable declaration:
two ways
Structure Variable Declaration Structure Variable Declaration
after Structure Template with Structure Template
Syntax
structure_name.member1;
strcuture_name.member2;
Example:
#include<stdio.h>
#include<conio.h>
struct mydeatil
{
char myname[25];
int age;
};
void main()
{
struct mydetail M1;
printf(“enter the name\n”);
scanf(“%s”,&M1.myname);
printf(“enter age”);
scanf(“%d”,&M1.age);
printf(“Menbers of structure\n”);
printf(“%s%d”,M1.myname,M1.age);
getch();
}
printf(“Menbers of structure\n”);
printf(“%s%d”,M1.myname,M1.age);
printf(“Menbers of structure\n”);
printf(“%s%d”,M2.myname,M2.age);
getch();
}
Array of structure
#include<stdio.h>
#include<conio.h>
struct mydeatil
{
char myname[25];
int age;
};
void main()
{
struct mydetail M[5];
int i;
for(i=0;i<=4:i++)
{
scanf(“%s”,&M[i].myname);
scanf(“%d”,&M[]i.age);
}
for(i=0;i<=4:i++)
{
printf(“%s”,&M[i].myname);
printf(“%d”,&M[]i.age);
}
getch();
}
Structure Pointer in C
In this section, we will discuss the Structure pointer in the C programming language.
Before going to the concepts, let's understand the Structure. The structure is the
collection of different data types grouped under the same name using
the struct keyword. It is also known as the user-defined data type that enables the
programmer to store different data type records in the Structure. Furthermore, the
collection of data elements inside the Structure is termed as the member.
#include<stdio.h>
#include<conio.h>
struct mydeatil
{
char myname[20];
int age;
};
void main()
{
struct mydetail *M1;
printf(“enter the name\n”);
scanf(“%s”,&M1->myname);
printf(“enter age”);
scanf(“%d”,&M1->age);
printf(“Menbers of structure\n”);
printf(“%s%d”,M1->myname,M1->age);
getch();
}
Preprocessor Directives in C
Preprocessor programs provide preprocessor directives that tell the
compiler to preprocess the source code before compiling. All of these
preprocessor directives begin with a ‘#’ (hash) symbol. The ‘#’ symbol
indicates that whatever statement starts with a ‘#’ will go to the
preprocessor program to get executed. We can place these preprocessor
directives anywhere in our program.
List of preprocessor directives in C
The following table lists all the preprocessor directives in C:
Preprocessor Directives Description
Types of C Preprocessors
There are 4 Main Types of Preprocessor Directives:
1. Macros
2. File Inclusion
3. Conditional Compilation
4. Other directives
Let us now learn about each of these directives in detail.
1. Object-like Macros
2. Function-like Macros
Object-like Macros
The object-like macro is an identifier that is replaced by value. It is widely used to
represent numeric constants
Syntax of Macro Definition
#define token value
where after preprocessing, the token will be expanded to its value in the
program.
.For example:
#define PI 3.14
Example:-
#include<stdio.h>
#include<conio.h>
#define PI 3.14
void main()
{
int r=5;
float area;
area=PI*r*r;
printf(“%f”,area);
getch();
}
Function-like Macros
The function-like macro looks like function call. For example:
C Predefined Macros
ANSI C defines many predefined macros that can be used in c program.
It is defined as 1 when
5 _STDC_ compiler complies with
the ANSI standard.
C #undef
The #undef preprocessor directive is used to undefine the constant or macro
defined by #define.
Syntax:
#undef token
Example:-
#include <stdio.h>
#define PI 3.14
#undef PI
main() {
printf("%f",PI);
}
Syntax:
#ifdef MACRO
//code
#endif
Example:-
#include <stdio.h> #include <stdio.h>
#include <conio.h> #include <conio.h>
#define NOINPUT void main() {
void main() { int a=0;
int a=0; #ifdef NOINPUT
#ifdef NOINPUT a=2;
a=2; #else
#else printf("Enter a:");
printf("Enter a:"); scanf("%d", &a);
scanf("%d", &a); #endif
#endif
printf("Value of a: %d\n", a); printf("Value of a: %d\n", a);
getch(); getch();
} }
C #ifndef
The #ifndef preprocessor directive checks if macro is not defined by #define.
If yes, it executes the code otherwise #else code is executed, if present.
Syntax:
#ifndef MACRO
//code
#endif
Example:-
1. #include <stdio.h> 1. #include <stdio.h>
2. #include <conio.h> 2. #include <conio.h>
3. #define INPUT 3. void main() {
4. void main() { 4. int a=0;
5. int a=0; 5. #ifndef INPUT
6. #ifndef INPUT 6. a=2;
7. a=2; 7. #else
8. #else 8. printf("Enter a:");
9. printf("Enter a:"); 9. scanf("%d", &a);
10. scanf("%d", &a); 10. #endif
11. #endif 11. printf("Value of a: %d\
12. printf("Value of a: %d\ n", a);
n", a); 12. getch();
13. getch(); 13. }
14. }
C #if
The #if preprocessor directive evaluates the expression or condition. If
condition is true, it executes the code otherwise #elseif or #else or #endif
code is executed.
Syntax:
Example:
1. #include <stdio.h> 1. #include <stdio.h>
2. #include <conio.h> 2. #include <conio.h>
3. #define NUMBER 0 3. #define NUMBER 0
4. void main() { 4. void main() {
5. #if (NUMBER==0) 5. #if (NUMBER==0)
6. printf("Value of Number is: %d",N 6. printf("Value of Number is: %d",N
UMBER); UMBER);
7. #endif 7. #endif
8. getch(); 8. getch();
9. } 9. }
C #error
The #error preprocessor directive indicates error. The compiler gives fatal
error if #error directive is found and skips further compilation process.
1. #include<stdio.h> 1. #include<stdio.h>
2. #ifndef __MATH_H 2. #include<math.h>
3. #error First include then com 3. #ifndef __MATH_H
pile 4. #error First include then com
4. #else pile
5. void main(){ 5. #else
6. float a; 6. void main(){
7. a=sqrt(7); 7. float a;
8. printf("%f",a); 8. a=sqrt(7);
9. } 9. printf("%f",a);
10. #endif 10. }
11. #endif