In this section we are going to provide C constants, Data type in C, Modifiers, Variables, Operators with examples etc .
Previous | Home | Next |
C constants
Constants also called a literals. Constant refer to fixed values that the program may not alter. constant can be any of the basic data types. C have a two type of constant:
- Numeric constant
- Character constant
C constant is showing in the figure:
Example of the backslash constant
#include<stdio.h> #include<conio.h> Void main() { int a=12; printf(“\n %d\’ \” \?”,a); getch(); }
Output of the program:
Data type in C
A data type in a programming language is a set of data with values having predefined characteristics.In c we write any program that have a variables . that variables have a some values. But that value will be which type we don’t know , so that always we define by the data type.
The definition of a variable will assign storage for the variable and define the type of data that will be held in the location. Types of data type: data type types is following given bellow:
Primary data type | pointer data type | structure data type | user defined data type |
integer | array | enumerated | |
float | string | typedef | |
Double | structure | ||
Long float | union |
int - data type int is used to define integer numbers.
Example
{ int Count; Count = 5; }
float - data type float is used to define floating point numbers.
Example
{ float Miles; Miles = 5.6; }
double - data type double is used to define BIG floating point numbers. It reserves twice the storage for the number. On PCs this is likely to be 8 bytes.
Example
{ double Atoms; Atoms = 2500000; }
char - data type char defines characters.
Example
{ char Letter; Letter = 'x'; }
Modifiers
Folllowing are the Modifiers that is used by the above Data types
- short
- long
- signed
- unsigned
The modifiers define the amount of storage allocated to the variable. The amount of storage allocated is not cast in stone. ANSI has the following rules:
short int <= int <= long int float <= double <= long double
What this means is that a 'short int' should assign less than or the same amount of storage as an 'int' and the 'int' should be less or the same bytes than a 'long int'. What this means in the real world is:
Type | Bytes | Bits | Range | |
short int | 2 | 16 | -32,768 -> +32,767 | (16Kb) |
unsigned short int | 2 | 16 | 0 -> +65,535 | ( 32Gb) |
unsigned int | 4 | 16 | 0->+4,294,967,295 | ( 4Gb) |
int | 4 | 32 | -2,147,483,648->+2,147,483,647 | ( 2Gb) |
long int | 4 | 32 | -2,147,483,648->+2,147,483,647 | ( 2Gb) |
signed char | 1 | 8 | -128 -> +127 | |
unsigned char | 1 | 8 | 0 -> +255 | |
float | 4 | 32 | ||
double | 8 | 64 | ||
long double | 12 | 96 |
Variables
A variable is a meaningful name of data storage location in computer memory. When using a variable you refer to memory address of computer.
Types of variables
In C variable are divided in two types
-
Global variable: global variable is a variable defined outside any function block. Global variable is also called external variable.
-
Local variable: local variable is a variable defined inside a function block. Local variable is also called automatic variables.
Variable Declaration Examples
Single declarations
int age; float amount; char last;
Multiple declarationsYou can repeat the the declaration line with a different variable on each line .
int age; int hrNumber;
But the normal way is to separate the variables by a comma. Although not required the names are easier to read if a space follows the comma.
int age, HouseNumber, quality; float distance, Discount; char first, second;
Scope of variables
The scope of a variable depends on where it is declared.
- The area of the program where that variable is valid, i.e. the parts of the program that have access to that variable. This is determined by the location of the declaration.
- The life span of that variable, i.e. the length of time that the variable remains in memory.
Where do you Declare Variables
Variables can be declared as
- Prior to the start of the main( ) function.
- Within the main function, after the opening {
- Within a function after the opening {
- Within a block of code, after the {
- Global or external variable Declared before the start of the main( ) function.
Static Variable
When static is used with a global variable it indicates that the variable is local to the current file. If the programmer would like the value of the variable to be remembered when the function is revisited then that variable must be declared as static.
Example
static int aVariable = 1;
In the example the initialisation to 1 occurs only on the first execution of this line of code, it is disregarded on further executions of the line.
static has a different meaning when used with global variables.
Operators in C
An operator is a symbol which helps the user give the command to computer, and computer to do a certain mathematical or logical manipulations. Operators are used in C language program to operate on data and variables.
C has a rich set of operators which can be classified as:
- Arithmetic operator
- Relational Operators
- Logical Operators
- Assignment Operators
- Increments and Decrement Operators
- Conditional Operators
- Bitwise Operators
- Special Operators
Arithmetic Operators
Arithmetic operators means do the all arithmetic operations like as addition, subtraction, multiplication , division, modules etc. All the operators have almost the same meaning as in other languages. Both unary and binary operations are available in C language. Unary operations operate on a singe operand, therefore the number 5 when operated by unary – will have the value –5.
Arithmetic Operators
Operator | Meaning |
+ | Addition or Unary Plus |
– | Subtraction or Unary Minus |
* | Multiplication |
/ | Division |
% | Modulus Operator |
Example
#include<stdio.h>//include header file stdio.h void main()//tell the compiler the start of the program { int numb1, num2, sum, sub, mul, div, mod;//declaration of variables scanf (“%d %d”, &num1, &num2); //inputs the operands sum = num1+num2; //addition of numbers and storing in sum. printf(“\n Thu sum is = %d”, sum);//display the output sub = num1-num2;//subtraction of numbers and storing in sub. printf(“\n Thu difference is = %d”, sub); //display the output mul = num1*num2;//multiplication of numbers and storing in mul. printf(“\n Thu product is = %d”, mul); //display the output div = num1/num2;//division of numbers and storing in div. printf(“\n Thu division is = %d”, div); //display the output mod = num1%num2;//modulus of numbers and storing in mod. printf(“\n Thu modulus is = %d”, mod); //display the output }
Integer Arithmetic
When an arithmetic operation is performed on two whole numbers or integers than such an operation is called as integer arithmetic. It always gives an integer as the result.
Let x = 20 and y = 5 be 2 integer numbers.
Then the integer operation leads to the following results.
x+y=27 x–y=15 x*y=100 x%y=0 x/y=4
In integer division the fractional part is truncated.
Floating point arithmetic
When an arithmetic operation is preformed on two real numbers or fraction numbers such an operation is called floating point arithmetic.
The floating point results can be truncated according to the properties requirement.
The remainder operator is not applicable for floating point arithmetic operands.
Let x=14.0 and y=4.0 then x+y=18.0 x–y=10.0 x*y=56.0 x/y=3.50
Mixed mode arithmetic
When one of the operand is real and other is an integer and if the arithmetic operation is carried out on these 2 operands then it is called as mixed mode arithmetic.
If any one operand is of real type then the result will always be real thus 15/10.0 = 1.5
Relational Operators
Often it is required to compare the relationship between operands and bring out a decision and program accordingly. This is when the relational operator come into picture.
C supports the following relational operators.
Operator | Meaning |
< | is less than |
<= | is less than or equal to |
> | is greater than |
>= | is greater than or equal to |
== | is equal to |
!= | is not equal to |
It is required to compare the marks of 2 students, salary of 2 persons, we can compare them using relational operators. A simple relational expression contains only one relational operator and takes the following form.
exp1 relational operator exp2 : Where exp1 and exp2 are expressions, which may be simple constants, variables or combination of them. Given below is a list of examples of relational expressions and evaluated values.
6.5 <= 25 TRUE -65 > 0 FALSE 10 < 7 + 5 TRUE
Relational expressions are used in decision making statements of C language such as if, while and for statements to decide the course of action of a running program.
Logical Operators
C has the following logical operators, they compare or evaluate logical and relational expressions.
Operator | Meaning |
&& | Logical AND |
|| | Logical OR |
! | Logical NOT |
Logical AND (&&) :
This operator is used to evaluate 2 conditions or expressions with relational operators simultaneously. If both the expressions to the left and to the right of the logical operator is true then the whole compound expression is true.
Example
a>b && x==10
The expression to the left is a > b and that on the right is x == 10 the whole expression is true only if both expressions are true i.e., if a is greater than b and x is equal to 10.
Logical OR (||)
The logical OR is used to combine 2 expressions or the condition evaluates to true if any one of the 2 expressions is true.
Example
a<m || a<n
The expression evaluates to true if any one of them is true or if both of them are true. It evaluates to true if a is less than either m or n and when a is less than both m and n.
Logical NOT (!)
The logical not operator takes single expression and evaluates to true if the expression is false and evaluates to false if the expression is true. In other words it just reverses the value of the expression.
Example
! (x >= y)
Assignment Operators
The Assignment Operator evaluates an expression on the right of the expression and substitutes it to the value or variable on the left of the expression.
x = a + b
Here the value of a + b is evaluated and substituted to the variable x.
In addition, C has a set of shorthand assignment operators of the form.
Var oper = exp;
Here var is a variable, exp is an expression and oper is a C binary arithmetic operator. The operator oper = is known as shorthand assignment operator
Example
x + = 1 is same as x = x + 1
The commonly used shorthand assignment operators are as follows:
Shorthand assignment operators
Statement with simple assignment operator | Statement with shorthand operator |
a = a + 1 | a += 1 |
a = a – 1 | a -= 1 |
a = a * (n+1) | a *= (n+1) |
a =a/(n+1) | a /= (n+1) |
a =a % b | a %= b |
Example for using shorthand assignment operator
#define N 100 //creates a variable N with constant value 100 #define A 2 //creates a variable A with constant value 2 main() //start of the program { int a; //variable a declaration a = A; //assigns value 2 to a while (a < N) //while value of a is less than N { //evaluate or do the following printf(“%d \n”,a //print the current value of a a *= a; //shorthand form of a = a * a } //end of the loop } //end of the program
Output :
2 4 16
Previous | Home | Next |