Posts

Showing posts from January, 2022

while loop

  #include <stdio.h> int main () {     int i = 0 ;     while ( i < 10 )     {         printf ( " %d \n " , i );         i ++;     }     return 0 ; }

type casting

  // typecasting syntax // (type) value #include <stdio.h> int main () {     float a = 10 ;     float b =( float ) 54 / 5 ; //here int & int will give int so we have to convert it to float for float no     printf ( " %d \n " ,( int ) a ); // here i changed the type of the value a     printf ( " %f " , b );     return 0 ; }

switch case

  #include <stdio.h> int main () {     int rank ;     printf ( "enter u r rank in 12 class:" );     scanf ( " %d " ,& rank );     switch ( rank ) // only int and characters are valid here in ( )     {         case 1 : //here "1,2,3" is not counting this is value of variable rank         printf ( "u won car as a gift" );         break ;         case 2 :         printf ( "u won scooty as a gift" );         break ;         case 3 :         printf ( "u won cycle as a gift" );         break ;         default : //this will execute always unless u break the switch         printf ( "this will execute when all cses are false" );     }     return 0 ; }

if else

  #include <stdio.h> int main () {     int age ;     printf ( "enter u r age:" );     scanf ( " %d " ,& age );     printf ( "u r age is %d \n " , age );     if ( age >= 18 )     {         printf ( "u can vote" );     }     else if ( age <= 10 )     {         printf ( "u can vote for kids" );     }         else     {         printf ( "sorry u cannot vote" );     }         return 0 ; }

goto statement

  #include <stdio.h> int main () {     label :         printf ( "hello harish \n " );         goto end ;     goto label ;     end :         printf ( "hi" );         return 0 ; } // goto statement is used to transfer program control to a predefined label

functions4

  // 4. without arguments and without return value #include <stdio.h> char func (); int main () {     int a ;     a = func ();     printf ( " %c " , a );     return 0 ; } char func () {     printf ( "hello world" ); }

functions3

  // 3. without arguments with return value #include <stdio.h> int takenumber (); int main () {     int a ;     a = takenumber ();     printf ( "the number entered is %d " , a );     return 0 ; } int takenumber () {     int i ;     printf ( "enter a number:" );     scanf ( " %d " ,& i );     return i ; }

functions2

  // 2. with arguments and without return value #include <stdio.h> void printstar ( int n ); int main () {     printstar ( 8 );     return 0 ; } void printstar ( int n ) {     int i ;     for ( int i = 0 ; i < n ; i ++)     {         printf ( " %c " , '*' );     }     }

functions1

  //1.with arguments and return value #include <stdio.h> int sum ( int a , int b ); // function declaration int main () {     int a , b , c ;     a = 3 ;     b = 5 ;     c = sum ( a , b ); //function calling     printf ( " %d " , c );     return 0 ; } int sum ( int a , int b ) // function definition {     return a + b ; }

format specifier

  #include <stdio.h> int main () {     int a = 5 ;     float b = 2.33 ;     printf ( "int: %d \n float: %10.2f " , a , b ); //here number between % and f specifies no of digits before and after the point     printf ( " \n float: %-10.2f u can see the space" , b ); //i can give negative no before point that will give space after the float no     return 0 ;             } // %c= character                     // %d= int // %f= float // %l= long integer // %lf= double // %Lf= long double

for loop

  #include <stdio.h> int main () {     int i , j , k ;     for ( i = 0 , j = 0 , k = 0 ; i < 10 ; i ++, j ++, k ++)     {                                         printf ( " %d %d %d \n " , i , j , k );       }     return 0 ; } //for loop expressions //expression1: u can initialize more than one variables //expression2: u can give more than one condition but the last condition will be taken onto consider //expression3: u can increment every variable value which u have initialized in for loop

do while

  int main () {     int i = 0 ;     do     {         printf ( " %d \n " , i );         i ++;     } while ( i < 11 );     return 0 ; }

constants

  //first way of defining constant /*#include <stdio.h> int main() {     const int a=6; // here we defined 'a' as constant     //a=10; (here we can not change variable 'a' because we defined it as constant)     printf("%d",a);     return 0; }*/ //second way of defining constant #include <stdio.h> #define a 10 // here we defined 'a' as constant int main () {     printf ( " %d " , a );     return 0 ; }