Pages

Sunday, May 21, 2017

Global variables

Global variables are available throughout the C program and may be used by any function and part of a program. Also, their value can be used in any part of C program during execution. We can create global variables by declaring them outside of any function. Generally we declare global variables in the beginning of C program. In the following program, the variable percentage is declared outside of all functions. We can use percentage in all function including main().

#include <stdio.h>
//Declaration of global variables.
int maths, science, english;
float percentage;

void result(void);
void display(void);

int main(void)
{
printf("Enter Subject Marks out of 100.\n");
printf("-------------------------------\n");
printf("Enter Marks of Maths:");
scanf("%d",&maths);
printf("Enter Marks of Science:");
scanf("%d",&science);
printf("Enter Marks of English:");
scanf("%d",&english);

result();
display();

return 0;
}
void result(void)
{
percentage = (maths+science+english)*100/300;
}
void display(void)
{
printf("Your percentage is %.2f.", percentage);
}

Output of Program

Enter Subject Marks out of 100.
-------------------------------
Enter Marks of Maths:80
Enter Marks of Science:90
Enter Marks of English:85
Your percentage is 85.00.

No comments:

Post a Comment