Showing posts with label C. Show all posts
Showing posts with label C. Show all posts
Tuesday, 1 April 2014
What happens when a function is called before its declaration in C


In C, if a function is called before its declaration, the compiler assumes return type of the function as int.

For example, the following program fails in compilation.

#include <stdio.h>
int main(void)
{
    // Note that func() is not declared
    printf("%d\n", func());
    return 0;
}

char func()
{
   return 'G';
}

The following program compiles and run fine because return type of func() is changed to int.

#include <stdio.h>
int main(void)
{
    printf("%d\n", func());
    return 0;
}

int func()
{
   return 10;
}

What about parameters?
            Compiler assumes nothing about parameters. Therefore, the compiler will not be able to perform compile-time checking of argument types and parity when the function is applied to some arguments. This can cause problems. For example, the following program compiled fine in GCC and produced garbage value as output.

#include <stdio.h>
int main (void)
{
    printf("%d", sum(10, 5));
    return 0;
}
int sum (int b, int c, int a)
{
    return (a+b+c);
}

There is this misconception that the compiler assumes input parameters also int. Had compiler assumed input parameters int, the above program would have failed in compilation.

It is always recommended to declare a function before its use so that we don’t see any surprises when the program is run.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
A few C programs that won’t compile in C++


Although C++ is designed to have backward compatibility with C there can be many C programs that would produce compiler error when compiled with a C++ compiler. Following are some of them :

1) In C++, it is a compiler error to call a function before it is declared. But in C, it may compile (See http://sh.st/qMeGg)

#include<stdio.h>
int main()
{
   func(); // func() is called before its declaration/definition
   return 0;
}

int func()
{
   printf("Hello");
   return 0;
}

2) In C++, it is compiler error to make a normal pointer to point a const variable, but it is allowed in C.

#include <stdio.h>
int main(void)
{
    int const j = 20;

    /* The below assignment is invalid in C++, results in error
       In C, the compiler *may* throw a warning, but casting is
       implicitly allowed */
    int *ptr = &j;  // A normal pointer points to const

    printf("*ptr: %d\n", *ptr);

    return 0;
}

3) In C, a void pointer can directly be assigned to some other pointer like int *, char *. But in C++, a void pointer must be explicitly typcasted.

#include <stdio.h>
int main()
{
    void *vptr;
    int *iptr = vptr; // In C++, it must be replaced with int *iptr = (int *)vptr;
    return 0;
}

This is something we notice when we use malloc(). Return type of malloc() is void *. In C++, we must explicitly typecast return value of malloc() to appropriate type, e.g., “int *p = (void *)malloc(sizeof(int))”. In C, typecasting is not necessary.

4) This is the worst answer among all, but still a valid answer. We can use one of the C++ specific keywords as variable names. The program won’t compile in C++, but would compiler in C.

#include <stdio.h>
int main(void)
{
    int new = 5;  // new is a keyword in C++, but not in C
    printf("%d", new);
}

Similarly, we can use other keywords like delete, explicit, class, .. etc.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Wednesday, 12 March 2014
C Program for Simple Calculator


A Simple Calculator program in C Language :

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

main()
{
  int x,y,ans,i;
  int choice;
  float div;
  char loop;
  ans=0;

  clrscr();

  do
  {
      printf("\n Do you wish to continue (Y/N) : ");
      scanf("%s",&loop);

      if (loop=='y' || loop=='Y')
      {
      clrscr();
      printf("\n Enter any two numbers ");
      printf("\n --------------------- ");

      printf("\n\n Enter the first number : ");
      scanf("%d",&x);

    printf("\n Enter the second number : ");
     scanf("%d",&y);

     clrscr();
     printf("\n Select the operation to be carried out ");
     printf("\n -------------------------------------- ");

     printf("\n 1. Addition ");
     printf("\n 2. Substraction ");
     printf("\n 3. Multiplication ");
     printf("\n 4. Division ");

     printf("\n Enter your choice : ");
     scanf("%d",&choice);

  switch(choice)
  {
     case 1 :
     {
       ans = x+y;
       printf("\n Answer = %d",ans);
       break;
     }
     case 2 :
     {
          ans = x-y;
          printf("\n Answer = %d", ans);
          break;
     }
     case 3 :
     {
          ans = x*y;
          printf("\n Answer = %d", ans);
          break;
     }
     case 4:
     {
          div = x/y;
          printf("\n Answer = %.2f", div);
          break;
     }
     default:
       printf("\n\n Illegal operation......");
       break;
   }
  }
  else
      printf("\n Bye....... Bye...........");
      getch();
 }while(loop=='y' || loop=='Y');
}

For more similar posts :  Click here

Program for Analog Clock in C Graphics


C Program  to draw an Analog Clock using <graphics.h> :

#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
#include<dos.h>

void main()

{

int gd=DETECT,gm;

int x=320,y=240,r=200,i,h,m,s,thetamin,thetasec;

struct  time t;

char n[12][3]={"3","2","1","12","11","10","9","8","7","6","5","4"};

initgraph(&gd,&gm,"");

circle(x,y,210);

setcolor(4);

settextstyle(4,0,5);

for(i=0;i<12;i++)

{

if(i!=3)

outtextxy(x+(r-14)*cos(M_PI/6*i)-10,y-(r-14)*sin(M_PI/6*i)-26,n[i]);

else

outtextxy(x+(r-14)*cos(M_PI/6*i)-20,y-(r-14)*sin(M_PI/6*i)-26,n[i]);

}

gettime(&t);

printf("The current time is: %2d:%02d:%02d.%02d",t.ti_hour, t.ti_min,t.ti_sec, t.ti_hund);

while(!kbhit())

{

setcolor(5);

setfillstyle(1,5);

circle(x,y,10);

floodfill(x,y,5);

gettime(&t);

if(t.ti_min!=m)

{

setcolor(0);

line(x,y,x+(r-60)*cos(thetamin*(M_PI/180)),y-(r-60)*sin(thetamin*(M_PI/180

)));

circle(x+(r-80)*cos(thetamin*(M_PI/180)),y-(r-80)*sin(thetamin*(M_PI/180))

,10);

line(x,y,x+(r-110)*cos(M_PI/6*h-((m/2)*(M_PI/180))),y-(r-110)*sin(M_PI/6*h

-((m/2)*(M_PI/180))));

circle(x+(r-130)*cos(M_PI/6*h-((m/2)*(M_PI/180))),y-(r-130)*sin(M_PI/6*h-(

(m/2)*(M_PI/180))),10);

}

if(t.ti_hour>12)

t.ti_hour=t.ti_hour-12;

if(t.ti_hour<4)

h=abs(t.ti_hour-3);

else

h=15-t.ti_hour;

m=t.ti_min;

if(t.ti_min<=15)

thetamin=(15-t.ti_min)*6;

else

thetamin=450-t.ti_min*6;

if(t.ti_sec<=15)

thetasec=(15-t.ti_sec)*6;

else

thetasec=450-t.ti_sec*6;

setcolor(4);

line(x,y,x+(r-110)*cos(M_PI/6*h-((m/2)*(M_PI/180))),y-(r-110)*sin(M_PI/6*h

-((m/2)*(M_PI/180))));

circle(x+(r-130)*cos(M_PI/6*h-((m/2)*(M_PI/180))),y-(r-130)*sin(M_PI/6*h-(

(m/2)*(M_PI/180))),10);

line(x,y,x+(r-60)*cos(thetamin*(M_PI/180)),y-(r-60)*sin(thetamin*(M_PI/180

)));

circle(x+(r-80)*cos(thetamin*(M_PI/180)),y-(r-80)*sin(thetamin*(M_PI/180))

,10);

setcolor(15);

line(x,y,x+(r-70)*cos(thetasec*(M_PI/180)),y-(r-70)*sin(thetasec*(M_PI/180

)));

delay(1000);

setcolor(0);

line(x,y,x+(r-70)*cos(thetasec*(M_PI/180)),y-(r-70)*sin(thetasec*(M_PI/180

)));

}

}