New to C programming

This is a discussion on New to C programming within the C Programming forums, part of the General Programming Boards category; Hi, I would like to get started on C programming, however, I am unsure about the following and made some ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    1

    New to C programming

    Hi,
    I would like to get started on C programming, however, I am unsure about the following and made some notes. Here are my questions. If possible, can you provide examples to the questions. Also, can anyone recommend a good begginers book for C programming. Thanks.

    1. There are a few types of integers such as short, int, and long. Why do we need more than one type of integers?

    2. why don't we simply define one variable for all?

    3. what will happen if I declare a long integer as short and vice versa?

    4. What is the difference between variable initialization and assignment? How do we initialize a variable of type <int> with a constant number?

    5. Why would you want to declare a variable that you are not going to use?

    6. What will be the content of variable if we do not assign a "zero" during initialization?

    7.Why don't we just assign the variable during variable declaration instead of doing the initialization and then assign the variable later?

    \\

    Just to let you know, I have been reading all the threads and it has sparked me to go into the programming field. Thanks for all the help.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    This sounds like home work. But in the off chance it's not:
    1) If you have a case where a potential value will never fall outside of a smaller version of an integeral type, why use a large one? There's no need really. What if I want five million variables? If I use a huge data type which can hold large values, and I only need small values, I've just wasted a ton of resources. Say we use C99's long long for everything. If all I'm doing is storing the alphabet, which fits in a bits, I'm wasting 56 bits for every single variable. That's taking up eight times the space I need. Seems sort of American doesn't it? I mean, wasteful. Wasteful was the word.

    2) See above.
    3) What do you mean "long as a short"? You mean assigning a value too big to too small a variable? It's truncated. The other way around, it's promoted.
    4) This is definately homework.
    5) I wouldn't. Homekwork.
    6) Undefined. Homework.
    7) You can.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Your C book shoudl cover most of these, or atleast some mild google research. To cover a few of them:
    1. The various integer types are distinct in the minimum size of can be. For instance if you need a value within a known range that fits into a short you can use that rather than a long. Most languages don't concern themselves with such details because they are generally not useful for most applications.
    2. I don't really understand what you are trying to ask here. Why don't we have 1 type for all things? What exactly does that mean? We types have different operations which need to be performed on them and they don't necesarly make sense for other types. So why have 1 type for everything, then you need to constantly remember how you want to interprete that variable.
    3. You will get a long integer rather than a short...That is all.
    4. Your C book should cover this fairly easily.
    5. I don't know, why would you? Who does this?
    6. Undefined.
    7. What?

    Please do some research on your own.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    56
    Read Sams Teach Yourself C in 21 Days, it answers all your questions.

Popular pages Recent additions subscribe to a feed

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