When I run this on my computer I get compile errors. However, I did copy it straight from a tutorial I found on the Internet.
#include <stdio.h>
#include <conio.h>
void main(){
int i = 9;
clrscr();
printf("The value of i is: %d\n", i);
printf("The address of i is: %u\n", &i);
printf("The value at the address of i is: %d\n", *(&i));
getch();
}
The errors:
$ cc "-Wall" -g ptrex6.c -o ptrex6
ptrex6.c:7:19: error: conio.h: No such file or directory
ptrex6.c:9: warning: return type of ‘main’ is not ‘int’
ptrex6.c: In function ‘main’:
ptrex6.c:11: warning: implicit declaration of function ‘clrscr’
ptrex6.c:14: warning: format ‘%u’ expects type ‘unsigned int’, but argument 2 has type ‘int *’
ptrex6.c:17: warning: implicit declaration of function ‘getch’
make: *** [ptrex6] Error 1
Mistakes:
conio.his not a standard C header. It might be unavailable on your system. Nevertheless, it’s not needed for printf(). That’s whystdio.his here. Remove it, and also remove clrscr(). It won’t work without the conio libraries. By doing this, you’ll able to compile your file, since the other messages are “just” warnings, not errors.Change your
main()function’s return type tointand return 0. That’s what the C standard specifies. You want this.Use the
%dformat specifier in place of%u. As the compiler message directly points out, %u is for unsigned integers, andintis explicitly signed. For integers >= 2 ^ 31, you’ll experience strange behavior problems.You’re using a wrong specifier one more time. Use
%pfor addresses/pointers, not %u/%d/whatever.Don’t explicitly believe/copy-paste from tutorials. Tutorials are not for copy-pasting, they’re to be thought of and learnt from.