A C program source code can be written in any text editor such as, notepad in windows, text edit on a Mac, and gedit in Linux. However the file should be save with extension .c (For Example: Hello.c)
Before starting the abcd of C language, you need to learn how to write, compile and run the first c program. Let's see how to write the first program in c.
To write the first C program, open the C console and write the following code:
/* This is my first program in C */
#include <stdio.h>
int main(){
printf("Hello C Language!");
printf("Learn free online IT tutorials at welcome2protec.com!");
return 0;
}
Output:
Hello C Language!
Learn free online IT tutorials at welcome2protec.com!
Now let's understand this program:
Comment:
Comment start with ’/*’ and end with ‘*/’. Comments are not mandatory but still it’s a good practice if you use them, it improves the readability of the code. A program can have any number of comments.
#include <stdio.h>
It is a header file stands for standard input output <stdio.h>. It contains the declaration related to input / output functions. The C programming language provides many standard library functions so that your program can use it such as printf(), scanf() etc. Since these functions declared in <stdio.h> header file. Therefore, to use these functions you need to include this header file.
main function():
Every C program has at least one function which is main(), main() function is the entry point of any C program. It is the point at which execution of program is started.
Return type of main() function:
The return type of main() function should always be either int or void. Return type depend on the return value if return value is integer type then return type will be int, and if it does not return a value then return type will be void.
Why it has a return type and what's the need of it ?
The compiler should know whether your program compiled successfully or it has failed. In order to know this it checks the return value of function main() If return value is 0 then it means that the program is successful otherwise it assumes that there is a problem, this is why we have a return 0 statement at the end of the main function.
Structure of main function:
Function name is followed by return type. There should be close parenthesis after function name. If there are parameters or arguments then it must be within this parenthesis. The block of code inside curly braces is function body.
printf():
The printf() function is used to print a message on the console (means output screen). It is a library function it's declaration is stored in <stdio.h> header file.
How to compile and run C program?
On Turbo C++ You can compile and run the c program by using flowing shortcut keys:
Press Alt + f9 to compile the program and press Ctrl + f9 to run the program.
Note: You can view the user screen any time by pressing the Alt +f5 key.
return 0:
The return 0 statement means, returns execution status to the OS. The 0 value is used for successful execution and 1 for unsuccessful execution. If you specified return type as void, then no need to write return 0 at the end of the program.
The result of a function is called its return value and the data type of the return value is called return type or result type.
0 Comments:
Post a Comment
Please don't enter any spam link in the comment box.