0% found this document useful (0 votes)
5 views11 pages

Struct 1

BCA 1st sem notes

Uploaded by

sakshipawar5668
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views11 pages

Struct 1

BCA 1st sem notes

Uploaded by

sakshipawar5668
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Structures

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

// structure declared struct structure_name {


beforehand data_type member_name1;
struct structure_name data_type member_name1;
variable1, variable2, .......; ....
....
}variable1, varaible2, ...;

Structure Variable Declaration Structure Variable Declaration


after Structure Template in main with Structure Template
function
struct myStructure { struct myStructure {
int myNum; int myNum;
char myLetter[20]; char myLetter[20];
}; }S1;
void main()
{
struct myStructure s1;
Access Structure Members
To access members of a structure, use the dot syntax ( .):

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();
}

Structure with multiple object


#include<stdio.h>
#include<conio.h>
struct mydeatil
{
char myname[25];
int age;
};
void main()
{
struct mydetail M1,M2;
printf(“enter the name\n”);
scanf(“%s”,&M1.myname);
printf(“enter age”);
scanf(“%d”,&M1.age);
printf(“enter the name\n”);
scanf(“%s”,&M2.myname);
printf(“enter age”);
scanf(“%d”,&M2.age);

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();
}

Preprocessors are programs that process the source code before


compilation. Several steps are involved between writing a program and
executing a program in C. Let us have a look at these steps before we
actually start learning about Preprocessors.

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

#define Used to define a macro

#undef Used to undefine a macro

Used to include a file in the source code


#include
program

Used to include a section of code if a


#ifdef
certain macro is defined by #define

Used to include a section of code if a


#ifndef
certain macro is not defined by #define

#if Check for the specified condition

Alternate code that executes when #if


#else
fails

Combines else and if for another


#elif
condition check

Used to mark the end of #if, #ifdef, and


#endif
#ifndef

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.

In C, Macros are pieces of code in a program that is given some name.


Whenever this name is encountered by the compiler, the compiler
replaces the name with the actual piece of code. The ‘#define’ directive is
used to define a macro.
A macro is a segment of code which is replaced by the value of macro. Macro
is defined by #define directive. There are two types of macros:

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:

1. #define MIN(a,b) ((a)<(b)?(a):(b))


#include<stdio.h>
#include<conio.h>
#define MIN(a,b) ((a)<(b)?(a):(b))
Void main()
{
int a,b;
printf(“enter the value of a and b”);
scanf(“%d%d”,&a,&b);
printf(“greater number is %d”,MIN(a,b));
getch();}

C Predefined Macros
ANSI C defines many predefined macros that can be used in c program.

No. Macro Description

represents current date in


1 _DATE_
"MMM DD YYYY" format.

represents current time in


2 _TIME_
"HH:MM:SS" format.

represents current file


3 _FILE_
name.

represents current line


4 _LINE_
number.

It is defined as 1 when
5 _STDC_ compiler complies with
the ANSI standard.

C predefined macros example


#include<stdio.h>
#include<conio.h>
void main()
{
printf("File :%s\n", __FILE__ );
printf("Date :%s\n", __DATE__ );
printf("Time :%s\n", __TIME__ );
printf("Line :%d\n", __LINE__ );
printf("STDC :%d\n", __STDC__ );
getch();
}
#include – File Inclusion Directive
#include is one of the file inclusion directive in C. #include preprocessor
directive is used to include the content of one file to another file i.e. source
code during the preprocessing stage. This is done to easily organize the
code and increase the reusability of code.
Syntax
#include <file_name>
or
#include "filename"

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);
}

Output:- Compile Time Error: 'PI' undeclared


C #ifdef
The #ifdef preprocessor directive checks if macro is defined by #define. If
yes, it executes the code otherwise #else code is executed, if present.

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:

1. #if expression Syntax with #else: 1. #if expression


2. //code 2. //if code
3. #endif 1. #if expression 3. #elif expression
2. //if code 4. //elif code
3. #else 5. #else
4. //else code 6. //else code
5. #endif 7. #endif

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

Compile Time Error: First include then 2.645751


compile

You might also like