Variables
are used to store and manipulate data. They are named memory locations that can hold different types of information, such as integers, floating-point numbers, characters, or user-defined data structures. Variables must be declared with a data type before use, and their values can be changed throughout the program's execution. Variables are essential for storing and processing data in C, making it a fundamental concept in programming.
int
number
=
22
;
In this example,
number
is a variable which store only interger value. Here, we assigned a value
22
in this variable and we can change this value in entire code.
There are three types for defining the Variable:
1. Variable Declaration
2. Variable Defination
3. Variable Initialisation
int
var
;
// Variable Defination
var
=
22
;
// Variable intialization
int
var
=
22
;
// Variable defination and declaration
Rules for naming Variable
1. Variable names must start with a letter (a-z, A-Z) or an underscore (_).
2. Following the first character, variable names can contain letters, digits (0-9), and underscores.
3. Variable names are case-sensitive, so "myVar" and "myvar" are considered different names.
4. C keywords (reserved words like "int," "if," "while," etc.) cannot be used as variable names.
5. Variable names should be meaningful and descriptive to make the code more readable (e.g., "count" instead of "x").
6. Variable names should not contain spaces or special characters like @, #, $, etc.
7. Names should not be too long or too short; it's a good practice to keep them reasonably concise yet informative.
Here are some valid variable names in C:
int
count
;
Copy Code
long
myvar
;
double
_value
;
double
temperatureReading
;
And here are some invalid variable names:
int
123var
;
// starts with a digit
Copy Code
long
myvar
;
// contains a hyphen
double
_value
;
// a reserved keyword
double
temperatureReading
;
// contains a space
C Comments
Comments are like sticky notes in your code. They don't do anything in your program; they're just there to help you or others understand what's happening. You can use them to explain things, make your code easier to read, or temporarily turn off a piece of code when you're finding and fixing problems. You can write short comments on one line or longer comments that cover multiple lines.
Single line Comments:
1. To make a single-line comment in your code, you simply begin with two forward slashes (//).
2. Anything written after these slashes on the same line is treated as a comment and is not processed or executed by the computer; it's just for human notes and won't affect the program's behavior.
Example to use single line comments in your program
#include
<
stdio.h
>
Copy Code
int
main
()
{
printf
(
"The Royal Coding!
");
return
0;
}
Multi line Comments:
1. When you want to write comments that span multiple lines in your code, you begin with /* and conclude with */.
2. Anything placed between /* and */ is completely disregarded by the compiler; it won't affect the program's execution.
Example to use multi line comments in your program
#include
<
stdio.h
>
Copy Code
int
main
()
{
printf
(
"The Royal Coding!
");
return
0;
}