general question

This is a discussion on general question within the C Programming forums, part of the General Programming Boards category; A few things i never quite found the answers for: what does return do. I never really understood when something ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    192

    general question

    A few things i never quite found the answers for:

    what does return do. I never really understood when something should return (1); or return (0);

    if you call a function, that say does a quick calculation, but doesnt pass anything back to the function taht called it, should it return 1 or 0?

    Is C language obselete? University is teaching us C, but why not C++? Its difficult in C to do things like basic graphics, etc, would you ever nowadays write a commercial program in C??

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    To start from the back: C is absolutely not obsolete. It may not (proportionally) be as important as it used to be, but there are many things that are actively developed in C.

    As to return, it does two things:
    1. Tells the compiler what you want your "result" (or "return value") from this function to be.
    2. Tells the compiler that "I would like to leave this function now, please".

    The "result" of a function is what you get back as a result, for example:
    Code:
        double a = sqrt(2.0);
    a is given the return value from sqrt, which [assuming correct C library code] should be about 1.41

    Your "middle" question about "what to return from a function that doesn't pass anything back", then the function should really be declared as a "void" function. That is the way to tell the compiler "I'm not going to return anything from this function". It makes it invalid to use the return value from the function.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    if you call a function, that say does a quick calculation, but doesnt pass anything back to the function taht called it, should it return 1 or 0?
    Depends on the convention you follow. If you just want to know if the function executed without errors or not, you could return any value you like. General convention in most libraries I have come across is that functions return 0 on success and -1 if there was an error.

    Is C language obselete? University is teaching us C, but why not C++? Its difficult in C to do things like basic graphics, etc, would you ever nowadays write a commercial program in C??
    C is not obsolete. But, it is not the language of choice for commercial product development either. Your choice of language depends strongly on the kind of product you are planning to develop. C is a good way to start off with learning how to program. I am pretty sure your university will have courses in C++ in higher semesters.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I wouldn't say that "it's not the choice for commercial development" - the choice of language, as you say, depends on many things. In smaller projects, and particularly where code-size vs. development time favours code-size [such as drivers, kernel code, embedded systems], the C option tends to be the choice.

    In cases where:
    1. Code is very large anyways.
    2. Development time is more important than code-size.
    then C++ or some other higher level language is preferred.

    Note I am NOT saying that C++ automatically is disqualified in the drivers, kernel, embedded scenario - far from it, I work with a rather large embedded OS that aside from some third party hardware drivers [written by the third party in C because they are designed to work on OTHER OS's too] is entirely written in C++, and it is for systems with medium-level memory constraints (expected to run on a system with a dozens, but not hundreds, of megabytes).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,985
    Quote Originally Posted by PING View Post
    C is a good way to start off with learning how to program.
    I don't think so, but that's subjective.
    I believe C++--or any other high level language for that matter--is a great--possibly the best--way to start off with programming.
    C is difficult because it was meant for low-level stuff, so in terms of getting lots done in little time, C is indeed obsolete.
    However, because C takes less time to implement vs C++, it's still used in spaces where every byte of memory or bit of performance counts. Especially when the systems are not large.

    So it depends on what you want to do...
    If you want to make general computer software, by all means, go for C++.
    If you want to make programming for special systems, many embedded systems, then think about learning C as an alternative.
    If you don't like low-level stuff and never intend to work on special systems, then by all means, go for C++.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    my opinion in this matter is different.learning c first definitely eases your understanding of other languages.that's why i think most of the universities still prefer to teach c instead of c++ or some other modern language.and yes as far as i know UNIX was the OS written completely in c which shows that operating systems can also be written in c.

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by BEN10 View Post
    my opinion in this matter is different.learning c first definitely eases your understanding of other languages.that's why i think most of the universities still prefer to teach c instead of c++ or some other modern language.and yes as far as i know UNIX was the OS written completely in c which shows that operating systems can also be written in c.
    The only reason I see that C++ is "better" for learning is it's object orientated. But that alone is hardly a good enough reason to choose C++ over other object-orientated languages. But the distinction for learning certainly depends on what's being taught and for what purpose.

    But let's not step into another argument, (some) people are rather one-sided.

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Quote Originally Posted by BEN10 View Post
    and yes as far as i know UNIX was the OS written completely in c which shows that operating systems can also be written in c.
    That statement is a bit misleading considering that most operating systems are written in a combination of assembly and C, while hardly any are written purely in C++.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Memloop View Post
    That statement is a bit misleading considering that most operating systems are written in a combination of assembly and C, while hardly any are written purely in C++.
    And a major reason for this has nothing to do with C or C++, but rather that most operating systems in use today were originally written some 10+ years ago by programmers with several years of experience - which meant that they started prgramming before the 1990's, most likely. Whilst C++ technically existed at that time, it was neither mature, nor commonly used.

    And you CAN NOT write an OS in C or C++ alone. There will be some lines of assembler involved to do some of the tricky bits that deal with specific registers of the processor. And for most machines, there is a need to have a few instructions of assembler code to set up a suitable environment to run C itself.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Each to his own, but some kernel developers still won't touch C++ with a ten-foot pole: Linux: C++ In The Kernel? | KernelTrap

    Clearly C++ leaves something to be desired when it comes to low-level programming, or there wouldn't be projects like EASTL and Embedded C++.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Memloop View Post
    Each to his own, but some kernel developers still won't touch C++ with a ten-foot pole: Linux: C++ In The Kernel? | KernelTrap

    Clearly C++ leaves something to be desired when it comes to low-level programming, or there wouldn't be projects like EASTL and Embedded C++.
    The "embedded C++" project would not compile our code where I work. We use for example namespaces [and I don't REALLY understand why namespaces are causing any problem with embedded systems].

    I think for a lot of things, C++ and OS kernel development require a bit more developer skill [and I don't say this to brag, as I don't actually work on the Kernel code myself other than by implementing drivers that are running in kernel mode]. But C++ can do all the things that C can, and if you understand what you are doing when you design your C++ code, you can do everything that a C kernel does, in almost identical manner, but with a more controlled environment.

    Note that most "embedded C++" (loosely termed, not specifically the EC++ project's definitition) is probably NOT used for Kernel code.

    However, C is often more "obvious" as to what goes on in the code.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,985
    Quote Originally Posted by Memloop View Post
    Each to his own, but some kernel developers still won't touch C++ with a ten-foot pole: Linux: C++ In The Kernel? | KernelTrap

    Clearly C++ leaves something to be desired when it comes to low-level programming, or there wouldn't be projects like EASTL and Embedded C++.
    No, it just shows that the developer is biased.
    It's true that C++ provides a lot of high-level constructs such as std::string, std::vector and so on. But then again, they don't HAVE to use them. And C++ supports far more things that C can only dream about. Yes, they can be implemented in C (because many C++ compilers is/was written in C), but it is a pain, difficult and time-consuming.
    Furthermore, they can easily write their own libraries that is specially suited for kernel work instead of using standard containers. But heck, you can still have full control over allocations with the use of Allocators.
    They are not even required to use exceptions if they do not wish to, either.

    So it boils down to that the kernel wasn't designed for modern C++ in mind, and so the infrastructure might not support everything that they need.
    Writing a C++ kernel from scratch, however, I'm sure you will get different results.

    And lastly, let me add that C++ can do all that C can and much more.
    And I believe one of the reasons for "Embedded C++" is to trim C++ down, to make it less challenging and time-consuming to create a C++ compiler for the system. Not that it is poor for low-level programming.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    I'm going to quote a comment from the page I linked to:
    If we are going to disable exceptions, not use object-oriented style and develop our own memory-management routines, what would be the point of using C++

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Memloop View Post
    I'm going to quote a comment from the page I linked to:
    If we are going to disable exceptions, not use object-oriented style and develop our own memory-management routines, what would be the point of using C++
    Of course, if you are NOT using object oriented style or any other feature of C++, then C++ is just a bigger C compiler. And there is no point in that. However, there are OS's out there that you can do "new" inside the kernel code [and it does what you expect, even if it's not EXACTLY THE SAME as in user-mode]. Try/catch may not be implemented for kernel code, and I doubt much kernel code uses STL for example. But that doesn't make object oriented style can't be used in a more limited way.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,985
    Quote Originally Posted by Memloop View Post
    I'm going to quote a comment from the page I linked to:
    Type safety, generic programming facilities, abstraction.
    And once more, the kernel is just built using year-old technologies, so it doesn't support any modern technologies. The foundation is flawed.
    That doesn't mean C++ is at fault - it means the foundation is.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A general question aout programming?
    By bradt93 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-26-2008, 12:00 PM
  2. winsock - general question
    By keira in forum Windows Programming
    Replies: 1
    Last Post: 09-28-2007, 02:56 PM
  3. another exercise question
    By luigi40 in forum C# Programming
    Replies: 3
    Last Post: 11-28-2005, 03:52 PM
  4. general programming compatability question
    By Metarectilinear in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 10-26-2002, 12:51 AM
  5. general question regarding a function parameter
    By mlupo in forum C Programming
    Replies: 7
    Last Post: 10-13-2002, 08:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21