Question:
In C programming, what does & mean in front of a letter?
Derek O
2010-01-26 07:08:55 UTC
This is the example
Also could you elaborarte a little on /t and %d as well, i kinda know what they are but need more clarification

int i;

switch(sscanf("%d", &i);

case EOF: printf("see %d\n", i);
break;
case 1: printf("\tyou");
case 0: printf("\tlater");
break;
default: printf("printed %d\ntimes\n", i);
Four answers:
Chris C
2010-01-26 07:52:40 UTC
I didn't try the code out, but here's the guidelines for the program as written.



1) sscanf("%d", &i); would seem to be wrong. It most likely would be scanf("%d", &i);

   The reason that I say this, is because sscanf() scans a string, whereas scanf() reads from the keyboard. See these 2 links: http://www.cplusplus.com/reference/clibrary/cstdio/sscanf/, and http://www.cplusplus.com/reference/clibrary/cstdio/scanf/

2) scanf("%d", &i); would simply scan in an integer from the command line. The reason that you give it the ampersand ('&'), is so that it can store the value from the command into that variable.

3) "%d" in the scanf(), and the sscanf(), and the printf() functions are used to specify that the variable is an integer. Here's a little more help with that: http://www.cplusplus.com/reference/clibrary/cstdio/printf/

4) The "\t", and "\n" are what is known as escape sequences. What that allows you do to in C/C++ is to put a specific character or number after it, to specify a specific ASCII characters. "\t" = tab, "\n" = newline. Here's a list of the ASCII and Extended ASCII set of characters: http://www.asciitable.com/

5) printf() is a function that allows you to display something on the console window. printf("\tyou"); would display a tab on the window, and then display the word "you". printf("see %d\n", i); would display the text: "see " (where is the number you entered), and move down to the next line of the display.

6) The switch ... case ... break statement is used to say "If the value is a zero (0), do this, if the value is a one (1) do this, if there is no specific match, then default to doing this. The "break", in a switch ... case set of statements is used to say you are done specifying what do do for the given case (i.e. 1, 0, etc.). Basically a switch ... case statement can make if ... else statements much easier to read.
Bastion 「A」
2010-01-26 07:31:18 UTC
And ampersand before a variable allows you to refer to its address in memory rather than its value. In this case, &i will return the address of the variable i rather than its value.



I'm not sure about the use of sscanf here though as I'm sure it is used to parse a string for a value. Obviously an address would be an integer, not a string, so I think you may have a problem here.



%d is used in printf to indicate that a variable is to be displayed as a decimal number. In this case, i is given as the variable to display so it will be displayed at the position of %d, as a decimal number.



Backslashed characters are typically escape sequences - combinations of characters used to express another type of character or to allow their usage where they wouldn't normally be allowed.



In this case, \t and \n are used. \t is used to denote a tab, while \n is used to denote a linebreak. When printf outputs the string to the screen, it will insert a tab at every \t, and a linebreak at ever \n.
anonymous
2010-01-26 07:21:37 UTC
Note that it is not / but \ and there is a BIG difference.



The \ or % are formating operators. For egzample \t means to place a tab.



for egzample the command printf("\tyou"); will output this



->you



The -> means a tabulation. It's like when you press TAB while writing a document in MS word or such.



The %d means the format of the input/output data type. d if I remember right stands for integers, while %f is for float etc.
Cipher-Nix
2010-01-26 07:18:33 UTC
i didn't understand the main question . . Well as far \t = tab.. Ex: in printf("\tyou") . . The word YOU prints after 1 tab space...



%d = decimal number..

Ex: in printf("see %d\n",i); output will be SEE 2. . Just assuming tat value of i=2. .. In place of %d value of i s printed. . %d s for decimal number only. .


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...