C - Define two variables of type int

Introduction

Consider the following code

Demo

// Using more variables
#include <stdio.h>

int main(void)
{
      int bugs;                 // Declare a variable called bugs
      int beginners;            // and a variable called beginners

      bugs = 7;                 // Store 7 in the variable bugs
      beginners = 7;            // Store 7 in the variable beginners

      // Display some output
      printf("%d beginners for %d bugs\n", beginners, bugs);
      return 0;// ww w .ja  v a  2s.c om
}

Result

You first declare two variables, beginners and bugs, with these statements:

int bugs;                       // Declare a variable called bugs
int beginners;                  // and a variable called beginners

You specify both variables as type int so they both can only store integer values.

They have been declared in separate statements.

Because they are both of the same type, you could have saved a line of code and declared them together like this:

int bugs, beginners;

When you declare several variables of a given type in one statement, the variable names following the data type are separated by commas, and the whole line ends with a semicolon.

int bugs,         // Declare a variable called bugs
    beginners;    // and a variable called beginners

The next two statements assign the same value, 7, to each variable:

bugs = 7;           // Store 7 in the variable bugs
beginners = 7;      // Store 7 in the variable beginners

The next statement calls the printf() function.

The first argument is a control string that will display a line of text.

%d conversion specifiers within the control string will be replaced by the values stored in the variables, beginners and bugs:

printf("%d beginners for %d bugs\n", beginners, bugs);

Related Topic