Coding for DUMMIES 01: Beginning to Coding in C language
First if we talk about C programming language, it is one of the oldest
language developed by Dennis Ritchie in 1970s, and it became base for many
programming languages. Also C language is still used in various field of
programming. it is considered as a high-level language but some consider it as
a mid-level because it supports both low-level and high-level features. C
language is considered as the mother language of all the modern programming
languages, as most of the compilers, Kernels, etc. are written in C language,
and most of the programming languages follow C syntax, for example, C++, Java,
C#, etc. Before we discuss the
problem let's talk about points to note
on C language before coding first,
• C
language is case sensitive - which means uppercase and lowercase letters are
handled differently.
example:- SUM , Sum and sum are
three distinct identifiers.
• Statements
in C must end with a semicolon (;). Forgetting to add a semicolon at the end of
a statement will show an error.
example:-
printf("HelloWorld");
• Every
C program consist of one or more functions and braces '{}' used to define the
function. code is written inside the braces.
Getting in to C coding.
Now lets write our program. first we need to use an IDE to write our
code . IDE is a single integrated application known as an Integrated
Development Environment that is used to
Editing, compiling, running, and debugging programs. There are many IDEs
in the world and the one we gonna use is called Quincy. In this post I’m going
to continue the explaining in Q&A format. I think that will help you to
read and gain knowledge in this post easily.
Question 1 : "Print your Curriculum Vitae."
Now lets get in to our first approach to this question.
In this approach what we have done is just displaying every text
directly and this is going to be very long difficult to keep track on the code.
code itself is simple . but if we have to create a CV for someone else we will
have to edit code from the beginning and you can imagine it is very boring to
do so. now lets see breakdown of the above code.
When coding every c program start as
#include <stdio.h>
- this tells the compiler to
include standard input output header file (stdio.h) as a part of the program.
int main()
{
}
int means integer , which means that the program is going to give an
integer value as output
main() is the name of the program. Main is a special name that indicates
precisely where the program is to begin execution.
{} shows start and end of the statements that's belongs to main
function. all statements written inside the braces are part of the main
function.
printf ("\n")
printf is a keyword used in C programming language to represent output.
after calling printf () parenthesis are used. everything inside the double
quotations will be displayed except the "\t and \n".
\t is tab character. its represent
a tab button in keyboard.
\n
is called the new line character and when it is used everything past
that will be displayed in a new line. also these characters are called escape
sequences. there are many escape sequences in c language.
semicolon (;) is used to mark the
end of the statement.
return 0; is written to mark the finish of main function. you can use any integer value but most programmers use to error check that if the program run and end without any errors it will shows 0 and if not it will shows 1 .
output of the above program will
be as follows:
now lets see and another way to get same output using little bit advanced keywords or we can call them using format specifiers/modifiers.
now in this code also same output
can be generated. but if you compare this code and the first code you can see
this code is little bit different. instead of directly writing name in this
code %s format modifier is used for the strings and %d is used for the
integers. what %s do is print right justified string characters as output .
printf("Intern at %s inc.
(%d).\n", "Apple", 2021);
in the above statement Apple is
assigned in to the place of %s and 2021 is assigned to the place of %d
so if we want to edit the details we don't have to search the entire code for the location, we just can look at the end of each statement and edit the code as follows.
Now you can understand that even for a one problem there can be many ways too get the correct output. The key is to select the approach that best meets your project's requirements while preserving code clarity and maintainability. Embrace the beauty of diversity in C programming!
So now we have talked about how can we get
same output for a problem with different code using C programming language. Now
lets move into how to decide the output of a C program. Before looking at my
answer it is better if you can decide the output of the following program, with
the knowledge you have gain at the beginning of this post.
Question 2 - What output would you expect from the following program?
#include <stdio.h>
This is the first statement of our
program. What does this really mean ? Every program running on a computer
requires the ability to input and output data. This is common to every other
program. So, every time when we start writing programs first we must have to
program these functions. This is really a time wasting work. To avoid this, all
these common functions are programmed and stored in C libraries. Therefore, we
can use these functions without editing again and again. In this way, the above
#include <stdio.h> indicates how to include the standard input output header
file which is stdio.h header file into our program.
int main (void)
This is the function that starts
the execution of the program. Lets break down this statement piece by piece and
talk about each one. The “int” part says that it will return a integer value.
“main” is the function in here . “(void)” simply means that the function takes
no arguments.
{}
Everything between the opening
curly brace and the closing brace is considered as the body of the main
function.
printf (“Testing…”);
In here printf is a function is C
language that used to display the characters between “” . “Testing…” is the
string what it is needed to display when the program is executing. The “ ; ”
represents the end of the statement . Similarly the same idea is repeated in
the following lines, which are printf (“….1”); , printf (“…2”); , printf
(“..3”);
/n
This is the new line character.
This means the content written after this “/n” character must display in a new
line immediately after this character.
return 0;
This is used at the end of the
main function. This represents that the program executed successfully without
any error. Instead of using zero, we can use any integer value in here. It will
not affect the program. But in standard way we use “0”.
So I hope after this explanation
you may now know what the answer for this question is.
So in here, #include
<stdio.h> and int main (void) statements will not be displayed in the
output. It will only display the strings or characters written within the “”
marks. So the output will be,
Testing…….1…2..3
I think now you have a better
understanding about the every part of the program than before. Now I am going
to give some questions to write program just only using printf function. It
will really help you to gain some experience in C programming, and it will help
you to understand how the environment works.
Question 3 - Write a program that prints the following text at the terminal.
In
C, lowercase letters are significant.
main is where program execution begins.
Opening and closing braces enclose program statements in a routine.
All
program statements must be terminated by a semicolon.
When editing this program make sure to use \n character , otherwise all the statements will arrange in a single line. So the answer is ,
So the program will be as follow. Rather than show them one
by one I will show all the six shapes in a single program.
I hope now you have a clear idea about how to program in C
language.
So from now finish today’s post and lets meet again with
more exercises to improve our handling in C. Have a nice day !
contribution : Question 1 - Sandakelum Kumarasiri @SK2208 5737
Question 2,4 - Ranishka Gunathilake @RG761 5641
Question 3 - Mahesh Wijenayaka @MW1513 5734