Definition and declaration

nishcom

nishcom

@nishcom-cRSnZs Oct 26, 2024
Would anyone please tell me what is the difference between Declaration and Definition in "c"?

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • MaRo

    MaRo

    @maro-Ce3knx Aug 10, 2008

    they're about the look of the function in a program

    Definition is writing the function and how it is working like
    void foo()
    {
    printf("foo");
    }
    
    Declaration is just writing the function header to implement in another function like:
    int main()
    {
    foo(); // this is declaration
    return 0;
    }
    
    And of course to write a declaration there should be a definition to implement.
  • banu

    banu

    @banu-d7GxOI Aug 11, 2008

    Both are based on functions and variables mostly.
    EXAMPLE
    For declaration

    void main()
    {
    write();
    }
    write() is a function call which is declared.

    For definition:
    void write()
    {
    printf("enter the name");
    scanf("%s",name);
    }
    Here the operation/function of write() is defined.
    definition must be given before declaration.
    i guess iam right
  • Amit Saste

    Amit Saste

    @amit-saste-58tp36 Aug 11, 2008

    nishcom
    Would anyone please tell me what is the difference between Declaration and Definition in "c"?
    Just giving simple example:

    Cnsider following as function declaration:
    public void add_ten_nos(int a[]); //This is Declaration

    public void add_ten_nos(int a[])
    {
    int sum;
    for(i=0;i<10;i++) //or whatever the Definition
    sum=sum+a:
    }

    Hope u got it!!
  • umesh936

    umesh936

    @umesh936-MrDSKh Aug 12, 2008

    hi
    Last one is correct(Amit)

    #include<stdio.h>
    void display(); // it is declaration
    int main()
    {
    display(); // here it is called
    }
    // now here is defination of function
    void display()
    {
    printf(" my name is XYZ");
    }
    ///it so simple..
  • mohanapriya

    mohanapriya

    @mohanapriya-97DVxa Aug 12, 2008

    hi i am pri.... actuall dis is my 1st post so i dnt no whether i m posting it to the right place or not.....
    i am in desperate need of a topic artificial inteligence on which i want to present a paper.....i feel engineering is absolutely not interesting until u do something technical....so please guide me ...
  • Mrunalini

    Mrunalini

    @mrunalini-toKmxn Aug 12, 2008

    Hi.. One more additional point is that..

    Function Definitions also known as proto-type, they are just an intimation given to the Compiler that I will be using the proto typed function in the future code execution.

    The syntax for Prototype is:
    <Return type> <Function Name>(Argument Data Types);
    Here semicolon is a must, and must be declared before the Function Definition.

    Whereas when a function is called the code which is executed is Function Definition, with a syntax:

    <Return Type> <Function Name>(arguments and their Datatypes)// No semicolon
    {
    Body of the code
    }


    Where as if we consider a variable.
    when you declare:
    int a;
    you are telling the compiler that I am going to use a variable 'a' as an integer variable, please allocate space for it.

    So here 'a' stores the address which is allocated by the compiler.
    When you say
    a = 5;

    The value 5 is set to the location 'a' in the memory.

    Hope this was helpful.. 😀
  • Prasad Ajinkya

    Prasad Ajinkya

    @prasad-aSUfhP Aug 12, 2008

    Ill keep the difference simple.

    Declaration - you are announcing to the compiler, that such and such function exists. This one shows the existence.
    Definition - you are displaying to the compiler, the exact working of such and such function. This one shows the innards and the existence.
  • Ashraf HZ

    Ashraf HZ

    @Ash Aug 13, 2008

    Ah.. can I use an analogy like this?

    Declaration: Mobile phone brand X
    Definition: 3G, SMS, mobile gaming, mp3 music, organizer etc etc..

    😛
  • regalrohit

    regalrohit

    @regalrohit-5B3Lqu Aug 16, 2008

    hi,in definition,variable space is allocated for variable and for some initial value.

    In declaration only intenifies the type of variable for function.In simple word in definition memory space is allocated.
    In declartion refer to the place where the variable nature is stated,no allocation is done.
    for example

    void show()\\"declaration"//
    {
    printf("singh is king");
    }
    #include<stdio.h>
    void main();\\"definition"
    {
    void show();
    clr scr();
    show();
    getch();
    }
  • shravya

    shravya

    @shravya-Ex8NEd Aug 16, 2008

    Declaration of variable:
    declaration - u can declare variables
    eg:int a;
    here variable 'a ' is declared as integer type that means u can store integers in variable a .
    ///ly if u decalre 'a' as char , you can store characters in a
    eg: char a;
    Definition:
    In functions function definition plays an imp. role .
    EG:
    void fun()//fun definition
    {
    printf("thanx");
    }

    that means here we r not declaring but instead we r writing some code in a function.
  • mata

    mata

    @mata-o6hJDi Aug 17, 2008

    hi friends........
    Declaration - u r telling the compiler in advance dat u will be using that function in future. this include d data type.name of the function and arguments
    Defination - this means dat u r giving functionality or writig body for the function ,what actually a function is going to do
  • niraj.kumar

    niraj.kumar

    @nirajkumar-6nokRG Aug 23, 2008

    In C --- in declaration memory is not allocated
    --- in definition memory get allocated