CS50’s Introduction to C: Part_1

Sachin Bhutekar
8 min readNov 23, 2020

This is 2nd blog in my ongoing CS50 Series. Readers are suggested to go through 0th blog for a better understanding of subject matter. In this blog, you will learn about the C Programming Language.

Credit: www.media361.com

In the previous blog, I discussed how to create your first game in Scratch. In this blog, I will discuss the following topics

  • C
  • Hello World: First C Program
  • Compiling
  • Data Types
  • Variables
  • Operators
  • Conditional Statements
  • Loops
  • Command Lines

C: Mother of All Programming Languages

Credit: GeeksforGeeks

Week 1 to Week 5 of CS50 takes us on a journey of programming with “Mother of All Programming Languages”. You guessed it correct! Its C. It is a little less user friendly as compared to scratch since it is purely text-based, but contains all features of Scratch

Hello World: First C Program

Let’s write our first program in C.

#include<stdio.h> //Header file
int main(void) //Main Function
{
printf(“Hello, World!”); //Print Hello, World!
}

The first line #include<stdio.h> at the beginning of program adds a file stdio.h (Standard Input Output) called Header File in our program, Header file contains the definition code of all Input Output functions like printf() which we are going to use in our program. A function is a piece of code written to perform a certain task.

The execution of C program starts from main() function i.e. from line int main(void). The computer locates the main() function and starts the execution of a program written inside curly braces {} after main().

The printf() function is an output function that displays the content on the output screen. The content to be displayed on the output screen must be written in between double quotes “ ” inside the braces ().

Every statement in C language must be ended by a semicolon (;). It is called a statement terminator. The Lines starting with // are called Comments and are written for a better understanding of code for the user. The computer just neglects the comments.

These smaller details or grammatical rules of programming language are called Syntax. The syntax must be followed strictly otherwise execution of the program will not take place.

Compiling

The code written in C language in an editor is called source code and has filename extension ‘.c’. But we know that computer only understands Machine language of 0s & 1s. Thus source code needs to be converted to machine code (compiled) before our computer can run it. Compiling is broad terms that involve the following processes

  1. Preprocessing
  2. Compiling
  3. Assembling
  4. Linking

Integrated Development Environment (IDE)

An IDE is a collection of all the tools (Editor, Preprocessor, Compiler, Assembler, etc) which are required to develop software (.exe file)using C language. CS50 provides its own virtual, cloud-based online environment called Sandbox & CS50 IDE for its students to write C programs.

You can use the following command lines in Sandbox or CS50 IDE.

  • To compile the program : make <program_name.c>
  • To run the program : ./<program_name.c>

There are also many online IDEs available like this as well as offline IDEs like Visual Studio (VS) Code, Eclipse, etc. You can check this video for setting up VS code for c programming on Windows PC.

Data Types

The names given to entities like variables, functions, etc. in the C program are called Identifiers. The data type of an identifier defines three things

  1. Type of data it can store
  2. Size of memory required to store the data
  3. Range of data that can be stored

There are 4 basic data types in the C. CS50 library provides two extra data types, string & bool which are not part of the C language originally.

Format modifier is the way to tell the compiler about what type of data it is going to take as input or print as output.

Variables

A variable is a name given to the memory location in computer memory. A “variable” is like a box that holds a value.

Declaration of the variable is a prior intimation to the compiler before its use. Once declared, you need not specify variable data type again in the program.

When we assign value to a variable then it's called asthe Variable Assignment.

If we declare and assign value to a variable simultaneously then it is called Initialization of variable.

Declaration : int x;

Assignment : x=7;

Initialization : int x=7;

Operators

In order to manipulate and work with identifiers, there are various operators provided in C which are as follows.

Arithmetic Operators

Arithmetic operators are used for performing arithmetic operations on identifiers.

Remember modulus (%) operator which returns reminder of division whereas division(/) operator returns the quotient of division.

Logical Operators

Logical operators are used for constructing the Boolean expressions in C. The expression which results in only two possible values true or false is called Boolean Expression.

Boolean expressions are very important because we use their evaluation result in conditional statements and loops to decide whether some block of code should be executed or not. In C, every non-zero value is equivalent to true and zero is false by default.

There are three logical operators in C.

  1. Logical AND (&&)
  2. Logical OR(||)
  3. Logical NOT(!)

Relational Operators

Relational operators are used for comparing the value of two identifiers to understand the relationship between them. Relational operators return either True or False based on the evaluation.

Don’t confuse assignment (=) operator which assigns the value of its RHS operand to its LHS operand with equal to the operator (==).

Conditional Statements

Conditional statements in C are very useful as they allow us to make a decision and follow some particular path based on user input.

  1. if Statement

The if statement is simplest of all conditional statements and works as follows.

  • Check boolean expression
    ▷ If true, run the code inside curly braces.
    ▷ If false, Skip the code inside curly braces.

2. if-else Statement

The if-else statement works as follows.

  • Check boolean expression
    ▷ If true, run code 1 inside the first pair of curly braces.
    ▷ If false, run code 2 inside the second pair of curly braces.

3. switch Statement
The switch statement is unique among all the conditional statements because it allows the user to specify the distinct cases instead of boolean expressions to make a decision.

In the given program, an integer is requested from the user to enter using scanf() function which will be stored in a variable i.

  • For i=1, “Case 1” will be printed and the break statement will take execution out of switch statement without executing other cases.
  • For i=2, “Case 2” will be printed and the break statement will take execution out of switch statement without executing other cases.
  • For i=3, “Case 3” will be printed and the break statement will take execution out of switch statement without executing other cases.
  • For other values of i, the default case will be executed and after printing “Default”, the execution of the switch statement will end.

It is very important to use a break statement in each case otherwise based on input, a certain case will be executed and it will not come out of the switch statement but will execute all remaining cases.

Loops

In C language, if we want to execute a certain piece of code repeatedly, then we can use Loops.

1. while loop
While loop is used when you want to repeat a piece of code for an unknown number of times.

The while loop repeats the following stages till the time Boolean expression is true.

  1. Evaluates the boolean expression
    ▷ If true, run the code inside curly braces and go to step 1.
    ▷ If false, exit from the loop.

If we give a condition which is always true, then it will execute the code an unlimited number of times and we have to use a break statement to stop the execution of the program.

2. do-while loop
Do while loop is used when you want to execute a piece of code at least once.

The do-while loop works as follows

  1. Run the code inside curly braces
  2. Evaluates the boolean expression
    ▷ If true, run the code inside curly braces and go to step 1.
    ▷ If false, exit from the loop.

3. for loop

For loop is used when you want to repeat the piece of code for a specific number of times. The for loop works as follows

  1. Initializes counter variable or sets its value.
  2. Checks boolean expression.
    ▷ If true, run the code inside curly braces and go to step 3.
    ▷ If false, exit from the loop.
  3. The Counter variable is incremented or decremented
  4. Go to stage 2.

Command Lines

Command Lines are keyboard-based commands that can be used to perform different tasks by typing them in the terminal of CS50 Sandbox or CS50 IDE or any Linux-based system.

Conclusion

That is a lot to learn for today. Thank You very much for your patient reading. You can find my next blog here. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. You can connect with me on LinkedIn or by visiting my website on Medium. Happy Learning.

--

--