Local Variables and Parameters

A variable is a named location in memory that is used to hold a mutable value. All variables have to be declared before they may be used. Note that in C, the name of the variable is completely independent from its type.

#include <stdio.h>


int main(void){
 
 int x, y, z;
 
 unsigned int unX, unY, shapoopie;
 
 double trouble, mint, dare;
 
 /*
  * use unsigned long
  * conversion specifier
  */
 printf("variable x is %lu bytes\n", sizeof(x));
 printf("variable dare is %lu bytes\n", sizeof(dare));
 
 return 0;
 
}

We can declare variables in two places: inside functions and outside all functions. Variables that can be declared within functions are either local variables or formal parameters. Variables declared outside of all functions are global variables.

Local variables can only be used inside the block of code in which they are declared. Local variables cease to exist after the block of code in which they are declared finishes executing. For this reason, we can have variables with the same name as long as they are contained within separate code blocks.

#include <stdio.h>

void functionExmp(void);


int main(void){
 
  printf("\n***inside main()***\n");
 
  int ace = 21;
 
  printf("value of ace=%d\n", ace);
 
  functionExmp();
 
  printf("value of ace=%d\n", ace);
 
  printf("***function main() finished***\n");
 
  return 0;
 
}

void functionExmp(void){
 
 printf("\t*inside functionExmp()*\n");
 int ace=1;

 printf("\tvalue of ace=%d\n", ace);
 
 printf("\t*functionExmp() finished*\n");
}

Note that we may declare local variables within any block of code delimited by curly braces. Declaring variables within the code of block helps modularize and compartmentalize code. Since the variable doesn’t exist anywhere else in the program outside of its own block, it cannot be altered accidentally by other code.

#include <stdio.h>


int main(void){
 
  int a=10;
 
  if(a>9){
    double dealing=19.19;
    printf("%f\t", dealing);
    dealing--;
    printf("%f\n", dealing);
    printf("%.2f\t", dealing--);
    printf("%.2f\n", --dealing);
  }
 
 
  return 0;
 
}

When we initialize a local variable to a value that value will be assigned to the variable each time the function is called.

#include <stdio.h>

int func1(void){
 
  printf("\n[entering func1()]\n");
 
  int i = 0;
 
  while(i<6){
    printf("i=%d\t", i++);
  }
 
  printf("i is now %d\n", i);
 
  printf("[exiting func1()]\n");
  return 0;
}

int main(void){
 
  int i = 1979;
 
  printf("\ni is %d\n", i);
 
 
  /*
   * call func1()
   */
  func1();
 
  printf("\ni is %d\n", i);
 
  /*
   * call func1() again
   */
  func1();
 
  printf("\ni is %d\n", i);
 
  return 0;
 
}

If a function is to receive arguments, it must declare variables that will accept the values of the arguments. The variables are called the parameters of the function. In behavior, they are the equivalent of any other local variable.

#include <stdio.h>

void intFunc(int param){
  printf("\n\t[inside intFunc()]\n");
  printf("\tparameter value is %d\n", param);
  printf("\t[exiting intFunc()]\n");
}

void doubleFunc(double param){
  printf("\n\t[inside doubleFunc()]\n");
  printf("\tparameter value is %f\n", param);
  printf("\t[exiting dobuleFunc()]\n");
}

int main(void){
 
  int j=-5;
  double dare=1.05;
 
  while(j++<5){
    printf("iterating while() loop");
    if(j%2==0){
     intFunc(j);
    } else {
     doubleFunc(dare++);  
    }
  }

  return 0;
 
}

Note that local variables are stored on the stack.

Advertisements