No cases in switch case : C Programming

No cases in switch case

void main()
{
 int choice = 2 ;
 switch(choice)
  {
  }
}

Explanation :

Consider the above switch case example in C Programming. In this example, we have written the switch case with no cases in switch case

Re-commanded Article : Rules of using switch case | Invalid ways of switch case

No cases in switch case - c programming

We never write such kind of statement because it is not required at all while writing code in C but still syntactically no cases in switch case is allowed.

switch(choice)
  {
  }

In this case switch case would be executed but nothing will be printed on the screen

C Mistakes – 1D array

Array is one of the important concept in c programming, while learning or practicing 1D Array most of the beginners unknowingly follow incorrect or invalid syntax of array. This tutorial will list out 1D Array : Mistakes in C Programming

C Programming 1D Array : Mistakes

Mistake 1 : Constant Expression Require

#include<stdio.h>
void main()
{
int i=10;
int a[i];
}

Consider the above example of 1D Array,

  1. We are going to declare an array whose size is equal to the value of variable.
  2. If we changed the value of variable then array size is going to change.
  3. According to array concept, we are allocating memory for array at compile time so if the size of array is going to vary then how it is possible to allocate memory to an array.

i is initialized to 10 and using a[i] does not mean a[10] because ‘i’ is Integer Variable whose value can be changed inside program.

Value of Const Variable Cannot be changed

we know that value of Const Variable cannot be changed once initialized so we can write above example as as below –

#include<stdio.h>
void main()
{
const int i=10;
int a[i];
}

or

int a[10];

Recommanded Article : Compile Time Initializing an Array

Mistake 2 : Empty Valued 1D Array

#include<stdio.h>
void main()
{
int arr[];
}

Consider the above example, we can see the empty pair of square brackets means we haven’t specified size of an 1D Array. In the example array ‘arr’ is undefined or empty.

Size of 1D Array should be Specified as a Constant Value.

Instead of it Write it as –

#include<stdio.h>
void main()
{
int a[] = {1,1};
}

above syntax will take default array size equal to 2. [Refer Article : Section B]

#include<stdio.h>
void main()
{
int a[] = {}; // This also Cause an Error
}

Mistake 3 : 1D Array with no Bound Checking

#include<stdio.h>
void main()
{
int a[5];
printf("%d",a[7]);
}
  1. Here Array size specified is 5.
  2. So we have Access to Following Array Elements – a[0],a[1],a[2],a[3] and a[4]
  3. But accessing a[5] causes Garbage  Value to be used because C Does not performs Array Bound Check.

If the maximum size of array is “MAX” then we can access following elements of an array –

Elements accessible for Array Size "MAX" = arr[0]
                                         = .
                                         = .
                                         = .
                                         = arr[MAX-1]

Mistake 4 .Case Sensitive

#include<stdio.h>
void main()
{
int a[5];
printf("%d",A[2]);
}

Array Variable is Case Sensitive so A[2] does not print anything it Displays Error Message : “Undefined Symbol A

C Programming 1D Array Tips

Tip 1 : Use #define to Specify Array Size

#include<stdio.h>
#define MAX 5
void main()
{
int a[MAX];
printf("%d",a[2]);
}
  • As MAX is replaced by 5 for every Occurrence , So In due Course of time if you want to increase or decrease Size of array then change should be made in
#define MAX 10

Tip 2 : a[i] and i[a] are Same

Visit this article which will explain how to access array randomly using a[i] and i[a].

C Contiguous Memory

What is Contiguous Memory ?

  1. When Big Block of memory is reserved or allocated then that memory block is called as Contiguous Memory Block.
  2. Alternate meaning of Contiguous Memory is continuous memory.
  3. Suppose inside memory we have reserved 1000-1200 memory addresses for special purposes then we can say that these 200 blocks are going to reserve contiguous memory.

Contiguous Memory Allocation

  1. Two registers are used while implementing the contiguous memory scheme. These registers are base register and limit register.
  2. When OS is executing a process inside the main memory then content of each register are as –
RegisterContent of register
Base registerStarting address of the memory location where process execution is happening
Limit registerTotal amount of memory in bytes consumed by process

contiguous memory

Here diagram 1 represents the contiguous allocation of memory and diagram 2 represents non-contiguous allocation of memory

  1. When process try to refer a part of the memory then it will firstly refer the base address from base register and then it will refer relative address of memory location with respect to base address

How to allocate contiguous memory ?

  1. Using static array declaration.
  2. Using alloc() / malloc() function to allocate big chunk of memory dynamically.

This article could be more useful for understanding the memory management concept in operating system.

Running C Program on Window 8

In this tutorial we are going to see, how we can install DOSBox in order to run the traditional C program in the Latest Window 8 environment. In this tutorial we will be learning to run C Program on Window 8.

The DOSBox is x86 immulator which is used to run traditional 16/32 bit DOS applications in 64 bit environment those are incompatible with DOS applications.

Steps to run C Program on Window 8:

  1. Basically first step to run C Program on Window 8 is to get the DOSBox installer. Click here and download the DOSBox setup.
  2. Once you have setup of the DOSBox, you need to download the setup for TC. Get TC setup by clicking on this link. (Get TC Setup)
  3. Now you have all required installers with us, we can start installation now.
  4. Extract the TC.zip file in the C:\TC\
  5. Unzip TC

  6. Now unzip the DOSBox0.74-win32-installer.zip and run the installer file to install the DOSBox.
  7. After installation of the DOSBox open the DOSBox Application by double clicking on the DOSBox icon on the desktop.
  8. DOSBox icon

  9. You need to type following command in the Prompt –
  10. mount e c:\TC\

    C Program on Window 8 - Mount E Drive with DOSBox

  11. Now switch to E drive using the following command.
  12. E:
  13. Now hit following command to install the TC.
  14. install.exe

    C Program on Window 8 - installing the TC in DOSBox

  15. Press enter to continue
  16. C Program on Window 8 - TC Installer Window

  17. Now Select “E” drive by typing E and hit enter key.
  18. C Program on Window 8 - Select E drive

  19. Now keep the source path as it is. Again hit enter key.
  20. C Program on Window 8 - Selecting the path

  21. Now using down arrow key navigate to the “Start Installation” menu and hit enter key.
  22. starting installation

  23. After installation gets started you can see the below blue window like this –
  24. Installation started

  25. After the installation you will be prompted with this window then press any key to view the help file
  26. open help

  27. Now when you will be prompted with the help window, then press escape key to close that window in order to return to the DOSBox
  28. Thus we have successfully installed TC using DOSBox.

Running TC using DOSBOX :

  1. Now we need to open the DOSBox option file to run TC as default application when we launch the DOSBox
  2. DOSBox Option file

  3. Now select the DOSBox 0.74 Option menu by searching in sidebar pane in Window 8
  4. Now go to the end in the opened dosbox-0.74.conf file and add the below code in the file –
  5. mount e c:\tc\
    e:
    cd tc
    cd bin
    tc
    
  6. save the file and close it.
  7. Now click on the DOSBox icon on the homepage to launch the DOSBox, TC will gets opened as default application. Enjoy the flavor of the DOS in Window 8 environment.

In the next tutorial we will be going to see how to open and run c program using the dosbox.
More help : Dosbox Installation guide

Use of pre/post Increment operator in expression

In this example we will be learning very typical example of C Program where we will be using pre increment and post increment operators in same expression.

Re-commanded Reading : Increment Operator in C | Decrements Operator in C

Postfix and Prefix Expression in Same Statement

#include<stdio.h>
#include<conio.h>

void main() {
   int i = 0, j = 0;
   j = i++ + ++i;
   printf("%d\n", i);
   printf("%d\n", j);
}

Output :

2
2

Explanation of Program

A picture worth 100 lines of text. Consider the below program picture which explains the above program.

j = i++ + ++i;

Above line of code have total four operators –

  1. Assignment Operator
  2. Post Increment Operator
  3. Pre Increment Operator
  4. Arithmetic Operator

Now we need to arrange all the operators in the decreasing order of the Precedence –

OperatorPrecedence Rank
Pre Increment1
Post Increment2
Arithmetic Operator3
Assignment Operator4

Now we have arranged all the operators in the decreasing order of the precedence. (Operator Precedence and Associativity Document)

Execution flow

  1. In the first step pre-increment operator gets an opportunity for execution. It will increment the value of variable
  2. In the second step post increment operator will be executed. It does not increment the actual value of variable but mark it as pending execution.
  3. Here pre-increment operation uses new Value of ‘i’ so [ i = 1 ].
  4. This ‘i = 1’ is used for Post Increment Operation which uses Old Value i.e [i = 1] .
  5. Both ‘1’s are added and value 2 is assigned to ‘j’.

Summary :

Step 1 : j = i++ + 1;
Step 2 : j = 1 + 1; (post-increment pending on i)
Step 3 : j = 2; (Addition)
Step 4 : j = 2; (Assignment)
Step 5 : Pending increment on i will be executed

C programming typedef keyword

We have already learnt about the keywords and data types in c programming. In this tutorial we will be learning the typedef keyword in C programming.

C programming typedef keyword

Consider the following example – Suppose we are storing only roll numbers then it would be very useful and meaningful if we have data type “roll”.

typedef keyword – defining types

#include<stdio.h>

int main() 
{
typedef int Roll;
Roll num1 = 40,num2 = 20;

printf("Roll number 1 : %d",num1);
printf("Roll number 2 : %d",num2);
return(0);
}

Explanation of Typedef :

In the above program we want to give another name to an integer data type i.e “roll”. We can do this using typedef statement as below –

typedef int Roll;
Roll num1 = 40,num2 = 20;

However we can also use integer data type after we create another data type “Roll”. typedef keyword would just create similar data type.

Application of typedef keyword

Renaming existing data type

typedef keyword is used to rename the existing data type. suppose you are storing price values in floating variables then it would be useful to have a data type called price, so you can create alias data type of floating point number.

typedef float price

using above statement we have created an alias of floating point data type which will behave similar to floating data type. We can declare variables of price data type using below syntax-

price p1,p2;

Creating structure type

typedef struct
{
  type member1;
  type member2;
  type member3;
}type_name;

Consider following example –

#include<stdio.h>
#include<conio.h>
#include<string.h>

typedef struct student {
   char name[50];
   int marks;
} stud;

void main() {
   stud s1;

   printf("\nEnter student record\n");

   printf("\nStudent name : ");
   scanf("%s", s1.name);
   printf("\nEnter student marks : ");
   scanf("%d", &s1.marks);

   printf("\nStudent name is %s", s1.name);
   printf("\nRoll is %d", s1.marks);

   getch();
}

Whenever we create a structure then we are treated it as new type of data. In C programming typedef keyword gives you facility to rename data type.

In the further chapter (in Structure) we will again go through this chapter in details.

Creating alias for complex type

Suppose you are declaring double pointer like below example –

int **ptr;

It would be helpful and meaningful if you create data type intDoublePointer like below –

typedef int** intDoublePointer;
intDoublePointer p1,p2;

Examples of typedef keyword

Below are some of the examples of typedef keyword in c programming.

C multiple increment operators inside printf

We have already learnt different ways of using increment operators in c programming. In this tutorial we will study the impact of using multiple increment / decrement operators in printf.

Re-commanded Article : Use of Pre/Post increment operator in same expression

Multiple increment operators inside printf

#include<stdio.h>

void main() {
   int i = 1;
   printf("%d %d %d", i, ++i, i++);
}

Output :

3 3 1

Pictorial representation

Sequence of Printing Evaluating Expressions in Printf

Explanation of program

I am sure you will get confused after viewing the above image and output of program.

  1. Whenever more than one format specifiers (i.e %d) are directly or indirectly related with same variable (i,i++,++i) then we need to evaluate each individual expression from right to left.
  2. As shown in the above image evaluation sequence of expressions written inside printf will be – i++,++i,i
  3. After execution we need to replace the output of expression at appropriate place
NoStepExplanation
1Evaluate i++At the time of execution we will be using older value of i = 1
2Evaluate ++iAt the time of execution we will be increment value already modified after step 1 i.e i = 3
2Evaluate iAt the time of execution we will be using value of i modified in step 2

Below is programmatic representation of intermediate steps –

After Step 1 : printf("%d %d %d",i,++i,1);
After Step 2 : printf("%d %d %d",i,3,1);
After Step 3 : printf("%d %d %d",3,3,1);
After Step 4 : Output displayed - 3,3,1

After solving expressions printing sequence will : i, ++i, i++

Examples of typical expresssions

Post increment operator in expression

We have already studied the pre and post increment operators in c programming. In this tutorial we will be learning different examples of post increment operator.

Examples of post increment operator in expression

Before going through the different examples we can consider below values as initial values for each program explained below –

int i=0,j=0;
int k = 0;
int temp=3;

Depending upon these initial values try to grasp the below programs –

1.Post Increment Verity No 1

int main() {
   int num = 0, result = 0, i = 0;

   for (i = 0; i < 5; i++) {
      result = num++;
      printf("%d", result);
   }
   return 0;
}

Output : [ 0 1 2 3 4 ]

Consider the above program where we have used following statement –

result = num++;

num++ is a post increment which means we are firstly assigning the old value of variable ‘num’ to ‘result’. After assigning the older value of ‘result’ we will be increment value of ‘num’

2.Post Increment Verity No 2

int main() {
   int num = 0, i = 0;

   for (i = 0; i < 5; i++) {
      printf("%d", num++);
   }
   return 0;
}

Output : [ 0 1 2 3 4 ]

Inside printf function we have used post increment operator. It will print the value of variable first and then it will increment the value of variable after execution of printf statement.

3.Post Increment Verity No 3

for(k=0;k<5;k++)
  {
  j = i++ + temp;
  printf("%d",j);
  }

Firstly old value of ‘i’ is added with temp and result of addition is assigned to ‘j’. After addition value of ‘i’ will be incremented

4.Post Increment Verity No 4

for(k=0;k<5;k++)
  {
  j = i++ < temp;
  printf("%d\n",j);
  }

Re-commanded Article : Conditional Operator in Expression

Firstly old value of ‘i’ is compared with ‘temp’ . Then result is assigned to j, and new value is used in second iteration.

5.Post Increment Verity No 5

for(k=0;k<5;k++)
  {
  j = i++;
  printf("%d",i);
  }
  • Old value of ‘i gets reflected only within same expression
  • In the above snippet Old value is assigned to ‘j’ . but in the next statement always ‘new value’ is used.
  • So while printing i Output will be = [1 2 3 4 5].

6.Post Increment Verity No 6

for(k=0;k<5;k++)
  {
  i = i++;
  printf("%d",i);
  }
  • Old value of ‘i is assigned to ‘i’ itself.
  • And again value is incremented after assigning.
  • So while printing i Output will be = [1 2 3 4 5].
  • But this Type is not supported by many compiles .

Note : 

  1. Same things happens while using Post-Decrement.
  2. Post Increment is Unary Operator.
  3. Post-Increment Operator has Highest Priority