Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Q: how variable declaration & initializing in C Language.

  1. Dec 21, 2009 #1
    hi plz tell me how variable declaration & initializing in C Language..whit examples..
  2. jcsd
  3. Dec 21, 2009 #2


    User Avatar
    Science Advisor

    Keeping it very simple...

    A simple variable declaration consists of a type, and then a name for the variable, and then a semi-colon.
    Code (Text):
       int variable;
    You can give an initial value with an equals sign and an expression; usually a constant, or a constant expression.
    Code (Text):
       int variable = 312;
    If you are just starting out with C, note that variables can be declared inside a function, or outside a function. I recommend that you define your variables inside your functions. Later on as you learn more about the language, you can learn when "static" variables (defined outside of your functions) can be used, but for the time being, stick with examples like this, I suggest.

    Cheers -- sylas
  4. Dec 21, 2009 #3


    User Avatar

    Staff: Mentor


    Don't be a lazy bum. Asking questions that are answered and explained in first chapter of every existing book on C programming means you have not even tried to find the answer on your own.
  5. Dec 21, 2009 #4
    Thanks...but Sir Borek..i conslut many teachers & book..but this is not clear in my mind..
    If u mind..i say sorry & i never ask next time....Allah HAfiz
  6. Dec 22, 2009 #5


    User Avatar

    Staff: Mentor

    If you have already looked in books, and don't understand what they say, then it would be better to try to tell us specifically what you don't understand about what they say, and use speciflc examples of C code as examples.

    Otherwise, why should we think that what we might write is more helpful than what you've seen already?
  7. Dec 22, 2009 #6
    If you're going to be doing any significant amount of C programming, I strongly recommend buying
    https://www.amazon.com/Programming-Language-2nd-Brian-Kernighan/dp/0131103628. It's one of the best books out there, and if you've consulted it first it should keep borek from biting off your head.

    It could very well be a terminology thing. Back when I first learned this madness, the terms "declaration" and "initialization" were just glossed over and everybody called it variable assignment or ran the two together. Giving hafiz16 the benefit of the doubt, he was probably a bit muddled on how the two terms differ and what they do and confused his poor proffs while trying to get himself sorted out. I looked at what google returns and can see how some of it maybe confusing to a total newbie.
    Last edited by a moderator: Apr 24, 2017
  8. Dec 24, 2009 #7
    Declaring and initializing a variable is simple and easy to do. Declaring is by defining a variable and the type of data that the variable is going to store. The main types of variables are: void (void), character (char), integers (int), float (float), and doubles (double).

    Here's some type of integers in C: short int and long int; and each of the integers can be signed or unsigned. There's three types of floating point numbers that are: float, double, and long double.

    To initialize a variable is assigning it a value using the = operator and give it the proper value to the variable. If you want to declare a string or and array of characters then you declare that the variable is a char and the variable is an array.

    Code (Text):

    char string[20] = "This is a sting";
    You have to use double quotes to wrap the string and not single quotes. This is important because single quotes means that the string is a character which is not. C supports logical data like in C++ which is true or false except that in C its false is 0 and true is any number other than 0.

    That's when you have a book on C, but most books are for C++ and not for C. I was lucky enough to find one book for C in my library.
  9. Dec 27, 2009 #8
    Please remember that in straight C variables must be declared before code in functions and that structures must be declared with the keyword "struct"

    Code (Text):
    struct whatEver {
        int x, y, z;
        float q;

    /* declare a whatEver variable named test */
    struct whatEver test;
    /* same thing with pointers */
    struct whatEver *pTest;
    C++ doesn't have these restrictions; variables can be declared anywhere (like in for loop headers!!!) and you don't need "class" or "struct" words before things.

    By the way, don't bother with K&R. It seems really overpriced. I would recommend 'C++: The Complete Reference' by Herbert Shildt or the little O'Reilly pocket guide for C (it has a pink cow on the cover and is only like $10).

    Good luck.
  10. Dec 27, 2009 #9
    You can find it for cheaper, and it's a really good book. It actually explains all the concepts, and does it really clearly and concisely (excellent example of technical writing.) Take it out from the library and see if it's your speed, but that's totally not a reason to not but the book. I've read dozens of books on different languages, and this ones my fave; python made more sense to me after reading it 'cause it's really good on paradigms and all the other technical stuff.

    If you're using a pocket guide, you may as well use one of the APIs posted online (The C Library Reference Guide) or the api for your OS.
  11. Dec 29, 2009 #10
    In your structure you forgot the semicolon after the closing curly brace. C is a good start to learn the basic libraries, functions, and the syntax if you want to then learn C++.
  12. Jan 1, 2010 #11
    Quite sorry about the missing ;. It seems like I do that all the time when switching languages; HTML and Basic get semicolons while I forget them in C.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook