#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
100 views Asked by y J. At
2
There are 2 answers
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
- How to call a C language function from x86 assembly code?
- What does: "char *argv[]" mean?
- User input sanitization program, which takes a specific amount of arguments and passes the execution to a bash script
- How to crop a BMP image in half using C
- How can I get the difference in minutes between two dates and hours?
- Why will this code compile although it defines two variables with the same name?
- Compiling eBPF program in Docker fails due to missing '__u64' type
- Why can't I use the file pointer after the first read attempt fails?
- #include Header files in C with definition too
- OpenCV2 on CLion
- What is causing the store latency in this program?
- How to refer to the filepath of test data in test sourcecode?
- 9 Digit Addresses in Hexadecimal System in MacOS
- My server TCP doesn't receive messages from the client in C
- Printing the characters obtained from the array s using printf?
Related Questions in PRINTF
- What are the limits on CUDA printf arguments?
- command in bash contains printf but the format is not hand over to variable
- va_args in c from <stdarg.h> does not work correctly
- convert sprintf() statement to printf() statement in awk
- Is there any way to recover from a printf()/puts() error?
- can't understand why this output is generated
- Convert a file of decimal numbers to the ASCII equivalent
- When printing string why I am getting warning when constant qualifier not used in c?
- Java printf not working as expected when dividing
- Initializing a Variable for Strings Using Ternary Operators in C
- how printf() function behaves in printf("%d %d %d",a,a=a+5,a);?
- printf(%d) printing a very big integer
- Why does bidimensional char array behave this way in C?
- How to prevent race condition when multiple threads are writing in the same file descriptor in C?
- Why is fprintf not working as intended in my code?
Related Questions in CHARACTER
- Notepad++ Remove Empty Spaces or characters after the specific LAST character
- ABAP convert Database char to lowercase
- Iterating through a string of long characters R
- Character and Numeric vectors, preserve decimal points in R
- Why do some non-ASCII Unicode symbols appended to strings disappear in Delphi 12?
- Count num of occurences of every 26 characters for every word in numpy
- Handwritten Tigrigna Character Recognition
- Get data from BIEN database using R for species names including characters like "-" and "x"
- for issuing in cbt CLI 'cbt deleterow <rowkey>', how can i escape space character in a rowkey?
- How to preserve midnight timestamp in R when converting from MDY-HMS to YMD-HMS
- Standards in char array declaration in C
- Converting characters to dates in R
- Godot - Character Animation looping repeatedly (constantly being rerun), bypassing any attempt to wait for it to finish
- What is this character format and how to decode to normal text?
- why doesn't the compiler convert character array to my custom-made String class?
Related Questions in C-STRINGS
- I need to create a malloc array of strings and print those strings out
- Is there a worked example of using CStrBufT with a CString?
- Function is returning null instead of array in C
- Nested strtok() calls to tokenize given string does not work as expected
- Looping through an array which contains a string with spaces
- Word Count in C
- Last character index of inverted string in C being the whole uninverted original string
- sprintf blocks stm32 program
- Calling SHGetKnownFolderPath from Python?
- Invalid Initialization of Non-Const Reference Error in C++
- Is this a legal C strdup function?
- TextBox String to open file
- How to pass a string to a function and return the same string changed in C?
- How to shift chars in a character array without a temp?
- How do I convert a long string into a smaller abbreviation consisting of the first character, last character and number of chars in between?
Related Questions in CONVERSION-SPECIFIER
- How %d works for "" in string ? Result: 4210704
- Problems Converting Floats
- the reason why type char can be converted in specifier %d?
- Using string specifier for char array?
- How to print two strings on the same line in c using printf
- Not able to print the absolute address of global variable address
- Not able to assign 64-bit value to uint64_t in C
- Are %f and %lf interchangeable when using printf?
- Why is int to float conversion failing in printf?
- Why does my C program not take input for the name of the cricketer after the loop reaches a value of 2 or greater? How can I correct this?
- A beginner question about the return value of the C ternary operator
- Difference between & address and pointer address- Hexadecimal and pointer type data
- Is the default value of malloc with the size of a single char P?
- Printf performing implicit casting
- Getting an integer from a string
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?
Popular Tags
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)
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