Posts

Showing posts from March 10, 2014

Difference between while & do while loops in C

C  Loop control statement Loop control statements in C are used to perform looping operations until the given condition is true. Control comes out of the loop statements once condition becomes false. Types of loop control statements in C: There are 3 types of loop control statements in C language. They are, for while do-while   Example program (For Loop) in C: #include <stdio.h> int main() { int i; for(i=0;i<10;i++) { printf("%d ",i); } } 1 2 3 4 5 6 7 8 9 10 11 12 #include <stdio.h>   int main ( ) {    int i ;      for ( i = 0 ; i < 10 ; i ++ )    {        printf ( "%d " , i ) ;    }        } Output: 0 1 2 3 4 5 6 7 8 9 Example program (while loop) in C:       In while loop control statement, loop is executed until condition becomes false . #include <stdio.h> int main() {

Special Operator in C

Special Operators in C: Below are some of special operators that C language offers.  S.no Operators Description 1 & This is used to get the address of the variable. Example : &a will give address of a. 2 * This is used as pointer to a variable. Example : * a  where, * is pointer to the variable a. 3 Sizeof () This gives the size of the variable. Example : size of (char) will give us 1. Example program for & and * operators in C: In this program, “&” symbol is used to get the address of the variable and “*” symbol is used to get the value of the variable that the pointer is pointing to.  #include <stdio.h> int main() { int *ptr, q; q = 50; /* address of q is assigned to ptr */ ptr = &q; /* display q's value using ptr variable */ printf("%d", *ptr); return 0; } #include <std

Relational Operators in C

Relational operators in C: Relational operators are used to find the relation between two variables. i.e. to compare the values of two variables in a C program. S.no Operators Example   Description 1 > x > y x is greater than y 2 < x < y x is less than y 3 >= x >= y x is greater than or equal to y 4 <= x <= y x is less than or equal to y 5 == x == y x is equal to y 6 != x != y x is not equal to y In this program, relational operator (= =) is used to compare 2 values whether they are equal are not. If both values are equal, output is displayed as ” values are equal”. Else, output is displayed as “values are not equal”. Note : double equal sign (= =) should be used to compare 2 values. We should not single equal sign (=). #include <stdio.h> int main() { int m=40,n=20; if (m == n) { printf("