Linked Questions
86 questions linked to/from What's the correct way to use printf to print a size_t?
37 votes
10 answers
20k views
Cross platform format string for variables of type size_t? [duplicate]
On a cross platform c/c++ project (Win32, Linux, OSX), I need to use the *printf functions to print some variables of type size_t. In some environments size_t's are 8 bytes and on others they are 4. ...
0 votes
0 answers
70 views
i used sizeof to find the size of a struct. if used with %f, it printed 0.000, with %d, it was printing correct result [duplicate]
I read that the size of a struct is at least the sum of individual fields inside it, and it can be greater than that when using struct array for alignment. What is alignment, why does it add that so ...
721 votes
20 answers
54k views
Can code that is valid in both C and C++ produce different behavior when compiled in each language?
C and C++ have many differences, and not all valid C code is valid C++ code. (By "valid" I mean standard code with defined behavior, i.e. not implementation-specific/undefined/etc.) Is there any ...
549 votes
14 answers
468k views
How can one print a size_t variable portably using the printf family?
I have a variable of type size_t, and I want to print it using printf(). What format specifier do I use to print it portably? In 32-bit machine, %u seems right. I compiled with g++ -g -W -Wall -...
352 votes
21 answers
107k views
Why do I get a segmentation fault when writing to a "char *s" initialized with a string literal, but not "char s[]"?
The following code receives seg fault on line 2: char *str = "string"; str[0] = 'z'; // could be also written as *str = 'z' printf("%s\n", str); While this works perfectly well: char str[] = "...
316 votes
11 answers
358k views
#pragma pack effect
I was wondering if someone could explain to me what the #pragma pack preprocessor statement does, and more importantly, why one would want to use it. I checked out the MSDN page, which offered some ...
161 votes
10 answers
163k views
How should I print types like off_t and size_t?
I'm trying to print types like off_t and size_t. What is the correct placeholder for printf() that is portable? Or is there a completely different way to print those variables?
112 votes
3 answers
68k views
Platform independent size_t Format specifiers in c?
I want to print out a variable of type size_t in C but it appears that size_t is aliased to different variable types on different architectures. For example, on one machine (64-bit) the following code ...
22 votes
4 answers
5k views
Why does sizeof(*"327") return 1 instead of 8 on a 64 bit system?
printf("%lu \n", sizeof(*"327")); I always thought that size of a pointer was 8 bytes on a 64 bit system but this call keeps returning 1. Can someone provide an explanation?
28 votes
2 answers
44k views
Packed bit fields in c structures - GCC
I am working with structs in c on linux. I started using bit fields and the "packed" attribute and I came across a wierd behavior: struct __attribute__((packed)) { int a:12; int b:32;...
41 votes
2 answers
3k views
Why are the addresses of argc and argv 12 bytes apart?
I ran the following program on my computer (64-bit Intel running Linux). #include <stdio.h> void test(int argc, char **argv) { printf("[test] Argc Pointer: %p\n", &argc); printf("[...
12 votes
7 answers
2k views
Different output for different compiler - C and C++ [duplicate]
Can you think of 'a program' which gives 'different outputs for a C and a C++ compilers' (yet gives consistent output under the same language)?
21 votes
3 answers
67k views
How do I print the size of int in C?
I am trying to compile the below on RHEL 5.6 , 64 bit, and i keep getting a warning "var.c:7: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘long unsigned int’" Here is my ...
21 votes
4 answers
27k views
How do positional arguments like "1$" work with printf()?
By man I find printf("%*d", width, num); and printf("%2$*1$d", width, num); are equivalent. But IMO the second style should be the same as: printf("%*...
18 votes
7 answers
29k views
How can I fix warnings like: "comparison between signed and unsigned"?
I have been advised to use the following options with GCC, as it helps to avoid a lot of common errors. It turns on a bunch of warnings and -Werror turns them into errors. gcc -pedantic -W -Wall -...