format-specifiers

What's the difference between %ul and %lu C format specifiers?

随声附和 提交于 2019-12-03 02:53:57
In an example of "C Primer Plus", the author has used %ul format specifier (in both scanf and printf) for unsigned long . When I try to generalize the problem, it seems that the %ul makes something wrong in my computer. But using %lu solved the issue. Actually, rather than focusing on the problem and the line of codes, I want to know about the difference between %ul and %lu . Maybe I could figure out what's wrong. Searching doesn't give me something useful (except that "they are different"). Any explanation or link/reference is appreciated. %lu is correct, while %ul is incorrect . A printf

I get previous float value when I am printing new value

天大地大妈咪最大 提交于 2019-12-02 07:07:11
I am getting output 0.23 from second printf . But typecasting gives required output. If I am not using type casting previous value is printed. Compiler version is GCC 6.3 #include <stdio.h> int main() { printf("%f ", 0.23); printf("%f", 0); return 0; } LINK FOR IDE in > printf("%f",0); You ask to print a double but you give an int , this is contradictory You are not in the case where the generated code makes a double from the int because printf is not int printf(const char *, double); but int printf ( const char * format, ... ); and the compiler does not look at the format to make the

What is the meaning of `printf(“%p”)` without arguments

两盒软妹~` 提交于 2019-12-02 05:13:57
问题 I of course know it used to output pointer with arguments. I read book Writing Secure Code by Michael Howard and David LeBlanc. One program in book demonstrates how stack overflow works by strcpy() Note printf() without arguments. #include <stdio.h> #include <string.h> void foo(const char* input) { char buf[10]; //What? No extra arguments supplied to printf? //It's a cheap trick to view the stack 8-) //We'll see this trick again when we look at format strings. printf("My stack looks like:\n%p

Wrong number of parameters to printf leads to strange results

∥☆過路亽.° 提交于 2019-12-02 04:01:34
#include <stdio.h> int main() { int i=10,j=20; printf("%d%d%d",i,j); printf("%d",i,j); return 0; } Using the Turbo C compiler, the output is like: 10 10 garbageValue 20 Can someone explain why this is? Srinath Reddy Dudi Which compiler you are using? I tested it on turbo c v2.0 and turbo c++ v 4.5 compilers and the output is 10 20 garbage value 10 This is the actual output you will get as you are using only two variables and three format specifiers. So it will print the values stored in the two variables and print a garbage value. In the second case you are using only one format specifier and

float variables in C [duplicate]

纵然是瞬间 提交于 2019-12-02 03:21:29
This question already has an answer here: Using %f to print an integer variable 6 answers May be this is a simple question but I am not sure about how float variables are stored in memory and why it is behaving in this way, can someone please explain about the following behavior. #include<stdio.h> int main () { int a = 9/5; printf("%f\n", a); return 0; } Output: 0.000000 I have looked at some information on how float variables are stored in memory, it has stuff about mantissa, exponent and sign. But I am not getting how to relate that here. int a = 9/5; performs integer division and ignores

%.#s format specifier in printf statement in c

可紊 提交于 2019-12-01 22:29:31
问题 Please explain the output. What does %.#s in printf() mean? #include<stdio.h> #include <stdlib.h> int main(int argc,char*argv[]){ char *A="HELLO"; printf("%.#s %.2s\n",A,A); return 0; } OUTPUT: #s HE 回答1: It's undefined behavior. # in printf format specifier means alternative form, but according to the standard, # is only used together with o , a , A , x , X , e , E , f , F , g , G , not including s . C11 §7.21.6.1 The fprintf function Section 6 # The result is converted to an ‘‘alternative

Dynamic Float Format Specifier in C

蓝咒 提交于 2019-12-01 03:40:20
Is there any way to have a user inputed float format specifier? For example, if I print this. float c = 15.0123 printf("%.2f", c); // outputs: 15.01 How can I assign the number of decimal places to a variable? Like: int n = 3; float c = 15.0123 printf("%.(%i)f", n, c); // outputs: 15.012 The precision can be specified by an argument with the asterisk * . This is called an argument-supplied precision. float c = 15.0123; int m = 2; printf("%.*f", m, c); printf("%.*f", n, c); that will print out c with n places after the decimal. 来源: https://stackoverflow.com/questions/9627075/dynamic-float

Dynamic Float Format Specifier in C

别等时光非礼了梦想. 提交于 2019-12-01 00:24:24
问题 Is there any way to have a user inputed float format specifier? For example, if I print this. float c = 15.0123 printf("%.2f", c); // outputs: 15.01 How can I assign the number of decimal places to a variable? Like: int n = 3; float c = 15.0123 printf("%.(%i)f", n, c); // outputs: 15.012 回答1: The precision can be specified by an argument with the asterisk * . This is called an argument-supplied precision. float c = 15.0123; int m = 2; printf("%.*f", m, c); 回答2: printf("%.*f", n, c); that will

How to print 1 byte with printf?

旧巷老猫 提交于 2019-11-30 23:38:47
I know that when using %x with printf() we are printing 4 bytes (an int in hexadecimal) from the stack. But I would like to print only 1 byte. Is there a way to do this ? Assumption:You want to print the value of a variable of 1 byte width, i.e., char . In case you have a char variable say, char x = 0; and want to print the value, use %hhx format specifier with printf() . Something like printf("%hhx", x); Otherwise, due to default argument promotion, a statement like printf("%x", x); would also be correct, as printf() will not read the sizeof(unsigned int) from stack , the value of x will be

Sequence Points in printf

与世无争的帅哥 提交于 2019-11-30 18:43:26
I read here that there is a sequence point: After the action associated with input/output conversion format specifier. For example, in the expression printf("foo %n %d", &a, 42) , there is a sequence point after the %n is evaluated before printing 42 . However, when I run this code : int your_function(int a, int b) { return a - b; } int main(void) { int i = 10; printf("%d - %d - %d\n", i, your_function(++i, ++i), i); } Instead of what I expect I get: 12 - 0 - 12 Meaning that there was not a sequence point created for the conversion format specifier. Is http://en.wikipedia.org wrong, or have I