Παραδείγματα προγραμμάτων 


Παραδείγματα με την συνάρτηση printf():


1. H printf() με int και float. 2. Η printf () με τον τελεστή αύξησης

#include <stdio.h>

main()
{
   int index;
   float num;

  index = 13;
  printf ("The value of index is: %d\n",index);
  num = 27.567;
  printf ("The value of num is: %f\n",num);
  printf ("Both index and num:\n");

  printf ("index -> %d and num -> %f \n",index,num);

}

#include <stdio.h>
main()
{
   int a=100;
   int aplus, plusa;

   aplus = a++;
   plusa = ++a;
   printf ("a :%d\n", a);
   printf ("aplus :%d\n", aplus);
   printf ("plusa :%d\n", plusa);
}

3. Παίζοντας με την printf (). Δοκιμάστε διάφορους προσδιοριστές 4.   printf () και ολίσθηση
#include <stdio.h>
 main () 
{
  int i = 123;
  double f = 3.1415926535;

  printf ("i = %i\n", i);
  printf ("i = %o\n", i);
  printf ("i = %x\n", i);
  printf ("i = %X\n", i);
  printf ("i = %+i\n", i);
  printf ("i = %8i\n", i);
  printf ("i = %08i\n", i);
  printf ("i = %+08i\n", i);
  printf ("f = %f\n", f);
  printf ("f = %10.3f\n", f);
  printf ("f = %+10.3f\n", f);
  printf ("f = %g\n", f);
  printf ("f = %10.6g\n", f);
  printf ("f = %10.6e\n", f);
}
#include <stdio.h>
main()
{
    unsigned int ch = 'F';     /* F: 01000110 */
    unsigned int res;

   printf("to ch as char: %c\n", ch);
   printf("to ch as int:%d\n", ch);
   res = ch << 1;               /* ολίσθηση αριστερά κατά 1 */
   printf("to res as int: %d\n", res);

}

 


Παραδείγματα με την συνάρτηση scanf():


 Διαβάζοντας με την scanf()

#include <stdio.h>

main()
{
int index;
float num;


printf (" Give me the integer index: ");
scanf("%d", &index);
printf (" Give me the integer index: ");
scanf("%d", &index);

printf("The value of index is %d\n",index);
num = 27.567;
printf("The value of num is %f\n",num);
}
 


Παραδείγματα με εντολές ελέγχου:


   

#include <stdio.h>
main()
{
  int x;
  int y;

  printf("Enter first number: ");
  scanf("%d", &x);
  printf("\nEnter second number: ");
  scanf("%d", &y);

/* Start to print the results */

  printf("\n%d + %d = %d", x, y, (x+y));
  printf("\n%d - %d = %d", x, y, (x-y));
  printf("\n%d * %d = %d", x, y, (x*y));

  if (y = = 0)
       printf("\nWarning: dividing by zero gets infinite");
  else
       printf("\n%d / %d = %d", x, y, (x/y));
}
 


 

int x,y,j,k,z,q;

.......

if ( (x= =y) && (j>k) ) 
        z=1;
else
        q=1;

 


 

 



Παραδείγματα με εντολές επανάληψης:


 /*for - if & if-else statements */
#include <stdio.h>
  main()
  {
    int data;

    for (data = 0; data < 10; data++)
    {
         if (data = = 2)
         printf("Data is now equal to %d\n",data);
         if (data < 5)
               printf("Data is now %d, which is less than 5\n",data);
         else
               printf("Data is now %d, which is greater than 4\n",data);
   }  /* end of for loop */
}

 

/*  "while" - loop */ 
#include <stdio.h>
 main()
 { 
     int count; 
    count = 0; 
    while (count < 6) 
    { 
        printf ("The value of count is %d\n", count); 
        count = count + 1; 
    } 
 }


/*  do-while loop */ 
#include <stdio.h> 
main() 

   int i; 
    i = 0; 
    do 
   { 
       printf("The value of i is now %d\n",i); 
       i = i + 1; 
     } 
    while (i < 5); 
}


/*  for loop */
#include <stdio.h>

main()
{
int index;

   for (index = 0; index < 6; index++)
     printf ("The value of the index is %d\n",index);
}


#include <stdio.h>
main()
{
int xx;
 
   for (xx = 5; xx < 15; xx++){
      if (xx == 8)
         break;
         printf("In the break loop, xx is now %d\n",xx);
   }
 
   for(xx = 5;xx < 15;xx = xx + 1){
      if (xx == 8)
         continue;
      printf("In the continue loop, xx is now %d\n",xx);
   }
}


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


#include <stdio.h>
main()
{
int dog,cat,pig;

   goto real_start;

   some_where:
   printf("This is another line of the mess.\n");
   goto stop_it;

/* the following section is the only section with a useable goto */
   real_start:
   for(dog = 1;dog < 6;dog = dog + 1) {
      for(cat = 1;cat < 6;cat = cat + 1) {
         for(pig = 1;pig < 4;pig = pig + 1) {
            printf("Dog = %d  Cat = %d  Pig = %d\n",dog,cat,pig);
            if ((dog + cat + pig) > 8 ) goto enough;
         };
      };
   };
   enough: printf("Those are enough animals for now.\n");
/* this is the end of the section with a useable goto statement */

   printf("\nThis is the first line out of the spaghetti code.\n");
   goto there;

   where:
   printf("This is the third line of spaghetti.\n");
   goto some_where;

   there:
   printf("This is the second line of the spaghetti code.\n");
   goto where;

   stop_it:
   printf("This is the last line of this mess.\n");

}


/*************************************************************************/
/* Δημιουργεί έναν πίνακα αντιστοιχίας μεταξύ βαθμών Farenheit και Κελσίου     */
/*  με βήματα των 10 βαθμών. Επίσης υποδεικνύει τα σημεία βρασμού και τήξης  */
/*************************************************************************/
#include <stdio.h>

main()
{
int count;           /* μεταβλτή για την επανάληψη                 */
int farenheit;     /* μεταβλητή για τους βαθμούς Farenheit  */
int centigrade;   /* μεταβλητή για τους βαθμούς Κελσίου  */

   printf("Centigrade to Farenheit temperature table\n\n");

   for(count = -2;count <= 12;count = count + 1){
      centigrade = 10 * count; 
      farenheit = 32 + (centigrade * 9)/5;
      printf("  C =%4d   F =%4d  ",centigrade,farenheit);
      if (centigrade == 0)
         printf(" Freezing point of water");
      if (centigrade == 100)
         printf(" Boiling point of water");
      printf("\n");
   } /* end of for loop */
}


 #include <stdio.h>
main()
{
int truck;

   for (truck = 3;truck < 13;truck = truck + 1) {

      switch (truck) {
         case 3  : printf("The value is three\n");
                   break;
         case 4  : printf("The value is four\n");
                   break;
         case 5  :
         case 6  :
         case 7  :
         case 8  : printf("The value is between 5 and 8\n");
                   break;
         case 11 : printf("The value is eleven\n");
                   break;
         default : printf("It is one of the undefined values\n");
                   break;
      } /* end of switch */

   } /* end of for loop */
}

 

#include <stdio.h>

float scores[10];
float total = 0;
int counter;

main()
{
for (counter = 1; counter < 10; counter++)
   {
      printf("Enter the score of student %d: ", counter);
      scanf("%f", &scores[counter]);
       /* Add the current score to the total */
       total +="scores[counter];"
    }
for (counter = 1 counter < 10; counter++)
 {
    printf("The score of student %d is %lf", counter, scores[counter]);
 }
printf("The total score is %lf", total);
printf("The average score is %lf", (total / 10));
}
Στην προηγούμενη ενότητα

Στην αρχική σελίδα