Announcement

Collapse
No announcement yet.

Learning C

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

    Learning C

    I am trying to teach myself the C programming language from a third-party programming book, "Beginning Linux Programming."

    I discovered that the C compiler was not installed on the default Kubuntu installation, so I installed it [I think!].

    Now the first line of every C program that I have ever seen is this--
    #include <stdio.h>

    I cannot find this include file anywhere. Has it been named something else by Kubuntu? If so, why does it not adhere to the standard? Am I going to be surprised by other "include" files?

    Here is the complete C program--

    #include <stdio.h>
    int main()
    {
    printf(:Hello World\n");
    exit(0);
    }

    Here are the error messages I received when I tried to compile the "Hello World" program--

    hello.c:1:19: error: stdio.h: No such file or directory
    hello.c: In function 'main':
    hello.c:5: warning: incompatible implicit declaration of built-in function 'printf'
    hello.c:6: error: syntax error before 'exit'
    hello.c.7:2: warning: no newline at end of file

    If I'm going to have trouble with this simple a program, how am I going to do with more complex programs?

    TIA.

    #2
    Re: Learning C

    1) You didn't get the program quite right. There's supposed to be an exclamation point after world.
    2) If you look in the /usr/include you should see a large number of files of the form *.h; stdio.h ahould be there. Did you load gcc by apt-getting the package build-essential or by apt-getting gcc? In either case the header files should have been included.

    BTW The main reason for compiling and executing hello.c is to verify that your compiler chain is working properly. You wouldn't want to discover that you haven't got some immportant component after trying to compile a 1000 line module.

    Comment


      #3
      Re: Learning C

      You probably need libc6-dev

      Code:
      localhost: ~> dpkg -S /usr/include/stdio.h
      libc6-dev: /usr/include/stdio.h
      In general, if you are going to be building software, it is a good idea to install the following meta-package: build-essential.

      Good luck, and don't settle for C. Go all the way for C++

      Comment


        #4
        Re: Learning C

        You were exactly right! When I installed libc6-dev, there was stdio.h.

        Now, how is a newbie like me supposed to know something like that? In other words, why was this library not installed when I installed gcc? Doesn't gcc DEPEND on this library?

        Do you know of an article or tutorial anywhere on the distribution CD or on the WWWeb written for those who want to start into development of software, be it C, C++, or Java?

        Thanks for your help.

        Comment


          #5
          Re: Learning C

          Originally posted by decatur-linux

          Do you know of an article or tutorial anywhere on the distribution CD or on the WWWeb written for those who want to start into development of software, be it C, C++, or Java?
          Search for "tutorials". This one is pretty good (for c++).
          http://www.cplusplus.com/doc/tutorial/

          But I would really start in python. You will learn many of the aspects of programmingmuch, much faster (because you don't need to compile every time you change the code, and because it is a higher level language). You can also learn some aspects of the Object Oriented paradigm. Heck, you can even write great KDE apps with pykde (python-kde).

          You may start reading here:
          http://docs.python.org/tut/tut.html

          It is really a good read, maybe a weekend read. At least read the first chapters.
          Then you can start playing with code. And then you start bouncing from example code to the theory (the tutorial or whatnot). That's the best way to learn IMHO. Get sample code for the web.

          Good luck and have fun !

          Comment


            #6
            Re: Learning C

            Originally posted by decatur-linux
            Now, how is a newbie like me supposed to know something like that? In other words, why was this library not installed when I installed gcc? Doesn't gcc DEPEND on this library?
            i'm by no means a newbie & i had trouble w/ it myself. To ensure totally clean setup i did following:

            installed fresh ubuntu 5.10 for amd64
            sudo apt-get update
            sudo apt-get upgrade
            sudo apt-get install gcc
            gcc hello_world.c

            and i got errors stating that stdio.h could not be found.

            after some amount of googling, I now understand that libc6-dev or build-essentials pkg fix this, but it still seems a problem that the following 'reasonable' logic failed to work.

            1) install os and update
            2) install compiler w/ smart package manager which should handle dependencies.
            3) compile hello world.

            so the reason is that gcc 'suggests' libc6-dev but does not 'require' it? Is there a good reason for this?

            One would think 'reasonably' that
            install compiler = able to compile "hello world."

            thanks,
            zach

            Comment

            Working...
            X