This document provides an introduction to writing and running a basic C program. It explains that C programs must be compiled and linked to produce an executable file. It then gives an example "Hello World" C program and discusses some key elements like header files, the main function, and using printf to output text. It also explains the compilation and linking process needed to translate the C source code into a binary executable file that can be run on a computer.
1. Lab 2 - Getting Started with C
1. Objectives
The objective of this lab is to make student understand their first C programs, its environment
how to compile it and Operator precedence.
2. Outcome
I. At the end of this lab student will know how to write a C program.
II. Student will understand the process of compiling and getting output.
III. Student will understand various C Operators.
3. Introduction
3.1 First C Program
C is a high-level programming language and that you need a C compiler to translate your C
programs into binary code that your computer can understand and execute. In this lab you'll
learn to write your first C program and the basics of a C program, such as
The #include directive
Header files
Comments
The main() function
The return statement
The exit() function
The newline character (n)
The void data type
Translating a C program into an executable file
Debugging
Example:
This is a very simple C program, which is saved in a file called Test.c. Note that the
name of a C program file must have an extension of .c.
In C, #include forms a preprocessor directive that tells the C preprocessor to look for a
file and place the contents of the file in the location where the #include directive
indicates.
/* Test.c: This is my first C program */
#include <stdio.h>
void main()
{
printf ("Howdy! This is my first C
program.");
}
2. The ``main'' function establishes the overall logic of the code. All C codes must have a
``main'' function.
Our Test.c code calls printf, an output function from the I/O (input/output) library
(defined in the file stdio.h). The printf line prints the message Howdy! This is my first
C program ' on ``stdout'' (the output stream )
``new line'' character, which brings the cursor onto the next line.
All functions in C can return values. The main () function itself returns a value. By
default, main () returns an integer. Return 0; that indicates that 0 is returned from the
main () function and the program is terminated normally.
3.2 Compilation and Linking:
First, a program written in C, called source code, is made. Then the source code is
compiled by a C compiler, which creates a new file. The new file is an object file.
You cannot execute the object file because there is some function code missing. You
have to finish the next step: linking. Linking is done by invoking a special program
called a linker, which normally comes with the compiler package.
A linker is used to link together the object file, the ANSI standard C library, and other
user-generated libraries to produce an executable file—the binary code. In this stage,
the binary code of the library functions that are called in the source code is combined
with the object file; the result is saved into a new file—an executable file.
Example:
#include <stdio.h>
void main( )
{
int p, n ;
float r, si ;
p = 1000 ;
n = 3 ;
r = 8.5 ;
/* formula for simple interest*/
si = p * n * r / 100 ;
printf ( "%f" , si ) ;
}