Increment and Decrement Operators in C

The increment and decrement operators provide a succinct way of incrementing and decrementing the value of a variable.The symbol for the increment operator is ++, and the symbol for the decrement operator is –. They are both unary operators.

#include <stdio.h>

int main(void){
  
  int i;
  
  i = 404;
  
  printf("Value: \t %d\n", i);
  
  /*
   * increment value of i
   */
  i++;
  
  printf("Increment Once: \t %d\n", i);
  
  /*
   * increment value of i again.
   */
  i++;
  
  printf("Increment Twice: \t %d\n", i);
  
  
  return 0;
   
}

The increment and decrement operator can be placed in either the prefix or the postfix position. The prefix position places the ++ or — in front of the variable, and the postfix position places the ++ or — after the variable.

#include <stdio.h>

void printValue(int a);

int main(void){
  
  int a = 5;
  
  printValue(a);
  
  a--;
  
  printValue(a);
  
  /*
   * decrement operator
   * in the postfix position
   */
  --a;
  
  printValue(a);
  
  printValue(--a);
  
  return 0;
  
}


void printValue(int a){
  
 printf("Value = %d\n", a); 
  
}

 

In many cases, we don’t care where we put the increment or decrement operator, we just care that it is placed there. After all, the main point of an increment or decrement operation is to increase or decrease the value stored in the variable.  This change in the variable’s stored value is not the result of the operation, but rather it’s aftermath, and here’s where using the ++ and — operators gets a little tricky.

#include <stdio.h>

int main(void){
  
  int i = 1701;
  int j = i;
  int k = 0;
  
  
  /*
   * prefix
   */
  --k;
  /*
   * postfix
   */
  k--;
  
  printf("k = %d\n", k);
  printf("k = %d\n", --k);
  
  k = ++i;
  printf("i = %d \t k = %d\n", i, k);
  
  k = j++;
  /*
   * this line will print
   * the same value as the one
   * above it
   */
  printf("i = %d \t j = %d\n", i, j);
  

  return 0;
  
}

When the result of the ++ and — operation is important, we must be clear on where we place the increment and decrement operators and why. If we place them before the variable, than the result is one plus or minus the value stored in the variable; if we place them after the variable, the result is just the value stored in the variable. Note that in both cases the value stored in the variable has been changed by the same amount, after the expression has been resolved. We can see this effect quite clearly when the ++ or — operators are used in function calls or assignment operations.

#include <stdio.h>

int main(void){
  
  int x, y;
  
  x = y = 73;
  
  printf("\n Pre \t Post\n");
  printf(" %d \t %d \n", x++, ++y);
  printf(" %d \t %d \n", x++, ++y);
  
  printf("\n %d \t %d\n", x--, --y);
  printf(" %d \t %d\n", x--, --y);
  
  
  return 0;
  
}

Again, the increment and decrement operators differ in terms of when the operation is performed. When placed before the variable, the increment and decrement operators modify the value stored in the variable before it is used in any enclosing expressions in the statement. When placed after the variable, the increment and decrement operators modify the value stored in the variable after it is used in any enclosing expressions in the statement.

Introduction to C Functions

A function is a named block of code that performs a specific task and optionally returns a value. A function is named. Every function has a unique name; which can be used to call the function from within another function. This function may pass information back to the function that called it in the form of a return value.

#include <stdio.h>


void firstFunction(){
 
  prinf("\n - This is our first function. - \n");
 
}


int main(void){
 
  printf("Let's call the function we defined!\n");
 
  firstFunction();
 
  printf("Let's call it again!\n");
 
  firstFunction();
 
  return 0;
 
}

To pass arguments to a function, we list them in parentheses following the function name. Be aware that the number of arguments and the type of each argument must match the parameters in the function definition. If a function has multiple arguments, the arguments are listed in order.

#include <stdio.h>

int cube(int val){
  return val * val * val;
}

int main(void){
 
  int x = 2;
  int y = 6;
 
  printf("The value of %d cubed is %d\n", x, cube(x));
  printf("The value of %d cubed is %d\n", y, cube(y));
 
  return 0;
 
}

A function definition contains the code that will be executed. The first line of a function definition is called the function header. The function header gives the function’s name. The header also gives the function’s return type and describes its arguments. A function header shouldn’t end with a semicolon. Following the header is the function body, containing the statements that the function will execute. The function body begins with an opening curly brace and ends with a closing curly brace.

#include <stdio.h>


void function1(int arg1){
  printf("\nBegin function1()\n");
  printf("function1() received the argument %d\n", arg1);
  printf("End function1()\n\n");
}

void function2(double arg1, char arg2){
  printf("\nBegin function2()\n");
  printf("function2() received the arguments:\n");
  printf("%f and %c\n", arg1, arg2);
  printf("End function2()\n");
}


int main(void){
  printf("Begin function main()\n");
 
  function1(42);
  function2(802.11, 'n');
 
  printf("End function main()\n");
 
  return 0;
 
}

Although the two terms are often used interchangeably, a parameter and an argument are different. A parameter is an entry in the function header; it acts as a placeholder for an argument. An argument is an actual value passed to the function by the calling program.

A function should know what kind of argument to expect, this means that the function must know the data type of each argument. A value of any of C’s data types may be passed to a function. The argument type is specified in the parameter list part of the function header. For each argument passed to the function, the parameter list must contain one item.

 #include <stdio.h>

double functionA(int x, double y){
   return x * y;
}

long long functionB(int y, int z){
  return (y*y) + (z*z);
}

int main(void){
  int a = 1999;
  int b = 1138;
 
  double y = 1.618;
  double z = 2.718;
 
 
  printf("%d * %f = %f\n", a, y, functionA(a, y));
  printf("%d * %f = %f\n", b, z, functionA(b, z));
  printf("%d^2 + %d^2 = %lld\n", a, b, functionB(a, b));
 
  return 0;
    
}

A function prototype is a model for a function that will appear later in the program. A program should include a prototype for each function that it uses. The prototype for a function is the same as the function header, only with a semicolon added to the end. Like the function header, the function prototype contains information about the function’s return type, name, and parameters.

#include <stdio.h>

double byHalf(double a);

int main(void){
 
  printf("Half of 4.4 is %f\n", byHalf(4.4));
  printf("Half of 7 is %f\n", byHalf((double)7));
  printf("Half of 12 is %f\n", byHalf((double)12));
 
 
  return 0;
 
}


double byHalf(double a){
  return a / 2.0;
}

Function prototypes should be placed before the start of the first function. Typically, function prototypes are all grouped together in one location.