Programming in C
Programming in C
convenient to use and giving the programmer more III Linker: Usually in structural programming we
time to address a client problem. But they do not use modular approach. In this approach, a program
provide the flexibility available in low level is divided into various modules. When a module is
languages. Some high-level languages include compiled, an object file of that module is generated.
BASIC, FORTRAN, PASCAL, COBOL, C, C++, JAVA A linker is an important utility program that takes
and soon. the object files of different modules, produced by the
assembler and compiler, and other code to join them
High level languages are not directly understood by into a single executable file. Usually, compiler
computers so we have required translator which automatically invokes the inker as the last step in
converts high level languages into machine compiling a program.
languages. There are following types of translators:
IV Loader: A loader is a special type of program that
I. Compiler: A compiler is a special type of program copies programs from a storage device to the main
that transform the source code written in high level memory, where they can be executed. The
languages into machine languages. The resultant functionality and complexity of most loaders are
code in 0’s and 1’s is called object code. Object code hidden form the users.
is used to create an executable program.
We can easily understand the compilation of a
A compiler is more intelligent than an assembler it source code in following diagram:
checks all kinds of limits, ranges, errors, etc. But its
program run time is more and occupies a larger part
of memory. It has a slow speed because a compiler
goes through the entire program and then translates
the entire program into machine codes.
are called Semantic Errors. A semantic error will be that take input from input devices and display
generated if the code makes no sense to the output on output devices are indicated with
compiler, even though it is syntactically correct. It is parallelogram in a flowchart.
like using the wrong word in the wrong place in the
English language. For example, adding a string to an
integer will generate a semantic error. Semantic
errors are different from syntax errors, as syntax
errors signify that the structure of a program is
incorrect without considering its meaning. On the
other hand, semantic errors signify the incorrect 3. Processing: A box represents arithmetic
implementation of a program by considering the instructions. All arithmetic processes such as
meaning of the program. adding, subtracting, multiplication and division are
indicated by action or process symbol.
The most commonly occurring semantic errors are:
use of un-initialized variables, type compatibility,
and array index out of bounds.
▪ Easy to understand.
▪ If changes are done in software, then the Computers are used extensively for performing
various types of analysis. The decision symbol is
flowchart must be redrawn.
used in flowcharts to indicate it. The general format
1.7 TECHNIQUES OF FLOW CHART of steps for flowcharting is as follows:
We can draw flow chart using following techniques: a) Perform the test of the condition.
b) If condition evaluates true branch to Yes steps.
1. Flowcharts for computations c) If condition evaluates false branch to No steps.
2. Flowcharts for decision making In decision making there are following types of flow
chart is defined
3. Flowcharts for loops
a) If-else condition:
1. Flowcharts for computations (Sequential flow)
b) Nested if condition
a) initialising a counter
b) execution of operations
c) testing the completion of operations
The basic structure of a C program is divided into 6 A comment which does not fit in one line. It presents
parts which makes it easy to read, modify, in multiple lines, then it is called multi line
document, and understand in a particular format. comment. it is also called as block comment. It
always enclosed with in /* ………………… */
Example:
/*This is example to
It won’t execute*/
2. Preprocessor Section
3. Definition #include<stdio.h>
are performed inside the curly braces of the main 3. Object files
program.
Object files are generated by compiler as a result of
• It is first executable statement. processing the source code file. Object files contain
• It is entry point. compact binary code of the function definitions.
• It is mandatory. Object files have a ‘.o’ or ‘.obj’ extension.
• It cannot be duplicated.
4. Executable file
• It is user defined function.
The executable file is generated by linker. The linker
We can write main() function in following ways
links the various object files to produce a binary file
i. void main() {} that can be directly executed. In windows executable
files have ‘.exe’ extension.
ii. void main(void){}
2.5 TOKENS IN C
iii. int main(){}
C language has some basic elements and
iv. int main(void){} grammatical rules. These basic elements are:
1. Character set,
6. Sub Programs 2. Variables
3. Data types,
User-defined functions are called in this section of 4. Constants,
the program. The control of the program is shifted to 5. keywords,
the called function whenever they are called from the 6. variable declaration etc.
main or outside the main() function. These are
1. C Character Set
specified as per the requirements of the
The characters that are used in C programs are
programmer.
given below
Example:
1.1 Alphabets
void fun(){} A, B, C …………………Z
a, b, c ………………….z
1.2 Digits
2.4 FILES USED IN C
0,1, 2, 3, 4, 5, 6, 7, 8, 9
Every C program has four types of files which are
1. Source file 1.3 Special characters
2. Header file
3. Object file All special characters are allowed.
4. Executable file
2. Escape Sequences
1. Source file Escape sequence is used for control the position of
cursor on output screen. C supports the
It contains the source code of the program. The file
combination of backslash (/) and some characters
extension of C source code is ‘.c’,
from the C character set to print these characters.
These character combinations are known as escape
2. Header file
sequences and are represented by two characters.
When working with large projects, it is often Escape Meaning Purpose
desirable to separate out certain sub routines from Sequence
the main() of the program. So, make subroutines \b backspace Moves the cursor to the
and store them in a different file called header file. If previous position of the
we miss the required header file in the C program current line
then compiler will generate an error. Some of the
\a bell (alter) Produces a beep sound
standard header files are:
for alert
unsigned for
\f from feed Moves the cursor to the sizeof while
initial position of the
next logical page. const do
volatile default
\0 null Used for terminate the
string.
\’ Single quotes Single quotation (1) The name should consist of only alphabets (a-z
and A-Z), digits (0-9) and underscored sign (_)
\” Double Double quotation
(2) Identifier name should be unique.
quotes
(3) First character should be an alphabet or
\? Question Present question mark underscore.
mark
(4) The name should not be a reserved keyword.
Blank, horizontal tab, vertical tab, newline, carriage (4) Identifiers are case sensitive i.e.; uppercase and
return, from feed are known as whitespace in C lowercase letters are considered different.
language. (5) White spaces are not allowed.
There are certain words that are reserved for doing 1. Sign qualifiers: signed, unsigned
specific tasks. These words are known as keywords 2. Size qualifiers: short, long
and they have standard, predefined meaning in C.
They are always written in lowercase. There are 1. Sign qualifiers: When the qualifier unsigned is
only 32 keywords available in C which are given used the number is always positive, and when
below: signed is used number may be positive or negative.
Variable Control Storage User Function If the sign qualifier is not mentioned, then by
related Statement classes defined related default signed qualifier is assumed. The range of
related related data values for signed data types is less than that of
types
unsigned type. This is because is signed type, the
char if auto struct return leftmost bit is used to represent the sign, while in
int else extern union void unsigned type this bit is also used to represent the
value.
float switch register typedef
double case Static enum 2. Size quantifiers: The size and range of different
data types on a 16-bit machine is given in the
long break
following table. The size and range may vary on
short continue machines with different word sizes.
signed goto
Example:
6453----->int
67238736UL or 67238736ul -->unsigned long
int
7655U or 7655u -->unsigned int
Datatype variable name; Formatted I/O functions are used to take various
inputs from the user and display multiple outputs
Here datatype may be char, int, float or double. to the user. These types of I/O functions can help
Example: int x; to display the output to the user in different
We can also declare more than one variable in a formats using the format specifiers. These functions
single declaration. are called formatted I/O functions because we can
use format specifiers in these functions and hence,
Example: int x, y, z; we can format these functions according to our
8.2 Initialization of Variables needs.
When a variable is declared it contains undefined These I/O supports all data types like int, float,
value commonly known as garbage value. If we char, and many more. We can use scanf() function
want, we can assign some initial value to the which takes input data from keyboard and printf()
variable during the declaration itself, this is called function which produce output. Both functions are
initialization of the variable. included in the header file stdio.h.
(iii) When input data length is more than the given Here ‘w’ is the integer number specifying the total
field width, then the input values are altered width of the input data (including the digits before
and stored in the variable as – and after decimal and the decimal itself). For
example:
Input-
123 1234 scanf(“%3f %4f “, &x, &y);
Result- (i) When input data length is less than the given
width, values are unaltered and stored in the
12 is stored in a and 3 is stored in b and the
variables.
rest of input is ignored.
Input
1.4.2 Format For Integer Output
2 2.3
% wd
Result
Here w is the integer number specifying the
2.0 is stored in x and 2.30 is stored in y.
minimum field width of the output data. If the
length of the variable is less than the specified field (ii) When input data length is equal to the given
width, then the variable is right justified with width, then the given values are unaltered and
leading blanks. stored in the given variables.
Result
The width specifier of first data is 3 while there are
2.3 is stored in x and 6.00 is stored in y.
only 2 digits in it, so there is one leading blank. The
width specifier of second data is 4 while there is
1.4.4 Format For Floating Point Numeric
only 1 digit, so there are 3 leading blanks.
Output
(ii) When the length of the variable is equal to the % w.nf
width specifier
Here w is the integer number specifying the total
Value of variables - width of the input data and n is the number of
123 4567 digits to be printed after decimal point. For example
Output : x = 3 . 0 , y = 3 . 6 0
a = 1 2 3 4 , b = 4 5 6 7 8
Value of variables
10.5 2345.67
Output :
x = 1 0 . 5 , y = 2 3 4 5 . 6 7
Value of variables
12.345 12.345678
Output :
x = 1 2 . 3 , y = 1 2 . 3 4
If the input is
fluxeducare
f l u x e d u c a r e
f l u x
f l u x
f l u x
2. UNFORMATTED I/O FUNCTIONS characters is typed by the user and this function
reads one character at one time until and unless the
Unformatted I/O functions are used only for
enter key is pressed. This function is declared in
character data type or string and cannot be used
stdio.h.
for any other datatype. These functions are used to
read single input from the user at the console and Syntax:
it allows to display the value at the console.
Variable-name = getchar();
These functions are called unformatted I/O
#include <conio.h>
functions because we cannot use format specifiers #include <stdio.h>
in these functions and hence, cannot format these void main()
functions according to our needs. {
char ch;
There are following types of unformatted I/O printf("Enter the character: ");
functions: ch = getchar();
printf("%c", ch);
2.1 getch()
}
This function reads a single character from the
keyboard by the user but doesn’t display that 2.4 putchar()
character on the console screen and immediately
returned without pressing enter key. This function is The putchar() function is used to display a single
declared in conio.h. It is also used for hold the character at a time by passing that character
screen. directly to it or by passing a variable that has
already stored a character. This function is declared
Syntax: in stdio.h.
getch(); or variable-name = getch(); Syntax:
#include <conio.h> putchar(variable_name);
#include <stdio.h> #include <conio.h>
#include <stdio.h>
void main()
void main()
{ {
char ch;
printf("Enter any character: "); printf("Enter any character: ");
ch = getchar();
// Reads a character but not displays putchar(ch);
getch(); }
}
2.5 gets()
2.2 getche()
gets() function reads a group of characters or strings
getche() function reads a single character from the from the keyboard by the user and these characters
keyboard by the user and displays it on the console get stored in a character array. This function allows
screen and immediately returns without pressing the us to write space-separated texts or strings. This
enter key. This function is declared in conio.h. function is declared in stdio.h.
Syntax: Syntax:
getche(); or gets(string_name);
Syntax:
functions. some examples of
puts(string_name ); these functions.
#include <stdio.h>
void main()
{
char name[50];
printf("Enter your name: ");
gets(name);
printf("Your name is: ");
puts(name);
}
2.8 putch()
Syntax:
putch(variable_name);
#include <conio.h>
#include <stdio.h>
void main()
{
char ch;
printf("Enter any character:\n ");
ch = getch();
printf("\nEntered character is: ");
putch(ch);
}
These functions do
These functions allow
not allow to take
us to take input or
1 input or display
display output in the
output in user
user’s desired format.
desired format.
These functions do
These functions support
2 not support format
format specifiers.
specifiers.