C Programming Basics to Write a Simple C Program
C programming basics is all about creating a c file, writing a c program, compiling it and then getting the output. For that we have to know basic things.
You should know some basic building blocks of c program they are:
Element | Explanation |
#include<stdio.h> | Include is apreprocessor directive. Using that we are including stdio header file. |
int main() | Main function of a c program. Execution starts from this function. |
{ | Indicates beginning of a function. |
printf(); scanf(); | printf() and scanf() are stdio.h library functions for input and output. |
} | Indicates end of a function. |
Example C Program
#include<stdio.h>
int main()
{
int a;
printf("Enter the value of a\n");
scanf("%d",&a);
printf("The entered value is %d \n",a);
return 0;
}
Output
Enter the value of a 10 The entered value is 10
Code explanation
- Declare a variable to store input data.
- Print to terminal so that user have to unserstand he/she has to enter a value using
printf()
function. - Read the number entered by user using
scnaf()
function. - Print the entered number.
Let us see in brief about the above program
The first line defines the header file. This is the preprocessor command that is used to support functions used in the program.
Following it there is the main function from which execution of c program begins. “{” opening brace which tells the compiler to begin execution from here.
The comment line is included to know the purpose of the program.
The variable “a” is declared which is of integer type. Following it there are some commands like printf, scanf which performs some tasks
The input is taken from the user using scanf function. It is printed on to the console.
“}” the closing brace indicates end of the c program.
Each c programming statements end with “;” terminator.
Basic structure of a c program
Following are the sections of c program
Documentation section:
This section introduces the c program. It is written between /*……*/. The statements written in this section wont be compiled by compiler.
Link section:
This section contains header files to support some of the functions to be used in c program.
Definition section:
This section is used to define variables.
Global declaration section:
The variables which are used anywhere in the program are defined in this section.
Function prototype declaration section:
This section is used to declare functions to be used in program. It gives information about function.
Main function:
The execution of c program begins here.
User defined function:
User can define his own functions to perform some task in program.
Read more on c program structure.
Steps to Write and get Output of C Program
- Create a c file which contains the code.
- Save the file.
- Compile it using any compilers such as gcc.
- Run the program in terminal.
- Capture output.
C Programming Characteristics
- General purpose programming language.
- Portable: Programs will easily run on any computer with minimal changes.
- Higher Speed.
- Function oriented programming language (procedural programming language)
- C programming language is a powerful general purpose language.
- C programming language is a middle level computer language.
- C programming is simple to learn and run the programs.
Why learn C?
The reasons being to learn C are as follows,
- Many think that C is outdated. This is due to the lack of awareness about C. Even though it is an old language, still it is used in many areas of the modern era. One such example that I can give you is Android. The core libraries of Android are written in C.
- Many of the modern programming languages are influenced by C at the conceptual level.
- The memory management is completely under the control of the programmer.
- Hardware-based programs are written in C, the Device driver software is written in C.
- Even nowadays IOT concepts are written in C.
- If one wants to learn to program, programming logic then it is better to start your journey with C. Because it does not provides many of the built-in features. One has to start from scratch. This will give you the idea to build and learn logic to develop the program.
- C is a portable code. The word “portable” means, it is not tied to any hardware/system.
- C provides good performance. Because the program written and compiled in C executes faster than any other languages as less processing overhead.
- C is a middle-level language. C reduces the gap between low-level and high-level languages. It can be used to write operating system code as well as application-level programming.
- Most of the concepts like computer networks, Operating system, Compiler design,…are based on C.
- C has fewer libraries as compared to other high-level languages.
- It is extensively used in embedded programming. It is used to control the micro-controller.
How to learn C?
- First setup C compiler on your computer.
- Then start to take tutorials available on websites or books available in market.
- Along with reading practice the codes.
- Don’t worry about bugs. Debugging helps to improve your knowledge.
- Practice more and more.
- Take quiz yourself.
People also ask for
Why do we use C in modern era?
Its use in the modern era are as follows,
1.Games
2.Database software
3.Web browsers
4.Development of new languages
5.Enterprise Softwares