⯈Algorithm and its Characteristics
⯈Elementary Problems
- Addition of two numbers
- Calculate area and circumference of circle
- Calculate area of triangle
- Calculate simple interest
- Calculate slope and distance between two points
- Convert length in feet to inches
- Weighted score in exam
- Convert temperature in degree Celsius to Fahrenheit
- Swap two numbers
- Swap two numbers without using extra variable
- Overview of C Programming Language
- Getting started with C Programming Language
- Keywords
- Identifiers
- Constants
- Operators
- Expression Evaluation
- Mathematical expression to C equivalent expressions
- Datatypes
- Variables
- Integer representation in C
- Character representation in C
- Type conversion in C
- sizeof operator
- Comments
- Mathematical Functions
- input output statements
- width specifiers in C
- structure of a C program
- header files
- Compilation process of a C program
- Types of initializations.
⯈Basic C Programs
- C program to add two numbers
- C program to find area and circumference of circle
- C program to swap two numbers
- C program to swap two numbers without using extra variable
- C program to swap two numbers using bitwise XOR
- C program to convert temperature in Celsius to Fahrenheit
- C program to calculate gross salary of an employee
- C program to count number of digits in a positive integer
- C program to count number of digits in binary representation
- C program to count number of digits in base ‘K’
- C program to convert kilometer to meter, feet, inches and centimeters
- C program to find first and last digit of a number
- C program to find minimum number of currency denominations
- C program to convert cartesian coordinates to polar coordinates
- C program to find distance between two places on earth in nautical miles
- C program to find slope and distance between two points
- C program to add 1 to the number using ‘+’ operator
- C program to find maximum of two numbers using ternary operator
- C program to find maximum of three numbers using ternary operator
- C program to find kth bit of a number
- C program to find last four bits of a byte
- C Program to reset right most set bit of a number
Variables in C
A variable is the name given to the memory location. The memory to store the data is allocated on the RAM (Random Access Memory).
We have also discussed memory diagrams for variables at the end.
You may watch the video posted at the end of this page.
Syntax to declare a variable
datatype varName(s);
- datatype can be int, float, double, char, struct, enum, union, array, pointer or signed, short or long of any primitive data type.
- variable name is any valid identifier name.
Examples
- int a;
- It creates a variable by name ‘a’ whose datatype is int. ‘a’ is a variable where any integer value (of integer range) can be stored.
- float b;
- It creates a variable by name ‘b’ whose datatype is float. ‘b’ is a variable where any floating value (of float range) can be stored.
- double c;
- It creates a variable by name ‘c’ whose datatype is double. ‘c’ is a variable where any double number (of double range) can be stored.
- char d;
- It creates a variable by name ‘d’ whose datatype is char. ‘d’ is a variable where any character value (of character range) can be stored.
- int x, y;
- Creates two variables ‘x’ and ‘y’ of type int.
Memory layout of RAM
- RAM is divided into addresses.
- The addresses are normally represented in hexa-decimal numbers.
- Addresses of RAM are unique. There are no duplicate addresses.
- Addresses of RAM belongs to set of whole numbers. These addresses are long long int (compiler dependent). Therefore, address of a computer takes 8 bytes of data.
- Whenever a variable is created the memory is allocated from the RAM. The memory is allocated wherever the free space is available.
- The memory allocated for a variable is continuous.
- But for different variables the memory allocated is random, it may or may not be continuous.
Note: There is memory layout of C program with respect to RAM, which we shall discuss later.
Whenever a variable is created the memory is allocated on the RAM. The contents of the memory allocated is Garbage or Junk value by default. Garbage is any un-known value.
Let us understand these with memory diagrams for some examples.
Memory diagrams for variables
int a;
- The address of the variable is randomly assumed.
- The memory allocated for the variable depends on the datatype. It allocates 4 bytes of data, since it is an integer.
- The value stored at the given address is GARBAGE. (By default)
int b = 10;
The variable ‘b’ holds the value 10. If the value is not initialized then it will be GARBAGE, if initialized the value of variable will be the given value. Here it is 10.
double c;
- The memory allocated for the variable depends on the datatype. It allocates 8 bytes of data, since the data type is double.
- The value stored at the given address is GARBAGE. (By default)
double d = 1.1;
float e = 2.3;
char ch = 'A';
- The character values are written in single quote.
- Character values are stores as ASCII (American Standard Code for Information Interchange) values internally.
- ASCII values are integers, Different characters have different ASCII values.
- There are 256 different ASCII values, as there are 256 characters (Range: 0 to 255)
- ASCII values for ‘A’ to ‘Z’ is 65 to 90
- ASCII values for ‘a’ to ‘z’ is 97 to 122
- ASCII values for ‘0’ to ‘9’ is 48 to 57
- Rest all ASCII values are for special characters.
Note: That the value of variable ch is stored as ASCII value internally.
int g = b + 3;
Create a new variable ‘g’, store in ‘g’ b+3.
- Evaluate RHS first, in RHS we have b+3, there is a variable ‘b’ whose value is 10, evaluate 10 + 3 = 13, store 13 in variable ‘g’.
int h = a + 2;
A new variable ‘h’ is created. Since the value of variable ‘a’ is Garbage (unknown), Garbage +2 = Garbage. Therefore, the value of variable ‘h’ is Garbage.
Note: If any one variable on the RHS of assignment operator (=) has Garbage value, then the variable on LHS will also have the Garbage value.
Hence make sure that all the variables on RHS of '=', their values are known before its first usage.
a = 25;
- The value ’25’ is stored in variable ‘a’.
- If no data type is written along with the variable name, then the existing variable will be used.
- If data type is written along with variable name, then the new variable will be created.
- There is an existing variable with name ‘a’. The value is Garbage, and it is overwritten with 25.
m = 25;
- Since we didn’t write any data type with the variable name, the existing variable has to be used.
- We see that there is no variable with name ‘m’. Hence it gives an error called undeclared variable m.
Note: A variable cannot be used unless it is declared.
double b = 9.8;
- The above declaration produces an error. As the variable name ‘b’ is already taken.
Note: There cannot be duplicate variable names in the same scope. More about scopes we will discuss in functions chapter.
int n = ch + 4;
- A new variable ‘n’ will be created.
- ‘n’ will have value of ch + 4, note that the variable ch is of type character, stored as ASCII value (int).
- Evaluate RHS first, ch + 4 = 65 + 4 = 69
- Therefore, n will have value of 69.
a = a + 8;
- Evaluate RHS first ( a+ 8) and store the value in ‘a’.
- Take recent value of a i.e, 25, 25 + 8 = 33, store 33 in ‘a’. (25 will be over-written)
- In shorthand notation we can write it as, a + = 8
c = 2/3;
- Note that variable ‘c’ is already declared of double datatype.
- ‘c’ can store real numbers.
- Evaluate RHS first, which is 2/3 = 0 (int/int = int)
- Store 0 in variable ‘c’, we see that 0 is an integer, convert 0 to real number it becomes 0.0, store 0.0 in variable c.
- Even though, the datatype is double, we lose answer, because RHS is an integer (2/3).
Note: If the data on RHS and datatype on LHS don't match there is a possibility of loss of data.
h = 2.0 / 3;
- Evaluate RHS first, 2.0 / 3 = 0.67 (real_number / integer = real_number)
- We want to store 0.67 in variable ‘h’. But ‘h’ is of type int. We can only store int values. Therefore, we convert 0.67 to integer which becomes 0.