C Character Data Types

The char Type

The char data type is used to store a single character.

The character must be surrounded by single quotes, like ‘A’ or ‘c’, and we use the %c format specifier to print it: Continue reading C Character Data Types

C Data Types

Data Types

As explained in the Variables article, a variable in C must be a specified data type, and you must use a format specifier inside the printf() function to display it:

Example

// 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);

Continue reading C Data Types

C Variables – Examples

Real-Life Example

Often in our examples, we simplify variable names to match their data type (myInt or myNum for int types, myChar for char types, and so on). This is done to avoid confusion. Continue reading C Variables – Examples

C Variable Names (Identifiers)

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). Continue reading C Variable Names (Identifiers)

C Declare Multiple Variables

Declare Multiple Variables

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

Example

int x = 5, y = 6, z = 50;
printf("%d", x + y + z);

Continue reading C Declare Multiple Variables

C Variable Values

Change Variable Values

If you assign a new value to an existing variable, it will overwrite the previous value:

Example

int myNum = 15;  // myNum is 15
myNum = 10;  // Now myNum is 10

You can also assign the value of one variable to another:

Example

int myNum = 15;

int myOtherNum = 23;

// Assign the value of myOtherNum (23) to myNum
myNum = myOtherNum;

// myNum is now 23, instead of 15
printf("%d", myNum);

Continue reading C Variable Values

C Format Specifiers

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. Continue reading C Format Specifiers

C Variables

Variables are containers for storing data values, like numbers and characters.

In C, there are different types of variables (defined with different keywords), for example:

  • int – stores integers (whole numbers), without decimals, such as 123 or -123
  • float – stores floating point numbers, with decimals, such as 19.99 or -19.99
  • char – stores single characters, such as 'a' or 'B'. Characters are surrounded by single quotes

Continue reading C Variables

C Comments

Comments in C

Comments can be used to explain code, and to make it more readable. It can also be used to prevent execution when testing alternative code.

Comments can be singled-lined or multi-lined.

Continue reading C Comments

C New Lines

New Lines

To insert a new line, you can use the \n character:

Example

#include <stdio.h>

int main() {
  printf("Hello World!\n");
  printf("I am learning C.");
  return 0;
}

You can also output multiple lines with a single printf() function. However, this could make the code harder to read: Continue reading C New Lines