Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Help with #define

Can you use string constants and variables with #define? or just constants?(written version of a number) and can you use character constants with #deinfe? I know that you can use #define with the written version of a number, like I do:


#define fifty;

and thats it? can you use string constants and string literals besides that origianl normal 'constant' ?


what is fifty? a variable? the users age, just written in age? if so! thats right!


Can you use variables as the names of arrays?


char arrayname[50];

puts("Enter your name"; /*is cout the same as 'puts??

gets(arrayname); /*is gets the same as cin>> ???


there! I got the users name with my code! now what?

I cant even see the users name! Can you only see it when its compiled and you cant see it in code right???


Just the users name was Mike, where would I see it? and where would it go? If so?


int age[10]; /*I took out 'char' cause I dont need a string from the user! only a number! or should I include the char? before the age[10] ?


int age[10];

puts("Enter your age";

gets(age);


Yeah, I know its just like my above words,"how do I see and know where the users age goes?, not seen in code?" For example, if the users age was 20, would it be like this:

#deinfe twenty???? /*using just a constant,for age!


Do I write that code after a print it? :


printf("%d
", age) ; does that make the users age come to view when compiled? , should I take out the newline character? please answer to all of these questions, cause they are driving me crazy in my code!




Comments

  • : Can you use string constants and variables with #define? or just constants?(written version of a number) and can you use character constants with #deinfe? I know that you can use #define with the written version of a number, like I do:




    The first thing you must realize with #defines is that they are not handled by the compiler the same way as constants. They are a direct textual substitution. In a lot of cases, this has the desired effect of simply acting as a constant. However, this is very bad:


    #define eight 3+5


    The reason this is bad is that if you used that macro somewhere in a case like this:


    int myVal=4*eight;


    You will not get the expected value of 32.




    Once you get the idea that it is just a textual substitution, then you can essentially do anything with macros (but probably shouldn't).



    : #define fifty;

    : and thats it? can you use string constants and string literals besides that origianl normal 'constant' ?




    All #define fifty will do is to define a preprocessor tag 'fifty' such that you can test for its existence. It is not defined as anything in particular. I'm not sure what the semicolon will do, there, since semicolons do not terminate preprocessor commands like #define.





    : what is fifty? a variable? the users age, just written in age? if so! thats right!




    'fifty' in this context is just a preprocessor definition. It means nothing to the compiler. If 'fifty' were defined as something, then whenever the preprocessor encountered the text string 'fifty' on its own it would substitute whatever the definition was before passing on that part of the code to the compiler.


    It is not a variable. it is not a constant. It is just a directive to tell the preprocessor that 'fifty' exists as a preprocessor tag.




    : Can you use variables as the names of arrays?


    An array is something that a variable can be. A 'variable' is a storage space that the program controls for some purpose. You can (and must) give a name to any type of variable including an array.



    : char arrayname[50];

    : puts("Enter your name"; /*is cout the same as 'puts??

    : gets(arrayname); /*is gets the same as cin>> ???




    Using the C function gets and the C++ object cin with the >> operator on 'arrayname' will not have the same behaviour. The >> operator for the object cin will read into a character array until a 'delimiter' (usually whitespace) is reached. gets will read in a line up until the end-of-line is reached.


    They are both dangerous in that they don't specify a maximum length of the array.


    The C function puts and the C++ object cout with the << operator will output an entire string. However, puts will append an end-of-line, whereas the >> operator on the C++ object cout will not.


    I suggest you don't mix C++ input/output with C input/output.





    : there! I got the users name with my code! now what?

    : I cant even see the users name! Can you only see it when its compiled and you cant see it in code right???




    From the code point of view, 'you' aren't important or automatically considered. You can't 'see' it because you haven't told the program to do anything with the data that it has acquired. It's probably still sitting in the array that you dumped it into.



    : Just the users name was Mike, where would I see it? and where would it go? If so?


    You gave 'gets' the array to put the data into. I expect the data is in the array. You wouldn't 'see' it at all (unless you inspected the array with a source-level debugger, or output it).



    : int age[10]; /*I took out 'char' cause I dont need a string from the user! only a number! or should I include the char? before the age[10] ?




    For heaven's sake, get a book on C.


    Let's deeply analyze what you have here. Let's start from the middle. 'age' is the name of the variable. The '[10]' says that it's an array of some sort with 10 elements. To see what type the elements are, we look on the other side, so it is in fact an array of 'int'. That means that each of the 10 elements is an int.


    If you don't need a character or a character array, then you don't need to declare one. 'char' is a C type, and is used (among other purposes) for declaring a variable that in some way is 'char' related (a single char, an array, a pointer, etc).




    : int age[10];

    : puts("Enter your age";

    : gets(age);




    This won't work. 'gets' is the C input routine for reading in a line of characters into a character buffer. You're attempting to give it an integer buffer.




    : Yeah, I know its just like my above words,"how do I see and know where the users age goes?, not seen in code?" For example, if the users age was 20, would it be like this:

    : #deinfe twenty???? /*using just a constant,for age!


    #defines are not constants. They are text substitutions. They can be used as constants if you define them correctly. If you really wanted the text 'twenty' to be replaced with 20 throughout your code, you'd do this:


    #define twenty 20





    : Do I write that code after a print it? :




    I have no idea what you mean.




    : printf("%d
    ", age) ; does that make the users age come to view when compiled? , should I take out the newline character? please answer to all of these questions, cause they are driving me crazy in my code!




    Provided 'age' is precisely a variable of type int, then this would output to the console the value of 'age' plus an end-of-line, at RUN-TIME, not at COMPILE-TIME. There is a difference.





Sign In or Register to comment.