variable Before using any variable in the program it must be required to declare first Declaration of variable means required to specify data t type name of variable followed by semicolon(;) In c programming language variable declaration required to place top of the program after opening the body before writing first statement . In variable declaration name of the variable must be required to start with alphabet or underscore only In declaration name of the variable existing keywords , operators , seperators and constant values are not allowed Maximum length of the variable name is 32characters, after 32 characters compiler will not consider remaining characters Syntax:- data type variable name; According to the syntax atleast single space must be required between data type and name of the variable When we are declaring multiple variables of single data type then we require to use coma (,) as a seperator EXAMPLES 1) inta; error 2) int a error 3) int a; valid statement 4) char ch; valid statement 5) float f; valid statement 6) int a b c ; error 7) int a,b,c; valid statement 8) int abc,d; valid but abc is considered as single variable 9) int 1a,1b,1c; error variable should not start with number int a1,b2,c3; valid int 1,2,3; error variable name should not be number int _1,_2,_3; valid variable can start with underscore int if; error variable should not be a keyword int If; valid since If is not a keyword if is a keyword int total-salary ; error minus should not be in variable name int total_salary ; valid statement
void main() { int i; float f; i=5/2; f=5/2; printf(i=%d f=%f,i,f); } o/p:- i=2 f=2.0 Value(i/p) int i; float f; -- 5/2 5.0/2 5/2.0 5.0/2.0 2/5 2.0/5 garbage 2 2 2 2 0 0 garbage 2.0 2.5 2.5 2.5 0.0 0.4 Operator behavior is always operand depend only and return value behavior is variable dependent In implementation when operator is returning an integer value and we require to store in float variable then automaticaly return value will be converted into float format by adding .0 i 2 5/2 f 2.0 If the operator is returning float data and we require to store in integer variable then we require to assign only decimal part
i 2 5.0/2 2.5 f 2.5 Difference between declaration of a variable and initialisation of a variable is in declaration of the variable after creating the memory it stores garbage value untill we are assigning the data In initialisation after creating the memory it stores default value what we assign int a; declaration int a =10; initialisation a=10; assigning CONTROL FLOW STATEMENT The execution flow the program is under control flow statements In C programming language we having 3 types of control flow statements 1)Selection statements Ex:-if ,else ,else if, switch 2)Iteration statements Ex:-while , for , do while 3)Jumping statements Ex:-break , continue, go to
SELECTION STATEMENT This statements are also called as decision making statements By using selection statements we can create conditional oriented block i.e; depends on the condition block can be execute or ignore Syntax to if :- if(condition) { statement1; true statement2; } Comes out of the block if the condition is false Constructing the body is always optional , it is recommended to use the body when we are having multiple statements for a single statement doesnt require to specify the body , if the body is not specified then automatically condition part will terminated with next semicolon(;) Else:-else is a keyword we can create alternate block for if condition Using else is an alternate for if condition . It is recommended to use when we have an alternate block When we are working with if and else only one block will work i.e; when the if block is false then only else block will execute SYNTAX:- if(condition) { statement1; statement2; } else { statement3; statement4; } If(condition) statement1; else statement2; if(condition) { statement1; statement2; } else statement3; if(condition) statement1; else { statement2; statement3; } EXAMPLES void main() { printf(a); printf(b); if(5>8!=0)//if(0!=0) { printf(hello); printf(welcome); } printf(c); printf(d); } Output:-abcd
Explanation:- Generally when are working the selection statements compiler is expecting the condition with variable type data, if we are passing constant values then it gives a warning message i.e; condition is always false or true Generally warnings can be ignored because we are able execute the program void main() void main() { { printf(hello); printf(hello); if(!5!=5) if(!5) ignore since !5 { printf(a); is zero printf(ab); printf(b); } printf(welcome); printf(welcome); } } output:- hellobwelcome Output:-helloabwelcome when we are constructing multiple statements with out constructing the body then only one statement will place with in the body because scope of the condition will terminate with next semicolon(;) void main() { printf(A); if(5>3//3>5) { printf(hello); printf(C);
} else printf(hii); } output:-AhelloC void main() { printf(hello); if(1!=3<5 && 0==5<8) { printf(A); printf(B); } else { printf(x); printf(y); } printf(welcome); } Output:-helloxywelcome void main() o/p:-CABwelcome { if the body is not specified printf(C); for else part then scope will if(2>5!=0 && 5>8!=1) close with next semicolon(;) { printf(A); printf(B); } else printf(hello); printf(welcome); } void main() according to syntax of if and { else when we are using else printf(hello); part it must be required to if(5!=5>2) start after if scope only or else printf(B); it gives an error printf(A); else { printf(welcome); printf(hii); } Output:- error misplaced else