Hello Friends, This Is A Tutorial For The Language C'.
Hello Friends, This Is A Tutorial For The Language C'.
Hello Friends, This Is A Tutorial For The Language C'.
HARDWARE
Hardware again is divided into three distinct parts.
1. Input Devices.
2. Output Devices
3. CPU
Input Devices : Input devices are those devices that are used
to send any instruction or command to the CPU. Example –
Keyboard, Mouse, Joystick, Lightpen, Scanner, Thumbwheel,
BCR ( Bar Code Reader) etc.
RAM :
ROM :
HIERARCHY OF COMPUTER
Primary
Secondary
Cache
CP
Memory
Memory
(RAM)
U
Cache Memory : Cache memory is a small memory that
contains the recently processed data in it. It is very fast due to
low memory space. It is used to boost up the performance of
the computer.
Fetching of Data :
Whenever there is a need of having some data, CPU generates
a request to both cache memory and RAM. If data is found in
cache memory, it is taken from that else RAM is checked. If the
required data resides in RAM, it is fetched else the request of
q33333333333333q333333333333333335the data is sent from
RAM to secondary memory like Hard disk and the data id
fetched from it to RAM and then it is sent to the CPU.
Introduction to ‘C’
1. Basic C programming
2. Conditional statements
a. If statement
b. Switch Statement
3. Jump statements
a. Go to statement
b. Break statement
c. Continue statement
4. Looping
a. While loop
b. Do...While loop
c. For loop
5. Pointers
6. Arrays
a. Single dimensional array
b. Multi dimensional array
c. Pointer to array
7. Strings
a. In form of array of characters
b. In form of pointers
8. Functions
a. Call by Value
i.Without return, No parameters
ii.Without return, with Parameters
iii.With return, No parameters
iv.With return, with parameters
b. Call by Reference
c. Recursion
d. Array in functions
9. Structure / Union
a. Simple structures
b. Array of structures
c. Pointer to structures
There are two type of screens we can use; the system program
screen and a user screen. The system program screen contains
all of the programs which are needed to be combined to form a
complete program and a user screen will have the output
/outcome result from that program. When we write a program
in any language, it is needed to be processed by someone so
that a result can be obtained. The compiler is the software
which processes the program written in C. Here I am saying it
in C only. Interpreter is also a software which performs the
same operation as of compiler. Every language have its own
compiler/interpreter. Several languages like java contains both
of them. C contains the compiler only and named as Turbo.
The difference in both is as:
Suppose we have a program of four lines, the compiler reads
whole program at once and compiles it as a whole however an
Interpreter reads the program line by line. So the whole process
can be shown by the following diagram.
Output
Source
of the
Compiled by Compiler
Program
program.
in C
Output/User
language.
Screen
Program
Screen
Memory :
A 65 01000001 a 97 0110000
1
B 66 01000010 b 98 0110001
0
C 67 01000011 c 99 0110001
1
D 68 01000100 d 100 0110010
0
E 69 01000101 e 101 0110010
1
F 70 01000110 f 102 0110011
0
G 71 01000111 g 103 0110011
1
H 72 01001000 h 104 0110100
0
I 73 01001001 i 105 0110100
1
J 74 01001010 j 106 0110101
0
K 75 01001011 k 107 0110101
1
L 76 01001100 l 108 0110110
0
M 77 01001101 m 109 0110110
1
N 78 01001110 n 110 0110111
0
O 79 01001111 o 111 0110111
1
P 80 01010000 p 112 0111000
0
Q 81 01010001 q 113 0111000
1
R 82 01010010 r 114 0111001
0
S 83 01010011 s 115 0111001
1
T 84 01010100 t 116 0111010
0
U 85 01010101 u 117 0111010
1
V 86 01010110 v 118 0111011
0
W 87 01010111 w 119 0111011
1
X 88 01011000 x 120 0111100
0
Y 89 01011001 y 121 0111100
1
Z 90 01011010 z 122 0111101
0
0 48 00110000 1 49 0011000
1
2 50 00110010 3 51 0011001
1
4 52 00110100 5 53 0011010
1
6 54 00110110 7 55 0011011
1
8 56 00111000 9 57 0011100
1
NOTE : All of the binary codes are in 8 digits. It is true for all of
the letters of English.
Computer Languages :
Compiler/Interp
Assembl
Assembl
Machine
4GL
HLL
er
reter
Languag
y
Languag
e
e
#include<stdio.h>
void main()
{
printf(“Hello, welcome to C”);
}
#
Its a pre-processor used for including header files or defining.
Include
it is used to call the header files into the program. As all of the
functions of C are defined in header files. Example : printf is a
function defined in stdio.h file. The operation that must be
performed by calling printf is described in stdio.h file. So if we
want to use printf in the program, stdio.h must be a part of the
program. Hence it is included in the program by include
statement.
<>
These works like a bracket to keep the complete name for the
header file.
stdio
this is a header file in the library of C and contains many
functions like printf , scanf, getc, putc, gets, puts etc.
.h
Indicates that it is a header file.
void
void means for blank. This indicates that main will not return
anything. This will be described in functions chapter.
main
main is the main function. Whenever a compiler compiles the
program, only main function is compiled. A program without
main function is always incomplete. It can not be compiled.
()
These brackets contains the parameters that are passed to the
functions. Even this will be described in functions topic.
{}
These brackets forms a block indicating that all of the things
written between them are part of the statement just above {. In
this program, printf statement is in the block of main function.
printf
printf is the statement which shows some output on the user
screen. The things written within double quotes(“”) are printed
as it is on the user screen except some cases.
;
Semicolon (;) indicates the end of the line. It is also called as
termination symbol.
GETTING OUTPUT :
To get the output of the program, we will first compile it by
pressing key F9. Then compiler will compile the program. If the
program is correct. Success will be shown else error will be
displayed.
Now, we will run the program by pressing Ctrl+F9. Now, the
program is finished running. If we want to see the output
screen, we will press Alt+F5.
Step 1: Compile F9
Step 2: Run Ctrl + F9
Step 3: See output Screen (If required) Alt + F5
NOTE :
Instead of writing void main() we can also write
PROGRAM 2 :
OUTPUT:
#include<stdio.h>
void main()
Hello,
{
Welcome
printf(“\nHello, \n\nwelcome \t\tto C”); to C
}
There is a very little difference in the previous program and this
one. There is a \n and \t in printf statement. It means for the
next line and tab space respectively. They both are used within
double quotes only. When we use \n, the cursor goes to the
next line and \t gives tab space. We can use them any number
of times according to our need and format of writing. They help
us to adjust the things in the display.
#include<stdio.h>
void main()
{
printf(“\n\nName\t\t:\tHimanshu Verma”);
printf(“\nCourse\t\t:\tB.Tech.\n”);
printf(“Branch\t\t:\tComputer Science”);
}
#include<conio.h>
This line will be below inclusion of stdio.h. getch() is the
function that holds the output screen till we press any key on
the keyboard. This stands for “get character”. The main
purpose of getch well be discussed later but here we will use it
to hold the screen. The benefit of it will be – we will not need to
press alt+F5 to see the screen after running the program.
When we will run the program, the screen will be holding for
any key from keyboard. We will use getch at the end of the
main function.
NOTE : This is not an hard and fast rule to use the clrscr at first
and getch at the last of the program. We can use them in
between also as our convenience.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr()
printf(“\nHello, Welcome to C.”);
getch()
}
Now, when we will run the program, it will hold for any key.
Also, the screen will be cleared at every compilation.
VARIABLES / CONSTANTS :
DATA TYPES :
There are many different data types in C. Data type means for
any variable that can store anything in it. A variable may
contain any type of value – it may be an integer type, it can
store values in decimal form i.e. floating type or a character.
Depending upon the nature of the variable, we uses them in
our programs. Generally, there are three basic data types
namely :
There are many other data types also like long, double etc. but
the basic data types are three only.
unsigned 1 0 to 255
char
char 1 -128 to +127
unsigned 2 0 to 65,535
int
short int 2 -32,768 to +32,767
int 2 -32,768 to +32,767
unsigned 4 0 to 4,294,967,295
long
long 4 -2,147,483,638 to +2,147,483,637
float 4 3.4 * 10-38 to 3.4* 1038
double 8 1.7* 10-308 to 1.7* 10308
long double 10 3.4* 10-4932 to 3.4* 104932
a=10;
b=20;
c=a+b;
printf(“\nThe sum of %d and %d is %d”,a,b,c);
getch();
}
In this program, two constants a and b area added in a variable
c. The value of a and b are fixed as 10 and 20. Hence they are
constants. The value of c depends upon the value of a and b so
a variable. Total memory consumption by this program is
2*3=6 bytes. To print the value of a variable, printf includes
some special symbols such as %d for integers. The table is
given as :
Int %d
Float %f
Char %c
Double %e
long int %ld
The symbols are used within the double quotes and the name
of the variables are given after the quotes after a separation by
comma (,). If we need to use more than one variables to
display, more number of % symbols can be used and the
variable names can be given in respective order.
We can use more than one number of data types in a single
program if required. We can also reduce number of variables in
the program. Let us see this from the previous example itself :
OUTPUT :
#include<stdio.h>
#include<conio.h>
10 + 20 = 30
void main()
{
int a=10, b=20;
clrscr();
printf(“\n%d + %d = %d”,a,b,(a+b));
getch();
}
scanf(“%d”,&a);
when this line comes for compilation, the screen waits for a
integer type value to enter. We then gives the value and press
enter key. The value given is then stored in a. The value must
be in the range of -32768 to +32767 as it is a integer.
OPERATORS :
Operator Name
= Assignment Operator
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo
++ Increment
-- Decrement
Arithmetic Operators
Operator Name
== Comparative Equal
< Less Than
<= Less than equals
> Greater Than
>= Greater than equals
!= Not equals
Relational Operators
Operator Name
Logical Operators
Operator Name
Bitwise Operators
printf(“\n\n\n\nRoll Number\t:\t%d”,RN);
printf(“\nSubject 1\t:\t%d\nSubject 2\t:\t%d”, s1, s2);
printf(“\nSubject 3\t:\t%d\nSubject 4\t:\t%d”, s3, s4);
printf(“\n\nTotal = %d\nPercentage = %f”, total, per);
getch();
}
Now, as we can see that the percentage is displayed up to six
points of decimal. It is not required. So to limit the floating point
variables, we can use some extra things in printf statement.
Suppose if we want to limit the floating point variable to 2
decimal points then we can use %0.2f instead of using %f in
printf statement. This is also true for the numbers with more
than 6 decimal points. If we need the number up to 20 decimal
points, we can use %0.20f. Try this.
A=5;
A++;
A=8;
A = A* 2;
SWAPPING PROGRAM :
A=10;
B=20;
B=A;
So, what you guess the values of A and B after these lines ???
Ans : B=10 and A=10
If I use
A=B
At the place of B=A, then the answer will change. Then the
result will be
A=20 and B=20.
temp = a;
a = b;
b = temp;
printf(“\nAfter swapping,\na = %d\tb = %d”, a, b);
getch();
}
Here, the value 20 is first given to temp, then a is assigned the
value of b i.e. 10 and then at the end, the value of temp (10) is
given to b to complete swapping process.
OUTPUT
// Swapping
: without using third variable.
Enter two integers to swap : 20 10
#include<stdio.h>
#include<conio.h>
Before swapping,
a = 20 b = 10
void main()
After swapping,
{ = 10
a b = 20
int a, b;
clrscr();
a = a + b;
b = a - b;
a = a - b;
printf(“\nAfter swapping,\na = %d\tb = %d”, a, b);
getch();
}
CHARACTER PROGRAM :
char ch;
ch = ‘H’;
char ch = ‘H’;
instead of giving the character itself, we can also give the ASCII
code of any letter to the variable. The variable will
automatically fetch the corresponding character and take it.
char ch = ‘H’;
or
char ch = 72;
// ASCII game.
#include<stdio.h>
#include<conio.h>
void main()
{
int a= 78;
char ch = ‘+’;
clrscr();
OUTPUT
printf(
: “%d %c\n%d %c”, a, a, ch, ch);
78 N getch();
43 +
}
Before swapping,
a = 20 b = 10
After swapping,
a = 10 b = 20
So, by this program we can say that the 78 contains the
character N and + has an ASCII code as 43. By this method, we
can have the A code of any character or find the character at
any ASCII code.