Welcome to An Introduction to C Programming Language course. This is a mini yet compulsory course for undergraduate computer science students. In this course, you are going to learn C programming language and do some low-level programming. There are numerous benefits of taking this course but one of them is - this course is a prerequisite of almost all the advance courses you are going to take as undergraduate computer science student.
This is a pass/fail course. You need to pass this course in order to get hacker badge on your student’s profile. If you don't get this badge on your profile then professor will know that you don’t know C programming and it might possible that you don't get enrollment in class where more students has applied than seats available and professor has to choose.
But, don't worry! This course is not challenging course. With more practice, hard work, and patience you can easily pass this course. This course has high passing ratio than any other courses within university!
Prerequisites
As you already have completed your first semester in computer science, you know some programming and that's the prerequisites of this course. You should know the basic concepts of programming such as variables, conditionals, loops, functions, standard libraries, how to write and execute the program and so on. These basic concepts are not going to explain in this course, they are assumed - you know these concepts.
Software
You need two software on your GNU/Linux system - text-editor and compiler. You are free to use any text-editor you like but for the compiler, you must have to use GCC or GNU Compiler Collection. Because, that is the compiler on which the course is depends on.
You can communicate with your seniors for the installation of these software.
Grade
Total grade for this course is 10(+2)
. You need at least 7
out of 10(+2)
to get badge. The grade is divided into three categories.
- Projects ( 3 in-course projects with weightage of 2 for each ) -
6
- Capstone ( capstone project, you are going to build at the end of this course ) -
2
- Contribution ( the contribution you need to do in open source project, will get more information in future email, when needed ) -
2
- Bonus ( a reserved task that we will release after second in-course project ) -
2
Finally, you will not get grade in terms of numbers. You will only get either pass or fail based on score. But, if you do an incredible job at projects, we have many things for you and that will help to build your career.
Ref. Books
This course does not depend on any book. The course notes are going to serve with each lecture. But, for the reference purpose you should purchase the K&R book. You also need some reference to standards such as C11 and C18. That is available online at devdocs.io and gcc reference manual.
Contact
This is the little introduction of this course. You might have questions regarding - anything. Feel free to ask me in personal or via email at - email. Please don’t ask or send email for installation.
Let’s Get Started
After covering this administriva stuff, we still have half an hour. In this half an hour, I’ll going to quickly cover what is C and why you should consider to use and after that we’ll write hello, world program in C.
C is the programming language that is twice older than you! and yet its been in top programming languages chart since last few decades. How? There are many reasons but some of them are:
- blazingly fast,
- provides no/little abstraction to hardware,
- has huge code base and experts,
- is small programming language and
- continuously evolves with backward compatibility!
C is de facto language in terms of speed. Many other languages have claim to achieve the speed near to C but none of them able to replace it! The reason of such a speediness is, an absence of abstraction to the hardware. In C, you are directly talking to hardware! That has numerous benefits when you are doing low-level programming. And that’s the reason why more than 40 operating systems are written in C. Yet the language is so small that it completely explain within 272 pages. Finally, C continuously evolves in terms of standard and make it more modern while maintaining backward compatibility. The current standard is C18.
That’s enough for introduction we will talk a lot about C in future sessions with great open discussion sections and via guest panels. Let’s quickly jump into text-editor and write our very first hello, world program.
Hello, world Program
I’ve opened the text-editor with blank window. Let me quickly save this blank program with name hello.c
. C language has .c
extension and hello
is the name of the program that satisfies the purpose of code, going to written inside of it. After saving this blank program, I have syntax highlighting. Good!
Next, we are going to print hello, world text in terminal. So, we need to write this text within hello.c
. Let me do that.
hello, world
But, this hello, world
is string, right? In program, you write string inside of double or single quotes. Agree? But in C, you must write string inside of double quotes. So, let me quickly wrap this text with double quotes and make it string.
"hello, world"
Great! This string is not going to print in terminal on its own. We need to invoke a function on it to print and the function is printf()
. This is how we use printf()
function.
printf("hello, world")
C is one of those languages the required semicolon to complete the statement. In C, semicolon is not optional but required. And that way I need to write,
printf("hello, world");
Semicolon at the end. Great. But you know, compiled languages like C is different than interpreted language in terms of execution. In interpreted languages, the execution start from top and execute each statement or expression that comes in flow. But, in compiled language you need to tell the starting point - a point from where the compilation process starts. This is where the main()
function comes in picture. Your program must contain one main()
function and this is the function from where your program will going to compile.
In order to run our printf()
statement, we need to put it inside of main()
function this way.
main() {
printf("hello, world");
}
This indentation inside of function is not required. It is just for readability purpose. But, it is required for this course. If your program contain confused indentation, you will likely lose the grade even if your project perfectly works!
You are going to use curly braces to define block like I did to define main()
function block. Whatever statements we write inside of this curly braces, are considered part of the main()
function.
After compilation of this main()
function, it returns something. It returns an integer with 0
or greater than 0
. Here, 0
means successful compilation and any other value than 0
mean something goes wrong with your program! Let’s add the type signature before the main()
function to indicate, what this main()
is going to be written and also write return 0
explicitly.
int main() { printf("hello, world"); return 0; }
Yes, this return
statement should be at the end of main()
function.
In C, you don’t get a single function by default and printf()
is not an exception. You need to include a library in which this function is written. The library or header file is stdio.h
and its standard header file that comes with C. You can include this file in our program like -
#include<stdio.h> int main() { printf("hello, world"); return 0; }
You need to write the include statement at the top of your program and the blank line after this include statement is optional. But, again it is strongly recommended and required for this course.
That’s it! This is our hello, world program in C. It’s time to execute this program.
Execution
We need a compiler to execute any C program. For this purpose, we are going to use GCC. This is the compiler that comes with most of the GNU/Linux system. A quick --version
command will let you know that C is installed on your system or not.
$ gcc --version gcc (GCC) 8.3.1 20190223 (Red Hat 8.3.1-2) Copyright (C) 2018 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
If you get similar output, then you have gcc
on your system else you need to install. In my case, I’ve gcc
so I can go further and execute this program. The execution of C program done in two steps —
- Compiles the program,
- Executes the compiled program.
To compile the program, this command is used.
$ gcc hello.c -o hello
This will compile your hello.c
program and after successful completion it will generate output (-o
) with name hello
. You will get nothing if compilation happen successfully in terminal. In my case, I don’t get anything, so that means my program execute successfully and has build hello
. A quick ls will show you the same.
$ ls
hello.c hello
In order to execute this hello
executable, I need to write
$ ./hello
hello, world
And we get the output! But, this is a little clumsy. A \n
after the hello, world string will help. Let’s add that.
#include<stdio.h> int main() { printf("hello, world\n"); return 0; }
Let’s compile and run the program again.
$ gcc hello.c -o hello && ./hello hello, world
Here, I’ve combines both commands into single one using &&
terminal operator. You will going to use so many terminal commands in this course along with C. So, be ready!
Looks like we are running out of time for this lecture. I know you might have many questions on this program and execution process. But, let’s first try from your side at home and then in the next lecture you can ask questions.
Labels: c