0% found this document useful (0 votes)
62 views

What Is C?: Rules To Name A Variable

C is a high-level programming language developed in the early 1970s. It was used to write early operating systems like Unix and is still widely used today to write operating systems, applications, and other programming languages. Variables in C allow storing and naming data in memory locations. Variables must be declared with a data type like int, char, float, etc. Variables are then defined, initialized, and can be used in a C program. Operators in C perform arithmetic, relational, logical, and other operations on variables and values. Common operators include +, -, *, /, ==, >, &, | and assignment operators like =, +=, -=, etc.

Uploaded by

Shriniv
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

What Is C?: Rules To Name A Variable

C is a high-level programming language developed in the early 1970s. It was used to write early operating systems like Unix and is still widely used today to write operating systems, applications, and other programming languages. Variables in C allow storing and naming data in memory locations. Variables must be declared with a data type like int, char, float, etc. Variables are then defined, initialized, and can be used in a C program. Operators in C perform arithmetic, relational, logical, and other operations on variables and values. Common operators include +, -, *, /, ==, >, &, | and assignment operators like =, +=, -=, etc.

Uploaded by

Shriniv
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

What is C?

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.

Declaring, Defining and Initializing a variable


Declaration of variables must be done before they are used in the program. Declaration does
the following things.
1. It tells the compiler what the variable name is.
2. It specifies what type of data the variable will hold.
3. Until the variable is defined the compiler doesn't have to worry about allocating memory
space to the variable.
4. Declaration is more like informing the compiler that there exist a variable with following
datatype which is used in the program.

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

_Complex do inline static continue void case unsigned typedef

_Imaginary double int struct goto char extern break volatile

auto else long switch signed float restrict enum return


Data Types and Sizes
There are four basic data types in C, their meaning, and their size:
Type Meaning Size (bytes) Size (bits)

char a single byte, capable of holding one character 1 byte 8 bits

int an integer 4 bytes 32 bits

float single-precision floating point number 4 bytes 32 bits

double double-precision floating point number 8 bytes 64 bits

Type Qualifiers
There are also qualifiers short, long, signed and unsigned, that can be applied to these basic
types.
Qualifier Size (bytes) Size (bits)

short int 2 bytes 16 bits

long int 8 bytes 64 bits

long double 12 bytes 96 bits


We have been talking about variable types and how many bytes they take up in memory. An
important quantity to know about is that one byte is made up of 8 bits. One bit can take on two
possible values: 0 or 1. An unsigned 8-bit variable can take on values between 0 and (2^{8})-1 =
255. A signed 8-bit variable can take on values between -127 and +127.

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.

How many bytes on your machine?


Here is a small C program that will print out the size of some basic C types on your machine.
Enter it into your source code editor, and save it to a file called my_types.c

#include <stdio.h>

int main(int argc, char *argv[])


{
printf("a char is %ld bytes\n", sizeof(char));
printf("an int is %ld bytes\n", sizeof(int));
printf("an float is %ld bytes\n", sizeof(float));
printf("a double is %ld bytes\n", sizeof(double));
printf("a short int is %ld bytes\n", sizeof(short int));
printf("a long int is %ld bytes\n", sizeof(long int));
printf("a long double is %ld bytes\n", sizeof(long double));
return 0;
}

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;
}

Explicit Type Conversion


Type Casting
There is a mechanism in C to perform type casting, that is to force an expression to be
converted to a particular type of our choosing. We surround the desired type in brackets and
place that just before the expression to be coerced. Look at the following example code:

#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

You might also like