Structure at Local Scope in C Language

Bhanu Priya
Updated on 24-Mar-2021 13:58:27

936 Views

Structure is a collection of different datatype variables, grouped together under a single name.General form of structure declarationThe structure declaration is as follows −struct tagname{    datatype member1;    datatype member2;    datatype member n; };Here, struct is the keyword.tagname specifies name of structure.member1, member2 specifies the data items that make up structure.ExampleThe following example shows the usage of the structure at a local scope.struct book{    int pages;    char author [30];    float price; };ExampleThe following program shows the usage the structure at a local scope. Live Demo#include struct{    char name[20];    int age;    int salary;   ... Read More

Pass Address of Structure as Argument to Function in C

Bhanu Priya
Updated on 24-Mar-2021 13:56:39

8K+ Views

There are three ways by which the values of structure can be transferred from one function to another. They are as follows −Passing individual members as arguments to function.Passing entire structure as an argument to function.Passing the address of structure as an argument to function.Now, let us understand how to pass the address of structure as an argument to function.The address of the structure is passed as an argument to the function.It is collected in a pointer to structure in function header.AdvantagesThe advantages of passing the address of structure as an argument to function are as follows −No wastage of ... Read More

C Error Handling Functions Explained

Bhanu Priya
Updated on 24-Mar-2021 13:53:46

859 Views

File is collection of records or is a place on hard disk, where data is stored permanently.Operations on filesThe operations on files in C programming language are as follows −Naming the fileOpening the fileReading from the fileWriting into the fileClosing the fileSyntaxThe syntax for opening a file is as follows −FILE *File pointer;For example, FILE * fptr;The syntax for naming a file is as follows −File pointer = fopen ("File name", "mode");For example, fptr = fopen ("sample.txt", "r"); FILE *fp; fp = fopen ("sample.txt", "w");Error Handling in FilesSome of the errors in files are as follows −Trying to read beyond ... Read More

Pass Entire Structure as Argument to Function in C

Bhanu Priya
Updated on 24-Mar-2021 13:47:03

4K+ Views

There are three ways by which the values of structure can be transferred from one function to another. They are as follows −Passing individual members as arguments to function.Passing entire structure as an argument to function.Passing the address of structure as an argument to function.Now let’s see how to Pass an entire structure as an argument to function.Name of the structure variable is given as argument in function call.It is collected in another structure variable in function header.The disadvantage is that a copy of an entire structure is created again by wasting the memory.ExampleThe following program shows how to pass ... Read More

Dynamic Memory Allocation of Pointer to Structure in C Language

Bhanu Priya
Updated on 24-Mar-2021 13:44:57

5K+ Views

Pointer to structure holds the add of the entire structure.It is used to create complex data structures such as linked lists, trees, graphs and so on.The members of the structure can be accessed using a special operator called as an arrow operator ( -> ).DeclarationFollowing is the declaration for pointers to structures in C programming −struct tagname *ptr;For example: struct student *s;AccessingIt is explained below how to access the pointers to structures.Ptr-> membername;For example − s->sno, s->sname, s->marks;ExampleFollowing is a C program that explains the dynamic memory allocation of structure in C programming − Live Demo#include #include struct person ... Read More

Difference Between Pointer and Reference

AmitDiwan
Updated on 24-Mar-2021 13:37:00

1K+ Views

In this post, we will understand the difference between pointer and reference.PointerIt can be initialized to any value.It can be initialized any time after its declaration.It can be assigned to point to a NULL value.It can be dereferenced using the ‘*’ operator.It can be changed to point to a different variable of the same type only.Exampleint val = 5; //code// int *p = &val;ReferenceIt has to be initialized when it is declared.It can’t be a NULL value.It can be used by a name.Once it has been initialized to a variable, it can’t be changed to refer to a variable object.Exampleint ... Read More

Difference Between If-Else and Switch

AmitDiwan
Updated on 24-Mar-2021 13:35:16

2K+ Views

In this post, we will understand the difference between if-else statement and ‘switch’ statement.If-elseDepending on the expression inside the statement, output would be generated.It uses multiple statements for multiple choices.This statement tests for equality.It can be used to test logical expressions.It can evaluate integer, character, pointer, floating-point type and boolean type.Just one of the ‘if’ or ‘else’ statement gets executed.If the condition inside the ‘if’ statement is false, then the ‘else’ statement is executed if it has been created.It is tough to edit if-else statement, especially if it is nested.SwitchThe statement that needs to be executed is decided by the ... Read More

Difference Between Exit 0 and Exit 1

AmitDiwan
Updated on 24-Mar-2021 13:31:28

2K+ Views

In this post, we will understand the difference between exit(0) and exit(1).exit(0)It is portable.It tells about the successful termination or completion of the program.It tells about the termination when the program is executed without any errors.The ‘EXIT_SUCCESS’ macro is used to return code 0.The ‘EXIT_SUCCESS’ can be defined as standard as zero.Syntaxexit(0);exit(1)It is not portable.It tells about the abnormal termination of the program.It tells about the termination if the program exited with certain error when the program was being executed.The ‘EXIT_FAILURE’ macro is used to return code 1.It is not restrict by standard to be 1 only.It can be used ... Read More

Difference Between Character Array and String

AmitDiwan
Updated on 24-Mar-2021 13:31:06

10K+ Views

In this post, we will understand the difference between character array and string.StringsThey are immutable.Once they are defined, they can’t be changed.It refers to a sequence of characters, which is represented as a single data type.It contains built-in functions such as substring(), charAt().The ‘+’ operator can be used to append strings together, which would form a new string.The charAt() method helps access characters at a specific index within a ‘String’.These strings are stored in the ‘String Constant Pool’.It is not preferred to store passwords in strings in Java.A string can be converted to a character array using toCharArray() method of ... Read More

Append Mode Operation of Files in C Language

Bhanu Priya
Updated on 24-Mar-2021 13:26:21

689 Views

File is collection of records or is a place on hard disk, where data is stored permanently.Need of filesEntire data is lost when a program terminates.Storing in a file preserves the data even if, the program terminates.If you want to enter a large amount of data, normally it takes a lot of time to enter them all.We can easily access the content of files by using few commands.You can easily move your data from one computer to another without changes.By using C commands, we can access the files in different ways.Operations on filesThe operations on files in C programming language ... Read More

Advertisements