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.
WRITING
THE FIRST C PROGRAM
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 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
This
is a preprocessor command that comes as the first statement in our code. All
preprocessor 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-built functions. By simply 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 results on the screen.
Every
C program contains a main() function
which is the starting point of the program. 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 concepts will be
clear to us when we read the chapter on Functions. So even if you do not
understand certain things, do not worry.
Programming Tip:
If you do not place a parenthesis after 'main', a compiler error will be
generated.
{}
The two curly brackets are used to group all the related statements of the main function. All the statements
between the braces form the function body. The function body contains a set of
instructions to perform the given task.
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 enclosed
within double quotes and put inside brackets.
Programming Tip:
Placing
a semi- colon after the parenthesis of main function will generate a compiler
error.
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. '\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 newline character, the other escape sequences supported by C language are
shown in Table 2.1.
Note
Escape
sequences are actually non-printing control characters that begin with a
backslash (\).
This
is a return command that is used to return the value 0 to the operating system
to give an indication that there were no errors during the execution of the
program.
Note
Every
statement in the main function ends with a semi- colon (;).
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 to
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 create an
. exe file for your program. This
.exe 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 program will be displayed on screen.
That is,
Welcome to the world of C
Note
The
printf and return statements have been indented or moved away from the left
side. This is done to make the code more readable.
Programming in C: Unit I (b): Introduction to C : Tag: : - Writing the First C Program
Programming in C
CS3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation