How to write C Program in Widows and UNIX/Linux
To write a C program, we first need to write the code . For
this, open a text editor. If you are a
Windows user you may use Notepad and if you prefer working on UNIX/Linux
you can use emacs or vi. Once the text editor is opened is opened on your
screen, type the following statements.
#include<stdio.h>
Int main()
{
Printf(“\n Welcome
to the world of C ”);
return 0;
}
Output
Welcome to the world of C
#include<stdio.h>
This is a pre-processor command that comes as the first
statement in our code. All pre-processor commands start with symbol hash(#).
The #include statement tells the compiler to include the standard input/output
library or header file (stdio.h) in the program. This file has some in-build
functions. So simply by including this file in our code we can use these
functions directly. The standard input/output header file contains functions
for input and output of data like reading values from the keyboard and printing
the result on the screen.
Int main()
int is the return value of the main function. After all the
statements in the program have been written, the last statement of the program
will return an integer value to the operating system. The two curly {} brackets
are used to group all the related statements of the function main. All the statements between the braces from
the function body. The function body contains a
set of instructions to perform the given task.
Printf(“\n Welcome to the world of C”);
The printf function is defined in the stdio.h file and is
used to print text on the screen. The message that has to be displayed on the
screen is encloused within double quotes and put inside brackets.
The message is quoted because in C a text(also known as a
string or a sequence of characters) is always put between inverted commas. The
‘\n’ is an escape sequence and represents a newline character. It is used to
print the message on a new line on the screen. Like the new line character, the
other escape sequence supported by C language.
return 0;
This is a return command that is used that to return the
value 0 to the operating system to give an indication that there were no errors
during the execution of the program.
Now that you have written all the statements using the text
editor, save the text file as first.c. If you are a Windows user then open the
command prompt by clicking start->Run and typing ‘command’ and clicking ok.
Using the command prompt, change the directory in which you had saved your file
and then type:
C:\>tc first.c
In case you are working on UNIX/Linux operating system, then
exit the text editor and type
$cc first .c –ofirst
The –o is for the output file name. If you leave out the –o
then the file name a .out is used. This command is used to compile your C
program. If there are any mistakes in the program then the compiler will tell
you the mistake you have made and on which line you made it. In case of errors
you need to re-open your C file and correct those mistakes. However, if
everything is right then no error(s) will be reported and the compiler will
created an exe file for your program. This .exe file can be file can be
directly run by typing.
‘hello.exe’ for windows and ‘./hello’ for UNIX/Linux
operating system.
When you run the exe file, the output of the programs will
be displayed on screen. That is
Welcome to the world
of C
Comments
Post a Comment