Please enable JavaScript to use CodeHS

C Documentation

Comments

int main() {
   /* comments like these are ignored by the compiler */
   printf("Hello, World! \n");
   return 0;
}

Printing

int main() {
   /* the next line will print Hello World! to the console */
   printf("Hello, World! \n");

   return 0;
}

Comparison Operators

/* Comparison operators return booleans (true/false values) */
/* is x equal to y */
x == y
/* is x not equal to y */
x != y
/* is x greater than y */
x > y
/* is x greater than or equal to y */
x >= y
/* is x less than y */
x < y
/* is x less than or equal to y */
x <= y

If Statement

if (boolean_expression) {
   /* statement(s) will execute if the boolean expression is true */
}
#include <stdio.h>

int main() {

/* local variable definition */
int a = 1;

/* check the boolean condition */
if (a < 100) {
      /* if condition is true then print the following */
      printf("a is less than 100\n" );
   }

   printf("a is : %d\n", a);

   return 0;
}

Math

/* Operators: */
+   Addition
-   Subtraction
*   Multiplication
/   Division
%   Modulus (Remainder)


/* Examples: */
int z = x + y;
int w = x * y;

/* Increment (add one) */
x++

/* Decrement (subtract one) */
x--

/* Absolute value */
int value = int abs(int x)

/* Square Root */
double sqrt = double sqrt(double x)

/* Rounding */
/* round() can be used to round numbers */
float pi = 3.14;
float roundedPi = round(pi);
/* print out 3: */
printf(roundedPi);

/* Example */
#include <stdio.h>
#include <math.h>

int main () {

int a = 1;
int b = 11;

if (a + b < 100) {
      /* if condition is true then print the following */
      printf("a + b is less than 100\n" );
   }

   return 0;
}

Arrays

/* syntax to declare an array */
type arrayName [ arraySize ];

/* declare a 7-element array called weights */
double weights[7];

/* initialize array */
double weights[5] = {10.0, 200.0, 45.4, 70.0, 99.0};

/* assign 4th elements of the ages array to be 33.0 */
weights[3] = 33.0;

/* index into array to access the third element */
double third = weights[2];

While Loop

/* while loop syntax */
while(condition) {
   statement(s);
}

/* while loop example */
#include <stdio.h>

int main() {

   /* local variable definition */
   int a = 1;

   /* while loop execution */
   while (a < 100) {
      printf("a is still less than 100!");
      a++;
   }

   return 0;
}

For Loop

/* for loop syntax */
for (initialize; condition; increment) {
   statement(s);
}

/* for loop example */
#include <stdio.h>

int main() {

   int a;

   /* for loop execution */
   for (a = 1; a < 100; a++) {
      printf("value of a is: %d\n", a);
   }

   return 0;
}

Function

#include <stdio.h>

/* declare the function */
int min(int num1, int num2);

int main() {

   /* local variable definition */
   int a = 100;
   int b = 200;
   int minimum;

   /* calling the min function and assigning minimum value found to minimum */
   minimum = min(a, b);

   printf( "Minimum value: %d\n", minimum );

   return 0;
}

/* function returning the minimum between two numbers */
int min(int num1, int num2) {

   /* local variable declaration */
   int result;

   if (num1 < num2) {
      result = num1;
   }
   else {
      result = num2;
   }

   return result;
}