Format Specifiers

Format specifiers are used together with the printf() function to tell the compiler what type of data the variable is storing. It is basically a placeholder for the variable value.

A format specifier starts with a percentage sign %, followed by a character.

For example, to output the value of an int variable, you must use the format specifier %d or %i surrounded by double quotes, inside the printf() function:

    
        #include<stdio.h>

            int main() {
            int myNum = 15;
            printf("%d", myNum);
            return 0;
            }
    

To print other types, use %c for char and %f for float:

    
        #include<stdio.h>

        int main() {
            // Create variables
            int myNum = 5; // Integer (whole number)
            float myFloatNum = 5.99; // Floating point number
            char myLetter = 'D'; // Character

            // Print variables
            printf("%d\n", myNum);
            printf("%f\n", myFloatNum);
            printf("%c\n", myLetter);
            return 0;
        }
    

To combine both text and a variable, separate them with a comma inside the printf() function:

    
        #include <stdio.h>

        int main() {
            int myNum = 5;
            printf("My favorite number is: %d", myNum);
            return 0;
        }
    

To print different types in a single printf() function, you can use the following:

    
        #include<stdio.h>
        int main() {
            int myNum = 5;
            char myLetter = 'D';
            printf("My number is %d and my letter is %c", myNum, myLetter);
            return 0;
        }
    
Add Variables Together

To add a variable to another variable, you can use the + operator:

    
        #include<stdio.h>
        int main() {
            int x = 5;
            int y = 6;
            int sum = x + y;
            printf("%d", sum);
            return 0;
        }
    

Declare Multiple Variables

To declare more than one variable of the same type, use a comma-separated list:

    
        #include<stdio.h>
        int main() {
            int x = 5, y = 6, z = 50;
            printf("%d", x + y + z);
            return 0;
        }
    

You can also assign the same value to multiple variables of the same type:

    
        #include<stdio.h>
        int main() {
            int x, y, z;
            x = y = z = 50;
            printf("%d", x + y + z);
            return 0;
        }
    

C Variable Names

All C variables must be identified with unique names.

These unique names are called identifiers.

Identifiers can be short names (like x and y) or more descriptive names (age, sum, totalVolume).

Note: It is recommended to use descriptive names in order to create understandable and maintainable code:

    
        // Good
        int minutesPerHour = 60;
        
        // OK, but not so easy to understand what m actually is
        int m = 60;
    

The general rules for naming variables are:

  • Names can contain letters, digits and underscores
  • Names must begin with a letter or an underscore (_)
  • Names are case sensitive (myVar and myvar are different variables)
  • Names cannot contain whitespaces or special characters like !, #, %, etc.
  • Reserved words (such as int) cannot be used as names