Skip to content

Operating System overview

example.js
console.log('This could come from a file or CMS!');

C programming 101

Installing dependencies…
npm install
example.c
#include <stdio.h>
int main(){
printf("%d\n", 127);
printf("%f\n", 3.1415926535);
printf("%d\n",122013);
printf("%0.4f\n", 0.00008);
printf("%0.1f\n", 0.00008);
}

I/O

  • Input comes from the standard input device, stdin
  • Output is generated by the standard output device, stdout
  • Functions to notice:
getchar()
putchar()
scanf()
printf()
#include <stdio.h>
int main(){
int c;
printf("I'm waiting for a character: ");
c = getchar();
printf("I waited for the '%d' character. \n", c);
printf("I waited for the '%c' character. \n", c);
return 0;
//I'm waiting for a character: d
//I waited for the '100' character.
//I waited for the 'd' character.
int ch;
ch = 'H';
putchar(ch);
ch = 'i';
putchar(ch);
putchar('!');
// Hi!
char prompt[] = "Press Enter to explode: ";
printf("%s", prompt);
getchar();
// Press Enter to explode:
char firstname[15];
printf("Type your first name: ");
scanf("%s", firstname);
printf("Nice to meet you, %s\n", firstname);
return 0;
// Type your first name:
// > chudeptrai
// Nice to meet you, chudeptrai
// Unless it's a string (char array), the variable is prefixed by the & operator
int fav;
printf("What is your favorite number ?");
scanf("%d", &fav);
printf("%d is you favorite number \n", fav);
}

Read text input which contains white space

#include <stdio.h>
int main(){
char fullname[10];
printf("What is your full name ?");
fgets(fullname,10,stdin);
printf("Nice to meet you, %s.\n", fullname);
return (0);
}

Conditional

#include <stdio.h>
#define SECRET 17
int main(){
int guess;
printf("Can you get the secret number ?");
scanf("%d",&guess);
printf("%d is your answer.\n",guess);
if (guess == SECRET){
puts("U're right");
} else {
puts("U're wrong");
}
}

Loop

int main(){
for (int i=0;i < 20; i++){
puts("Sore shoulder surgery!");
}
return 0;
}

Function

void graph(int count){
for (int i = 0; i< count; i++){
putchar('*');
}
putchar('\n');
}
int main(){
graph(5);
}

Array

#include <stdio.h>
int main(){
int highscore[4];
for (int i =0; i < 4; i++){
printf("Your #%d high score: ", i);
scanf("%d",&highscore[i]);
}
puts("Here are you high scores");
for (int i =0; i < 4; i++){
printf("#%d high score: \n", highscore[i]);
}
}

Character array (string)

#include <stdio.h>
int main(){
char sentence[] = "Random text";
int index = 0;
while (sentence[index] != '\0'){
putchar(sentence[index]);
index++;
}
putchar('\n');
printf("%s\n", sentence); // similar result
puts(sentence); // similar result
char firstname[15];
char lastname[15];
puts("Hello, enter your first name: ");
scanf("%15s",firstname);
puts("OK, enter your last name: ");
scanf("%15s", lastname);
printf("Hello %s\t%s", firstname, lastname);
}

Array as function params

#include <stdio.h>
#define SIZE 5
void printArray(int a[]){
for (int i=0; i< SIZE; i++){
printf("%d\t",a[i]);
}
putchar('\n');
}
void arrayInc(int a[]){
for (int i=0; i< SIZE; i++){
a[i] += 1;
}
}
int main(){
int arr[] = {1,2,5,7,9};
puts("Here's your array: ");
printArray(arr);
arrayInc(arr);
printArray(arr);
// Here's your array:
// 1 2 5 7 9
// 2 3 6 8 10
return 0;
}

Text manipulating

Char Functions

  • Library: ctype.h
FunctionReturns TRUE when ch is
isalnum(ch)A letter of the alphabet (upper- or lowercase) or a number
isalpha(ch)An upper- or lowercase letter of the alphabet
isascii(ch)An ASCII value in the range of 0 through 127
isblank(ch)A tab or space or another blank character
iscnctrl(ch)A control code character, values 0 through 31 and 127
isdigit(ch)A character 0 through 9
isgraph(ch)Any printable character except for the space
ishexnumber(ch)Any hexadecimal digit, 0 through 9 or A through F (upperor lowercase)
islower(ch)A lowercase letter of the alphabet, a to z
isnumber(ch)See isdigit()
isprint(ch)Any character that can be displayed, including the space
ispunct(ch)A punctuation symbol
isspace(ch)A white-space character, space, tab, form feed, or an Enter, for example
isupper(ch)An uppercase letter of the alphabet, A to Z
isxdigit(ch)See ishexnumber()
toascii(ch)The ASCII code value of ch, in the range of 0 through 127
tolower(ch)The lowercase of character ch
toupper(ch)The uppercase of character ch
#include <stdio.h>
#include <ctype.h>
#define SIZE 5
int main(){
char phrase[] = "When in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume among the powers of the earth, the separate and equal station to which the Laws of Nature and of Nature's God entitle them, a decent respect to the opinions of mankindrequires that they should declare the causes which impel them to the separation.";
int index, alpha, blank, punct, upper, lower;
index = alpha = blank = punct = upper = lower = 0;
while (phrase[index] != '\0'){
if (isalpha(phrase[index])){
alpha++;
}
if (isblank(phrase[index])){
blank++;
}
if (ispunct(phrase[index])){
punct++;
}
if (isupper(phrase[index])){
upper++;
}
if (islower(phrase[index])){
lower++;
}
index++;
}
printf("\"%s\"\n",phrase);
puts("Statistics: ");
printf("%d alphabet characters. \n", alpha);
printf("%d blank characters. \n", blank);
printf("%d punct characters. \n", punct);
printf("%d upper characters. \n", upper);
printf("%d lower characters. \n", lower);
return 0;
}

”String” Functions

  • Library: string.h
FunctionWhat it does
strcmp()Compares two strings in a case-sensitive way. If the strings match, the function returns 0.
strncmp()Compares the first n characters of two strings, returning 0 if the given number of characters match.
strcasecmp()Compares two strings, ignoring case differences. If the strings match, the function returns 0.
strncasecmp()Compares a specific number of characters between two strings, ignoring case differences. If the number of characters match, the function returns 0
strcat()Appends one string to another, creating a single string out of two
strncat()Appends a given number of characters from one string to the end of another.
strchr()Searches for a character within a string. The function returns that character’s position from the start of the string as a pointer.
strrchr()Searches for a character within a string, but in reverse. The function returns the character’s position from the end of the string as a pointer
strstr()Searches for one string inside another string. The function returns a pointer to the string’s location if it’s found.
strnstr()Searches for one string within the first n characters of the second string. The function returns a pointer to the string’s location if it’s found.
strcpy()Copies (duplicates) one string to another
strncpy()Copies a specific number of characters from one string to another.
strlen()Returns the length of a string, not counting the \0 or NULL character at the end of the string.
  • Example 1: compare 2 string
char password[] = "taco";
char input[15] ;
printf("Enter your password: ");
scanf("%s",input);
if (strcmp(password,input) == 0){
puts("Password accepted");
} else {
puts("Wrong");
}
  • Example 2: concat
char firstname[15];
char lastname[15];
printf("Enter first name: ");
scanf("%s",firstname);
printf("Enter last name: ");
scanf("%s", lastname);
strcat(firstname,lastname);
printf("Full name : %s\n", firstname);

Struct

struct human{
char name[45];
struct date birthday;
};
int main(){
struct scores player[4];
for (int i=0;i<4; i++){
printf("Enter player %d name: ", i+1);
scanf("%s",player[i].name);
printf("Enter player %d score: ", i+1);
scanf("%d", &player[i].score);
}
}

Command Prompt

int main(int argc, char *argv[]){
for (int i=0; i < argc; i++){
printf("%s\n",argv[i]);
}
return 0;
}
// ./main chu dep trai => chu \n dep \n trai \n

Typedef

#include <stdio.h>
typedef int stinky;
int main()
{
stinky a = 2;
printf("Everyone knows that: ");
printf("%d + %d = %d\n",a,a,a+a);
int b = a;
printf("B = %d\n",b);
return 0;
}