Q: how variable declaration & initializing in C Language.

In summary, variable declaration and initializing in C Language involves assigning a name and data type to a variable, followed by an equal sign and the initial value. This allows the computer to allocate memory for the variable and store the initial value. This process is crucial in C Language as it allows for efficient memory management and proper data manipulation in a program.
  • #1
hafiz16
4
0
hi please tell me how variable declaration & initializing in C Language..whit examples..
 
Technology news on Phys.org
  • #2
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
 
  • #3
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.
 
  • #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
 
  • #5
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?
 
  • #6
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:
  • #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:
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.
 
  • #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:
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.
 
  • #9
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.
 

Related to Q: how variable declaration & initializing in C Language.

1. What is the difference between variable declaration and initialization in C language?

Variable declaration refers to the act of defining a variable by specifying its data type and name. On the other hand, initialization refers to assigning a value to that variable. In C language, a variable can be declared without being initialized, but it is good practice to initialize a variable at the time of declaration.

2. How do you declare a variable in C language?

To declare a variable in C language, you need to specify the data type followed by the variable name. For example, int num; will declare a variable named num of type int.

3. What does it mean to initialize a variable?

Initializing a variable means assigning a value to it at the time of declaration. This value can be a constant, an expression, or the value of another variable. For example, int num = 10; will initialize the variable num with a value of 10.

4. Can a variable be reinitialized in C language?

Yes, a variable can be reinitialized in C language. This means that you can assign a new value to a variable that was previously declared and initialized. For example, num = 20; will reinitialize the value of num to 20.

5. What is the purpose of declaring and initializing variables in C language?

Variables are used to store and manipulate data in a program. Declaring a variable ensures that the compiler knows what type of data that variable will hold, and initializing it gives it an initial value to work with. This allows for more efficient and accurate coding in C language.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
8
Views
918
  • Programming and Computer Science
12
Replies
397
Views
14K
  • Programming and Computer Science
Replies
9
Views
1K
Replies
46
Views
3K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
4
Replies
122
Views
13K
  • Programming and Computer Science
Replies
22
Views
2K
Back
Top