Q: how variable declaration & initializing in C Language.

Click For Summary

Discussion Overview

The discussion centers around the topic of variable declaration and initialization in the C programming language. Participants explore basic concepts, provide examples, and address common misunderstandings related to these topics.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • Some participants explain that a simple variable declaration consists of a type, a name, and a semicolon, with examples such as int variable;.
  • Others mention that initialization can be done using an equals sign and an expression, providing examples like int variable = 312;.
  • There are suggestions that variables should be declared inside functions, with references to the use of static variables later on.
  • Some participants express frustration with the original poster's question, suggesting that the information is readily available in introductory materials on C programming.
  • One participant emphasizes the importance of understanding the difference between declaration and initialization, noting that terminology can be confusing for beginners.
  • Another participant lists the main types of variables in C, including void, char, int, float, and double, and discusses the initialization of strings and arrays.
  • There are mentions of the need to declare variables before code in functions and the specific syntax for structures in C.
  • Some participants recommend specific books for learning C, while others critique the pricing and content of popular texts.
  • A participant points out a syntax error in a structure declaration, highlighting the importance of semicolons in C.

Areas of Agreement / Disagreement

Participants express a mix of agreement on basic concepts of variable declaration and initialization, but there is disagreement regarding the appropriateness of the original poster's question and the clarity of existing resources. The discussion remains unresolved regarding the best approach to learning these concepts.

Contextual Notes

Some participants note that terminology differences may contribute to confusion, and there are references to various resources that may or may not be suitable for beginners. The discussion reflects a range of experiences with learning C, indicating varying levels of understanding among participants.

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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
Replies
22
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 397 ·
14
Replies
397
Views
21K
  • · Replies 9 ·
Replies
9
Views
2K
Replies
46
Views
4K
  • · Replies 14 ·
Replies
14
Views
3K
  • · Replies 122 ·
5
Replies
122
Views
17K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K