Logo
Unit 1 – C Basics - Data Types and Variables

C Basics - Data Types and Variables

Duration: 5 minutes

Hello, future C programmers!

After our initial “Hello” to C, it’s time to delve into the basics. In this tutorial, we will focus on data types and variables which are very two fundamental concepts in C programming.

Understanding Variables and Data Types

In C, a variable is a storage location with an associated symbolic name (an identifier) that contains some known or unknown quantity of information. Let’s establish some guidelines for naming variables:

Variable names must begin with a letter or an underscore (_). Remember, that they cannot start with a number. A few more pointers to note:

  • Variables can consist of letters, numbers, and underscores.
  • Variable names are always case-sensitive, so keep a strict eye out for those.
  • Reserved words (keywords) in C, such as “for,” “while,” “if,” “else,” “int,” “float,” etc., cannot be used as variable names.

C supports several data types, but today we’ll focus on the basics:

Data TypesDescriptionExample
’int’Integer data type for whole numbers without a decimal point.1, 2, 10, -15
’float’Floating-point data type for real numbers that can have a decimal point.3.14, -0.01, 8.76, 5.0
’char’Character data type for single characters.‘a’, ‘1’, ’$'
'bool’Boolean data type for representing True or False values.1 (True), 0 (False)

Writing Your First Variables

Let’s write some C code to get a hang of the different types of variables:

#include
int main() {
// an integer
int num = 10;
printf("%d\n", num);
// a float
float pi_value = 3.14;
printf("%f\n", pi_value);
// a character
char greeting[] = "Hello, C!";
printf("%s\n", greeting);
// a boolean (using integers 1 for True, 0 for False)
int is_c_fun = 1;
printf("%d\n", is_c_fun);
return 0;
}

Here, num, pi_value, greeting, and is_c_fun are variable names, and 10, 3.14, “Hello, C!”, and 1 are their respective values. While, the printf function is used to display the value of each variable.

Assignment

Now it’s your turn. Create four variables in C: a Boolean (using integers), a string (array of characters), an integer, and a float. Assign values to them and display the results using the printf function.

Conclusion

Congratulations on completing this module! You’ve gained a solid understanding of declaring and working with various types of variables in C. As we progress, we’ll explore more advanced concepts and build upon these fundamentals. Happy coding!

Next Tutorial: C Basics: Operators

5 minutes Minutes

Continue

Code on the Go with our Mobile App!

Unleash your coding potential anytime, anywhere!

Download Now!