Swap 2 numbers using Addition and Subtraction: C

In this video tutorial we shall learn how to swap two integer numbers without using a temporary variable and by simply making use of addition and subtraction.

Related Read:
Basic Arithmetic Operations In C
Swap 2 Numbers Using a Temporary Variable: C
Swap 2 Numbers Without Using a Temporary Variable: C

 
#include < stdio.h >

int main()
{
    int a, b;

    printf("Enter 2 integer numbers for, a and b\n");
    scanf("%d %d", &a, &b);

    printf("You entered a = %d and b = %d\n", a ,b);

    a = a + b;
    b = a - b;
    a = a - b;

    printf("After swapping a = %d, b = %d\n", a, b);

    return 0;
}

Output:
Enter 2 integer numbers, for a and b
30
20
You entered a = 30 and b = 20
After swapping a = 20, b = 30

Scanf(): For user input

In above c program we are asking user to enter the values for variable a and b. You can know more about scanf() method/function in this video tutorial: Using Scanf in C Program

Swap 2 numbers using only Addition and Subtraction: C


[youtube https://www.youtube.com/watch?v=mm4XJ2DEVS4]

YouTube Link: https://www.youtube.com/watch?v=mm4XJ2DEVS4 [Watch the Video In Full Screen.]


Swapping 2 Numbers In C using only addition and subtraction: Logic

In our above program we are asking user to enter integer value for a and b.
If the user enters 30 and 20 for a and b respectively. Then our program executes below logic to swap the values of variable a and b.

Step 1: Add values of a and b and store it in variable a.
i.e.,
a = a + b;
50 = 30 + 20;
So a = 50;

Step 2: Now subtract value of b from value of a and store it in variable b.
i.e.,
b = a – b;
30= 50 – 20; (value of a = 50 according to Step 1)
So value of variable b is now 30.

Step 3: Now subtract value of b from value of a and store it in variable a.
i.e.,
a = a – b;
20= 50 – 30; (Value of a = 50 from Step 1, and Value of b = 30 from Step 2)

Finally value of a = 20(from step 3) and value of b = 30(from step 2).

This is how we swap the value of variable a and b by just making use of addition and subtraction in C programming language.

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Basic Arithmetic Operations In C

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

Basic Arithmetic Operations In C


[youtube https://www.youtube.com/watch?v=4mjiL9uwPCg]

YouTube Link: https://www.youtube.com/watch?v=4mjiL9uwPCg [Watch the Video In Full Screen.]


Precedence and associativity of arithmetic operators

1st priority: * / %
2nd priority: + –
3rd priority: =

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.

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Keywords, Constants, Variables: C

Computers do not understand English language. It can only understand machine code, a binary stream of 1s and 0s.

Learning The Building Blocks of C programming Language

To learn any language we need to first learn alphabets, then we learn to write sentences and then to write paragraphs. Similarly, in learning C programming language, we first learn about the Character Set(Alphabets, Digits, Special Symbols), next we learn words(keywords, variables, constants), and then we learn statements(C programming instructions), and then we write C programs.

Keywords, Identifiers And Literals: C


[youtube https://www.youtube.com/watch?v=D4ePj0ill5k]

YouTube Link: https://www.youtube.com/watch?v=D4ePj0ill5k [Watch the Video In Full Screen.]


Keywords

Every word in a C program is classified as either a keyword or an identifier. All keywords have fixed meanings predefined in the language and these meanings can not be changed.

Identifier / Literals

In programming languages, constants are usually called as literals and variables are called as identifiers.

Constants / Variables

As name suggests, a constant is an entity whose value doesn’t change during the course of program execution. A variable, on the other hand, is an entity whose value may change during the course of program execution.



C Constants

C Constants can be divided into 2 categories.
1. Primary Constants.
2. Secondary Constants.

Primary Constants are further divided into Integer Constants, Real Constants and Character constants.

Keyword for Integer is int and the format specifier is %d.
Keyword for Real is float or double and the format specifier is %f.
Keyword for Character is char and the format specifier is %c.

 
#include < stdio.h >
int main()
{
    const int a = 5;

    a = a + 1;

    printf("Value of a is %d", a);
}

Output:
error: Assignment of read-only variable ‘a’.

a is a constant variable and thus its value can not be changed through the course of program execution.

 
#include < stdio.h >
int main()
{
    int a = 5;

    a = a + 1;

    printf("Value of a is %d", a);
}

Output:
Value of a is 6

Note: Literals are constant values and not constant variables.

The Best IDE For C Programming

There are many IDE(Integrated Development Environment) available for C programming language, but I would prefer using Code::Blocks or Visual Studio Editor.

For this video tutorial series we’ll be using Code::Blocks.

You can visit CodeBlocks website and navigate to Downloads section, and then to Binaries section and depending upon your Operating System, download the IDE and install it. Downloading and installation of Code::Blocks is shown in the video posted below. You can follow along and get started with programming in C language.

 
#include < stdio.h >
int main()
{
    printf("Microsoft\n Apple\n Oracle\n Google\n Yahoo\n");
    return 0;
}

Output:
Microsoft
Apple
Oracle
Google
Yahoo

Above program is just an example. You need not worry if you didn’t understand it. I’ll be explaining it in the next video, in detail. For now, if possible type the program as it is and try to compile and execute it on your own by seeing the video. Try to change the content inside printf function and run compilation(Build) and execution(Run) once again and see the results for yourself.

The Best IDE For C Programming


[youtube https://www.youtube.com/watch?v=xsWy-sMzIj0]

YouTube Link: https://www.youtube.com/watch?v=xsWy-sMzIj0 [Watch the Video In Full Screen.]


Note:
1. Code::Blocks is available for all the popular Operating Systems. So whatever I show in the video tutorial is applicable for everyone irrespective of the OS they’re using.

2. Code::Blocks comes with C/C++ compiler, so you need not install compiler or any other plugins separately.

3. Make sure to download the latest version of the IDE. And make sure the file you download has ( codeblocks-17.12mingw-setup ) mingw in it. That’s how we can know it comes shipped with c/c++ compiler by default.

Why Learn ‘C’ As Your First Programming Language?

Let’s know a little bit of history of C programming. Many of you may not be interested in knowing the history of C – you maybe here just to learn how to get started with Coding in C Program. I understand that. You’ll want to know the history soon, once you learn the basics of C programming. History is very important. But for now I’ll let you know about it in 1 line!

History of C Programming Language

C Programming language was developed at AT&T’s Bell Lab in 1972. It was designed and developed by a man called Dennis Ritchie (September 9, 1941 – October 12, 2011).

Why name it as C?
Before designing C, there was a programming language called “B”. So Dennis Ritchie named it as “C”. Little did he know “C Programming” language would get so popular among programmers – even after 3 decades of its inception.


C programming language

Why Learn C As A First Programming Language?

Beginners tend to get into arguments/discussion as to how C++, C#, Java has improved upon C and how “Object Oriented Programming” has so many advantages over C. But let me tell you, Operating Systems like Windows, iOS, OSx, Unix/Linux(Kernel), android are all written in C. Looks like there is something which is good in C because of which companies like Microsoft, Apple, Unix/Linux Foundation, Google are choosing C Programming language to develop and maintain their Operating Systems. These are only a few popular companies I’ve listed. There are many many companies solely dependent upon C programming for their software development. We’ll learn more along the course of these video tutorials.

Why Learn C As A First Programming Language?


[youtube https://www.youtube.com/watch?v=s88KJDI1P_o]

YouTube Link: https://www.youtube.com/watch?v=s88KJDI1P_o [Watch the Video In Full Screen.]


Advantages of learning C

1. Object Oriented Programming(OOP) method present in C++, C#, Java provides a huge advantage for programmers. But the basic programming elements like looping, variables, declaration, arithmetic operations, logical operations etc are almost similar to what you learn in C. Learning C definitely gives you an extra edge before jumping on to learn C++ or C# or Java.

2. Programs written in C are very efficient. It’s performance and speed of execution is unique. Even today device drivers are written in C for this reason. The code is compact and has high performance even in low memory availability.

3. Your smartphone, digital camera, washing machine, microwave oven, refrigerator, smart bulbs, smart fans etc you name it, all these have microprocessors attached to it, which has program embedded in it. Since these micro-processor in these devices have limited memory and need to respond to user inputs instantly, the program of choice is inevitably C, in most cases.

4. Most 3D gaming frameworks like “DirectX” are built using the C Programming language. Because for good gaming experience speed of execution matters a lot. Since C programs are robust, reliable and fast these 3D gaming framework choose C programming over others. Even animation special effects in movies are done using C programs.

5. Its flexible and portable: Since C program allows user to handle memory directly we can write programs very flexibly according to our needs. We can manage memory flexibly. “Portable” because the same program source code works on all operating systems without the need for much modification. All Operating System have C Compiler and they compile and give object code and executable which suits and works for that particular OS.

6. Did you know, many compilers and interpreters for other languages like FORTRAN, Pascal, Perl, Python etc are written in C. Why is it so? As I told you before, C is reliable, portable and fast.

7. Using C programming language we can directly access hardware. This is very powerful feature. We’ll discuss this in detail in coming video tutorials.

8. C program enables programmer to manipulate individual bits of memory. But “with power comes responsibility” – we need to handle memory carefully or else we may end up writing code which might behave abruptly. For this reason Java has its own builtin memory management feature called Garbage Collection.

9. C implementation has a large library of useful functions. Ex: scanf, printf, functions to write data to file etc.

10. C programming language is easy to learn and understand.

We can go on and keep writing advantages of learning C Program. But let me stop here and assure you that you’re in the right place and C is the best choice as your first programming language to learn.

Make sure to practice all the programs we post here and quickly you’ll know the advantage for yourself. And soon you’ll be an advanced user and if you keep learning you’ll be an expert.

There are many Job opportunities for people who are experts in C Programming Language. We’ll even post Job Board shortly and try to list job offers for C program experts. Stay tuned for all the good stuff we’ll post on this page: C Programming: Beginner To Advance To Expert

Request

Please let us know some advantages and disadvantages or anything informative about C programming language in the comment section below. This will be helpful for others joining you on later date to start learning ‘C Programming language’. Sharing is caring. Also please share this article with your friends and followers on Social Media.