What Is C?: Rules To Name A Variable
What Is C?: Rules To Name A Variable
C is a high-level programming language that was first developed by Dennis Ritchie at Bell Labs in the
early 1970s. Unix was one of the first operating systems to be written in C. Microsoft Windows, Mac OS
X, and GNU/Linux are also written in C. Lots of other high-level languages like Perl, PHP, Python, R,
Matlab, Mathematica, etc, are written in C.
Currently (as of August, 2018) C is #2 in popularity according to the TIOBE (The Importance Of Being
Earnest) index.
Variables in C Language
When we want to store any information(data) on our computer/laptop, we store it in the
computer's memory space. Instead of remembering the complex address of that memory space
where we have stored our data, our operating system provides us with an option to create
folders, name them, so that it becomes easier for us to find it and access it.
Similarly, in C language, when we want to use some data value in our program, we can store it
in a memory space and name the memory space so that it becomes easier to access it.
The naming of an address is known as variable. Variable is the name of memory location. Unlike
constant, variables are changeable, we can change value of a variable during execution of a
program. A programmer can choose a meaningful variable name. Example : average, height,
age, total etc.
Datatype of Variable
A variable in C language must be given a type, which defines what type of data the variable will
hold.
It can be:
char: Can hold/store a character in it.
int: Used to hold an integer.
float: Used to hold a float value.
double: Used to hold a double value.
void
Rules to name a Variable
1. Variable name must not start with a digit.
2. Variable name can consist of alphabets, digits and special symbols like underscore _.
3. Blank or spaces are not allowed in variable name.
4. Keywords are not allowed as variable name.
5. Upper and lower case names are treated as different, as C is case-sensitive, so it is
suggested to keep the variable names in lower case.
Defining a variable means the compiler has to now assign a storage to the variable because it
will be used in the program.
To define a function we must provide the datatype and the variable name. We can even define
multiple variables of same datatype in a single line by using comma to separate them.
Initializing a variable means to provide it with a value. A variable can be initialized and defined
in a single statement.
A variable is a named memory location to store data which is used in the program.
Here is a list of reserved keywords in C that cannot be used as variable names:
_Bool default if sizeof while short for const register union
Type Qualifiers
There are also qualifiers short, long, signed and unsigned, that can be applied to these basic
types.
Qualifier Size (bytes) Size (bits)
So when a variable is signed, it can take on negative values, and half of it's total range is spread
below zero, and the other half above zero.
A signed int can take on values between -2,147,483,648 and +2,147,483,648. If we want to be
able to represent integers larger than +2,147,483,648 then we can either use more bits (e.g. by
using a long int), or by forcing all 32 bits of our int to be used on the positive side of zero.
An unsigned int (4 bytes or 32 bits) can take on values between 0 and 4,294,967,295.
#include <stdio.h>
Operators in C
Operators are the foundation of any programming language. Thus the functionality of C/C++
programming language is incomplete without the use of operators. We can define operators as
symbols that helps us to perform specific mathematical and logical computations on operands.
In other words we can say that an operator operates the operands.
For example, consider the below statement:
c=a+b
Here, ‘+’ is the operator known as addition operator and ‘a’ and ‘b’ are operands. The addition
operator tells the compiler to add both of the operands ‘a’ and ‘b’. C/C++ has many built-in
operator types and they can be classified as:
Arithmetic Operators: These are the operators used to perform arithmetic/mathematical
operations on operands. Examples: (+, -, *, /, %,++,–).
Arithmetic operator are of two types:
1. Unary Operators: Operators that operates or works with a single operand are unary
operators.
For example: (++ , –)
2. Binary Operators: Operators that operates or works with two operands are binary
operators.For example: (+ , – , * , /)
Relational Operators: Relational operators are used for comparison of the values of two
operands. For example: checking if one operand is equal to the other operand or not, an
operand is greater than the other operand or not etc. Some of the relational operators are
(==, > , = , <= ).
Logical Operators: Logical Operators are used to combine two or more
conditions/constraints or to complement the evaluation of the original condition in
consideration. The result of the operation of a logical operator is a boolean value either
true or false.
Bitwise Operators: The Bitwise operators is used to perform bit-level operations on the
operands. The operators are first converted to bit-level and then calculation is performed
on the operands. The mathematical operations such as addition , subtraction ,
multiplication etc. can be performed at bit-level for faster processing.
Assignment Operators: Assignment operators are used to assign value to a variable. The
left side operand of the assignment operator is a variable and right side operand of the
assignment operator is a value. The value on the right side must be of the same data-type
of variable on the left side otherwise the compiler will raise an error.
Different types of assignment operators are shown below:
“=”: This is the simplest assignment operator. This operator is used to assign the value
on the right to the variable on the left.
For example:
a = 10;
b = 20;
ch = 'y';
“+=”:This operator is combination of ‘+’ and ‘=’ operators. This operator first adds the
current value of the variable on left to the value on right and then assigns the result to
the variable on the left.
Example:
(a += b) can be written as (a = a + b)
If initially value stored in a is 5. Then (a += 6) = 11.
“-=”:This operator is combination of ‘-‘ and ‘=’ operators. This operator first subtracts
the current value of the variable on left from the value on right and then assigns the
result to the variable on the left.
Example:
(a -= b) can be written as (a = a - b)
If initially value stored in a is 8. Then (a -= 6) = 2.
“*=”:This operator is combination of ‘*’ and ‘=’ operators. This operator first multiplies
the current value of the variable on left to the value on right and then assigns the result
to the variable on the left.
Example:
(a *= b) can be written as (a = a * b)
If initially value stored in a is 5. Then (a *= 6) = 30.
“/=”:This operator is combination of ‘/’ and ‘=’ operators. This operator first divides the
current value of the variable on left by the value on right and then assigns the result to
the variable on the left.
Example:
(a /= b) can be written as (a = a / b)
If initially value stored in a is 6. Then (a /= 2) = 3.
Other Operators: Apart from the above operators there are some other operators
available in C or C++ used to perform some specific task. Some of them are discussed
here:
sizeof operator: sizeof is a much used in the C/C++ programming language. It is a
compile time unary operator which can be used to compute the size of its
operand. The result of sizeof is of unsigned integral type which is usually
denoted by size_t. Basically, sizeof operator is used to compute the size of the
variable. To learn about sizeof operator in details you may visit this link.
Comma Operator: The comma operator (represented by the token ,) is a binary
operator that evaluates its first operand and discards the result, it then evaluates
the second operand and returns this value (and type). The comma operator has
the lowest precedence of any C operator. Comma acts as both operator and
separator. To learn about comma in details visit this link.
Conditional Operator: Conditional operator is of the form Expression1 ?
Expression2 : Expression3 . Here, Expression1 is the condition to be evaluated. If
the condition(Expression1) is True then we will execute and return the result of
Expression2 otherwise if the condition(Expression1) is false then we will execute
and return the result of Expression3. We may replace the use of if..else
statements by conditional operators.
Type Conversions
There are two kinds of type conversion we need to talk about: automatic or implicit type
conversion and explicit type conversion.
Implicit Type Conversion
The operators we have looked at can deal with different types. For example we can apply the
addition operator + to an int as well as a double. It is important to understand how operators
deal with different types that appear in the same expression. There are rules in C that govern
how operators convert different types, to evaluate the results of expressions.
For example, when a floating-point number is assigned to an integer value in C, the decimal
portion of the number gets truncated. On the other hand, when an integer value is assigned to
a floating-point variable, the decimal is assumed as .0.
This sort of implicit or automatic conversion can produce nasty bugs that are difficult to find,
especially for example when performing multiplication or division using mixed types, e.g.
integer and floating-point values. Here is some example code illustrating some of these effects:
#include <stdio.h>
int main()
{
int a = 2;
double b = 3.5;
double c = a * b;
double d = a / b;
int e = a * b;
int f = a / b;
printf("a=%d, b=%.3f, c=%.3f, d=%.3f, e=%d, f=%d\n", a, b, c, d, e, f);
return 0;
}
#include <stdio.h>
#include <stdio.h>
int main()
{
int a = 2;
int b = 3;
printf("a / b = %.3f\n", a/b);
printf("a / b = %.3f\n", (double) a/b);
return 0;
}
String Conversion Library Functions
There are some built-in library functions in C to perform some basic conversions between
strings and numeric types. Two useful functions to know about convert ascii strings to numeric
types: atoi() (ascii to integer) and atof() (ascii to floating-point). We need to #include the
library stdlib.h in order to use these functions.
To convert from numeric types to strings things are a bit more difficult. First we have to allocate
space in memory to store the string. Then we use the sprintf()built-in function to "print" the
numeric type into our string.
Here is some example code (typeConvert.c) illustrating conversion of strings to numerics, and
vice-versa:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char intString[] = "1234";
char floatString[] = "328.4";
int myInt = atoi(intString);
double myDouble = atof(floatString);
printf("intString=%s, floatString=%s\n", intString, floatString);
printf("myInt=%d, myDouble=%.1f\n\n", myInt, myDouble);
int a = 2;
double b = 3.14;
char myString1[64], myString2[64];
sprintf(myString1, "%d", a);
sprintf(myString2, "%.2f", b);
printf("a=%d, b=%.2f\n", a, b);
printf("myString1=%s, myString2=%s", myString1, myString2);
return 0;
}
intString=1234, floatString=328.4
myInt=1234, myDouble=328.4
a=2, b=3.14
myString1=2, myString2=3.14