01 - C Basics

Link to the replit example

Hello, welcome to C programming. Lets take a look at the basic coding structure of C.

Below is the sample code to print "Hello Mountain View HS!". Click Run to test it.

The code starts with #include "stdio.h" . This is processed before the program is compiled, and the "stdio.h" stands for the "standard input and output header". It contains the basic tools and functions that we will use in C programming. You can read more about this "preprocessing phase" here: Link, or read more about other standard header files in C programming here: Link..

The code then opens up with a function called main. This is where the code you write goes. The int, void, return 0, are properties of a function. We'll get into that later.

int main(void){

return 0;

}

The code below is one of the first syntax commands we will be learning in the next module. It is used to print text on screen. Note that there is a semicolon ";" at the end of the command. This is true of all commands in C.

printf("Hello Mountain View HS!\n");

Comments

Comments in C can be included in two main types:

A single-line comment - This comment lies on a single line of text and is denoted by two forward slashes "//". The program ignores all of the text in a row after two forward slashes.

Example:

int num=5; //I am creating a variable called "num" and setting it equal to the integer 5

A multi-line comment - This comment can take up any amount of space and any amount of lines. Instead of just signaling the beginning of the comment as above (since the program knows the comment ends at the end of the line), you must now denote the start and end of the comment. We use a forward slash and asterisk to do this. The comment will start with a "/*" and end with a "*/". Notice that the asterisk is always on the inside, where the comment is written.

Example:

int num=5;

/* I just created the variable "num" and have it set equal to the integer 5.

Below I am going to write a program that will square this integer and print the resulting number (25) */

Note: Purely for aesthetics and readability, some programmers like to include multi-line comments in a box of asterisks as shown below.

Example:

/***********************************************

* This is a multi line comment

* that is completely surrounded

* by a cool, starry border!

***********************************************/

Compiled Language vs Interpreted Language

Disclaimer - I am not the world's expert on this. This will be an explanation for the lay-person and not for experts.

Python is usually considered an "interpreted" language. In general, Python code is not "compiled" as whole or, in other words, converted into machine language as a whole before being run. Instead, all the different CPUs out there get a Python interpreter. The Python code you run is then fed to the Python intepreter as input. The interpreter executes each statement of code after it is done reading it. in this way, the last line of your code may have an error, but you can still often run the beginning of your program completely fine until the program encounters the error. As such, most all errors encountered are "run-time errors" meaning they are only experienced when you run the code and get to that line.

C, on the other hand, is compiled into assembly language as a whole, before the program is run. Because of this, your program needs to be free of syntax errors and such before your entire program can be compiled (for example, missing semi-colons, missing parenthesis, mixing up data types, etc). However, since it is not actually running the code when compiling, it will not recognize run-time errors such as user-input errors, or looking at an array index that is out of range.

FAQs

Question - Why is it "printf" and not just "print"?

Answer - The printf stands for "print formatted for standard input output header" or just "print formatted". There is no "print" function in C. The stdio.h stands for the standard input output header and includes all of these functions and their specific formatting.