CS50’s Introduction to C: Part_1

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.

Image for post
Image for post
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

C: Mother of All Programming Languages

Image for post
Image for post
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 which 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 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 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 which involve the following processes

  1. Preprocessing
Image for post
Image for post

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 their own virtual, cloud-based online environment called Sandbox & CS50 IDE for its students to write C programs.

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

  • To compile the program : make <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

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

Image for post
Image for post

Format modifier is the way to tell the compiler There are four basic data types in C.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 called the 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 in C, there are numbers of operators provided in C which are as follows.

Arithmetic Operators

Arithmetic operators are used for performing arithmetic operations on identifiers.

Image for post
Image for post

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

Logical Operators

Logical operators are used to construct Boolean expressions in C. The expression which evaluates 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 (&&)
Image for post
Image for post

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.

Image for post
Image for post

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
Image for post
Image for post

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

Image for post
Image for post

The if-else statement works as follows.

  • Check boolean expression
    ▷ If true, run the code 1 inside first pair of curly braces.
    ▷ If false, run the code 2 inside 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 expression to make a decision.

Image for post

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.

It is very important to use a break statement in each case otherwise based on input, the certain case will be executed and it will not come out of 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.

Image for post
Image for post

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 condition which is always true, then it will execute the code 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.

Image for post
Image for post

The do-while loop works as follows

  1. Run the code inside curly braces

3. for loop

Image for post
Image for post

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.

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.

Image for post
Image for post

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.

Written by

Computer Science enthusiast & aspiring Data Scientist. I write to share my CS learnings with others. LinkedIn: https://www.linkedin.com/in/sachin-bhutekar-cs/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store