Today lets learn how to swap 2 integer numbers using a temporary variable in C.
#include < stdio.h >
int main()
{
int x = 10, y = 20, temp;
printf("X = %d, Y = %d\n", x, y);
temp = x;
x = y;
y = temp;
printf("After swapping X = %d, Y = %d\n", x, y);
return 0;
}
Output: X = 10, Y = 20 After swapping X = 20, Y = 10
Here we take 3 variables, x, y and temp; We store 10 in x and 20 in y. 1. First we copy the value present in x to temp. So now, both x and temp variables have value 10. 2. Next we copy value of y to x. So now, both x and y have value 20. 3. Next, copy the value of temp to y. So now both y and temp have value 10.
Finally after executing above 3 step logic, x has value 20 and y has a value of 10 in it.
Today lets learn about basic arithmetic operations in C programming language.
What we learn in this video tutorial?
We shall learn, addition, subtraction, multiplication, division and modular division in this video tutorial. And we’ll also learn how to use pow() method present in math.h library file.
#include < stdio.h >
#include < math.h >
int main()
{
int a = 10, b = 2;
char m = 'A', n = 'a';
printf("\nAddition of a and b is %d\n", (a+b));
printf("\nSubtraction of a and b is %d\n", (a-b));
printf("\nMultiplication of a and b is %d\n", (a*b));
printf("\nDivision of a and b is %d\n", (a/b));
printf("\nModular Division of a and b is %d\n", (a%b));
printf("\n3 to the power of 2 is %f\n", pow(3,2));
printf("\nASIIC value of A and a is %d and %d\n\n", m, n);
return 0;
}
Output:
Addition of a and b is 12 Subtraction of a and b is 8 Multiplication of a and b is 20 Division of a and b is 5 Modular Division of a and b is 0 3 to the power of 2 is 9.000000 ASIIC value of A and a is 65 and 97
When there is tie between same priority operators then we check the associativity. For example, for * and /, associativity is same. i.e., left to right.
So in the expression: c = a * b + a / b; a * b gets executed first.
ASCII values
When you try to apply Arithmetic Operation on characters(alphabets, character digits, special characters) its correspondent ASCII value gets operated on. ASCII value of A is 65 and ASCII value of a is 97. This is the reason why capital A and small letter a are treated differently in C programming language. Under the hood, they have different ASCII values.
Note 1: All the variables and constants are called operands. All the arithmetic symbols(+, -, /, *, %) are called operators. = is called assignment operator.
Note 2: Only one variable is allowed on the left hand side of the equation or expression. Ex: c = a + b is valid. a + b = c is invalid.
Note 3: You can’t store a decimal value in a integer variable. So if the result of evaluation of an expression has decimal value and you’re assigning it to an integer variable, then only the integer part gets stored. Decimal part will be discarded. So be careful while choosing the data type of variables while performing arithmetic operation.
Note 4: We need to always, explicitly mention the arithematic operators. Ex: c = (a + b)(a – b) throws error. c = (a + b) * (a – b); is the correct way in C programming.
Note 5: An integer value divided by an integer value gives back an integer value. A float value divided by a float value gives back a floating point value.
Note 6: Modular division gives reminder of division. Ex: c = 10 / 2; gives c = 5; c = 10 % 2; give c = 0;
Also, c = -3 % 2; gives c = -1 c = 3 % -2; gives c = 1
Modular division doesn’t take float values as its operands. Ex: c = 3.0 % 2.0; gives error
Note 7: math.h library file has other mathematical operations related methods like: pow(), sin(), cos(), tan(), abs(), sqrt() etc.
We’ll look more arithmetic operators as and when we encounter them in programs.
We’ve already learnt how to use int, float and char in our previous video tutorial. Now lets learn the rules to construct integer, float/real, character constants in C programming language.
1. It must have atleast 1 digit. We can have anything from 0 to 9. 2. If user enters decimal point, it’ll be discarded by the program and only the integer part will be stored in the int variable. 3. We can assign positive or negative digit by appending the digit with + or – symbol. If nothing is specified it’ll be considered as positive. 4. Comma, space etc are not allowed. 5. Allowed range for integer constant is -2147483648 to +2147483647 for Visual Studio and gcc compilers. For Turbo C / C++ compilers allowed range for integer constants is -32768 to +32767.
1. It must have atleast 1 digit. Anything from 0 to 9 is allowed. If decimal value is not specified compiler will insert 0s for decimal value. 2. It can be either positive or negative value. If + or – symbol is not specified, it’ll be considered as positive. 3. No comma or space allowed.
Rules for Constructing Character Constants
1. Character constant must have only single alphabetic or Single digit or special symbol and must be enclosed in single quote.
In our previous video tutorial you learnt about Integers, Float and Character data types and their format specifier. In today’s video lets see an example of using all 3 data types along with strings.
You’ll also learn to get user input from console using scanf function/method present in stdio.h header file.
Example: int, float, char
#include< stdio.h >
int main()
{
int a;
float b;
char ltr;
printf("Enter 2 numbers and a single character\n");
scanf("%d %f %c", &a, &b,
Output:
Enter 2 numbers and a single character
1 2 i
You entered 1, 2.000000 and i
Example for string type
#include < stdio.h >
int main()
{
char c[10];
printf("Enter a company name\n");
scanf("%s", c);
printf("\nYou entered %s", c);
return 0;
}
Output: Enter a company name Microsoft You entered Microsoft
Using Scanf() To Read Int, Float, Char and String Data Type: C Programming
Keyword for Integer is int and its format specifier is %d. Keyword for Float is float and its format specifier is %f. Keyword for Character is char and its format specifier is %c.
Note 1: If you input decimal value for a integer variable, it only stores integer part of the value and discards the decimal value.
Note 2: printf and scanf are kind of opposite. Because printf converts all the numbers, characters etc and displays everything in text format on to the console. While, scanf takes all the text entered by the user in the console and converts them into respective data type based on the format specifier present in scanf statement.