C Foundation

What is C? C Compiler Installation C Extensions C Compiler C Interpreter C Program Structure

C Basics

C Keywords C Data Types C Identifiers C Variables C Constant C Escape Sequences C Constant and Volatile C Typecast

Operators

What is Operator C Comma Operator C Arithmetic Operators C Relational Operators C Logical Operators C Bitwise Operators C Conditional Operators C : : Operator C Operator Priority

Basic IO's

Basic IO's C Formatted Functions C Unformatted Functions C Common Functions

Control Statements

What is Control Statement C if Statement C if else Statement C Nested if Statement C Else if Statement C Break Statement C Continue Statement C Switch Statement C Goto Statement

Looping

What is Control Loop C for Loop C Nested for Loop C while Loop C Nested while Loop C do while Loop C Nested do while loop

Functions

What is Function C User Defined Functions C Recursion C Passing Parameters

Scope

Scope C Local Scope C Global Scope

Storage Classes

What is Storage Class C Auto C Extern C Static C Register

Array

What is Array C One Dimensional Array C Two Dimensional Array C Multi Dimensional Array C Arrays Of Strings

String

What is String C String Functions

Pointer

What is Pointer C Pointers Arithmetic C Pointer to Pointer C Pointers and Arrays C Pointers and Strings C Pointer to Functions Void Pointers Null Pointers C Null and Void Pointer

Structure

What is Structure C Struct within Struct C Array within Structure C Pointer to Structure C Structure and Function C Enum C Bitfield Structure C Type def

Union

What is Union

Files

What is File C read a file C write a file C File Handling C Error Handling C Low Level Disk I/O C Other file functions

Memory Allocation

What is Memory Allocation C Malloc() C Calloc() C Free() C Realloc() C Coreleft()

C Reference

All ASCII Code Basic C Questions

C Interview

C Interview Sets All Star Patterns All Number Patterns All Alphabet Patterns All Series Patterns
The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

Structures and Functions

C allows programmers to pass a single or entire structure information to or from a function. A structure information can be passed as a function arguments. The structure variable may be passed as a value or reference. The function will return the value by using the return statement.

C program - Passing Structure Members To Functions

Lets code and have some fun

structure-functions.c
#include <stdio.h>
int add(int, int) ;     //function declaration
int main()
{
//structures declartion
struct addition{
    int a, b;
    int c; 
}sum;
printf("Enter the value of a : ");
scanf("%d",&sum.a);
printf("\nEnter the value of b : ");
scanf("%d",&sum.b);
sum.c = add(sum. a, sum.b);     //passing structure members as arguments to function
printf("\nThe sum of two value are : ");
printf("%d ", sum.c);
return 0;
}
//Function definition
int add(int x, int y)
{
int sum1;
sum1 = x + y;
return(sum1);
}
  • Enter the value of a 10
  • Enter the value of b 20
  • The sum of two values 30

Note:

The structure addition have three variables( a, b, c) which are also known as structure members. The variables are reads through scanf function. The next statement illustrates that the structure members can be passed to the function add. The add function defined to perform addition operation between two values.

C program - Passing The Entire Structure To Functions

Lets code and have some fun

structure-functions-1.c
#include <stdio.h>
//structures declaration
typedef struct {
    int a, b;
    int c;
}sum;
void add(sum) ;     //function declaration with struct type sum
int main()
{
sum s1;
printf("Enter the value of a : ");
scanf("%d",&s1.a);
printf("\nEnter the value of b : ");
scanf("%d",&s1.b);
add(s1);     //passing entire structure as an argument to function
return 0;
}
//Function Definition
void add(sum s)
{
int sum1;
sum1 = s.a + s.b;
printf("\nThe sum of two values are :%d ", sum1);
}
  • Enter the value of a 10
  • Enter the value of b 20
  • The sum of two values 30

Note:

The program uses the keyword typedef to create the structure type sum. The variable "s1" is declared as structure type sum.

Passing Structures Using Pointers

The Call by value method is inefficient to pass the large structures to functions. C provides the efficient method, known as call by reference to pass large structures to functions. Call by reference method is achieved by passing pointers to functions. Pointer is a variable, used to hold or store the address of another variable.

C program - passing Structures By Using Pointers

Lets code and have some fun

structure-functions-2.c
#include <stdio.h>
//Structure declartion
struct employee {
    char name[40];
    int empid;
    int experience;
}emp;
void displaydetails(struct employee*);  //function declaration
int main()
{
struct employee *empptr;   //pointer declaration
empptr = &emp;   //initial
printf("\nEnter the name of the Employee : ");
scanf("%s", empptr->name);
printf("\nEnter the Employee Id : ");
scanf("%d",&empptr->empid);
printf("\nEnter Experience of the Employee : ");
scanf("%d",&empptr->experience);
displaydetails(empptr);
return 0;
}
//Function Definition
void displaydetails(struct employee *empptr)
{
printf("\n---------Details List--------- \n ");
printf("Employee Name : %s",empptr->name);
printf("\nEmployee ID : %d ",empptr->empid);
printf("\nEmployee Experience : %d ",empptr->experience);
}
  • Enter the name of the Employee : Jiju
  • Enter the Employee Id : 16
  • Enter Experience of the Employee : 3
  • ---------Details List---------
  • Employee Name : Jiju
  • Employee Id : 16
  • Employee Experience : 3

Note:

The structure employee has the variable "emp". The pointer variable is declared inside the main function and initialized as &emp. Now the pointer empptr hold the address of structure. The employee structure members can be accessed by using the "empptr->membername". The calling function displaydetails has a pointer "empptr" as an argument. Finally the function will display the details of employee.

Report Us

We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

Report

We to update you