Basic Parts Of C Program
If you don’t read my previous posts you can read them and follow the instructions. However today we are going to write our 1st c program also we will try to understand each part of this program. Besides, we will try to learn how to use codeblocks for coding. So, after reading this post you will be able to learn the following terms:
C Language Basics:
The C programming language is a low-level, interpreted language that allows for easy creation of executables and scripts. It uses fundamental data types such as int, double, char*, and structs to represent information in programs. Additionally, the C programming language provides various built-in functions that allow for concise control over input/output operations as well as complex mathematical calculations.
Structure Of C Program With Each Parts Of It Explanation:
To understand the structure of the C program, first, open codeblocks. Then press cntrl+shift+n to open a new blank file. Then press cntr+s to save that file on your desktop. Chose the folder where you want to store your code. Then give it a name and put .c as an extension.
Then save it. Then write the following code:
#include <stdio.h>
int main ()
{
printf(“Happy coding”);
return 0;
}
Just write the code like that and then run it by pressing F9. If you press F9 then this code will compile and run directly. Or you can compile this code first from the menu bar then run it. The output will be:
It gives the output of Happy coding. Process returned 0 (0*0) means nothing. Just don’t think about it now. execution time 0.944 s means. To give this output the system takes 0.944 s. This thing is very much important for a coder’s life. You will find it when you will enter a deeper level. However, in the last line, it was written Press any key to continue. This means if you press any of the keys on your keyboard this pad will disappear. To run again you can either press F9 or run it from the menu bar.
Now, look at the code. At the top we write #include<stdio.h> . stdio.h is a header file. Header files are written with .h extension. We will familiar with other header files later. stdio means standard input-output. And this whole thing means including a standard input-output file in the program. By declaring this we can use a built-in input-output function in our program. Then int main(). This is the main function. Whatever you do it should be into the main function. Every c program contains a must and only 1 main function. Whatever you want to do it should be between the { and } range. {} is called the scope of the main function. Anything was written between these second brackets they are stored in the main function. However, just keep in mind that to write a c program you must declare one main function and your code must be between the main function.
Then printf(“Happy coding”); printf() is a standard output function. It is a built-in function which already built in the stdio.h. So, every time before writing any c program we will declare header files on the top then the main function, and then our code between the main functions { and }. However, anything written between ” ” on the printf() function will be printed as an output. You are allowed to use any number of printf() functions in a c program. Then return 0. You don’t have to think about it now. Just try to use this in every c program.
You can see that I have used; after printf() and return 0 as they are statements. Because in the c program you must use a semicolon after each statement. Because in time of compilation the compiler compiles the program one by one statement. If you don’t put any semicolon after any statement it will show an error on that line while compiling the code and will stop compiling the rest of the code.
Now, I want to share one thing with you. That is you can see I have used some space after each line in the code. This is called Indentation. When you are started any scope resolution means any statements into the second brackets try to use 4 spaces after the { line. This is a good practice for the programmer. You can press Tab on your keyboard to make 4 spaces in one press. So, try to maintain indentation during coding. But if you can’t then after writing the whole code just press the right button on your mouse then press Format use AStyle. Then whole the code will automatically align in standard format maintaining indentation.
Now if you want to increase the text size of your code. Just hold by pressing cntrl on your keyboard then scroll the mouse up and down. Up means increasing the size of text and down means decreasing the size of the text.
So, that’s it for today I hope you understand the whole thing. But if you don’t then comment below I will try to answer. Now, I am giving you one task.
Write a c program that will print Stay home, stay safe. Write the code and comment below. Don’t see others’ code from comments. Try to maintain indentation too.