NO

Author Topic: need help on a C program(beginner)  (Read 6379 times)

xpranav

  • Guest
need help on a C program(beginner)
« on: October 29, 2009, 04:37:58 AM »
i am a beginner in C and need help....have a 64-bit vista (Pelles C 64-bit).......below is a program for adding 2 nos........it is made in win64 cosole (EXE) project....

#include<stdio.h>
#include<conio.h>
    main()

{
   float x,y,z;
   clrscr();
   printf("\nEnter the value of x :");
   scanf("%f",& x);
   printf("\nEnter the value of y :");
   scanf("%f",& y);

   z=x+y;

   printf("\nAnswer =%f",z);
   getch();

}


Building add.obj.
C:\Users\pranav\Documents\Pelles C Projects\Samples\Standard C\Hello\add.c(3): warning #2099: Missing type specifier; assuming 'int'.
C:\Users\pranav\Documents\Pelles C Projects\Samples\Standard C\Hello\add.c(7): warning #2027: Missing prototype for 'clrscr'.
C:\Users\pranav\Documents\Pelles C Projects\Samples\Standard C\Hello\add.c(16): warning #2027: Missing prototype for 'getch'.
Done.


what is missing here............can somebody help........and what is prototype here..........is working of Pelles C different from Turbo C?...........is there some shotcut key to compile,execute etc. in Pelles C.

thank u all

Offline AlexN

  • Global Moderator
  • Member
  • *****
  • Posts: 394
    • Alex's Link Sammlung
Re: need help on a C program(beginner)
« Reply #1 on: October 29, 2009, 07:46:38 AM »
    main()

Should be:
int main()

and select in the project options: "Define compatibility names".
best regards
 Alex ;)

xpranav

  • Guest
Re: need help on a C program(beginner)
« Reply #2 on: October 29, 2009, 02:47:39 PM »
thnx a lot Alex........i'm done

xpranav

  • Guest
Re: need help on a C program(beginner)
« Reply #3 on: October 29, 2009, 02:50:32 PM »
thanks again Alex  ;D but can u illustrate what "missing prototype" means...........i'm just asking for my knowledge purpose

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: need help on a C program(beginner)
« Reply #4 on: October 29, 2009, 03:18:50 PM »
but can u illustrate what "missing prototype" means

In general it means that the function or data type is not defined.

If you check the help file, you will notice that getch starts with an underscore, so the correct use is _getch(), if you do not set the switch to use compatibility names..
---
Stefan

Proud member of the UltraDefrag Development Team

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: need help on a C program(beginner)
« Reply #5 on: October 30, 2009, 04:00:47 AM »
To enforce the check for errors and type mismatch when passing parameters to a function or using the return values the C99 standard require that you explicitely declare the type of variables involved in your functions. The knowledge of this info's allows the compiler to check for errors. For example if you are trying to pass a char pointer where an int is required. Moreover it allows compiler to make automatic conversion for the compatible types (i.e int to float, float to double, etc).
The way you tell (declare) to the compiler which types of data your function requires as parameters, and which type of data the function give back as a result is called a prototype.
A prototype is the header line of a function without the body (you can just copy the header and terminate it with ";").

I.e.

Code: [Select]
int myfunc(int a, float b);

int myfunc(int a, float b)
{
      return a + b;
}

In the sample above the first time the function appears it acts as a prototype, it tells the compiler how many parameters requires, which data type are required for parameters and which type of data returns.
So when you use the function the compiler can check if all values are compliant or compatible and give you warning or errors if the number of arguments is wrong, or the data types are incompatible and so on.
A prototype to be effective have to appear of course before you call the function. if you put the prototype after the compiler don't know yet the function characteristics and cannot make any check.
If no prototype exist, or is misplaced (after call) the compiler assumes K&R syntax that states that when a data type is unknown it would be assumed to be an int. Of course if your data is float, double or a pointer the result will be disastrous.
The prototypes are generally put in an include file (.h) and included at beginning of your program (like: #include <stdio.h>) so they are available to the compiler before any line of your code.
The prototypes are largely used to allow checking against library code where source is not available.
If take a look to stdio.h for example you'll find a lot of library functions prototypes.

Last thing, in prototypes you can omit the variable names becouse the compiler need to know the types of data and don't care about the name you assigned to them. So myfunc prototype can be written also:
Code: [Select]
int myfunc(int, float);
The C99 defines the standard names for library functions. M$ put in its compiler many functions specific for the use on M$DOS and WINDOWS systems that becomes a defacto standards only for M$ systems, but the C99 standard has never included that functions in the standard set.
PelleC intend to be fully compliant with C99 standard, so that functions are renamed, generally prefixing an underscore to the M$ name. Anyway to allow the compilation of old pieces of code without a lot of changes, and also to keep compliance with actual M$ compilers, has been added the compatibility names switch for this purpose as Stefan said.
« Last Edit: October 30, 2009, 04:13:52 AM by frankie »
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

xpranav

  • Guest
Re: need help on a C program(beginner)
« Reply #6 on: October 31, 2009, 06:17:54 PM »
thanks Stefan Pendl & frankie.........dude thanks for all that great info....i guess i'm going to learn C from really nice and for sure great people like u. :)
« Last Edit: October 31, 2009, 06:20:24 PM by xpranav »