You have learned from our previous posts about why should you learn C
and have got an introduction to C, its character set, constants,
variables, and keywords, then you have learned about syntax and comments
and have also made a program in C.
And now it is time to learn about the most important functions in any
programmer's journey with the C language.
If you haven't, I strongly suggest checking out the following links:
1.) https://collegeintegral.blogspot.com/2022/06/introduction-to-c-why-should-you-learn.html
2.)https://collegeintegral.blogspot.com/2022/06/introduction-to-c-constants-variables.html
3.)https://collegeintegral.blogspot.com/2022/06/c-your-first-program-syntax-and-comments.html
With that out of the way let's get started with the main()
As customary let's create a College Integral code and run it.
With that out of the way let's get started with the main()
As customary let's create a College Integral code and run it.
#include <stdio.h>
int main()
{
printf("Hello College Integral, it is about time we learn about main()");
return 0;
}
we see the main function.
main or in this case main() is a function that is required in any C program. It cannot have a different name, it must always be main().
"main(){}" all the statements come inside the curly braces {}.
int main()
{
statement 1;
command 2;
instruction 3;
}
The main function has int written before it and that is because the main function always returns an integer value.
In general, you will see a statement return 0; inside the main() function as in this case it means the success of the code or program that was being run and in the case that the function doesn't work as desired we can always return a non zero number through return.
That will indicate that the program didn't work as intended or the program was a failure.
NOTE: Any variable must be declared before it can be used.
To take input is to use the scanf() function and to take output is to use the printf() function.
Like other programming languages, C does not have an inbuilt function to print anything on the screen (or show output on the screen), thus we use readymade libraries and their functions.
"#include <stdio.h>", stdio.h library is the library that has the printf() function.
NOTE: #include is what we would call a preprocessor directive and we shall discuss more about what this is and what it does but for now let's just understand one thing and that is the # include part is used to include any external library.
printf("<format string representing the type of value>", <list of variables in the same order as their format string>);
Above is the syntax generalized for the reader to understand the working of printf()
This format string is of the form %(character), the character inside the () being the marker of the type of values to be printed.
Some of these are as follows:
1.) %f: this is used to print real values
2.) %d: this is used to print integer values
3.) %c: this is used to print character values
This same format is used for scanf %(a) where a is the format specifier.
scanf("<format string representing the type of value>", &<list of variables in the same order as their format string>);
NOTE: scanf and printf take input and print output from and to the console screen.
#include <stdio.h>
int main()
{
int example1, example2;
scanf("%d%d",&example1,&example2);
printf("%d\t%d",example1,example2);
return 0;
}
scanf and printf are shown with a basic example, we have two integer values with the variable names example1 & example2 respectively, these are scanned using scanf and printed on the console using printf.
NOTE: This post aims to provide a brief overview and this is not an exhaustive detailing of these functions.