CS50’s Introduction to C: Part 2

Sachin Bhutekar
5 min readDec 2, 2020

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

Credit: www.media361.com

In the previous blog, I discussed some important concepts of C language. In this blog, I will discuss the following topics

  • Debugging
  • Functions
  • Variable Scope
  • Arrays
  • Strings
  • Command Line Arguments.

Debugging

It's human nature to commit mistakes. The bugs are mistakes in a program that we didn't intend to make. Debugging is the process of finding and fixing these mistakes.

CS50 IDE provides a special function help50 to help us fixing Syntactical errors. You can use help50 with the help of the following command
help50 ./program_name

Debugger CS50 IDE

The debugger allows us to run each line of code one by one and to observe what’s going on inside computer memory at each stage. The program execution pauses at a breakpoint. From there onwards, you can run each line of the program manually with a step-over button in the debugger window. The debugger highlights the line at which execution is paused and the value of different variables at that step. We can use the debugger in CS50 IDE as follows

  1. Set a breakpoint by clicking beside the program line number
  2. Enter debug50 ./program_name in terminal
  3. Use step over (F10) to execute the next line.
  4. Use ctrl + c to close the debugger.

Functions

CS50 defines a function as a black box that takes a set of inputs and provides a single output. It's called a black box because if you are not writing a function, you don't need to know about its implementation as long as the output is as you expected. For example, we used printf() function in the ‘Hello World’ program without knowing how it is implemented.

Function behavior can be predicted based on its name. Hence if you are writing a function then it is important to give relatively obvious names and functions should be well documented.

Creating a Function

To create and use the function, we need to

  • Declare a function.
  • Define a function.
  • Call the function.
Creating a Function

Why use functions?

  1. Organization
    Functions help break up complicated problems into more manageable parts
  2. Simplification
    Smaller components tend to be easier to design, implement & debug.
  3. Reusability
    You need to write functions only once but you can use them as often as you need.

Variables Scope

The scope is characteristic of a variable that defines from which function they can be accessed. There are two types of variables based on scope.

1. Local Variable

The variable that can only be accessed within the function in which they are declared. In the following code, x is local to the function twice() and no other function can access it, not even main(). The variable answer is local to the main().

int main(void)
{
int answer;
answer = twice(5);
printf(“Answer=%i”, answer);
}
int twice(int x)
{
return x*2;
}

Local Variable Vs Global Variable

2. Global Variable

If a variable is declared outside of all functions, then it is called Global Variable and any function in the program can access it. In the following code, global is declared outside and thus can be accessed by any function in the program.

int global=5;
int main(void)
{
twice();
printf(“Answer=%i”, global);
}
int twice(void)
{
global*=2;
}

Arrays

The array is a collection of values of the same data type at contiguous memory locations.

  • Each array is partitioned into small identically sized blocks of space called Elements
  • Each of which can store a certain amount of data of similar data type.
  • Each element can be accessed directly by an index.
  • Elements of an array of length n are indexed starting with 0 and ending with n-1.
  • We need to specify the size of an array at the time of its declaration which cannot be modified later.

There are two types of Arrays

  1. Single Dimensional Array
  2. Multi-Dimensional Array
    (More than 1 dimension)
Arrays

Strings

Strings are actually an array of characters. For string str, we can access its every character by index str[0], str[1], and so on. The string ends with a special character called Null Character (\0) or Null Terminating Character. It is a byte whose all bits are set to 0. So whenever we create a string, we always need one extra byte in memory for the null character.

If we have a single-dimensional string “Hello”, then it will be stored in memory as follows.

String

If we have a two-dimensional string “Hello World”, then it will be stored in memory as follows.

Strings

Command Line Arguments

So far we far the program has been starting with int main(void). We have been collecting input from users after the program has been started through in-program functions.

If we want the user to provide the data to our program before our program starts, then you have to modify the form of the main() function as follows, where we provide these two special arguments to main() that enable you to know what data the user has provided and how much data they provided.

int main (int argc, string argv[])

  • int argc(argument count)
    This integer type variable will store the number of command-line arguments that the user has typed when the program was executed. If we provide n arguments, it will store integer (1+n). If we don't provide an argument then it will store 1.
  • string argv[] (argument vector)
    This array of strings stores, one string per element, the actual text typed by the user at the command line when the program was executed. The first element argv[0] stores the command to run the program.
Command Line Arguments

Conclusion

I hope that these two blogs on C language will be helpful to you. This series of blogs on CS50 will continue. You can find my next blog here. Thank you very much for your patient reading. 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.

--

--