In this 2nd part of Full C programming language course, we will learn about Variables, Constants and keywords in C language.
If you want to learn programming, you need to know some basic variables first. Variables store data, such as numbers, text, Boolean values (true/false), and objects. You can use them to create calculations and perform operations on those numbers. In this tutorial, we’ll look at what variables are, how they work, and how to declare and initialize them in C.
What Are Variables?
In computer science, a variable is any item that holds a value. These values could be numbers, letters, words, Boolean values (true or false), or even objects. If you have ever worked with spreadsheets before, you probably already have experience with variables. In Excel, for example, cells are considered variables.
When working in C, variables are declared using the keyword “variable” followed by the name of the variable. Variable names should always start with lowercase letters since they are case-sensitive. A variable only stores information; it does not do anything else. To make a change to the variable, you would use assignment statements. Assignments allow you to set the value of a variable.
To declare a variable, use the following statement without single quotes:
`int varName = 10;`
The above code declares a variable named `varName`. In order to assign a value to the variable, use an assignment statement:
`varName = 20;`
A variable can hold different types of values, depending on its type. Let's say you wanted to declare two variables—one to hold integers, and the other to hold characters. To do this, use the following syntax:
`char myCharVariable = 'a'; //declares an integer variable called myCharVariable`
`char myStringVariable = "Hello World!"; //declares a string variable called myStringVariable`
There are three basic types of variables: numeric, character, and Boolean. Each type has its own special characteristics.
Numeric variables work similar to their counterparts in math. Numeric variables store actual numbers. They may represent whole numbers, integers, fractions, decimals, negative numbers, or floating point numbers. You cannot store non-numeric values (such as strings) in a numeric variable.
Character variables are much simpler than numeric ones. They simply hold characters. This means that each character represents one number, just like the digits 0–9. When declaring a character variable, you don't need to specify the size of the variable. It defaults to one byte in length. To declare a character variable, use the following declaration:
`char myCharVariable = 'a';
Variables
In programming languages variables are stored memory locations that hold values. A variable's value can then be changed to store any type of data. In c++, we define variables using "variable name" followed by ":" (colon) and followed by its type. E.g., double x 5; means double x is a variable of type double, holding a value of 5.
Types
Types consist of a set of rules for how each piece of data is interpreted. If you give a program a number, it will probably do something different than if you gave it a string. There are two types of variables, integer and floating point. Integer numbers are whole numbers without decimals (0-255). Floating points numbers have decimal places (0-999), and can go much higher than integers can (I'm not sure what happens after that.)
Values
The value of a variable is whatever data was assigned to it at some point in time. Once a variable has a value, it cannot change until the line where it was declared again. E.g., once variable x equals 0, it cannot be modified to equal 10 unless it is re-assigned. The same goes for strings. You can't modify them once they've been initialized. So if you declare a string s"hello", then write s"world", it won't work because you're trying to assign a new string to a variable that already has one. Instead, you need to use strcpy(new_string,"world") or strcat(currentString," world").
Declaring variables
You can declare variables before assigning them a value, which is called declaring them "before use." When you declare a variable, you tell the compiler that you intend to use it later. To declare a variable, just put it inside parentheses. E.g., int x10; declares a variable named x that holds the number 10.
Rules for naming Variables
Assigning values
Once you declare a variable, it can be given a value. A value is basically a data object. Any data type can be assigned to a variable. E.g., a char, short, float, long, long long, etc... Variable names are case sensitive. One exception to this rule is that you can leave off a variable's type, as long as you know exactly what kind of data you want it to hold. E.g., char *str instead of char*str would mean the pointer variable str holds a character array, i.e. a string. You can only do this when you're pretty sure about the data type you want to assign. On the other hand, simply typing int x; assigns the current value of x to x, and does nothing else.
Assignment operators
An assignment operator takes 2 things, the right side of the assignment and the left side of the assignment. So the statement xy; means take the value y and assign it to the variable x. E.g., x5; means take the value 5 and assign it to x.
Incrementing/Decrementing operators
There are many ways to increase a number, but the most common ones are ++ and --. ++ increments a number by 1, while -- decrements a number by 1.
Constants
Keywords
For example, if, while, auto, struct etc are all keywords in C language. and you cannot use this as the names of variables. since they are already reserved in c language. There are a total of 32 keywords in C language.
Access previous parts of this course by clicking the links below:
0 Comments
If you have any doubts, please let me know