A program to display “Hello Sweety!” as the output. All the explanations are in the form of comments. And everything between /* and */ is a comment and everything else is a part of the code. To prevent confusion, the program is first written without the comments.
The Program (without comments/explanation) :
#include <stdio.h>
#include <conio.h>int main()
{
printf(“Hello Sweety!“);
getch();
return 0;
}
The Program (with comments for explanation) :
#include <stdio.h> /* a header file which has the function
printf used to display text as output */#include <conio.h> /* another header file that has the function
used below */int main() /* main – the main function,
int – function returns an integer type,
() – for arguments, no arguments in this case */{ /* main function begins */
printf(“Hello Sweety!“); /* shows Hello Sweety! on the screen */
getch(); /* waits for a character before closing program */
return 0; /* since main function should return integer */} /*main function ends */
It may not look like it but this is one of the most important programs you will ever learn. The syntax used in the program can be generalized as following :
#include <filename.h> is the general syntax you’ll use to include any header file. You’ll need to include many header files as you deal with more complex problems. stdio.h contains the printf function that we’ve used to display output.
return-type function-name (arguments) {…} is used to define a function. In this case the function is called main and the return type is integer with no arguments.
printf(“…”); a function that shows whatever is inside the double inverts on the output. This is your first C statement. All C statements end with a semi-colon (;)
getch(); this function waits for a character input from the user and then exits the program. Try running the code without it and with it and you’ll know what i mean. This function is actually part of a header file conio.h but will work without it on most compilers – but no harm adding the conio.h header
return value; Since the main function must return an integer value, we return 0. However, your program will run without it, but you should make it a practice since you’ll need it in the long run.
By the way, All the code we write goes in a new source file on your compiler. (File > New > Source File) Once you type in the above code, you need to compiler & run your program. It should give the following output :
[Note – we added the header file conio.h for getch();]