C is one of most used Programming language till date. It is one of the most powerful language and mother of many modern Programming language (eg. Python , Ruby)
π C is a compiled language, unlike Python or Ruby , C programs must be translated from human readable code to machine readable code.
The program which translates human readable C code to machine readable code is called a compiler
You'll be Astonished to know that the most Compilers of C (eg. GCC) is also written in C
You'll find people who says , C is so hard but i say nothing is hard it just needs a good teacher to explain everything.
So without further talking let's take our first step in the world of C Programming.
Setting Up Workspace
As i mentioned earlier C is a compiled language so you need a compiler such as GCC to compile C programs.
Today we'll be using Repl.it to compile and Run C programs online.
If You want to run and compile C Programs in Your local device Read This Article
To Run and Compile C programs online without installing anything , Use REPL.it
When Opening Repl.it you'll see a sample program is already written in the left pan. Clear it first.
Writing Traditional Hello World
As I mentioned earlier Hello World is traditional program which every programmer Writes at first when learning new language. Which just prints Hello World text in a console window
Hello World program in C is a bit longer than Python or Ruby.
Let's Write
#include<stdio.h>
int main()
{
printf("Hello World\n");
return 0;
}
Write the above code in the left pane of repl.it or if using Local device , write the above script in a file called hello.c and execute the command in terminal
gcc -o hello hello.c
If you're in repl.it click the *Run * button and if on local device execute this command
./hello
if on a linux or Mac Device
Or
hello
on MS Windows Device
If everything is fine, you'll see a Hello World text in console/terminal
If there's any problem, feel free to let me know in the comments bellow π
Otherwise Let's Go To The Next Chapter
Understanding The Hello World Program
Writing code without understanding it is really useless. Copying-Pasting will not work forever.
So, Let's Try to Understand The Above Code.
Let's Try To Understand From First Line
#include<stdio.h>
It includes instructions for C compiler and the definition of printf
, this is like a dictionary for a C Compilers, from this Compilers know what to do with printf
Let's Step in the next line, We'll find this,
int main()
int
is the acronym of integer
here main()
is a function , everything inside a function is taken as a instructions , from this our program knows what to do. There may be other functions , but main()
is a special function , in every program this function is executed first.
And this ()
parentheses help Compilers identify that it is a function.
Functions are like a jar which contains some special instructions of what to do.
In the next line we'll find {
a curly brace , it denotes the begining of a function , it's like a the lid of a jar.
In the next line we'll find
printf("Hello World");
printf
is a also like a function but pre-defined , things inside its quotation marks will be written in. Console window when we'll run our program.
Parentheses ()
denotes the begining and ending of the printf
function
We see a Text , Hello World\n
inside quotation marks this is the text which will be written in console window and at the end there's a \n
which tells our program to start a new line and put the cursor in the new line.
Try changing the text and running it
Actually it's not necessary but it is necessary when working with a program which needs to print two or more lines. But you should keep it as a good practice.
You'll see a semicolon
;
at the end of line, it's not for style, it's necessary , it helps compiler to understand that this line has ended now.
in the next line we'll find
return 0;
which is not actually for us , but for machine it returns 0 to the machine, and that's how machine understand that the program ran successfully and now ending.
If a program returns a number other than, Computer will think that it has not run successfully. Try Changing it and running it.
And finaly at the end , }
ending curly brace denotes that our main function is ended.
Must Read This Comment : https://dev.to/tux0r/comment/4o9i
So guys that's for now, if you've any questions or got any problems please let me know in the comments below π
If You Like My Work (My Articles, Stories, Softwares, Researches and many more) Consider Buying Me A Coffee β π€
Top comments (7)
Well, you asked for it.
C is not the "mother" of Python and Ruby. Both are rather Lisp-like.
No, there are C interpreters.
This is not recommended. You should add a space before the
<
.Not necessarily. In WinAPI applications,
WinMain()
can be used, in other applications you can (probably) tell the linker where to start.No.
()
is the lazy form of(void)
, it tells the compiler that this method does not have any parameters. (Which, strictly spoken, is not even true either.main()
has two default parameters,int argc
andchar** argv
. You can omit them if you don't need them, but your operating system will pass them to your application anyway.)... and every other block which spans more than one line.
I think him is refer to the main and first interpreters written in C:
Python -> CPython
Ruby -> Ruby MRI, YARV
Thank You For Pointing my faults, i learnt a lot.
Thank You.
I'll fix it but for now i think it would a good idea to unpublish it.
That's up to you. :-)
;-)
I added this line in the post.
I was able to follow your instructions in the article and got the program to compile correctly on Repl.it. Thanks for that bit of info. I am always looking for a good way to practice online.
Keep going. Keep writing. Keep learning. I liked the article.
Thanks,
You can try this :
youtube.com/channel/UCWe_os3p4z3DB...