Featured Post

Showing posts with label C Programming. Show all posts
Showing posts with label C Programming. Show all posts

Friday, October 14, 2016

switch statement in C



 A switch statement is a multi way decision point. In if else statement there are only two choices(either condition is true or false). When there two or more choices  then we have to use switch statement.

Syntax:
     
                             switch(expression)
                             {
                                   case constant1:
                                    statement(s); /* code to be executed */
                                    break;
                                   case constant2:
                                    statement(s);
                                    break;
                                    .
                                    .
                                    .
                                    .
                                    .
                                    .
                                     default:
                                      /* default code to be executed /
                               }

Every switch statement followed by number of case statements(possible expression values). Every case statement contains a block of statements to be executed. If expression value matches any case constant then corresponding block statements will be executed. If the expression value does not match any case constant then default block will be executed.

/* C program to demonstrate the use of switch statement */

main()

{
   int ch,a,b,c;
   printf("\n 1. Addition  \n 2. Subtraction   \n 3. Multiplication    \n 4. Division ");
   printf("\n enter your choice");
   scanf("%d",&ch);
    printf("\n enter any two numbers");
    scanf("%d%d",&a,&b);
    switch(ch)
    {
            case 1:
                           printf("\n addition of %d and %d is %d",a,b,a+b);
                           break;
             
            case 2:
                         printf("\n subtraction of %d and %d is %d",a,b,a-b);
                           break;
            case 3:
                       printf("\n multiplication of %d and %d is                                                     %d",a,b,a*b);
                           break;
            case 4:
                           printf("\n division of %d and %d is %d",a,b,a/b);
                           break;
             default:
                          printf("\n enter the correct choice");
    }
}

OUTPUT:
       1. Addition
       2. Subtraction
       3. Multiplication
       4. Division
   enter your choice 1
   enter any two numbers
   12    6
   addition of 12 and 6 is 18

                             LOOPS in C

C programming, switch statement, difference between if else and switch statement, conditional statements, iterative statements. default statements, goto statements, break statement, cotinue







Thursday, October 13, 2016

Sample string programs in C without using built functions


/* C program to find the length of the given string with out using built in functions */

main()
{
   char a[30];
   int l;
   puts("enter any string");
   gets(a);
   for(l=0;a[l]!='\0';l++);  
   printf("\n length of given string %s is is %d",a,l);
}

OUTPUT:
  enter any string   ashok
  length of given string ashok is 5


/* C program to copy one string  to another string with out using built in functions */
main()
{
  char a[30],b[30],l,i;main()
{
  char a[30],b[30],l,i;
  printf("\n enter any string");
  gets(a);
  for(l=0;a[l]!='\0';l++); / * length of the string */
  for(i=0;i<l;i++)
  {
     b[i]=a[i]; /* copying characters from a to b */
  }
  printf("\n copied string is");
  puts(b);
}

OUTPUT
  enter any string
  anil
  copied string is anil



/* C program to compare two string with out using built in functions */

main()
 {
     char [30],b[30];
     int l1,l2,i=0;
     printf("enter the first string");
     gets(a);
      printf("enter the second string");
     gets(b);
     for(l1=0;a[l1]!='\0';l1++); /* length of the first string */    

     for(l2=0;a[l2]!='\0';l2++); /* length of the second string */     
      if(l1!=l2)
      {
      printf("both strings are not equal");
        exit(1);  /* program ends */
       }
      for(i=0;a[i]!='\0';i++)
     {
           if(a[i]!=b[i])
           {
              c=1;
               break;
           }
      }
    if(c==0)
    printf("\n both strings are equal");
   else
    printg("\n both string are not equal");

OUTPUT:

enter the first string
ashok 
enter the second string
ashoi
both string are not equal


string handling in C, string programs without using built in functions, palindrome program with out using built function c . string length, string reverse

String handling in C


   A string is a collection of characters i.e., string is a character array. Every character array in C ends with null character '\0'.  All string handling functions are defined  in header file string.h

char a[30]; /* declaration of character array 'a' and 30 bytes will be allocated to 'a' in memory by                                       compiler*/

initialisation of character array 

char a[10]="ashok"; /* initialises first five characters with ashok and remaining characters with null                                        character */

Also we can initialise the array using the following syntax:

char a[10]={'a','s','h','o','k'};



/* sample program */

main()
{
   char a[10]="ashok";
   printf("%s",a);
}
OUTPUT:
      ashok

gets() and puts() functions
 
   gets() function is used to read a string from the keyboard
   puts() function is used to display a string on the screen

/* C program to read a string from keyboard and display the same on the screen */

main()
{
     char a[30];
     printf("enter your name");   /* can be replaced with puts("enter your name"); */
    gets(a);    /* can be replaced by scanf("%s",&a); */
    puts(a);
}

OUTPUT:
           enter your name
           ashok
           ashok 

note: scanf() function exits when a space is entered through keyboard 

The following are the some of the built-in functions in C

strlen()  :    This function accepts one string argument and returns the length of the string

strlcmp(): This function accepts two strings and returns zero if both string are equal otherwise 1

strcat():   This function accepts two string and concatenates one string to another string

strcpy():  This function accepts two parameters and copy one string to another string

strupr():  This function converts the given string to upper case

strlwr():  This function converts the given string to lower case

strrev():  This function reverse the given string

/* C program using built function */
#include<string.h>
main()
{
   char a[30],b[30],c[30];
  int length;
   printf("enter the first string");
   gets(a);
   printf("enter the second string");
   gets(b);
   length=strlen(a);
   printf("\n length of first string is %d",length);
   strcpy(str1,str3);
   printf("c=%s",c);
   printf("\n reverse of a given string %s is %s", c,strrev(c));
   printf("\n concatenation of %s and %s is %s",a,b,strcat(a,b);
   printf("\n upper case of %s is %s", a,strupr(a));
  }

OUTPUT:
     enter the first string
     ashok
     enter the second string
     kumar
     length of first string is 5
     c=ashok
     reverse of a given string ashok is kohsa
     concatenation of ashok and kumar is ashokkumar
     upper case of ashok is ASHOK


string handling in c, character arrays, string handling built functions in c, strcat(), strcmp(), strcpy(), strrev(), strlen(), string palindrome, sample progrma on string built in functions 




Linear Search and Binary Search

Linear Search:
                    In linear search we search for an element from first to last element in a array. There is no consideration of arrangement of elements in array. For example if  'a' is an element of length 10 then we search from a[0] to a[9] to find the required element. When the element is found we break the searching process.

/* C Program to implement linear search */

main()
{
   int a[100],ele,i,c=0,n;
   printf("\n enter your range");
   scanf("%d",&n);
   printf("\n enter your elements");
   for(i=0;i<n;i++)
   {
        scanf("%d",&a[i]);
   }
   printf("\n enter the element to be searched");
   scanf("%d",&ele);
   for(i=0;i<n;i++)
   {
      if(a[i]==ele)/* Comparing each and every value in array with search element */
       {
              c=1;
              printf("\n element is fount at location %d",i+1);
              break;
        }
    }
   if(c==0)
   printf("\n element is not found");
 }

OUTPUT:
  enter your range  
  10
  enter your elements
  10 20 30 40 50 67 78 89 90 99
  enter the element to be searched 78
  element is found at location 7

Binary search:
     Using Binary search we search for an element in an array of elements. To implement binary search, the list of elements in an array must be arranged in ascending order or descending order. In binary search we calculate mid position and compare the value at mid position with requires search position. Accordingly the first and last positions of the array are adjusted until the search element is found at mid position or first position crosses last position(search fail).

/* C program to implement Binary Search */

main()
{
   int a[100],n,ele,i,first, last,mid,c=0;
   printf("\n enter your range");
   scand("%d",&n);
   printf("\n enter your elements");
   for(i=0;i<n;i++)
   {
      scanf("%d",&a[i]);
    }
    printf("\n enter the element to be searched");
    scanf("%d",&ele);
   first=0;
   last=n-1;
   while(first<=last)
   {
       mid=(first+last)/2;
       if(a[mid]==ele)
        {
            c=1;
            printf("\n element is found at location %d",mid+1);
            break;
         }
        else if(a[mid]<ele)  /* element is in right hand side of mid position */
         {
            first=mid+1;
          }
        else  /* element is in left hand size of mid position */
           last=mid-1;
    }
       if(c==0)
       printf("\n element is not found");
}

OUTPUT:
   enter your range
    10
  enter your elements
  10 20 30 40 50 60 70 80 90 100
  enter the element to be searched 
  50
  element is found at location 5


                                      Functions

linear search, binary search, hashing, searching , sorting, biggest and smallest element in an array, bubble sort, insertion sort, selection sort, bucket sort. initialisation of one dimensional array, two dimensional array.matrix addition , matrix multiplication, symmetric matrix.

               
 











Arrays in C



Array:
    An array is a collection of similar data items. For example group of integer data items is called integer array. Similarly float array is group of float data items.

Consider a scenario of saving 50 roll numbers in a class. We need 50 variables to store 50 roll number ( numbers) in memory. Using arrays we can save 50 numbers in memory using a single variable name for example a.

   int  a[50]; /* array declaration. "a" occupies 50*2=100 bytes of memory*/
   /*first number is referred in memory as a[0], second number in memory is referred as a[1] and so on and last number in memory is referred as a[49].*/

Initialization of array elements :

Compile time initialization:
             
                  int a[6]={10,21,32,34,45,67};
                  
               which means a[0]=10, a[1]=21,a[2]=32,a[3]=34,a[4]=45,a[5]=67.

/*Write a C program to read 5 numbers and display the same 10 number on the screen*?

main()
{
   int a[5]={10,20,30,40,50};
  printf("%d\t%d\t%d\t%d\t%d",a[0],a[1],a[2],a[3],a[4]);
}
output:
               10    20    30   40    50


Run time initialization:
          Allocating the values to array member at run time is called run time initialization. Using scanf function we can give values to an array at run time.


/*Write a C program to array values from the keyboard and display the same on the screen*/

main()
{
   int a[100],i,n;
   printf("\n enter your range");
   scanf("%d",&n);
   for(i=0;i<n;i++)
   {
     scanf(%d",&a[i]);
   }
  for(i=0;i<n;i++)
  {
    printf("%d \t ", a[i]);
  }
}

OUTPUT
Enter your range 6
10
23
32
34
45
67
10   23    32   34   45   67

/*Write a C program to find the sum and total of 6 subjects*/

main()
{
  int a[6],sum=0,i;
  float avg;
  printf("\n enter your six subject marks");
  for(i=0;i<6;i++)
  {
     scanf(%d",&a[i]);
  }
 for(i=0;i<6;i++)
 {
   sum=sum+a[i];
  }
  avg=total/6.0;
printf("total and average of six subjects are %d \t %f", sum, avg);
}

OUTPUT:
enter your six subject marks

40
50
40
40
40
30

total and average of six subjects are 240    40







Arrays in c, initialization of arrays, run time initialization, two dimensional arrays, linear search, binary search







C lab sample programs


/* C program to swap two numbers without using temporary variables */
  
main()
{
     printf("\n enter any two values");
     scanf("%d%d",&a,&b);
     printf("\n Before swapping a=%d\tb=%d",a,b);
     a=a+b;
     b=a-b;
     a=a-b;
     printf("\n After swapping a=%d\tb=%d",a,b);
}

OUTPUT:
    enter any two values 5  10
    Before swapping a=5     b=10
    After swapping a=10     b=5
 


/* C program to find the roots of the quadratic equation */
main()
{
  int a,b,c,r1,r2,d;
  clrscr();
  printf("\n enter the coefficients of a quadratic equation");
  scanf("%d%d%d",&a,&b,&c);
  d=b*b-(4*a*c);
  if(d==0)
  {

     r1=-b/(2*a);
     r2=-b/(2*a);
     printf("roots are equal %d and %d",r1,r2);
  }
  if(d>0)
  {
     r1=(-b+sqrt(d))/(2*a);
     r2=(-b-sqrt(d))/(2*a);
     printf("roots are real %d and %d",r1,r2);
 }
 if(d<0)
 {
     printf("\n roots are imaginary");
 }
}

OUTPUT:
enter the coefficients of a quadratic equation
1  -2  1
roots are equal 1    1



/* C program to find the biggest of three numbers using ternary operator */
main()
{
  int a=7775,b=12,c=225,x;
  x=a>b?(a>c?a:c):(b>c?b:c);
  printf("biggest number is %d",x);

}

OUPUT:
biggest number is 7775



/* C program to  find the sum of digits of a given number */

main()
{
  int n,r,sum=0,n1;
  printf("\n enter any number");
  scanf("%d",&n);
  n1=n;
  while(n>0)
  {
     r=n%10;
     sum=sum+r;
     n=n/10;
  }
  printf("\n sum of digits of %d is %d",n1,sum);

OUTPUT:
enter any number
132
sum of digits of 132 is 6




/* C program to check whether the given number is palindrome or not */

main()
{
  int n,r,sum=0,n1;
  printf("\n enter any number");
  scanf("%d",&n);
  n1=n;
  while(n>0)
  {
     r=n%10;
     sum=(sum*10)+r;
     n=n/10;
  }
  if(sum==n1)
  printf("%d is palidrome  number",n1);
  else
  printf("%d is not palindrome number",n1);
}

OUTPUT:
enter any number
121
121 is a palindrome number




/* C program to check whether the given number is armstrong or not */

main()
{
  int n,r,sum=0,n1;
  printf("\n enter any number");
  scanf("%d",&n);
  n1=n;
  while(n>0)
  {
     r=n%10;
     sum=sum+(r*r*r);
     n=n/10;
  }
  if(sum==n1)
  printf("%d is armstrong number",n1);
  else
  printf("%d is not armstrong",n1);
}

OUTPUT:
enter any number
153
153 is armstrong




/* C program to check whether the given number is prime number or not */

main()
{
  int n,c=0,i;
  printf("enter a number");
  scanf("%d",&n);
  for(i=2;i<=n/2;i++)
  {
     if(n%i==0)
     {
c=1;
break;
     }
  }
  if(c==0)
  printf("%d is prime number",n);
  else
  printf("\n %d is not prime number",n);
}

OUTPUT:
enter a number 34
34 is not prime number



/* C program to print multiplication tables up to the given range */

main()
{
  int n,i,j;
  printf("\n enter your range");
  scanf("%d",&n);
  for(i=1;i<=n;i++)
  {
     for(j=1;j<=10;j++)
     {
printf("\n %d * %d = %d",i,j,i*j);
     }
      printf("\n ");
  }
}

OUTPUT:
enter your range 10



/* C program to print Fibonacci Sequence with in the given range */

main()
{
  int n,a=0,b=1,c;
  printf("enter your range");
  scanf("%d",&n);
  printf("\n %d \t %d",a,b);
  c=a+b;
  while(c<=n)
  {
    printf("\t%d",c);
    a=b;
    b=c;
    c=a+b;
  }
}

OUTPUT:

enter your range 25
0    1   1   2  3   5   8   13   21


C lab sample program, c lab manual c lab r16 lab manual , c lab sample programs , c lab R16, palindrome, armstrong, prime number or not, call by value, fibonacci sequence, multiplication tables



Tuesday, June 28, 2016

File handling in C: miscellaneous

                               http://cjuschools.blogspot.in/          

 FILE handling in C

Before going to file lets execute the following c program
main()
{
   int a,b,c;
  clrscr();
  printf("enter any any two values");
  scanf("%d%d",&a,&b);
  c=a+b;
  printf("addition of %d and %d is %d",a,b,c);
 getch();
}

output: enter any two numbers
5
3
addition of 5 and 3 is 8
lets execute again!
enter any two values
13
12
addition of 13 and 12 is 25
every thing is ok ! but wat about previous input and output value?



so this is the problem while executing c program using  standard input output devices.

So if want to store input data and output data permanently then we have to go for files.


got it?

So lets move out attention to files.
So if we want to store input data and output data permanently then we have to go for files.


got it?

A file is a collection of data stored on a disk.
Simple to say file handling deals with creation of text documents, editing text, and deletion of text documents. 
For example consider creation of notepad documents, copy of one notepad document to another document etc
So to do operations on file we have some library function. 
fopen()-->to open a text file
getc()-->to read a single character from a file where the pointer is pointing
putc()-->to put a single character in a file where the pointer is pointing
getw()-->to read a single integer value from a file where the pointer is pointing
putw()---> to put a single integer in a file where the pointer is pointing
fprintf()-->to display different types of data on to the screen at a time
fscnaf()-->to read different types of data from the key board in a single function
fclose()--->to close the open file

Hope you  all understand wat am saying?

lets go in depth

fopen():
  
   fopen() function is used to open an existing file or to create the new file if not exist

   Note the every file program in c begins with:

   FILE *fp;
  where fp is a pointer pointing to the some location of the file.

 now we write fopen() syntax

fp=fopen("file name","mode");

example:
 main()
{
 FILE *fp;
fp=fopen("hello.txt","w");/*opening file */
-----
-------
------
fclose(fp);/*closing file pointer*/
}

seeing above example one more thing discuss
mode:

   a mode is ,nothing but it says what type of action to be taken on file
  r--->to open a file in read mode.(make sure the opened file must exist other wise error returns)


file handling in c, r,w,a modes, fopne(),fopen,fseek,getc,ptuc,fgetc(),fputc(),fclose(),getw(),putw().

Difference between structure and union



Both structure and union are user defined data types!
 Structure is a user defined data type where we can store many related different data types at a single place in memory!for example if we want to store the student in memory then we have to store roll number name and group and grade and so on .  This can be accomplished using struct data type.

struct variable name
{
  data type data member1;
  data type data member2;
  data type data member3;
-------
}variables;

example:
struct student
{
  int rollno;
 char name[20];
 char group[10];
 float grade;
}s1,s2,s3;  /* s1 s2 s3 are student1 student2 and student3*/

the student data type occupies 36 bytes[2+20+10+4];

The same we can do with union data type
union variable name
{
  data type data member1;
  data type data member2;
  data type data member3;
-------

}variables;

example:
union student
{
  int rollno;
 char name[20];
 char group[10];
 float grade;
}s1,s2,s3;  /* s1 s2 s3 are student1 student2 and student3*/

the only difference is using struct it takes 36 bytes of memory. but with union it takes only 20 bytes of memory. union data type takes and fixes the size to largest data type available in struct! in the above declaration  name data member takes 20 bytes.




Difference between structure and union! Struct and union data type , size of struct and size of union  , user defined data types, enum data types, data types classification, arrays structures union boolean data types. program using struct and union student data base program using struct and union

Pointers in C

                                                          Pointers in C


   Simply a pointer is variable which holds the address of another variable.
   Then What is the differnce between the a normal variable and pointer variable?
   What happens when we declare the variable like

      int a=34;
     
  The above declaration tells the compiler that 'a' is a integer variable and reserves two bytes of memory for 'a'.
   Now the question is where the 'a' is in memory?
   
      well do u remember the scanf function?

    scanf("%d",&a);
 
in scanf fucntion there is a symbol '&' nothing but it represents the address of the variable 'a' in memory(for example 1024 address)

While using pointers we use * and &

 declaration of pointer variable:
      datatype * variable=value;
     here datatype is int float or char or even void data type
   
     for example int *a;
     here 'a' is a integer pointer i.e, it can store only integer address
     
    int *b;
    int c=10;
    b=&c;
     now b conatins the address of c.
   
    /*pointers in c*/
   
     main()
     {
      int *p;
      int q=20;/*pointer variable declaration*/
      p=&q;
      printf("\nvalue of q is %d ",q);
      printf("\nvalue of q is %d ",*(&q));
      printf("\nvalue of q is %d ",*p);
      printf("\nAddress of q is %u ",p);
      printf("\nAddress of q is %u ",&q);
     }
    output:
        value of q is 20
        value of q is 20
        value of q is 20
        address of q is 1190
        address of q is 1190


/*  swapping of two numbers using pointers*/

    void swap(int *,int *);
    void main()
    {
       int a=5,b=6;
       printf("before swapping a=%d\tb=%d",a,b);
      swap(&a,&b);
      printf("after swapping a=%d\tb=%d",a,b);
   }
    void swap(int *p,int *q)
   {
     int temp;
   temp=*p;
   *p=*q;
   *q=temp;
   printf("\m after swapping a=%d\tb=%d",a,b);
   }

  output:
   before swapping a=5      b=6
    after swapping a=6      b=5
 
   
                                             phython programming

Pointers in c, c programming using pointers, pointer declaration, pointer initialization void pointer integer pointer character pointer float pointer swapping of two numbers using pointers. Advantages of pointers. pointer arithmetic copy of one string to another string using pointers reverse of a string using pointers



   

Arrays in C

   /* arrays in c */

Before going to discuss about arrays lets go through some sample program

main()
{
  int a,b;
  printf("\n enter any two values");
  scanf("%d%d",&a,&b);
  printf("the given two values are a=%d\tb=%d",a,b);
}

output:
    enter any two values
     10 20
   the given two values are a=10   b=20

well this program good for reading two values
so what if we want to read 50 variables from keyboard ?
one solution is intialising 50 variables like a,b,c,d,,,,,,,, and reading varibales of
keyoard from scanf function.
But declaring 50 variables is looking complex

Arrays:
     main()
     {
        int a[10];
         a[0]=101;
         a[1]=102;
         a[2]=103;
         a[3]=104;
         a[4]=105;
         a[5]=106;
         a[6]=107;
         a[7]=108;
         a[8]=109;
         a[9]=110;
       printf("\n%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d",a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
     }
 
     output:101   102 103 104 105 106 107 108 109


main()
{
   int a[10];/* a is an array which occupies (10*2)bytes of memory */
   int i,n;
   printf("enter your range");
   scanf("%d",&n);/* no of elements */
   printf("enter %d elements",n);
   for(i=0;i<10;i++)
   {
      scanf("%d",&a[i]);
    }
   printf("\n\nentered elements are");
   for(i=0;i<10;i++)
   {
      printf("\t%d",a[i]);
   }
}

output:
    enter your range
    10
    enter 10 elements
    1 2 3 4 5 6 7 8 9 10


    entered elements are 1 2 3 4 5 6 7 8 9 10
 
/* program to calculate the total marks and average marks for give six subjects*/

  main()
  {
    int a[6],i,total=0;
     float avg;
     printf("\n enter your six subject marks");
     for(i=0;i<6;i++)
      {
        scanf("%d",&a[i]);
      }
     for(i=0;i<6;i++)
     {
      total=total+a[i];
     }
     avg=total/6.0;
    printf("\n total marks is %d",total);
    printf("\average marks is %f",avg);
  }
 
output:
    enter your six subject marks
        90 90 90 80 100 90
       total marks is 540
       average marks is 90




arrays in c, derived data type array, array declaration, array initialization , sample program of arrays linear search using arrays binary search using arrays, calculating using total and average marks of a student,matrix multiplication

     

ternary operator in C


        
/* Biggest of two numbers using i) if else statement and ii) ternary operator */

Before going to discuss about ternary operator lets check the below program

/* biggest of two numbers *?
main()
{
   int a,b;
   printf("\n enter any two numbers");
   scanf("%d%d",&a,&b);
   if(a>b)
   {
      printf("%d is big",a);
    }
   else
   {
     printf("%d is big",b);
   }
 }

output: enter any two numbers 10 20
20 is big


In the above program we assigend a value  to 10 and b value to 20
Here 'if' is called decision making statement i.e., based on the decision either 'if' part or 'else' part will be executed.
Here 'else' part will be executed because condition is false(0). So in output we see like "20 is big"
(Note: if condition is true(1) 'if' part will be executed)

Now the same thing can be performed using tenary(condional) operator.   (?  :)



/* biggest of two numbers */
main()
{
   int a,b,big;
   printf("\n enter any two numbers");
   scanf("%d%d",&a,&b);
 
   big=(a>b)?a:b;

   printf("%d is big",big);
}

output:   enter any two numbers 10 20
 20 is big

In the above program output is printed as "20 is big"

We can divide a ternary operator in c into three parts
first part conditiona part(here a>b?)
second part positive part(true part) here a   (equivalent to if part)
third part negative part(false part) here b     (equivalent to else part)
 In the above part a is assigned a value 10 and b is assigned a value 20.
and big=(a>b)?a:b;

In evaluating this expression the compiler starts from left and move to right.
first conditional part will be evaluated i.e., a>b?  ( here condition is false)
then compiler skips the second part amd moves to third part (since condition is false, the compiler moves to thrid part) i.e., big is assigned a value to b.
So output printed is "20 is big"



NOTE: if else statement can be replaced by ternay operator.




c programming, if statments, else if statements, nested if, elsed if ladder, arrays,strings, strucutres, unions, userdefiend data types, bit wise operators, ternary operator, conditional operator, file handling in c, fseek function, ftell fucntion, rewind function, logical operators , switch statements, is ternary statement is equlivalent to if else statements,biggest of two numbers, poitner in c, switch statement in c, decision making in c, control strcutures in c. biggest of three numbers

printf() and back slash characters


how to print something on screen using C printf() function?

consider a simple code given below-

main()  /* program begins here */
{
  printf("hello this is my first program");
}

if we execute this code we get output on screen as

output : 
hello this is my first program

In c if we want to print something on screen we have to use printf() function which is available in standard library function stdio.h

whatever we give in printf() function in between two inverted comas that is printed same(except some special symbols).

main()
{
  printf("hai Programmers");
}

output: 
hai Programmers

remember that every printf() function in C must be terminated by a semicolon symbol(;).

Now my task is i want to give a tab space in between two fields hi and programmers

main()
{
   printf("hi\tprogrammers");/
}

output:
hi    programmers.

\t has a special meaning in c which gives one tab space when it is encountered by a C compiler 

now my task is i want to print hi in one line and programmers in second line

main()
{
  printf("hi");
printf("\n programmers");
}
output:
  hi
  programmers

\n has a special meaning in c which moves cursor to next line and prints next word or line in next line  when it is encountered by a C compiler 



you may get doubt why i cant print two words in two lines using two printf() statements without using \n symbol. let me explain what happens
main()
{
 printf("hi");
printf("programmers");
}

output:
hiprogrammers

what happens here is after printing first word hi then cursor is at location immediately after hi. so when second printf() function is executed then second word programmers printed immediately after hi.

note:   \t is used for allocating one tab space between two words.
           \n is used to move the cursor next line and prints other lines.
           \a is used to give a beep sound  
            let us compare two things for example in word document and C editor
            

                         action                                   command or key                                  command or key

                                                                      (MSWORD)                                            (C output )
                         to move to new line              press enter key                                             \n
                        to give one tab space             press Tab key                                               \t

lets close our session with this program

main()
{
  printf("hi");
  printf("\n hello\t programmers");
}

output

hi
hello    programmers


Any queries or comments?
(c programming, c programming variable intialisation, datatypes in c, integer data types in c, float data types in c,int,float,char,data range, decision making, looping, entry control looops, exit control loops, for,while,do while , if else , nested if , switch, functions, recursive functions, nested loops, tower of hanoi, pointers, strcutres , unions , enum, files in c, binar files, random access in files, fseek,ftell, rewind, copy of one file to other file, merging, continue, break, goto, realtional operators, bitwise operators, logical operators, shift operators procedural languages.
back slash characters,printf statement.