c variable values code example

Example 1: variable parameters in c

#include <stdio.h>
#include <stdarg.h>

double average(int num,...) {

   va_list valist;
   double sum = 0.0;
   int i;

   /* initialize valist for num number of arguments */
   va_start(valist, num);

   /* access all the arguments assigned to valist */
   for (i = 0; i < num; i++) {
      sum += va_arg(valist, int);
   }
	
   /* clean memory reserved for valist */
   va_end(valist);

   return sum/num;
}

int main() {
   printf("Average of 2, 3, 4, 5 = %f\n", average(4, 2,3,4,5));
   printf("Average of 5, 10, 15 = %f\n", average(3, 5,10,15));
}

Example 2: declaring variables in c

// First declaring and then initializing
int a;
int a = 5;
// Declaring and initializing at one time
int b = 6;

// Basically used datatypes 
// 1. int // integer
int num = 2;
// 2. float // decimal number
float number = 234.87;
// 3. char // character
char ch = 'p';
// 4. char[] // string
char name[] = {'B','i', 'l','l'}
			// or
char fav_fruit[] = "apple"

Tags:

C Example