Posts

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("

File Programming

#include<stdio.h>     int main()     {         FILE *ptr_file;         int x;         ptr_file =fopen("output.txt", "w");         if (!ptr_file)             return 1;         for (x=1; x<=10; x++)             fprintf(ptr_file,"%d\n", x);         fclose(ptr_file);         return  0;     }

String Programming

Image
C Programming String In C programming, array of character are called strings. A string is terminated by null character  /0 . For example: "c string tutorial" Here, "c string tutorial" is a string. When, compiler encounters strings, it appends null character at the end of string. Declaration of strings Strings are declared in C in similar manner as arrays. Only difference is that, strings are of  char  type. char s[5]; Strings can also be declared using pointer. char *p Initialization of strings In C, string can be initialized in different number of ways. char c[]="abcd"; OR, char c[5]="abcd"; OR, char c[]={'a','b','c','d','\0'}; OR; char c[5]={'a','b','c','d','\0'}; String can also be initialized using pointers char *c="abcd"; Reading Strings from user. Reading words from user. char c[20]; scanf("