data type

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • aman

    data type

    is it true that c compiler automatically picks a variable as an
    integer if we can't explicitly declare it at beginning of a program????

  • santosh

    #2
    Re: data type

    aman wrote:
    is it true that c compiler automatically picks a variable as an
    integer if we can't explicitly declare it at beginning of a program????
    No. In C objects have to be declared before they are used.

    Comment

    • santosh

      #3
      Re: data type

      santosh wrote:
      aman wrote:
      >
      >is it true that c compiler automatically picks a variable as an
      >integer if we can't explicitly declare it at beginning of a program????
      >
      No. In C objects have to be declared before they are used.
      I should amend that for global objects it's true that the type defaults to
      int when it's not specifically mentioned. However it's extremely bad
      practise.

      Comment

      • Richard Heathfield

        #4
        Re: data type

        santosh said:
        aman wrote:
        >
        >is it true that c compiler automatically picks a variable as an
        >integer if we can't explicitly declare it at beginning of a program????
        >
        No. In C objects have to be declared before they are used.
        True in C99, but who's got that?

        rjh@linux:~/scratchcat foo.c
        #include <stdio.h>
        foo(x)
        {
        return printf("%d\n", x);
        }
        int main(void)
        {
        foo(42);

        return 0;
        }

        me@here:~/scratchmake
        gcc -W -Wall -ansi -pedantic -Wformat-nonliteral -Wcast-align
        -Wpointer-arith -Wbad-function-cast -Wmissing-prototypes
        -Wstrict-prototypes -Wmissing-declarations -Winline -Wundef
        -Wnested-externs -Wcast-qual -Wshadow -Wconversion -Wwrite-strings
        -Wno-conversion -ffloat-store -O2 -g -pg -c -o foo.o foo.c
        foo.c:3: warning: return-type defaults to `int'
        foo.c:3: warning: function declaration isn't a prototype
        foo.c: In function `foo':
        foo.c:3: warning: type of `x' defaults to `int'
        gcc -W -Wall -ansi -pedantic -Wformat-nonliteral -Wcast-align
        -Wpointer-arith -Wbad-function-cast -Wmissing-prototypes
        -Wstrict-prototypes -Wmissing-declarations -Winline -Wundef
        -Wnested-externs -Wcast-qual -Wshadow -Wconversion -Wwrite-strings
        -Wno-conversion -ffloat-store -O2 -g -pg -o foo foo.o -lm
        me@here:~/scratch./foo
        42
        me@here:~/scratch>

        --
        Richard Heathfield <http://www.cpax.org.uk >
        Email: -http://www. +rjh@
        Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
        "Usenet is a strange place" - dmr 29 July 1999

        Comment

        • Ben Pfaff

          #5
          Re: data type

          Richard Heathfield <rjh@see.sig.in validwrites:
          santosh said:
          >
          >aman wrote:
          >>
          >>is it true that c compiler automatically picks a variable as an
          >>integer if we can't explicitly declare it at beginning of a program????
          >>
          >No. In C objects have to be declared before they are used.
          >
          True in C99, but who's got that?
          >
          rjh@linux:~/scratchcat foo.c
          #include <stdio.h>
          foo(x)
          I believe that this is a declaration of x. (It is a declaration
          that does not explicitly give the type of x.)
          {
          return printf("%d\n", x);
          }
          --
          "You call this a *C* question? What the hell are you smoking?" --Kaz

          Comment

          • Richard Heathfield

            #6
            Re: data type

            Ben Pfaff said:
            Richard Heathfield <rjh@see.sig.in validwrites:
            >
            <snip>
            >>
            >rjh@linux:~/scratchcat foo.c
            >#include <stdio.h>
            >foo(x)
            >
            I believe that this is a declaration of x. (It is a declaration
            that does not explicitly give the type of x.)
            I, in turn, believe that this was roughly what the OP was getting at - i.e.
            implicit int.

            <snip>

            --
            Richard Heathfield <http://www.cpax.org.uk >
            Email: -http://www. +rjh@
            Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
            "Usenet is a strange place" - dmr 29 July 1999

            Comment

            • jacob navia

              #7
              Re: data type

              Richard Heathfield wrote:
              santosh said:
              >
              >aman wrote:
              >>
              >>is it true that c compiler automatically picks a variable as an
              >>integer if we can't explicitly declare it at beginning of a program????
              >No. In C objects have to be declared before they are used.
              >
              True in C99, but who's got that?
              >
              rjh@linux:~/scratchcat foo.c
              #include <stdio.h>
              foo(x)
              {
              return printf("%d\n", x);
              }
              int main(void)
              {
              foo(42);
              >
              return 0;
              }
              >
              me@here:~/scratchmake
              gcc -W -Wall -ansi -pedantic -Wformat-nonliteral -Wcast-align
              -Wpointer-arith -Wbad-function-cast -Wmissing-prototypes
              -Wstrict-prototypes -Wmissing-declarations -Winline -Wundef
              -Wnested-externs -Wcast-qual -Wshadow -Wconversion -Wwrite-strings
              -Wno-conversion -ffloat-store -O2 -g -pg -c -o foo.o foo.c
              foo.c:3: warning: return-type defaults to `int'
              foo.c:3: warning: function declaration isn't a prototype
              foo.c: In function `foo':
              foo.c:3: warning: type of `x' defaults to `int'
              gcc -W -Wall -ansi -pedantic -Wformat-nonliteral -Wcast-align
              -Wpointer-arith -Wbad-function-cast -Wmissing-prototypes
              -Wstrict-prototypes -Wmissing-declarations -Winline -Wundef
              -Wnested-externs -Wcast-qual -Wshadow -Wconversion -Wwrite-strings
              -Wno-conversion -ffloat-store -O2 -g -pg -o foo foo.o -lm
              me@here:~/scratch./foo
              42
              me@here:~/scratch>
              >
              heathfield is using the wrong compiler!

              d:\lcc\test>typ e th1.c
              #include <stdio.h>
              foo(x)
              {
              return printf("%d\n", x);
              }
              int main(void)
              {
              foo(42);

              return 0;
              }

              d:\lcc\test>lc th1.c
              Warning th1.c: 2 no type specified. Defaulting to int
              Warning th1.c: 8 Missing prototype for 'foo'
              0 errors, 2 warnings

              --
              jacob navia
              jacob at jacob point remcomp point fr
              logiciels/informatique

              Comment

              • Joachim Schmitz

                #8
                Re: data type

                "jacob navia" <jacob@nospam.o rgschrieb im Newsbeitrag
                news:47028ae8$0 $5103$ba4acef3@ news.orange.fr. ..
                Richard Heathfield wrote:
                >santosh said:
                >>
                >>aman wrote:
                >>>
                >>>is it true that c compiler automatically picks a variable as an
                >>>integer if we can't explicitly declare it at beginning of a program????
                >>No. In C objects have to be declared before they are used.
                >>
                >True in C99, but who's got that?
                >>
                >rjh@linux:~/scratchcat foo.c
                >#include <stdio.h>
                >foo(x)
                >{
                > return printf("%d\n", x);
                >}
                >int main(void)
                >{
                > foo(42);
                >>
                > return 0;
                >}
                > me@here:~/scratchmake
                >gcc -W -Wall -ansi -pedantic -Wformat-nonliteral -Wcast-align -Wpointer-arith
                > -Wbad-function-cast -Wmissing-prototypes -Wstrict-prototypes -Wmissing-declarations
                > -Winline -Wundef -Wnested-externs -Wcast-qual -Wshadow -Wconversion -Wwrite-strings
                > -Wno-conversion -ffloat-store -O2 -g -pg -c -o foo.o foo.c
                >foo.c:3: warning: return-type defaults to `int'
                >foo.c:3: warning: function declaration isn't a prototype
                >foo.c: In function `foo':
                >foo.c:3: warning: type of `x' defaults to `int'
                >gcc -W -Wall -ansi -pedantic -Wformat-nonliteral -Wcast-align -Wpointer-arith
                > -Wbad-function-cast -Wmissing-prototypes -Wstrict-prototypes -Wmissing-declarations
                > -Winline -Wundef -Wnested-externs -Wcast-qual -Wshadow -Wconversion -Wwrite-strings
                > -Wno-conversion -ffloat-store -O2 -g -pg -o foo foo.o -lm
                >me@here:~/scratch./foo
                >42
                >me@here:~/scratch>
                >
                heathfield is using the wrong compiler!
                >
                d:\lcc\test>typ e th1.c
                #include <stdio.h>
                foo(x)
                {
                return printf("%d\n", x);
                }
                int main(void)
                {
                foo(42);
                >
                return 0;
                }
                >
                d:\lcc\test>lc th1.c
                Warning th1.c: 2 no type specified. Defaulting to int
                Warning th1.c: 8 Missing prototype for 'foo'
                0 errors, 2 warnings
                Why? He basically got the same warnings as you got. (When compiling, not
                when linking, no surprise there)
                Hmm, actually he got 3 warnings, your compiler produced 2

                Bye, Jojo


                Comment

                • Ben Pfaff

                  #9
                  Re: data type

                  Richard Heathfield <rjh@see.sig.in validwrites:
                  Ben Pfaff said:
                  >
                  >Richard Heathfield <rjh@see.sig.in validwrites:
                  >>rjh@linux:~/scratchcat foo.c
                  >>#include <stdio.h>
                  >>foo(x)
                  >>
                  >I believe that this is a declaration of x. (It is a declaration
                  >that does not explicitly give the type of x.)
                  >
                  I, in turn, believe that this was roughly what the OP was getting at - i.e.
                  implicit int.
                  I can see that the OP's article could be interpreted that way.
                  It wasn't my interpretation.
                  --
                  "The fact that there is a holy war doesn't mean that one of the sides
                  doesn't suck - usually both do..."
                  --Alexander Viro

                  Comment

                  • santosh

                    #10
                    Re: data type

                    jacob navia wrote:
                    Richard Heathfield wrote:
                    >santosh said:
                    >>
                    >>aman wrote:
                    >>>
                    >>>is it true that c compiler automatically picks a variable as an
                    >>>integer if we can't explicitly declare it at beginning of a program????
                    >>No. In C objects have to be declared before they are used.
                    >>
                    >True in C99, but who's got that?
                    >>
                    >rjh@linux:~/scratchcat foo.c
                    >#include <stdio.h>
                    >foo(x)
                    >{
                    > return printf("%d\n", x);
                    >}
                    >int main(void)
                    >{
                    > foo(42);
                    >>
                    > return 0;
                    >}
                    >>
                    >me@here:~/scratchmake
                    >gcc -W -Wall -ansi -pedantic -Wformat-nonliteral -Wcast-align
                    >-Wpointer-arith -Wbad-function-cast -Wmissing-prototypes
                    >-Wstrict-prototypes -Wmissing-declarations -Winline -Wundef
                    >-Wnested-externs -Wcast-qual -Wshadow -Wconversion -Wwrite-strings
                    >-Wno-conversion -ffloat-store -O2 -g -pg -c -o foo.o foo.c
                    >foo.c:3: warning: return-type defaults to `int'
                    >foo.c:3: warning: function declaration isn't a prototype
                    >foo.c: In function `foo':
                    >foo.c:3: warning: type of `x' defaults to `int'
                    >gcc -W -Wall -ansi -pedantic -Wformat-nonliteral -Wcast-align
                    >-Wpointer-arith -Wbad-function-cast -Wmissing-prototypes
                    >-Wstrict-prototypes -Wmissing-declarations -Winline -Wundef
                    >-Wnested-externs -Wcast-qual -Wshadow -Wconversion -Wwrite-strings
                    >-Wno-conversion -ffloat-store -O2 -g -pg -o foo foo.o -lm
                    >me@here:~/scratch./foo
                    >42
                    >me@here:~/scratch>
                    >>
                    >
                    heathfield is using the wrong compiler!
                    Why? gcc is emitting all the diagnostics that you'd expect and one more than
                    what lcc-win32 emits.

                    Comment

                    • jacob navia

                      #11
                      Re: data type

                      santosh wrote:
                      jacob navia wrote:
                      >
                      >Richard Heathfield wrote:
                      >>santosh said:
                      >>>
                      >>>aman wrote:
                      >>>>
                      >>>>is it true that c compiler automatically picks a variable as an
                      >>>>integer if we can't explicitly declare it at beginning of a program????
                      >>>No. In C objects have to be declared before they are used.
                      >>True in C99, but who's got that?
                      >>>
                      >>rjh@linux:~/scratchcat foo.c
                      >>#include <stdio.h>
                      >>foo(x)
                      >>{
                      >> return printf("%d\n", x);
                      >>}
                      >>int main(void)
                      >>{
                      >> foo(42);
                      >>>
                      >> return 0;
                      >>}
                      >>>
                      >>me@here:~/scratchmake
                      >>gcc -W -Wall -ansi -pedantic -Wformat-nonliteral -Wcast-align
                      >>-Wpointer-arith -Wbad-function-cast -Wmissing-prototypes
                      >>-Wstrict-prototypes -Wmissing-declarations -Winline -Wundef
                      >>-Wnested-externs -Wcast-qual -Wshadow -Wconversion -Wwrite-strings
                      >>-Wno-conversion -ffloat-store -O2 -g -pg -c -o foo.o foo.c
                      >>foo.c:3: warning: return-type defaults to `int'
                      >>foo.c:3: warning: function declaration isn't a prototype
                      >>foo.c: In function `foo':
                      >>foo.c:3: warning: type of `x' defaults to `int'
                      >>gcc -W -Wall -ansi -pedantic -Wformat-nonliteral -Wcast-align
                      >>-Wpointer-arith -Wbad-function-cast -Wmissing-prototypes
                      >>-Wstrict-prototypes -Wmissing-declarations -Winline -Wundef
                      >>-Wnested-externs -Wcast-qual -Wshadow -Wconversion -Wwrite-strings
                      >>-Wno-conversion -ffloat-store -O2 -g -pg -o foo foo.o -lm
                      >>me@here:~/scratch./foo
                      >>42
                      >>me@here:~/scratch>
                      >>>
                      >heathfield is using the wrong compiler!
                      >
                      Why? gcc is emitting all the diagnostics that you'd expect and one more than
                      what lcc-win32 emits.
                      >
                      Gosh!

                      I just did not see them in all the linking stuff!!

                      Excuse me. I will eat my hat now!
                      :-)


                      --
                      jacob navia
                      jacob at jacob point remcomp point fr
                      logiciels/informatique

                      Comment

                      • jacob navia

                        #12
                        Re: data type

                        Joachim Schmitz wrote:
                        "jacob navia" <jacob@nospam.o rgschrieb im Newsbeitrag
                        news:47028ae8$0 $5103$ba4acef3@ news.orange.fr. ..
                        >Richard Heathfield wrote:
                        >>santosh said:
                        >>>
                        >>>aman wrote:
                        >>>>
                        >>>>is it true that c compiler automatically picks a variable as an
                        >>>>integer if we can't explicitly declare it at beginning of a program????
                        >>>No. In C objects have to be declared before they are used.
                        >>True in C99, but who's got that?
                        >>>
                        >>rjh@linux:~/scratchcat foo.c
                        >>#include <stdio.h>
                        >>foo(x)
                        >>{
                        >> return printf("%d\n", x);
                        >>}
                        >>int main(void)
                        >>{
                        >> foo(42);
                        >>>
                        >> return 0;
                        >>}
                        >> me@here:~/scratchmake
                        >>gcc -W -Wall -ansi -pedantic -Wformat-nonliteral -Wcast-align -Wpointer-arith
                        >> -Wbad-function-cast -Wmissing-prototypes -Wstrict-prototypes -Wmissing-declarations
                        >> -Winline -Wundef -Wnested-externs -Wcast-qual -Wshadow -Wconversion -Wwrite-strings
                        >> -Wno-conversion -ffloat-store -O2 -g -pg -c -o foo.o foo.c
                        >>foo.c:3: warning: return-type defaults to `int'
                        >>foo.c:3: warning: function declaration isn't a prototype
                        >>foo.c: In function `foo':
                        >>foo.c:3: warning: type of `x' defaults to `int'
                        >>gcc -W -Wall -ansi -pedantic -Wformat-nonliteral -Wcast-align -Wpointer-arith
                        >> -Wbad-function-cast -Wmissing-prototypes -Wstrict-prototypes -Wmissing-declarations
                        >> -Winline -Wundef -Wnested-externs -Wcast-qual -Wshadow -Wconversion -Wwrite-strings
                        >> -Wno-conversion -ffloat-store -O2 -g -pg -o foo foo.o -lm
                        >>me@here:~/scratch./foo
                        >>42
                        >>me@here:~/scratch>
                        >heathfield is using the wrong compiler!
                        >>
                        >d:\lcc\test>ty pe th1.c
                        >#include <stdio.h>
                        >foo(x)
                        >{
                        > return printf("%d\n", x);
                        >}
                        >int main(void)
                        >{
                        > foo(42);
                        >>
                        > return 0;
                        >}
                        >>
                        >d:\lcc\test> lc th1.c
                        >Warning th1.c: 2 no type specified. Defaulting to int
                        >Warning th1.c: 8 Missing prototype for 'foo'
                        >0 errors, 2 warnings
                        Why? He basically got the same warnings as you got. (When compiling, not
                        when linking, no surprise there)
                        Hmm, actually he got 3 warnings, your compiler produced 2
                        >
                        Bye, Jojo
                        >
                        >
                        Sorry Joachim, I just did not see the warnings between all that
                        linking stuff.



                        --
                        jacob navia
                        jacob at jacob point remcomp point fr
                        logiciels/informatique

                        Comment

                        • Joachim Schmitz

                          #13
                          Re: data type

                          "jacob navia" <jacob@nospam.o rgschrieb im Newsbeitrag
                          news:47028dfd$0 $27377$ba4acef3 @news.orange.fr ...
                          Joachim Schmitz wrote:
                          >"jacob navia" <jacob@nospam.o rgschrieb im Newsbeitrag
                          >news:47028ae8$ 0$5103$ba4acef3 @news.orange.fr ...
                          >>Richard Heathfield wrote:
                          >>>santosh said:
                          >>>>
                          >>>>aman wrote:
                          >>>>>
                          >>>>>is it true that c compiler automatically picks a variable as an
                          >>>>>integer if we can't explicitly declare it at beginning of a
                          >>>>>program??? ?
                          >>>>No. In C objects have to be declared before they are used.
                          >>>True in C99, but who's got that?
                          >>>>
                          >>>rjh@linux: ~/scratchcat foo.c
                          >>>#include <stdio.h>
                          >>>foo(x)
                          >>>{
                          >>> return printf("%d\n", x);
                          >>>}
                          >>>int main(void)
                          >>>{
                          >>> foo(42);
                          >>>>
                          >>> return 0;
                          >>>}
                          >>> me@here:~/scratchmake
                          >>>gcc -W -Wall -ansi -pedantic -Wformat-nonliteral -Wcast-align -Wpointer-arith
                          >>> -Wbad-function-cast -Wmissing-prototypes -Wstrict-prototypes -Wmissing-declarations
                          >>> -Winline -Wundef -Wnested-externs -Wcast-qual -Wshadow -Wconversion -Wwrite-strings
                          >>> -Wno-conversion -ffloat-store -O2 -g -pg -c -o foo.o foo.c
                          >>>foo.c:3: warning: return-type defaults to `int'
                          >>>foo.c:3: warning: function declaration isn't a prototype
                          >>>foo.c: In function `foo':
                          >>>foo.c:3: warning: type of `x' defaults to `int'
                          >>>gcc -W -Wall -ansi -pedantic -Wformat-nonliteral -Wcast-align -Wpointer-arith
                          >>> -Wbad-function-cast -Wmissing-prototypes -Wstrict-prototypes -Wmissing-declarations
                          >>> -Winline -Wundef -Wnested-externs -Wcast-qual -Wshadow -Wconversion -Wwrite-strings
                          >>> -Wno-conversion -ffloat-store -O2 -g -pg -o foo foo.o -lm
                          >>>me@here:~/scratch./foo
                          >>>42
                          >>>me@here:~/scratch>
                          >>heathfield is using the wrong compiler!
                          >>>
                          >>d:\lcc\test>t ype th1.c
                          >>#include <stdio.h>
                          >>foo(x)
                          >>{
                          >> return printf("%d\n", x);
                          >>}
                          >>int main(void)
                          >>{
                          >> foo(42);
                          >>>
                          >> return 0;
                          >>}
                          >>>
                          >>d:\lcc\test>l c th1.c
                          >>Warning th1.c: 2 no type specified. Defaulting to int
                          >>Warning th1.c: 8 Missing prototype for 'foo'
                          >>0 errors, 2 warnings
                          >Why? He basically got the same warnings as you got. (When compiling, not
                          >when linking, no surprise there)
                          >Hmm, actually he got 3 warnings, your compiler produced 2
                          >>
                          >Bye, Jojo
                          >
                          Sorry Joachim, I just did not see the warnings between all that
                          linking stuff.
                          It's not me you own an apology...

                          Bye, Jojo


                          Comment

                          • Flash Gordon

                            #14
                            Re: data type

                            Richard Heathfield wrote, On 02/10/07 19:01:
                            Ben Pfaff said:
                            >
                            >Richard Heathfield <rjh@see.sig.in validwrites:
                            >>
                            <snip>
                            >>rjh@linux:~/scratchcat foo.c
                            >>#include <stdio.h>
                            >>foo(x)
                            >I believe that this is a declaration of x. (It is a declaration
                            >that does not explicitly give the type of x.)
                            >
                            I, in turn, believe that this was roughly what the OP was getting at - i.e.
                            implicit int.
                            The OP should note that there are ways of defining a variable that avoid
                            explicitly providing a type. E.g.
                            {
                            auto i;
                            }
                            C99 has removed implicit int from the language, so I believe the above
                            is not valid C99.
                            --
                            Flash Gordon

                            Comment

                            • Keith Thompson

                              #15
                              Re: data type

                              santosh <santosh.k83@gm ail.comwrites:
                              santosh wrote:
                              >aman wrote:
                              >>
                              >>is it true that c compiler automatically picks a variable as an
                              >>integer if we can't explicitly declare it at beginning of a program????
                              >>
                              >No. In C objects have to be declared before they are used.
                              >
                              I should amend that for global objects it's true that the type defaults to
                              int when it's not specifically mentioned. However it's extremely bad
                              practise.
                              There isn't really a distinction between "global" and other objects.
                              C90 allows implicit int in a number of contexts. For example:

                              static x; /* x is int by default */
                              auto y; /* y is int by default; allowed only inside a function */
                              register z; /* z is int by default; allowed only inside a function */

                              Generally an object (variable) declaration requires *either* a storage
                              class *or* a type.

                              But declaring a variable without specifying its type is considered
                              poor style, and the newer C99 standard forbids it.

                              You can do it, but you shouldn't.

                              --
                              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                              San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
                              "We must do something. This is something. Therefore, we must do this."
                              -- Antony Jay and Jonathan Lynn, "Yes Minister"

                              Comment

                              Working...