Swap 2 Numbers Without Using a Temporary Variable: C

Today lets learn how to swap 2 integer numbers without using a temporary variable in C. We’re using arithmetic operation in our logic. We’re using addition and subtraction in this c program to swap the 2 numbers.

 
#include < stdio.h >

int main()
{
    int x = 10, y = 5;

    printf("Before swapping x = %d and y = %d\n", x, y);

    x = x + y - ( y = x );

    printf("After swapping x = %d and y = %d\n", x, y);

    return 0;
}


Output:
Before swapping x = 10 and y = 5
After swapping x = 5 and y = 10

Swap 2 Numbers Without Using a Temporary Variable: C


[youtube https://www.youtube.com/watch?v=tTzdGK5t-X4]

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


Swapping 2 Numbers In C using addition, subtraction and assignment operators: Logic

Here we take 2 variables, x and y; We store 10 in x and 5 in y.

We add the values of x(10) and y(5) and then subtract the value returned when we assign x value to y.

Note: When we assign a value to a variable, the same value is returned.
Ex: temp = (a = 10);
In this statement, when we assign 10 to a, the whole expression returns 10 and we can store it in variable temp, for our reference.

Swap 2 Numbers Using a Temporary Variable: C

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

Swap 2 Numbers Using a Temporary Variable: C


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

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


Swapping 2 Numbers In C: Logic

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.

Rules for Constructing Variable Names: C

Rules for constructing variable names are same for all the data types.

Related Read:
Rules for Constructing int, float, char constants: C

Rules for Constructing Variable Names: C


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

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


Rules for constructing variable names

1. First character in a variable name must be an alphabet or an underscore( _ ).

2. Variable name can have alphabet, digits and underscore.

3. No commas, space allowed in variable name.

4. No other special symbols other than underscore is allowed.

5. C variables are case sensitive. Ex: name and Name are two different variables.

6. You can not use keywords / reserve words as variable name in C.

Valid Variable Names
_name
user_name
age20
iMac

Invalid Variable Names
9hundred
Micro soft
Apple$computers

Note: All these rules for constructing variable names apply for all the data types in C.

Rules for Constructing int, float, char constants: C

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.

Related Read:
Rules for Constructing Variable Names: C

Rules for Constructing int, float, char constants: C


[youtube https://www.youtube.com/watch?v=4o-ZJfi559Q]

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


Rules for Constructing Integer Constants

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.

Invalid Integer Constants
10,000
10 000
999999999999

Rules for Constructing Real Constants

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.

Invalid Integer Constants
“A”
“Microsoft”
‘Apple’
‘123’
“$%^”

include directive in C Program

Usually you can see these “include” directive at the very top of your source code. It’s called a preprocessor directive.

All preprocessor statements in C start with a hash(#) symbol. include directive is similar to imports statement in Java.

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

Output:
Microsoft
Apple
Oracle
Google
Yahoo

include Preprocessor Directive In C Program


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

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


This “include” directive causes one file to be included in another. For example, when we include stdio.h (header file) in our above program, all the content(code) gets imported or included in our program source code. This way we can make use of any function present inside stdio.h library file in our c program.

Car Program

We are writing Car simulation program in C – we write all the related code in different files by group them together and then include it in our main Car program. For example, we can separate wheels, color and display related things and then include all those things in our main Car program. This way our main Car program source code looks less clumsy and more over we need not worry about the implementation details present in those files.

Ex: When we include stdio.h file, we do not worry about the implementation details of printf() method. We simply use it for displaying content to the console. All the implementation details are present in stdio.h header file.

There are 2 ways of including header files

 
#include < stdio.h >

#include "Mylib.h"

When we use < and > to wrap around the file name, the linker looks for the files in standard library storage folder. When we wrap the file name in double quotes it looks for the file in current directory.

Usually user written, custom header files are wrapped in double quotes.

We’ll be writing our own library files in this video series. But for now it’ll get too complicated if we go any further.

Note: There are many preprocessor statements other than include directive. We’ll discuss them as and when we use it in our C programs.