From the course: Complete Guide to C Programming Foundations

Unlock the full course today

Join today to access over 24,500 courses taught by industry experts.

Declaring variables

Declaring variables

- [Instructor] A variable is a storage container that holds values that can change. The variable must be declared before it's used. It's given a data type and a name. It can be initialized to a given value, though this assignment can come later. Variables are declared in a statement traditionally at the top of the function they're used in as shown here at the main function, lines five through seven. The format is data type followed by the variable name, character variable c, integer variable i, and float variable f. You can declare a variable elsewhere in the code, which works as long as the variable is declared before it's used. At line 17, double variable d is declared. It's used on the following statement at line 18. Using a variable that's not declared generates an error. If I comment out line 17 and then attempt to run the program, you see the error. This is one reason why I prefer to declare variables at the start of a function. The variable name must start with a letter and can…

Contents