GoCoding.org

C Language Basics

by | Aug 14, 2021 | C

Introduction

In this article we will try to explore the C Language Basics. In the previous article, we have seen how to write a program.

The “C language” code.

#include<stdio.h>
int main ()
{
  printf (“C language”);
 return0;
}

On compiling the above code, “C language” got printed on the screen as output.

Till now, we have just written a code that can print anything on the screen. Now, let’s try to understand this code in detail so that we can print other statements.

Understanding the C language basics

Let’s start with printf (“C language”);

printf is a predefined function in C which prints whatever is written inside the round braces ( ) on the output window.

Note: printf function only works with ( ) braces.

A function takes input from the user, performs operations on it, and returns an output.

In the above code, printf takes “C language” as input and shows “C language” as the output on the screen.

So, we are clear with this printf statement. There is a semicolon at the end of the statement. A semicolon (;) is used to end a statement. Whenever we want to tell the compiler that a statement has ended, ‘;’ is used.

Using ‘;’ at the end of printf (“C language”);, indicates that we have ended that statement there. Anything written after that will be considered as the new statement by the compiler.

#include<stdio.h>
int main ()
{
  Printf (“C language”); return0;
}

 

The above code is completely valid. The statements printf (“C language”); and return 0; will be treated as different statements even though they are written in the same line.

Moving onto the next part of the code:

int main( ): main is a function. The execution of a C program starts from the main function, which means it is compiled first. int before main means the function will return an integer.

Note: All C programs must have this main() function since the execution of every C program starts from this function.

The statements written inside the main function are called the body of the main( ) function.

{ } : Curly braces are used to enclose the body of the main( ) function. Statements written inside these braces form the body of the main.

One more statement is written inside the body of the main function in last, i.e.

return 0;  As already stated, the main( ) function returns an integer value because it is written with ‘int’. Therefore, returning 0( an integer value) here., The return keyword is used to return some value from a function.

The main function gets terminated by this return statement.

We will discuss the function chapter in a much better way later on. For now, we just have a piece of basic knowledge to write programs.

Now, coming to the very first statement written in the code, i.e.

#include<stdio.h>: stdio.h stands for standard input-output. It is a C library that contains the definition of a function like ‘printf’. Thus, the compiler will get to know about the function of ‘printf’ from this library only.

#include is a preprocessor that links stdio.h with the rest of the code.

(Preprocessor and C library will be discussed in later chapters in detail.)

Thus, ‘#include’ will make ‘stdio.h’ library available for use, and then the compiler will get to know what exactly ‘printf’ is. Then execution of the main function will start from the ‘main’ function and allow ‘printf’ to give the message on the screen. ‘return 0’ will return 0 and indicate that our program has been run successfully.

So, the format to write a C program is like this:

#include<stdio.h>

Int main( )
{
  statement 1;
  statement 1;
  statement 1;
  ……….
  statement n;
   return 0;
}

 

Note: \n is a newline character that is used for changing lines or printing new lines.

See the changes with the newline character when used in the above code:

#include<stdio.h>
int main ()
{
  Printf (“C language\n”);
 return0;
}

 

This will print a new line after executing the statement written inside the ‘printf’ function.

Let’s try out some more examples:

Code:

#include<stdio.h>
int main ( )
{
printf(“Hi!\nbrother”);
}

 

Output:

C Language Basics

 

 

Practice Question

Try to print the pattern shown below:

*

***

*****

 

Commenting in C

There are some statements written inside the code that are ignored by the compiler while compiling. These statements are called comments.

Comments are used to make the code more precise and clear.

Comments are written after “//”. However, these are single-line comments. If we change the line, then the new line will not be part of the comment.

Commenting in C Single Line Comment

Multiline commenting can be written inside “ /*……..*/”.

Commenting in C Multi Line Comment

While making notes of the various concepts of the C language, we should remember that commenting done in the code is very helpful for other students/beginners to understand the code. Otherwise, it is difficult for them to understand.

Moreover, the person who has written the code is usually not the one who will modify it. So, using comments while coding is a good habit.

Note: Avoid too much commenting. It is not recommended to use excess comments in the code.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.