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 Array name is base address

This tutorial is just to show how array name holds a base address of an array.

Array name is base address

Consider the below example of an array in c programming –

#include<stdio.h>

void main()
{
 char s[]="cat";
 int i;
 
  for(i=0;s[i];i++)
      printf("\n%c %c %c %c",s[i],*(s+i),*(i+s),i[s]);
}

Answer

c c c c
a a a a
t t t t

Explanation :

  1. Array name is the base address of the array, here ‘s’ holds the base address of an array.
  2. ‘i’  is the index number/displacement from the base address.
  3. Array arr[i] can be Represented as –
arr[i] = *(array name + displacement)
       = *(arr + i)
       = Value at (Array name + Displacement)
       = Value at (Base Address + Displacement)

similarly we can conclude that –

s[i] = *(s + i)
i[s] = *(i + s)

Consider the above example with practical example, Suppose we have an array having starting address 1000 –

Array name is base address

Now in this case array ‘s’ represents an array of character so we can say that ‘s’ holds a base address of a character array.

RepresentationExplanationExpressionValue
s[i]First element of array*(s + 1)'a'
i[s]First element of array*(s + 1)'a'
*(s+i)First element of array*(s + 1)'a'
*(i+s)First element of array*(s + 1)'a'

Pre Incrementing Macro : C Preprocessor MCQ

This is typical question related to preprocessor directive. What will happen if we perform Pre incrementing macro operation.

Pre Incrementing Macro Question

Guess What will be the output of following code snippet when we perform pre incrementing macro operation –

#include<stdio.h>
#define MAX 10

int main(){
    int num;
    num = ++MAX;
    printf("%d",num);
    return 0;
}

Options :

NoOptions
A10
B11
CCompile Error
DRun Time Error

Output of the Above Program


Explanation :

num = ++MAX;

will be expanded as

num = ++10;
    = Compile Error

Macro Preprocessor only replaces occurance of macro symbol with macro symbol value so preprocessor will replace MAX with 10.

Expanded Program :

int main(){
    int num;
    num = ++10;
    printf("%d",num);
    return 0;
}

Above expanded code is now given to compiler. Compiler will throw error : Lvalue Require Error. (because Increment Operator and Decrement Operators should be used with variable)

Pre Incrementing Macro

Pre incrementing macro will results into error as compiler tries to increment the constant number.

Sizeof Operator MCQ : sizeof operator and Function

Must Read Tip To Solve Following Problem :

  1. First one must understand that – sizeof is not an function it’s an operator.
  2. Inside C Program we can calculate size of all data types including user defined and in built data types.
  3. However Sizeof Operator cannot calculate Size when we pass function name as parameter.
  4. However Sizeof Operator can take address of Function because in c “Address of variable or function” is always of type integer.

Sizeof Operator Question :

Now try to solve following question by referring above tips –

#include<stdio.h>

int main()
{

printf("\n%d",sizeof(&main)+2);

return(0);
}

Output with Explanation

4

Re-commanded Reading : Size of structure without Sizeof Operator

Explanation :

  1. &main will gives us Address where we have defined the actual function.
  2. sizeof operator will treat address as “integer” data type.
  3. In the 32 bit compiler, Size of Integer = 2 bytes (in Turbo C/C++ Compiler), therefor output is 4.
  4. Requirement of the memory for storing the integer data type will vary from from compiler to compiler.
  5. Consider the below program –
    sizeof operator

  6. Now we have main function at any random memory location say 1000 then –
    &main = 1000
    
  7. Thus 1000 is an address of the memory location thus we can say that sizeof function will always return us the size of starting address.
  8. We cannot compute the sizeof the function by passing the function name of address of function as parameter to sizeof operator.

Must Read : Sizeof Pointer | Nested Sizeof | Size of void pointer

C return value of main function

In this example we are going to see, what is return value of main function.

Return value of main function

#include<stdio.h>

int main()
{
int c = 5;

printf("%d", main || c);

return(0);
}

Output :

1

Explanation :

  1. The return value of main function is always a positive Value.
  2. Inside C Program, Function is nothing but set of instructions combined together in a single block.
  3. Function Block may have certain starting address.
  4. This Address gets printed when we try to print function name inside printf statement.

When we try below statement then –

printf("%d", main);

then each time we get different output. While writing this article value printed on the screen was 657

C return value of main function

OR Operator with main

In the above example we have OR’ing of two positive numbers so we will get 1

int c = 5;
printf("%d", main || c);

Return value of main function is positive non-zero number. c is also non-zero number.

So ORing of two non zero numbers is always TRUE i.e 1 in C Programming.

Re-commanded Articles :

While going through the stackoverflow, I found good discussion on the similar topic. Topic contain the discussion about return value of main function.

C Printf Puzzle 1 : Guess value of X : “Hello World”

In this example we need to find the value of X in such a way that we should get “Hello World” as output

C programming puzzle

#include<stdio.h>
void main()
{
if(X)
  {
   printf("Hello");
  }
else
  {
   printf("World");
  }
}

What would be value of X in order to get Hello World as output ?

Different ways to solve c programming puzzle

Way 1 : Use of printf statement

#include<stdio.h>
void main()
{
if(printf("Hello")-5)
  {
   printf("Hello");
  }
else
  {
   printf("World");
  }
}

Explanation of the Program :

  1. Printf Returns Number of Characters Printed On Screen
  2. Printf Inside “If Statement” will print “Hello” and returns Integer(number of Charcaters Printed)
  3. Subtract 5 from That number so We will get if(0) which is False Condition.
  4. Inside “else” block we are printing “world” .

Way 2 : Output :

#include<stdio.h>
void main()
{
if(!printf("Hello"))
  {
   printf("Hello");
  }
else
  {
   printf("World");
  }
}

Way 3 : Output :

#include<stdio.h>
void main()
{
if((printf("Hello") > 0 ? 0 : 1))
  {
   printf("Hello");
  }
else
  {
   printf("World");
  }
}

Variable Name validity : Switch case MCQ

Switch case & Variable Name Validity :

This Program at a First Glance is error Free but It has one Serious Bug , Will you remove it without compiling it ?

#include<stdio.h>

void OS_Solaris_print()
{
    printf("Solaris - Sun Microsystemsn");
}

void OS_Windows_print()
{
    printf("Windows - Microsoftn");
}

void OS_HP-UX_print()
{
    printf("HP-UX - Hewlett Packardn");
}

int main()
{
    int num;
    
    printf("Enter the number (1-3):\n");
    scanf("%d",&num);
    
    switch(num)
    {
        case 1:
            OS_Solaris_print();
            break;
        case 2:
            OS_Windows_print();
            break;
        case 3:
            OS_HP-UX_print();
            break;
        default:
            printf("Hmm! only 1-3 \n");
            break;
    }
}

Output :

Compile Error

Explanation

Consider the above program and look into program carefully. You will find below statement inside the program.

case 3:
    OS_HP-UX_print();
    break

Now we can see name of the function name is not a valid identifier in C programming.

Re-commanded Tutorial : Rules for declaring valid identifiers in C programming.

Variable Name validity

OS_HP-UX_Print() is not a valid identifier or function name. Special character dash should not be used in identifier or variable name or in function name.

We can use following function name instead the one specified in the above example –

OS_HP_UX_print();

Printf MCQ 24 : Character array of Length = 2 Assigned to Character Variable

Printf Multiple Choice Questions 24  : Character array of Length = 2 Assigned to Character Variable


Predict the Output of the Following Program if Given Things are – 

#include<stdio.h>
void main()
{
char ch = 'AB';
printf("%c",ch);
}

Options :

  1. A
  2. AB
  3. Compile Error
  4. Run Time Error

[toggle title=”Output”]A[/toggle]


Switch to Printf MCQ Home :  Click Here


How and Why ?

  1. Output is ‘A’ is Printed
  2. Reason being that , Character Variable can atmost array of Two Characters
  3. Example ‘n’ Though it has two Characters but it is Interpreted as Single Character , Similarly ‘AB’ is theoratically considered as Single Variable

Rule :

  1. Character Variable can Store atmost Two Characters

Function returning printf as parameter

In this example, we will be learning, how can we write function returning printf statement ? Tutorial also explains the flow of program execution.

Function returning printf as parameter

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

int func1() {
   return(printf("Hello"));
}

void main()
{
  int i;
  i = func1();
  printf("Value of i = %d",i);
}

Output :

Hello
Value of i = 5

Program explanation

We know that, printf statement returns the integer value i.e total number of characters printed on the screen.

ExampleReturn ValueExplanation
printf("Hello");5Total Value printed on screen = 5
printf("Hi Abhi");7Space is considered as printable character
printf("123");3Digits can be considered as printable
printf("hi\n\t");4\t and \n are considered as single character

In the above example firstly printf gets executed and the return value of printf statement is returned to main function.

Flow of execution

  1. Firstly func1() is called from the main function.
  2. In the func1() function printf() statement is called.
  3. It will print the message on the screen and return value of the printf() statement will be returned to main() function
  4. So function func1() will return integer 5 to main function and return value gets stored inside the variable 'i'

Function passing printf as parameter

We already know instead writing function returning printf, we can also pass printf as parameter to the function. Working of the printf statement as parameter is shown in the below program –

#include<stdio.h>

int sum(int i,int j) {
   return(i+j);
}

void main()
{
  int i;
  i = sum(printf("Hi\n"),printf("Hello\n"));
  printf("Value of i = %d",i);
}

Output :

Hello
Hi
Value of i = 9

In the above example 2nd parameter i.e 2nd printf() statement is printed firstly on the screen because the comma operator returns the rightmost operand

Now when printf statement gets executed completely then function gets invoked like this –

i = sum(printf("Hi\n"),printf("Hello\n"));
  = sum(3,6);
  = 9

Printf MCQ 10 : Backslash Character in Printf

Printf Multiple Choice Questions  : Backslash Character in Printf


Question : Guess the output of Program

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

void main()
{
clrscr();
char *str1="Indii";
char *str2="ian";
printf("%s\b\b%s",str1,str2);
getch();
}

Options :

  1. Indiiian
  2. ianindii
  3. Indian
  4. Indiian

[toggle title=”Output”]Indian[/toggle]


Switch to Printf MCQ Home :  Click Here


How and Why ?

  1. ‘b’ – Backslash Character Moves Cursor Position one Character Back
  2. In Printf there are two ‘b’ so It Moves Cursor Position on 4th Position  ‘i’ (Indii)
  3. After it Overwirte “ii” by “ian” and gives “indian”