C

C is a programming language. It is used in vital low-level software such as kernels. It was designed to be the unifying language due to its easy portability at a time when certain computer languages were bound to hardware. It may occasionally produce errors due to the vagueness of its specification - it contains a lot of undefined behavior as to make the compiler choose the most efficient. For example, data type sizes may differ on each platform. It is sometimes referred to as "portable assembly" for its low level nature.


Data type cheatsheet | Function type cheatsheet


History


Barebones program

int main(void)
{	

	return 0;
}

int main (void) - The main function. Every C program starts here. the one in example has no extra parameters (is void)

return 0 - Signals end of function. Every function in C returns a variable. Here, it does nothing, but we can check it to see if the program worked correctly - if there was a buffer overflow, the value would overflow to 1. The higher the number, the more errors.

getchar() - expects a character to be pressed before proceeding, use if your terminal closes immediately

Commenting

Comments can be written with // or /* */.

int main(void)
{	
    // This is a comment
    // going over multiple lines
	
    /* This is also a comment
    going over multiple lines*/
	
    return 0;
}


Input and output (printing)

#include<stdio.h>

int main(void)
{
	printf("Hello world.\n");
	
	return 0;
}

Outputs 'Hello world.'

<stdio.h> - Input/output library

printf - 'printf formatted' - directly outputs to the terminal

\ - an escape character used to note special changes in text like new lines - \n. \b - backspace, \r - return, \t - tab, \"... To include the backslash by itself, include a double backslash - \\.

#include<stdio.h>

int main(void)
{	
	printf(
	" O   O"
	"   U  "
	"._____. - hello!"
	);
	return 0;
}

Math operators


Data

Data types

int someint;
someint = 12;

char somechar;
somechar = 'g';

char somestring;
somestring = "text in the string.";

int somearray[] = {3, 7, 9, 12};

printf("%d", somearray[1]);
struct character
{
	char name[10];
	int hp;
	int mp;
}
int main (void)
{
	struct character mage;
	mage.hp = 20;
}

Including data types in print function - % - format specifier:

Strings are manipulated differently than characters or numbers. The size of string includes null terminator at the end of string - \0.

strcpy(dest, src); - copies, cannot copy more characters than destination, can copy more than in source, if less, won't copy null terminator, need to manually add

strncpy(dest, src, 5); - limited amount of characters, can be set to larger than the source we are copying

char string[] = "This is a string."
int length = strlen(string);

Input

#include<stdio.h>

int main(void)
{
	printf("Please enter your name.\n");
	scanf("%d", &name);
	printf("Your name is: %d.\n", name);
	
	return 0;
}

Conditionals

Ex. 1:

if (x == 1) printf("x equals 1");

Ex. 2:

if (x == 3)
{
	printf("1st condition");
}
else if (x == 4)
{
	printf("2nd condition");
}
else
{
	printf("3rd condition");
}

Loops

Functions that repeat until a condition is met.

While loop - used for checking variables, mostly not declared ahead of time:

while (i < 8)
{
	printf("%d\n", i);
	i++; // increment by 1
}

For loop - conventionally used with a single one-use variable

for (i = 0; i < 8; i++)
{
	printf("%d\n", i);
}

break; - break out of loop body, terminate early

continue; - skip remainder of loop body, but loop back up

Switch statement:

switch(i)
{
	case 0:
	// do something
	break;
	case 1:
	// do something
	break;
	case 7:
	// do something
	break;
	case 25:
	// do something
	break;
}

Comparing variables

Boolean comparisons (number greater than 1 counts as 1).

#include 

int main(void)
{
  int input1;
  int input2;
  int output;
  
  input1 = 0;
  input2 = 0;
  // input 0 or 1
  
  if (input1 == 1 && input2 == 1) {
	printf("TRUE");
	output = 1;
  }
  else {
	printf("FALSE");
	output = 0;
  }
  
}

Functions within functions

We can call other functions within the main function. If the function returns a value, it should be put above the function it is included in.

#include <stdio.h>

void timesfive (int num) {
	int result = num*5;
	return result;
}
int main(void) {
	int num = 5; //input number *5
	printf("%d", timesfive(num));
	getch();
	return 0;
}

Constants

Data whose value does not change, unlike variables. Defined in the same area as library inclusions.

#define PI 3.14159

Libraries & header files

Header files are black box abstractions built for specific functions. Libraries are an assembly of these files. Most used header files in the C standard library (libc):

Libraries can be built not just generally, but for any specific function, such as "smallchesslib", a library for making a chess game.

.h - header file - used for declarations (setup, declaring important variables..)

.c - main body of program, executes everything within itself and within header files. .c has to include the header file:

#include <stdio.h>
#include "library.h"

Additional information

C traditionally has 3 file streams: stdin, stdout, sterr. stdin is connected to the keyboard, stdout to the screen/terminal.

Keywords to not use in C


Basic programs: Hello, world | Find largest number in array | 99 bottles | Generate triangle | Generate pyramid

Additional functionalities not described in above tutorial: Write a file | Randomness


External articles: C Pitfalls, "So you think you know C?" paper Programming style, Quine