LEARNING C

MY NOTES ON THE C LANGUAGE
0

C

EASY

last hacked on Oct 08, 2018

C is a general-purpose, imperative computer programming language, supporting structured programming, lexical variable scope and recursion, while a static type system prevents many unintended operations. By design, C provides constructs that map efficiently to typical machine instructions, and therefore it has found lasting use in applications that had formerly been coded in assembly language, including operating systems, as well as various application software for computers ranging supercomputers to embedded systems. **Interesting fact**: The Linux operating system is mostly written in C.
# **First C Program** Create the source code file and name it `firstProgram.c`: $ touch firstProgram.c Open `firstProgram.c` and input the following code: #include <stdio.h> int main(void){ printf("Hello World!\n"); return 0; } Compile it using `clang`: $ clang -o firstProgram firstProgram.c Now run your build: $ ./firstProgram The terminal should **output**: Hello World # **If Then** Create your source code file and name it `ifThen.c`: $ touch ifThen.c Open `ifThen.c` and enter into it the following code: #include <stdio.h> int main(void){ int x = 10; if(x == 10){ printf("x == 10\n"); } return 0; } Compile it using `clang`: $ clang -o ifThen ifThen.c Now run your build: $ ./ifThen The terminal should **output**: x == 10 --- # **While Loop** Create your source code file and name it `whileLoop.c`: $ touch whileLoop.c Open `whileLoop.c` and enter into it the following code: #include <stdio.h> int main(void){ int x = 0; while(x < 8){ printf("x = %d\n", x); x++; } } Compile it using `clang`: $ clang -o whileLoop whileLoop.c Now run your build: $ ./whileLoop The terminal should **output**: x = 0 x = 1 x = 2 x = 3 x = 4 x = 5 x = 6 x = 7 --- # **For Loop** Create your source code file and name it `forLoop.c`: $ touch forLoop.c Open `forLoop.c` and enter into it the following code: #include <stdio.h> int main(void){ int x = 16; for(int i = 0; i < x; i++){ printf("x == %d\n", i); } } Compile it using `clang`: $ clang -o forLoop forLoop.c Now run your build: $ ./forLoop The terminal should **output**: x == 0 x == 1 x == 2 x == 3 x == 4 x == 5 x == 6 x == 7 x == 8 x == 9 x == 10 x == 11 x == 12 x == 13 x == 14 x == 15 --- # **Initializing Strings** Create your source code file and name it `initializingStrings.c`: $ touch initializingStrings.c Open `initializingStrings.c` and enter into it the following code: #include <stdio.h> #include <string.h> int main(void){ char x[16] = "Hello World!"; printf("%s\n\n", x); for(int i = 0; i < strlen(x); i++){ printf("%c\n", x[i]); } return 0; } Compile it using `clang`: $ clang -o initializingStrings initializingStrings.c Now run your build: $ ./initializingStrings The terminal should **output**: Hello World! H e l l o W o r l d ! ---

COMMENTS







keep exploring!

back to all projects