Introduction to C
Introduction
• C was developed in the early 1970s by Dennis Ritchie at Bell Laboratories.
• C was initially developed for writing system software.
• Today, C has become a popular language and various software programs are written using this
language.
• Many other commonly used programming languages such as C++ and Java are also based on C.
Characteristics of C
• A high-level programming language.
• Small size. C has only 32 keywords. This makes it relatively easy to learn.
• Makes extensive use of function calls.
• C is well suited for structured programming.
1
Introduction
• Stable language.
• Quick language.
• Facilitates low level (bitwise) programming.
• Supports pointers to refer computer memory, array, structures and functions.
• C is a core language.
• C is a portable language.
• C is an extensible language.
2
Structure of a C Program
• A C program contains one or more functions.
• The statements in a C program are written in a logical sequence to perform a specific
task.
• Execution of a C program begins at the main() function.
• You can choose any name for the functions. Every program must contain one function
that has its name as main().
3
First C Program
// This is first program in C
#include<stdio.h>
int main()
{
printf("\n Hello world ");
return 0;
}
4
Files used in a C Program
Files in a C program
Source File Header File Object File Executable File
Source code file
• The source code file contains the source code of the program. The file extension of any C
source code file is “.c”. This file contains C source code that defines the main function
and maybe other functions.
• The main() is the starting point of execution when you successfully compile and run the
program.
• A C program in general may include even other source code files (with the file extension
.c).
5
Files used in a C Program
Header Files
• When working with large projects, it is often desirable to make sub-routines and store them in a
different file known as header file. The advantage of header files can be realized when
a) The programmer wants to use the same subroutines in different programs.
b) The programmer wants to change, or add, subroutines, and have those changes
be reflected in all other programs.
• Conventionally, header files names ends with a “.h” extension and its name can use only letters,
digits, dashes, and underscores.
• While some standard header files are available in C, but the programmer may also create his own
user defined header files.
6
Files used in a C Program
Object Files
• Object files are generated by the compiler as a result of processing the source code file.
Object files contain compact binary code of the function definitions. Linker uses this
object file to produce an executable file (.exe file) by combining the of object files
together. Object files have a “.o” extension, although some operating systems including
Windows and MS-DOS have a “.obj” extension for the object file.
Binary Executable File
• The binary executable file is generated by the linker. The linker links the various object
files to produce a binary file that can be directly executed. On Windows operating
system, the executable files have “.exe” extension.
7
Compiling and Executing C Program
• The compilation process is done in two steps.
• In the first step, the preprocessor program reads the source file as text, and
produces another text file as output. Source code lines which begin with
the hash symbol are not written in C but in the preprocessor language.
• The output of the preprocessor is a text file which does not contain any
preprocessor statements. This file is ready to be processed by the compiler.
The linker combines the object file with library routines (supplied with the
compiler) to produce the final executable file.
8
Compiling and Executing C Program
Source File Pre- Compiler
Object
proces
s Files
Library Files Library Linker Executable
Files Files
Source File Pre- Compiler Object
proces Files
s
Library Files
9
Comments in C Program
USING COMMENTS
• It is a good programming practice to place some comments in the code to help the reader
understand the code clearly.
• Comments are just a way of explaining what a program does. It is merely an internal program
documentation.
• The compiler ignores the comments when forming the object file. This means that the
comments are non-executable statements.
C supports two types of commenting:
• // is used to comment a single statement. This is known as a line comment. A line comment can
be placed anywhere on the line, and it does not require to be specifically ended as the end of
the line automatically ends the line.
• /* is used to comment multiple statements. A /* is ended with */ and all statements that lie within
these characters are commented.
10
Keywords in C Program
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
11