BASIC C PROGRAMING
Steps in learning C programming:-
Alphabets, digits, special char -> constants, variables, keywords -> instructions -
> program
Constants :- is entity which doesn’t change
Called as literals
Variables:- is also a entity but changes ,called as identifiers.
Keywords :- it’s a pre- defined word which means that the computer has
already explained these c words. 32 words r there.
They should not be used as variable names.
Types of c constants
Primary:- integer, real, character
Secondary:- array, pointer, string, structure, union, enum.
Real constants r called floating point constants.
Types of c variables
Variables r case sensitive so must begin with alphabet.
The calculation stored in some cells (location) of comp memory.
Since the value can change in location the name given to these location is
called variable names.
Eg:- avg ,pop_e_89
Comment in a prgm is used to clarify either the purpose of the prgm
(or) the purpose of some statement in a prgm.
They r always written within/* */. @ such time they r used to mention
the purpose of the stmts.
Eg:- /*formula for simple intrest*/
Si=p*n*r/100
Any no. can be written @ any place in the prgm.
Comment cannot be nested i.e 2 comments cannot be written besides.
They cannot be slit i.e writing 1stmt in many lines.
Main()
Most crucial part of c programming.
Is a funcn . contains a set of stmts.
All statements that belong to main() are enclosed with { } braces.
Funcn in c also return a value, main() returns integer valve so we
write int before main() . The integer we r returning is 0 (means
success) .
Void main() doesn’t require writing return 0.
Variables and their usage:-
Any variable used in prgm must be declared before its used.
Eg:- int p , r
float r, si
si=p*r*n/100
printf( )
is a function that can print multiple constants and variables.
if printf() should be used then #include<stdio.h> should be there.
Becoz #include is the pre-processor derivative.
Compilation and execution:-
Compilar compiles the human language to machine language.
After coding the prgm we need to execute the prgm.
Scanf( )
The prgm itself should ask the the user to enter the values ,so this is
where the scanf() come in hand.
‘&’ before the variable is very necessary.
“&” is the ‘Address of’ operator for the variable.
C INSTRUCTIONS
Types of instruction:-
a) Type declaration instruction:- is a instruction to declare the type of
variable in c prgm .
b) Arithmetic:- used to perform the arithmetic operations on constants &
variables. +-*/
c) Control:- used to control the sequence of execution of various stmts in c
prgm.
Eg:- int i=2, r=4
float a=2.3 , d=4.56*6.9
note: constants and variable together r called ‘operands’.
Arithmetic can be of integer mode, real (float) mode, mixed(integer + real)
modes.
C allows only z=k*l is legal whereas k*l=z is illegal.
Modular division operator 10/2 yields 5 while 10%2 yields 0.
% cannot be used with floats.
Arithmetic operations can be performed on int ,floats, char.
No operator is assumed to be present. i.e we should write the operators.
There is no c prgm to perform the exponential operator.
We should use #include<math.h>.
Hierarchy of operations:-
Priority Operators description
1st */ % Multiplication, division,
/-1st than * modular division
2nd +- Addition , Subtraction
3rd = Assignment
Control instructions:-
a) Sequence control c) repetition (or) loop control
b) Selection (or) decision d) case control
DECISION CONTROL INSTRUCTION
The if-else statement:-
If and else is used implement the decision control instruction.
Syntax:- if ( this condition is true)
Statement1;
Else
Statement2;
If is enclosed with parentheses.
This condition is expressed using ‘relational’ operator.
This expression Is true if
x==y x is equal to y
x !=y x is not equal to y
x<y x is less than y
x>y x is greater than y
x>=y x is greater or equal to y
x<=y x is lesser or equal to y
Nested if-else:-
Its perfectly alright if we write another if-else construct within either
the if block or the else block. This is called nesting.
Syntax:-
If(i==1)
Printf(“more questions you ask, more u know !\n”);
Else
{
If(i==2)
Printf (“ if u ask question u r a fool for some time!\n”);
Else
Printf(“ if u don’t ask a ques u r a fool for lifetime!\n”)
}
Common mistake in if stmt is that we should not use semicolon ( ; ). If
we use then the stmt then and there terminates it does not go the nest
condition statements.