C Programming for Beginners Day 1
C is a general purpose programming language. C program can be used to write everything. That may be
- An application software like word or excel
- Operating system like windos
- Complex programs like Python, interperter
- RDBMS like Oracle
What is a Language ?
It is a system of communication.
What is a Computer programming language ?
A language used to write instructions in human readable format and later an be converted using compiler to computer readable format is known as programming language. Computer programming languages are of 2 types, low level and high level. Low level are machine friendly and high level are programmer friendly.
C programming is a which type of language ?
C is a low level programming language that relates closely to the way machine works. Still it is easy to learn.
To create and run a c program we need two things.
- A text editor
- A C Compiler
We can use any text editor like notepad in windows to write C program and then use compiler to compile it. After compilation the compiler will create an executable file with .exe extension. Then we can run that file.
If you have a computer to practice you can download and install code blocks.
If you donot have a computer to practice, donot worry. You can practice using a smart phone also. In this course we will practice it on android mobile. For this so many applications are available on Google Play store. We will use CppDroid app.
Why use IDE?
Codeblocks, CppDroid these programs are known as IDEs. Full form of IDE is Integrated Development Environment. To make things easier we use IDE ( Integrated Development Environment ). IDE allows us to create, compile and run programs from a single environment. Code Blocks comes with a compiler called GCC to compile C and C++ programs and the most important thing it is free to use.
Where to Download and How to install Code Blocks?
- goto http://www.codeblocks.org/downloads/26 and select codeblocks-20.03mingw-setup.exe
because only this option will install GCC compiler on your computer. The version mentioned here is 17.12 but it may change when you are going to download.
- After downloading you can double click the setup file to install it.
- After installation run it and choose DCC compiler and all the default options.
- Now you are ready to write C Program and execute them.
#include < stdio.h >
int main() {
/* my first program in C */
printf(“Hello, World! \n”);
return 0;
}
C Program Structure
Let us take a look at the various parts of the above program step by step.
The first line of the program #include <stdio.h> is a preprocessor command, means before processing the program this command should be executed. This command tells a C compiler to include stdio.h file before going to actual compilation.
The next line int main() is the main function from where the program execution begins. It is the core of every C program. All C language programs must have a main() function as a part of it.
The next line /* My first program in C */ is a comment in this program. We are putting comments for our convenience. C compiler will ignore every thing written with in /* and */.
The next line printf(…) is another function available in C programming which is used to display the string with in parenthesis on the screen. In this case
Hello, World!
will be displayed on the screen followed by a line feed because of \n.
The next line return 0; is used to terminate the main() function and returns the value 0. If (2 no point) we write void main() then return statement is not required.
What are tokens in C program ?
A token is that the smallest individual unit during a C program . Tokens could also be either keywords, identifiers, constants, variables or any symbols which has some meaning in C language. A C program also can be called as a set of varied tokens.
For example, the subsequent C statement consists of 5 tokens −
Statement : printf(“Hello World!”);
Tokens :
printf
(
“Hello World!”
)
;
Explain the role of semicolon in a C Program ?
Semicolon plays an important role in a C program. It is used as a statement terminator. Means each individual statement must be terminated with an semicolon. Generally absence of semicolon at the end of any statement, will definitely mislead the compiler to think that this statement is not yet finished. So it will add the next statement to it, which may lead to compilation(syntax) error.
In other ways if a statement is too long and does not fit in the screen you can break it to a new line and at the end put a semicolon. C compiler will treat these two lines as a single statement. Simply to say from one semicolon to another semicolon is treated as a single statement in most of the cases.
printf(“Hello World!”); return 0;
The above line has two statements. For better readability we can write it as below:
printf(“Hello World!”);
return 0;
but both have the same meaning.
In some cases semicolon is not required at the end. We will discuss about them individually in detail, while discussing about that specific statement. Here we will have a quick look on most of them.
- Generally block statements do not need to have semicolons after them, which is why we don’t need a semicolon after the closing brace.
- Except
do...while
all other control statements likeif
,do
,while
,switch
etc do not need a semicolon after them. - Macro definitions do not require a semicolon to terminate. We will read about macro later.
What is the roll of comment in C program?
Comments are also very important part of any program. But these are not compiled by the compiler. Generally we write comments for better understanding of the program. If you are writing a large program and after a long time you want to make some changes in it. Then the comments you made previously will be very helpful for you. Other wise it is nearly impossible to remember which functions and identifiers are used for which purpose.
But for compiler comments have no use. This is for the use of programmer only to remember the use of functions and identifiers in a better way. Comments are started with /*
and ends with
*/
. Means the strings enclosed with /*
and */
is treated as comment.
/* This is my first C program */
#include < stdio.h >
int main() {
/* my first program in C */
printf(“Hello, World! \n”);
return 0;
}
Here the comment is written to know that it is your first C program. But this line has nothing to do with the program. Absence of this line will not make any impact on the output.
Generally there are two types of comments in C programming:
- Single line comment
- Multiple line comment
// This is used to create a single line comment.
/* This is my first C program */
#include < stdio.h >
int main() {
/* my first program in C */
printf(“Hello, World! \n”);
return 0;
}
In our previous example we have used /*...*/
for comment. But in this program we used //
for comment. This implies that /*...*/
can also be used for single line comment. Now see below example of multiple line comment.
/* This is my first C program
I learnt this example from https://ttrc.in */
#include < stdio.h >
int main() {
/* my first program in C */
printf(“Hello, World! \n”);
return 0;
}
For Life time valid video tutorials of this course with quiz and certification : Click Here
Downloadable video tutorials for TTRC Player : Click Here
You must log in to post a comment.