0% found this document useful (0 votes)
3 views10 pages

C Programming: Functions and Variables

The document provides several C programming examples, including functions for reversing an integer, checking if a number is a palindrome, and finding the largest of three numbers. It also explains the concepts of variable scope, detailing local variables, global variables, and formal parameters with corresponding code snippets. Each section illustrates the usage of these concepts in practical programming scenarios.

Uploaded by

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

C Programming: Functions and Variables

The document provides several C programming examples, including functions for reversing an integer, checking if a number is a palindrome, and finding the largest of three numbers. It also explains the concepts of variable scope, detailing local variables, global variables, and formal parameters with corresponding code snippets. Each section illustrates the usage of these concepts in practical programming scenarios.

Uploaded by

manuelsaju21
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Q. Write a C program to read an integer number and reverse it using a function.

23
User defined function for abs().

#include<stdio.h>
#include<math.h>
int main ()
{
int n;
int absolute(int);
printf("\nEnter the number“);
scanf(“%d”, &n);
printf("The abs value is %d”, absolute(n));
}
int absolute(int x)
{
if (x<0)
return(-1*x);
else
return(x);
}

24
IS NUM A PALINDROME???

#include<stdio.h> Eg:- 12321


int main()
{
int reverse(int n)
int num, rev; {
int reverse(int); int s=0, r;
while(n>0)
Printf(“Enter the number”); {
scanf(“%d”, &num); r=n%10;
s=10*s+r;
rev=reverse(num);
n=n/10;
if(rev==num) }
printf(“%d is a palindrome”, num); return(s)
}
else
printf(“%d is a not palindrome”, num);
}
25
PROGRAM TO CALCULATE THE LARGEST OF THREE NUMBERS.
#include<stdio.h>
int main()
{
int max, a, b,c;
int max_value(int,int,int);
printf("Enter three numbers : ") ;
scanf("%d %d %d", &a, &b, &c) ;
max=max_value(a,b,c);
printf("Max of three: %d",max);

}
int max_value(int x, int y, int z)
{
int big;
big = x > y ? (x > z ? x : z) : (y > z ? y : z) ;
return big;
} 26
SCOPE OF VARIABLES
A scope in any programming is a region of the program where a defined variable
can have its existence and beyond that variable it cannot be accessed. There are
three places where variables can be declared in C programming language −
Inside a function or a block which is called local variables.
Outside of all functions which is called global variables.
In the definition of function parameters which are called formal parameters.
Let us understand what are local and global variables, and formal parameters.

27
LOCAL VARIABLES
Variables that are declared inside a function or block are called local variables.
They can be used only by statements that are inside that function or block of code.
Local variables are not known to functions outside their own. The following example
shows how local variables are used. Here all the variables a, b, and c are local to
main() function.
#include <stdio.h>
int main ()
{ /* local variable declaration */
int a, b;
int c;
/* actual initialization */
a = 10; b = 20; c = a + b;
printf ("value of a = %d, b = %d and c = %d\n", a, b, c);
return 0;
}

28
GLOBAL VARIABLES
 Global variables are defined outside a function, usually on top of the program.
Global variables hold their values throughout the lifetime of your program and they
can be accessed inside any of the functions defined for the program.
 A global variable can be accessed by any function. That is, a global variable is
available for use throughout your entire program after its declaration. The following
program show how global variables are used in a program

#include <stdio.h>
/* global variable declaration */
int g;
int main ()
{ /* local variable declaration */
int a, b;
/* actual initialization */
a = 10; b = 20;
g = a + b;
printf ("value of a = %d, b = %d and g = %d\n", a, b, g);
return 0;
}
29
A program can have same name for local and global variables but the value of local
variable inside a function will take preference. Here is an example −

#include <stdio.h>
/* global variable declaration */
int g = 20;
int main ()
{ /* local variable declaration */
int g = 10;
printf ("value of g = %d\n", g);
return 0;
}

When the above code is compiled and executed, it produces the following result −

value of g = 10

30
FORMAL PARAMETERS
 Formal parameters, are treated as local variables with-in a function and they take
precedence over global variables. Following is an example −

#include <stdio.h>
/* global variable declaration */
int a = 20;
int main ()
{ /* local variable declaration in main function */
int a = 10; int b = 20; int c = 0;
printf ("value of a in main() = %d\n", a);
c = sum( a, b);
printf ("value of c in main() = %d\n", c);
return 0;
}
/* function to add two integers */
int sum(int a, int b)
{ printf ("value of a in sum() = %d\n", a);
printf ("value of b in sum() = %d\n", b);
return a + b;
}
31
When the above code is compiled and executed, it produces the following result −

value of a in main() = 10
value of a in sum() = 10
value of b in sum() = 20
value of c in main() = 30

32

You might also like