Difference between revisions of "C language"

From Bitnami MediaWiki
Jump to navigation Jump to search
Line 1: Line 1:
 +
==Elements of C==
 +
<syntaxhighlight lang=C>
 +
#include <stdio.h>
 +
void swap(int *pa, int *pb) {
 +
  int t = *pa;
 +
  *pa = *pb;
 +
  *pb = t;
 +
  //  int t = a;
 +
  //a = b;
 +
  //b = t;
 +
  printf("swap: a = %d, b = %d\n", *pa,*pb);
 +
}
  
 +
int main(void){
 +
  int a = 21;
 +
  int b = 17;
 +
  swap(&a, &b);
 +
  printf("main: a = %d, b = %d\n", a, b);
 +
 
 +
  return 0;
 +
}
 +
</syntaxhighlight>
  
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
* pass-by-value is the default. In the swap example,
  
  

Revision as of 17:26, 18 March 2022

Elements of C

#include <stdio.h>
void swap(int *pa, int *pb) {
  int t = *pa;
  *pa = *pb;
  *pb = t;
  //  int t = a;
  //a = b;
  //b = t;
  printf("swap: a = %d, b = %d\n", *pa,*pb);
}

int main(void){
  int a = 21;
  int b = 17; 
  swap(&a, &b);
  printf("main: a = %d, b = %d\n", a, b);
  
  return 0;
}







  • pass-by-value is the default. In the swap example,


void increment(void) {
  static unsigned int counter = 0;
  counter++; 
  printf("%d", counter);
}

int main(void) {
  for (int i = 0; i < 5; i++) {
    increment();
  }
  return 0; 
}