Q: how variable declaration & initializing in C Language.

AI Thread Summary
Variable declaration in C involves specifying a type and a name, followed by a semicolon, such as "int variable;". Initialization assigns a value to the variable using the equals sign, like "int variable = 312;". Variables can be declared inside or outside functions, but beginners are advised to declare them within functions for clarity. The discussion also highlights the importance of understanding the difference between declaration and initialization, as well as the correct syntax for strings and structures. Resources like specific programming books are recommended for deeper understanding, while emphasizing that basic concepts are often covered in introductory materials.
hafiz16
Messages
4
Reaction score
0
hi please tell me how variable declaration & initializing in C Language..whit examples..
 
Technology news on Phys.org
hafiz16 said:
hi please tell me how variable declaration & initializing in C Language..whit examples..

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:
   int variable;

You can give an initial value with an equals sign and an expression; usually a constant, or a constant expression.
Code:
   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
 
http://lmgtfy.com/?q=declaring+variable+in+C

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.
 
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
 
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?
 
hafiz16 said:
i conslut many teachers & book..but this is not clear in my mind..
If you're going to be doing any significant amount of C programming, I strongly recommend buying
https://www.amazon.com/dp/0131103628/?tag=pfamazon01-20. 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.

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.
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:
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:
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.

Borek said:
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.

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.
 
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:
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.
 
technoweasel said:
By the way, don't bother with K&R. It seems really overpriced.
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.

O'Reilly pocket guide for C (it has a pink cow on the cover and is only like $10)
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.
 
  • #10
technoweasel said:
Code:
struct whatEver {
    int x, y, z;
    float q;
}

/* declare a whatEver variable named test */
struct whatEver test;
/* same thing with pointers */
struct whatEver *pTest;

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++.
 
  • #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.
 
Back
Top