The C programming language is just like the English language. When we learn the English Language is to first learn the alphabets or characters used in that language, and then learn to combine these alphabets to form words, then to combined words to form sentences and sentences are combined to form paragraphs. For learning ‘C’ language, we must know the alphabets, numbers, and special symbols which are used in ‘C’. This character set is as follows.
Learning ‘C’ language is just similar and so easy.
Alphabets
A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z
a, b, c, d, e, f, g, h, I, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z,
Digits
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Special Symbols
~ ` ! @ # $ % ^ & * ( ) _ - + = | \ [ ] { } : ; ‘ “ < > , . ? / etc.
Later constants, variables and keywords are used to construct ‘C’ program. Constants, variables and keywords have already discussed in the previous post. Finally, these constants, variables, and keywords are combined to form an instruction. A group of instructions would be combined to form a program. These instructions are basically four types.
- Type Declaration instruction: to declare the type of variables used in a ‘C’ program
- Input/ Output instruction: to perform the function of supplying input data to a program and obtaining the output results from it.
- Arithmetic instruction: to perform arithmetic operations between constants and variables.
- Control instruction: to control the sequence of execution of various statements in a ‘C’ program.
Declaration of 'C' Instruction
This instruction is used to declare the type of variables being used in the program. This procedure should be a must before using the variables.
A declaration is as follows.
Ex.: int basic;
Float rs, grosssal;
Char name, code;
Input / Output Instruction
There are various library functions available for I / O. These can be classified into three broad categories.
- Console I/O function – functions to receive input from the keyboard and write output to Visual Display Unit.
- Disk I/O functions – functions to perform I/O operations on a floppy disk or hard disk.
- Port I/O functions – functions to perform I/O operations on various ports.
Here we will see the basic ideas of console I/O function and later on in details.
The functions printf(), and scanf() is under the category of formatted console I/O functions. These functions allow us to supply the input in a fixed format and obtaining the output in the specified form.
Examples of printf() statements are as follows.
Main ()
{
int a = 100;
int b = 2;
int c = 25;
int result;
float n;
result = a – b * c;
printf(“ a – b * c = %d \n”, result );
n = c / b + a ;
printf(“c / b +a = %f \n “, n );
}
Program Output
a – b * c = 50
c / b + a = 112.5
The general form looks like this…
Printf ( “format string “, list of variables );
The format string can contain:
- Characters that are simply printed as they are like ( a – b * c )
- Conversion specifications that begin with a % sign like ( % d % f )
- Escape sequence that begins with a \ sign like (\n)
Conversion Specifications
The %d and %f used in the printf() are called conversion characters. They tell printf() to print the value of decimal (%d) integer and float (%f).
Following is the list of conversion characters that can be used with the printf() function.
Data type
|
Description
|
Conversion Character
|
Integer
|
Short signed
|
%d or %i
|
|
Short unsigned
|
%u
|
|
Long signed
|
%ld
|
|
Long unsigned
|
%lu
|
|
Unsigned hexadecimal
|
%x
|
|
Unsigned octal
|
%o
|
Real
|
Float
|
%f
|
|
Double
|
%lf
|
Character
|
Signed character
|
%c
|
|
Unsigned character
|
%c
|
String
|
|
%s
|
Escape Sequences
We have already seen \n is an escape sequence. When \n inserted in a printf() ‘s format string, the cursor goes to the beginning of the next line. The new line character is an ‘escape sequence’, so-called because the backslash symbol (\) is considered an ‘escape’ character: it causes an escape from the normal interpretation of a string so that the next character is having a special meaning. The following chart shows a complete list of these escape sequences.
Esc. Seq.
|
Purpose
|
Esc. Seq.
|
Purpose
|
\n
|
New line
|
\t
|
Tab
|
\b
|
Backspace
|
\r
|
Carriage return
|
\f
|
Form feed
|
\a
|
Alert
|
\’
|
Single quote
|
\”
|
Double quote
|
\\
|
backslash
|
|
|
Scanf()
Scanf() allows us to enter data from keyboard that will be formatted in a certain way.
The general form of scanf() statement is as follows:
scanf ( “format string”, list of addresses of variables );
Example
Scanf ( “%d %f %c”, &c, &a, &ch);
Here we are sending addresses of variables (addresses are obtained by using ‘&’ the ‘address of’ operator ) to scanf() function. This is necessary because the values received from the keyboard must be dropped into variables corresponding to these addresses.
Arithmetic Instruction
A ‘C’ arithmetic instruction consists of a variable name on the left-hand side of = and combines of variables and constants on the right-hand side of = through arithmetic operators like +, _, *, and /.
The arithmetic operators, their priorities are as follows. The priority or precedence in which the operations in an arithmetic statement are performed is called the hierarchy of operations.
Priority
|
Operators
|
Description
|
1st
|
*/ %
|
Multiplication, division, modular division (store remainder of division)
|
2nd
|
+ -
|
Addition, subtraction
|
3rd
|
=
|
assignment
|
Ex.: int ad;
float kot, delta, alpha, beta, gamma;
ad = 3200;
kot = 0.0056;
delta= alpha * beta / gamma + 3.2 *2 / 5;
The variables and constants together are called operands which are operated the arithmetic operators and the result is assigned to the variable which is on left-hand side of assignment operator =.
A-C arithmetic statement could be of three types.
1. Integer mode arithmetic statements – all operands are either integer variables or integer constants.
Ex.: int a, b, c;
c = a * 234 + b – 7654;
2. Real mode arithmetic statements – all operands are either real constants or real variables.
Ex.: float p, q, r;
r = p * q /100.0;
3. Mixed mode arithmetic statements – some of the operands are integers and some of the operands are real.
Ex.: float avg;
int a, b, c, num;
avg = (a + b + c + num )/4;
Control instructions in 'C'
The control instructions determine the ‘flow of control’ in a program. These are four types.
- Sequence Control Instruction – ensures that the instructions are executed in the same order in which they appear in the program.
- Selection or Decision control Instruction – allows the computer to make the decision as to which instruction is to be executed next.
- Repetition or loop Control Instruction – it helps the computer to execute a group of statements repeatedly.
- Case-Control Instruction – it also allows the computer to make the decision as to which instruction is to be executed next.
well do you like this post, if yes please give the comments right below, your comments encourage me to write the post.
This post is very useful for me
ReplyDeletenice
ReplyDeletenice
ReplyDelete