Can anybody tell me why this happening in c [duplicate] - c

This question already has answers here:
Can anybody tell me why this is happening in c language
(3 answers)
Closed 5 years ago.
#include <stdio.h>
//Compiler version gcc 6.3.0
int main(void)
{
int a=10;
printf("%d"+1,a);
return 0;
}
Output. --- d
#include <stdio.h>
//Compiler version gcc 6.3.0
int main(void)
{
#include <stdio.h>
//Compiler version gcc 6.3.0
int main(void)
{
int a=10;
printf("%d"+36,a);
return 0;
}
Output. --- p
Explain me why this happening... Whenever i change value of +1 it print different outputs...

The behaviour of your code is undefined.
"%d"+1 actually is adding 1 to a const char* pointer (the literal "%d" will decay to a const char* pointer under certain circumstances), which actually takes you to the d in that string literal! The printf formatter is therefore not appropriate for your arguments.
"%d"+36 is simply going to do very bad things indeed, since you don't own the memory 36 places on from the start of "%d"
Don't you mean something like printf("%d", a + 1); &c.?
Lastly, what the deuce is an Int? Didn't you mean int?

Related

Hi, im trying to start my first C language in VS code, but i can’t run it [duplicate]

This question already has answers here:
What should main() return in C and C++?
(19 answers)
Closed 1 year ago.
#include <stdio.h>
void main(void)
{
printf("hello");
}
I can’t seem to get the code to run. I tried using some extensions/terminal but the problem does not solve.
If you use int as the type of main, it would be a good idea to include a return statement. The following works in VS Code
#include <stdio.h>
int main()
{
printf("Hello");
return 0;
}
You need int main (void), not void main(void).
See the Microsoft documentation.

regarding the size of a packed structure [duplicate]

I am trying the following, with gcc on win32.
#include <stdio.h>
struct st { char c; int x; } __attribute__ ((packed));
int main() {
printf("%d\n", sizeof(struct st));
return 0;
}
I would expect that the printed value is 5, but it's 8.
With the following, however, I get 5.
#include <stdio.h>
#pragma pack(1)
struct st { char c; int x; };
int main() {
printf("%d\n", sizeof(struct st));
return 0;
}
There must be something wrong in my program, but I can't see what.
I have read gcc's manual and several questions on SO about this, and I'm still puzzled. Any hint?
Also from the answers to these questions on SO, I understand that I should not use packed structs for marshalling, and I probably won't use it much, but I still would like to understand what I'm not able to see in such a short program.
Note: the problem occurs with both gcc-4.9.2 and gcc-4.8.4.
You have the attribute in the wrong place - try this:
struct st { char c;
int x __attribute__ ((packed));
};
As per the example in the gcc manual, this will cause x to be packed such that it immediately follows c.
Of course you shouldn't really be doing this in the first place, as your code will break on certain architectures, and even where it doesn't break there may be performance penalties.
Working fine on my environment Centos 5.11 (64bit)
prints 5 for the first case you mentioned.
gcc version 4.9.1 (GCC)
gcc file.c
./a.out
5

Macro in GCC and VC++ [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 7 years ago.
I got this code compiled in VC++ and GCC that produces different outputs and appreciate if someone can point me out where the things get wrong.
#include "stdio.h"
#define Cube(x) x*x*x
int main(void){
int x=5;
printf("%d\r\n", Cube(x++));
return 0;
}
In GCC, the displayed value is 210 (=5*6*7) and in VC++2010 its 125 (=5*5*5).
If I do this,
#include "stdio.h"
#define Cube(x) x*x*x
int main(void){
int x=5;
printf("%d\r\n", Cube(++x));
return 0;
}
VC++ prints 512 (=8*8*8) and GCC prints 392 (=7*7*8).
Appreciate if someone can say whats going on.
The line
printf("%d\r\n", Cube(x++));
is pre-processed to:
printf("%d\r\n", x++*x++*x++));
That is cause for undefined behavior.
printf("%d\r\n", ++x*++x*++x));
is also cause for undefined behavior.
See
Why the output of this program is 41? is it undefined behavior?
Why are these constructs (using ++) undefined behavior?
You can avoid the problem by converting Cube to a function. The program below is well behaved.
#include "stdio.h"
int Cube(int x)
{
return x*x*x;
}
int main(void)
{
int x=5;
printf("%d\r\n", Cube(x++));
printf("%d\r\n", Cube(++x));
return 0;
}

strlen EXC_BAD_ACCESS in Xcode - c

This is my code:
#include <stdio.h>
#include <string.h>
int main() {
char x[256];
strcpy(x, "Gonzo!");
printf(strlen(x));
}
I'm not sure that I am coding this correctly. I want to know the string length of x.
I am using XCode 5+ and I get the error:
EXC_BAD_ACCESS(code=1, address=0x6)
The first argument of printf() is a const char*, which specifies the format. The posted code passes in 6 as the starting memory location for the char* which is incorrect and is causing the error. Use:
printf("%d\n", strlen(x));
See the linked printf() reference documentation.

C program(malloc) is not compiling in ubuntu 11.04

I have installed the gcc compiler from this sudo apt-get install build-essential command
and my program code is
#include<stdio.h>
main()
{
int *b;
b = (int*)malloc(10*sizeof(int));
printf("b=%u\n\n",b);
printf("b+1=%u\n\n",(b+1));
printf("b+2=%u\n\n",(b+2));
b[2]=4;
printf("*(b+2)=%d\n\n",*(b+2));
}
when i try to compile this program from cc -c program.c command
then i get some error
You're missing #include <stdlib.h> (for malloc), and the format strings are wrong. Use %p to print pointers.
Also, you don't need to (and probably shouldn't) cast the return value of malloc (in C).
And the correct signature for main without parameters is:
int main(void)
Corrected code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *b;
b = (int*)malloc(10*sizeof(int));
printf("b=%p\n\n", (void*) b);
printf("b+1=%p\n\n",(void*) (b+1));
printf("b+2=%p\n\n",(void*) (b+2));
b[2]=4;
printf("*(b+2)=%d\n\n",*(b+2));
return 0;
}
I don't know why it worked in the video, it's probably using some strange non-standard compiler.
But your errors are because you are using int instead of unsigned int and you pass pointers to printf when it expects unsigned int.

Resources