INTRODUCTION PART 1

What is C Programming Language? Basics, Introduction


What is C programming?

C is a general-purpose programming language that is extremely popular, simple and flexible. It is machine-independent, structured programming language which is used extensively in various applications.

C was the basic language to write everything from operating systems (Windows and many others) to complex programs like the Oracle database, Git, Python interpreter and more.

It is said that 'C' is a god's programming language. One can say, C is a base for the programming. If you know 'C,' you can easily grasp the knowledge of the other programming languages that uses the concept of 'C'

Where is C used? Key Applications

  1. 'C' language is widely used in embedded systems.
  2. It is used for developing system applications.
  3. It is widely used for developing desktop applications.
  4. Most of the applications by Adobe are developed using 'C' programming language.
  5. It is used for developing browsers and their extensions. Google's Chromium is built using 'C' programming language.
  6. It is used to develop databases. MySQL is the most popular database software which is built using 'C'.
  7. It is used in developing an operating system. Operating systems such as Apple's OS X, Microsoft's Windows, and Symbian are developed using 'C' language. It is used for developing desktop as well as mobile phone's operating system.
  8. It is used for compiler production.
  9. It is widely used in IOT applications.

Why learn 'C'?

As we studied earlier, 'C' is a base language for many programming languages. So, learning 'C' as the main language will play an important role while studying other programming languages. It shares the same concepts such as data types, operators, control statements and many more. 'C' can be used widely in various applications. It is a simple language and provides faster execution. There are many jobs available for a 'C' developer in the current market.

'C' is a structured programming language in which program is divided into various modules. Each module can be written separately and together it forms a single 'C' program. This structure makes it easy for testing, maintaining and debugging processes.

'C' contains 32 keywords, various data types and a set of powerful built-in functions that make programming very efficient.

Another feature of 'C' programming is that it can extend itself. A 'C' program contains various functions which are part of a library. We can add our features and functions to the library. We can access and use these functions anytime we want in our program. This feature makes it simple while working with complex programming.

Various compilers are available in the market that can be used for executing programs written in this language.

It is a highly portable language which means programs written in 'C' language can run on other machines. This feature is essential if we wish to use or execute the code on another computer.

How 'C' Works?

C is a compiled language. A compiler is a special tool that compiles the program and converts it into the object file which is machine readable. After the compilation process, the linker will combine different object files and creates a single executable file to run the program. The following diagram shows the execution of a 'C' program

Nowadays, various compilers are available online, and you can use any of those compilers. The functionality will never differ and most of the compilers will provide the features required to execute both 'C' and 'C++' programs.

Following is the list of popular compilers available online:

  • Clang compiler
  • MinGW compiler (Minimalist GNU for Windows)
  • Portable 'C' compiler
  • Turbo C

C Hello World! Example: Your First Program


Here, is a Hello World program in C

#include<stdio.h>	//Pre-processor directive
void main()		//main function declaration
{
printf("Hello World");	//to output the string on a display
getch ();		//terminating function
}

Here is the code explanation:

Pre-processor directive

#include is a pre-processor directive in 'C.'

#include <stdio.h>, stdio is the library where the function printf is defined. printf is used for generating output. Before using this function, we have to first include the required file, also known as a header file (.h).

You can also create your own functions, group them in header files and declare them at the top of the program to use them. To include a file in a program, use pre-processor directive

#include <file-name>.h

File-name is the name of a file in which the functions are stored. Pre-processor directives are always placed at the beginning of the program.

The main function

The main function is a part of every 'C' program. We can represent the main function in various forms, such as:

  • main()
  • int main()
  • void main()
  • main(void)
  • void main(void)
  • int main(void)

The empty parentheses indicate that this function does not take any argument, value or a parameter. You can also represent this explicitly by placing the keyword void inside the parentheses. The keyword void means the function does not return any value, in this case, the last statement is always getch ().

#include<stdio.h> //Pre-processor directive int main() //main function declaration { printf("Hello World"); //to output the string on a display return 0; //terminating function }

In the above example, the keyword int means the function will return an integer value. In this case, the last statement should always return 0.

The source code

After the main function has been declared, we have to specify the opening and closing parentheses. Curly brackets { }, indicate the starting and end of a program. These brackets must be always put after the main function. All the program code is written inside these brackets, such as declarative and executable part.

The printf function generates the output by passing the text "Hello World!"

The semicolon ; determines the end of the statement. In C, each statement must end with a semicolon.

So we have successfully installed the compiler and now can begin working in 'C.' We will write a simple program that will say hello to us. Let's start.

How to write Comments in C Programming

What Is Comment In C Language?

A comment is an explanation or description of the source code of the program. It helps a developer explain logic of the code and improves program readability. At run-time, a comment is ignored by the compiler.

There are two types of comments in C:

1) A comment that starts with a slash asterisk /* and finishes with an asterisk slash */ and you can place it anywhere in your code, on the same line or several lines.

2) Single-line Comments which uses a double slash // dedicated to comment single lines

Example Single Line Comment

// single line comment example

Here is an example of comments type

// C program to demo 
// Single Line comment 
#include <stdio.h> 
int main(void) 
{ 
  
	// This is a single line comment 
	printf("Guru99"); 
	return 0;  // return zero
}

Example Multi Line Comment

/* Sample Multiline Comment
Line 1
Line 2
….
…
*/

Example Multi Line Comment

#include <stdio.h>
int main() {
/* in main function
I can write my principal code
And this in several comments line */
int x = 42; //x is a integer variable
printf("%d", x);
return 0;} 

Why do you need comments?

A good programmer who writes codes understood by a human is better than a programmer who generates codes understood only by the machine.

So, it is highly recommended to insert comments to your code because it is good programming practice. Comments do not affect a program because the compiler ignores them.

Comments help the developer understand the logic/algorithm of the code if he revisits it after a long time.



Comments

Post a Comment