0% found this document useful (0 votes)
16 views4 pages

Topic: Programming Basics: Structure of C Program, Syntax, Errors Structure of A C Program

The document outlines the basic structure of a C program, including key components such as preprocessor directives, the main function, and the role of braces and semicolons. It explains the purpose of standard input/output functions like printf and scanf, as well as the types of files used in a C program, such as source, header, object, and executable files. Additionally, it categorizes common errors encountered in programming, including syntax, semantic, logical, runtime, and compile errors.

Uploaded by

mdsahib882
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views4 pages

Topic: Programming Basics: Structure of C Program, Syntax, Errors Structure of A C Program

The document outlines the basic structure of a C program, including key components such as preprocessor directives, the main function, and the role of braces and semicolons. It explains the purpose of standard input/output functions like printf and scanf, as well as the types of files used in a C program, such as source, header, object, and executable files. Additionally, it categorizes common errors encountered in programming, including syntax, semantic, logical, runtime, and compile errors.

Uploaded by

mdsahib882
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Topic: Programming Basics: Structure of C program, Syntax , errors

STRUCTURE OF A C PROGRAM
The structure of a C program is a protocol (rules) to the programmer, which he has to follow while
writing a C program. The general basic structure of C program is shown in the figure below.

Based on this structure, we can sketch a C program.


Example:
/* This program accepts a number & displays it to the user*/
#include <stdio.h>
void main(void)
{
int number;
printf( "Please enter a number: " );
scanf( "%d", &number );
printf( "You entered %d", number );
return 0;
}
Stepwise explanation:
#include
The part of the compiler which actually gets your program from the source file is called the
preprocessor.
#include <stdio.h>
#include is a pre-processor directive. It is not really part of our program, but instead it is an instruction
to the compiler to make it do something. It tells the C compiler to include the contents of a file (in
this case the system file called stdio.h).
The compiler knows it is a system file, and therefore must be looked for in a special place, by the fact
that the filename is enclosed in <> characters
<stdio.h>
stdio.h is the name of the standard library definition file for all Standard Input and Output functions.
Your program will almost certainly want to send information to the screen and read things from the
keyboard, and stdio.h is the name of the file in which the functions that we want to use are defined.
The function we want to use is called printf. The actual code of printf will be tied in later by the linker.
The ".h" portion of the filename is the language extension, which denotes an include file.
void
This literally means that this means nothing. In this case, it is referring to the function whose name
follows.Void tells to C compiler that a given entity has no meaning, and produces no error.
Main In this particular example, the only function in the program is called main. A C program is
typically made up of large number of functions. Each of these is given a name by the programmer
and they refer to each other as the program runs.C regards the name main as a special case and will
run this function first i.e. the program execution starts from main. A
parameter to a function gives the function something to work on.
{ (Brace)
This is a brace (or curly bracket). As the name implies, braces come in packs
of two - for every open brace there must be a matching close one. Braces
allow us to group pieces of program together, often called a block. A
block can contain the declaration of variable used within it, followed by a
sequence of program statements. In this case the braces enclose the
working parts of the function main.
;( semicolon)
The semicolon marks the end of the list of variable names, and also the end of
that declaration statement.
All statements in C programs are separated by ";" (semicolon) characters. The
";" character is actually very important. It tells the compiler where a given
statement ends. If the compiler does not find one of these characters
where it expects to see one, then it will produce an error.
scanf
In other programming languages, the printing and reading functions are a part
of the language. In C this is not the case; instead they are defined as
standard functions which are part of the language specification, but are
not a part of the language itself. The standard input/output library contains
a number of functions for formatted data transfer; the two we are going to
use are scanf (scan formatted) and printf (print formatted).
printf
The printf function is the opposite of scanf. It takes text and values from
within the program and sends it out onto the screen. Just like scanf, it is
common to all versions of C and just like scanf, it is described in the
system file stdio.h.The first parameter to a printf is the format string,
which contains text, value descriptions and formatting instructions.

FILES USED IN A C PROGRAM


Source File- This file contains the source code of the program. The file extension of any c
file is .c.
The file contains C source code that defines the main function & maybe other functions.
Header File- A header file is a file with extension .h which contains the C
function declarations and macro definitions and to be shared between
several source files.
Object File- An object file is a file containing object code, with an extension
.o, meaning relocatable format machine code that is usually not directly
executable. Object files are produced by an assembler, compiler, or other
language translator, and used as input to the linker, which in turn typically
generates an executable or library by combining parts of object files.
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.
Types of error
a) Syntax Errors: Errors in syntax (grammar) of the program.
b) Semantic Errors: Errors in the meaning of the program.
c) Logical Errors: Errors in logic of the program. Compiler cannot
diagnose these kinds of errors.
d) Runtime Errors: i) Insufficient memory ii)Floating exception
e) Compile Errors: i) parse error ii)implicit declaration iii) no
matching function iv)Unsatisfied symbols v)incomplete type
vi)cannot call member function vii)bad argument viii)cannot
allocate an object

You might also like