#include <stdio.h>
int main(void)
{
char var = 'z';
printf("[buf]: %s \n", &var); // the output is z~~~~~, but the first char of output is only z. why??
}
I wonder why &a(Data type: char) is printed as value not address, using %s
98 Views Asked by y J. At
2
There are 2 best solutions below
0
Eric Postpischil
On
%s tells printf to accept a pointer to the first character of a string and to print that string, up to the null character that indicates its end. Since you pass the address of a single character, printf prints that and continues looking in memory for more characters to print, until it finds a byte containing zero. For %s, when you pass a pointer to a single character, rather than an array of characters terminated by a null character, the behavior is not defined by the C standard.
To print an address, use %p and convert the pointer to void *:
printf("%p\n", (void *) &var);
Related Questions in C
- Passing arguments to main in C using Eclipse
- kernel module does not print packet info
- error C2016 (C requires that a struct or union has at least one member) and structs typedefs
- Drawing with ncurses, sockets and fork
- How to catch delay-import dll errors (missing dll or symbol) in MinGW(-w64)?
- Configured TTL for A record(s) backing CNAME records
- Allocating memory for pointers inside structures in functions
- Finding articulation point of undirected graph by DFS
- C first fgets() is being skipped while the second runs
- C std library don't appear to be linked in object file
- gcc static library compilation
- How to do a case-insensitive string comparison?
- C programming: Create and write 2D array of files as function
- How to read a file then store to array and then print?
- Function timeouts in C and thread
Related Questions in PRINTF
- How to read a file then store to array and then print?
- Why does command prompt show numbers before I begin?
- Awk syntax with printf - not desired format
- How to return a 16 bit value as 64 bit?
- Raspberry Pi: printf() doesn't work with wiringPi
- Program crash while trying to print a bidimensional array
- Output EOF using %f
- std::cout and printf array
- Why is this floating point addition result not correct?
- the precision of printf with specifier "%g"
- Behavior of "printf("%d");"
- Having problems with fprintf
- Using Gawk and Printf in a Bash script
- Mathematical expression in print command in Linux
- Different output when using cout + printf vs printf only
Related Questions in CHARACTER
- How can I use multiple quotation marks in the system() function?
- Count number of ones in a array of characters
- Find out if Character in String is emoji?
- Convert Character to Int in Swift 2.0
- OCR serial number CRC, check algorithm
- How to order characters in a data frame in R to be numerical?
- Why are the ⟨ and ⟩ characters handled so oddly?
- What is a code point and code space?
- how to extract characters of a language
- Characters arbitrarily being changed
- Can i use the following code or it's incorrect?
- python character set conversion by the compiler
- Count the number of times you can make a given word from the letters of a larger text
- R Creating a Character Column from a Numeric Column w/o using For Loop
- Read lines from file, iterate over each line and each character in that line
Related Questions in C-STRINGS
- Converting long double to CString
- C program Strings Example how come the result is 98?
- Copy vector<char> into char*
- C - Simple Linked List program that handles strings
- c reading and writing strings visual studio 2013
- C character array and its length
- Why gets() is deprecated?
- C++ : Dynamic C-String Usage in ifstreamObject.getline(c string, char limit)
- Why does my variable change after strtok() and fgets() without modifying it?
- how do I delete allocated memory and still return its value from method
- How to read string until two consecutive spaces?
- Reading different types in C from File
- Differences between single-quotes and double-quotes in C
- C++ Difference between new char[size] and new char[size]()
- How to evaluate tokens in C?
Related Questions in CONVERSION-SPECIFIER
- c structure: Input requires number line by line by hitting "enter" after each number
- sscanf(s, "%u", &v) matching signed integers
- Problems Converting Floats
- How %d works for "" in string ? Result: 4210704
- Need help on format string vulnerability
- Printf is not working properly after a new line character
- This is another example of my neverending confusion related to memory and pointers in C
- Default element value is used instead of user defined in C?
- Difference between " %[^\n]%*c" and " %[^\n]" for consecutive scanf in C
- What does "%%%ds" mean in a c program formatting text?
- How to create a char* substring of a char* string in C?
- When I going to print a hexadecimal value using printf it takes the input as decimal value why?
- I wonder why &a(Data type: char) is printed as value not address, using %s
- Taking character into array and printing it in C
- Why is there no output when I use a float conversion specifier in C
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
The conversion specifier
sis designed to output strings (or their parts): sequences of characters terminated by the zero character'\0'.To output the address of an object there is the conversion specifier
p.Here is a demonstrative program.
The program output might look like
As for the code in your question then it has undefined behavior because the expression
&vardoes not point to a string because the variablevaris defined likeIf you want to output its address then you can do it as