Different views of the same variable in C Language

Asked

Viewed 242 times

3

I wrote the following code and, beside the line the displayed results. I’d like to understand why the different exhibitions.

#include <stdio.h>

int main()
{
int inteiro = 1;
float decimal = 6.1;
char caractere1 = 'g';
char caractere2 = "k";

printf("O valor inteiro e %i\n", inteiro); // aparece 1 (está ok)
printf("O valor decimal e %f\n", decimal); // aparece 6.100000 (está ok)
printf("O caractere1 e %c\n", caractere1); // aparece g (está ok)
printf("O caractere2 e %c\n\n", caractere2); // aparece d (está errado - com certeza porque coloquei aspas duplas na declaração da variável)

printf("O valor inteiro e %i\n", &inteiro); // aparece 2293564
printf("O valor decimal e %f\n", &decimal); // aparece 6.099998
printf("O caractere1 e %c\n", &caractere1); // aparece 7
printf("O caractere2 e %c\n", &caractere2); // aparece 6

return 0;
}
  • gives a look here http://www.cplusplus.com/reference/cstdio/printf/ ta explaining all tags that can be used in printf

  • %c read only a letter or an integer number, when you use double quotes on the char it’s like a "set of char letters[]"

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.

2 answers

3

It’s not very secret, it’s different because the code asked to be different.

Numerical representations for humans

One thing I realize that people don’t understand is that there’s a huge difference between the data and the textual representation for humans to read that this data may have.

The die exists on its own. It does not matter how it is in the computer, because it exists as a lot of electrons and this serves for nothing in our point of view.

On a somewhat more abstract level we represent as bits, which is already a mathematical concept invented by man. A bit can be 0 or 1. A set of bits form other numbers.

We can also say that the computer only knows how to perform 3 operations (addition, multiplication and negation of bits), all the others are combinations of these.

How do we build these graphics from applications or games? Piecing bits together so that it makes sense. Everything that happens on the computer is like this.

How do we make texts appear on the screen? We connect a lot of dots on the screen that in certain positions and colors draw what we know as letters, numbers and other symbols. And the exact way you write this can vary quite a bit. Think of fonts. Then the operating system or an application that runs on it will take a number in memory and see which points they should call to have the character design that is represented by this number.

All this you don’t see, but it’s happening on the computer. There’s no magic or miracle, it’s just math and at the lowest level, physics.

But how do we know that a number in memory is a letter? It is an adopted convention that certain numbers amount to certain characters that we understand. The most widely used convention is ascii table. But there are others and, in theory, you can create yours. You can say that the number 1 is a drawing of an elephant, that the 25 is nothing, that the 345 is a red circle. It doesn’t matter. Of course standard tables make more sense.

When you use an argument on printf() is saying how to display that number that is in the memory position indicated by the variable, expression or literal. There could be some argument about shutting down the computer if you wanted to, it just doesn’t make much sense to do this.

Weak typing

Some programming languages do not let a number be interpreted differently than it was planned, this is characteristic of strong typing languages. C is a weak typing language. C does whatever it takes with any information that’s in there.

This is very flexible and powerful, but a bit dangerous. You have to know what you’re doing and be more careful.

Explaining each item

printf("O valor inteiro e %i\n", &inteiro); // aparece 2293564

He’s having the address of the variable inteiro, then you’re right.

printf("O valor decimal e %f\n", &decimal); // aparece 6.099998

The same thing, and now printing with the decimals. Note that the variable has been declared as float, but a literal was used double, looks the same, but not always the result will be exactly the same. This is a little more complicated to explain here.

printf("O caractere1 e %c\n", &caractere1); // aparece 7

Again picked up the address. The %c works with only 1 byte, so you’re just picking up a part of the address, which already had little meaning, now has even less.

printf("O caractere2 e %c\n", &caractere2); // aparece 6

The same.

printf("O caractere2 e %c\n\n", caractere2); // aparece d (está errado - com certeza porque coloquei aspas duplas na declaração da variável)

Now the same thing happened, but in a different way. You are having a character printed based on what you receive in the variable. The problem has already occurred in assigning the variable. It took a data that is of the type char * and tried to store it in a variable that’s just char, then he took the memory address of string and kept in caractere2 soiling the memory since the variable has space for only 1 byte and the address has 4 or 8 bytes. All wrong.

Understand that what is being shown on the screen is always a string, which happens to be composed of characters that we interpret as numerical digits.

Other issues

I don’t really like the variable name decimal (nor am I speaking of the fact that the name does not describe the datum but rather the type, after all it is only a demonstration) because it is not quite a decimal. It is, but from a computer that is quite different from the decimal we know. That’s why he calls float and double and not decimal.

I had to use a configured compiler to accept certain wrong constructs, otherwise I wouldn’t even compile it.

Notice that different things appear, after all the memory is different.

#include <stdio.h>

int main() {
    int inteiro = 1;
    float decimal = 6.1f;
    char caractere1 = 'g';
    char *caractere2 = "k";
    printf("O valor inteiro e %i\n", inteiro); // aparece 1 (está ok)
    printf("O valor decimal e %f\n", decimal); // aparece 6.100000 (está ok)
    printf("O caractere1 e %c\n", caractere1); // aparece g (está ok)
    printf("O caractere2 e %c\n\n", caractere2); // aparece d (está errado - com certeza porque coloquei aspas duplas na declaração da variável)
    printf("O valor inteiro e %i\n", &inteiro); // aparece 2293564
    printf("O valor decimal e %f\n", &decimal); // aparece 6.099998
    printf("O caractere1 e %c\n", &caractere1); // aparece 7
    printf("O caractere2 e %c\n\n", &caractere2); // aparece 6
    
    printf("O caractere2 e %s\n", caractere2);
    printf("O valor inteiro (somado para dar algo imprimível) mostrado como caractere e %c\n", inteiro + 64);
    printf("O endereco de decimal e %d\n", &decimal);
    printf("O inicio do caractere2 e %c\n", caractere2[0]);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

There is a situation where the formation of memory causes a problem because of the dirt in the memory. If you run some lines and others do not work, it works even if the ones that generate problem are executed without some others. Their composition creates a problem.

That’s why I always say that working and being right are different things, the same code generates different results on different machines because it’s not right.

Funciona e estar certo são coisas distintas

  • "I had to use a configured compiler to accept certain wrong constructs, otherwise I wouldn’t even compile it." I’m using Codeblocks... which would be the ideal compiler for those who are starting?

  • Codeblocks is not a compiler. If using Windows I would install Visual Studio and use the Microsoft compiler anyway. If it’s Linux use GCC, but newer versions always. I’ve been leaning towards Clang as well.

1

To better understand

You remember the scanf("%d",&whole); ?

scanf(); is read command;

%d (or %i) is to represent whole decimals;

& is to represent the address;

Logo scanf("%d",&integer); you go by integer content at certain memory position.

This one you’re taking the content inside the memory position:

 printf("O valor inteiro e %i\n", inteiro); // aparece 1 (está ok)
 printf("O valor decimal e %f\n", decimal); // aparece 6.100000 (está ok)
 printf("O caractere1 e %c\n", caractere1); // aparece g (está ok)
 printf("O caractere2 e %c\n\n", caractere2); // aparece d (está errado - 
com certeza porque coloquei aspas duplas na declaração da variável)

This one you’re getting the address from memory:

printf("O valor inteiro e %i\n", &inteiro); // aparece 2293564
printf("O valor decimal e %f\n", &decimal); // aparece 6.099998
printf("O caractere1 e %c\n", &caractere1); // aparece 7
printf("O caractere2 e %c\n", &caractere2); // aparece 6

Browser other questions tagged

You are not signed in. Login or sign up in order to post.