2+2=2 in C (double arithmetics) - c

I have absolutely no idea why it returns 2 for a=2 and b=2..
Any ideas?
#include <stdlib.h>
int main()
{
double a,b,c;
printf("a=");
scanf("%d", &a);
printf("b=");
scanf("%d", &b);
printf("c=");
scanf("%d", &c);
printf("x=%d", a+b);
return 0;
}

The specifier "%d" expects an integer and you are passing the address of a double. Using the wrong specifiers in scanf leads to undefined behavior.
Also, using the wrong specifier in printf is the same thing. Because printf takes a variable number of arguments a + b which is a double can't be converted to an integer.

%d is for reading integers, use %f or %lf for float/double.

printf should use something like %f instead of %d. The same for scanf.

If you want to accept a float input, use scanf (and printf) with the %lf formatting character, not %d (which is for integers).
The behavior of your current program is undefined, since the scanf calls are writing an integer to a float variable. Also, you're missing include <stdio.h> at the top of your program. To catch errors like these, turn on the warnings in your C compiler:
$ gcc so-scanf.c -Wall
so-scanf.c: In function ‘main’:
so-scanf.c:6:5: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
so-scanf.c:6:5: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
so-scanf.c:7:5: warning: implicit declaration of function ‘scanf’ [-Wimplicit-function-declaration]
so-scanf.c:7:5: warning: incompatible implicit declaration of built-in function ‘scanf’ [enabled by default]
so-scanf.c:7:5: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘double *’ [-Wformat]
so-scanf.c:9:5: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘double *’ [-Wformat]
so-scanf.c:11:5: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘double *’ [-Wformat]
so-scanf.c:13:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Wformat]

Related

string poniter in c is not working when writen with *

I had written this c program for study purpose and I have found that
printf("%s",a); works but printf("%s",*a); does not works.
I had defined a like this char *a[]="how are you";
why *ais not pointing towards the string?
I am getting this error
test2.c: In function ‘main’:
test2.c:7:10: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
printf("%s \n",*a);
~^ ~~
%d
It is undefined behaviour because of %s printf interprets *a as an address, but it's actually not an address and if you treat it as address it points to some location that is read protected for your program. So,may be you get segmentation fault.

How to compile and use Netgen (network generator)?

I want to use Netgen - a generator for capacitated networks. But I've got two problems:
If I run make then I get the following error:
cc -O -DDIMACS -c -o netgen.o netgen.c
In file included from netgen.c:86:0:
netgen.h:45:6: error: conflicting types for ‘random’
long random(long, long); /* generate random integer in interval */
^
In file included from netgen.h:31:0,
from netgen.c:86:
/usr/include/stdlib.h:321:17: note: previous declaration of ‘random’ was here
extern long int random (void) __THROW;
^
netgen.c: In function ‘main’:
netgen.c:522:11: warning: format ‘%ld’ expects argument of type ‘long int’, but argument 2 has type ‘int’ [-Wformat=]
printf("n %ld\n", i + 1);
^
netgen.c:535:11: warning: format ‘%ld’ expects argument of type ‘long int’, but argument 2 has type ‘int’ [-Wformat=]
printf("n %ld s\n", i + 1);
^
netgen.c:538:11: warning: format ‘%ld’ expects argument of type ‘long int’, but argument 2 has type ‘int’ [-Wformat=]
printf("n %ld t\n", i + 1);
^
netgen.c:550:11: warning: format ‘%ld’ expects argument of type ‘long int’, but argument 2 has type ‘int’ [-Wformat=]
printf("n %ld %ld\n", i + 1, B[i]);
^
netgen.c:553:9: warning: format ‘%ld’ expects argument of type ‘long int’, but argument 4 has type ‘int’ [-Wformat=]
printf("a %ld %ld %ld %ld %ld\n", FROM[i], TO[i], 0, U[i], C[i]);
^
<builtin>: recipe for target 'netgen.o' failed
make: *** [netgen.o] Error 1
I know the conflict is a result of using stdlib.h and random.c - how can I fix this?
The second problem is that I don't know how to use the problem sets (for example problems.8 and problems.40 (see link above))
Thank you in advance!

Warnings in compilation

I am trying to compile a simple program written in c on my new desktop. It installed completely normally without any error on my older machine but for some reason it is giving me lots of compilation warnings when I am trying to compile it here. I installed gcc, g++, xpm and xlib which are the pre requisites of this program. Could it be that I am missing some library or something on my new machine?
Here are the warnings I get:
gcc -o view_qsfr qsfr_2005a_tool_box.c -lX11 -lm -lc -L/usr/X11R6/lib -lXpm
In file included from qsfr_2005a_tool_box.c:44:0:
qsfr_2005a_readQSFR.c: In function ‘Read_QSFR’:
qsfr_2005a_readQSFR.c:706:19: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[201]’ [-Wformat=]
scanf("%s",&fname_extra);
^
In file included from qsfr_2005a_graphics.c:8:0,
from qsfr_2005a_tool_box.c:45:
rotated.c: In function ‘XRotPaintAlignedString’:
rotated.c:450:9: warning: return makes integer from pointer without a cast [-Wint-conversion]
return NULL;
^
rotated.c:453:36: warning: passing argument 3 of ‘XCreateGC’ makes integer from pointer without a cast [-Wint-conversion]
my_gc=XCreateGC(dpy, drawable, NULL, 0);
^
In file included from qsfr_2005a_graphics.c:7:0,
from qsfr_2005a_tool_box.c:45:
/usr/include/X11/Xlib.h:1584:11: note: expected ‘long unsigned int’ but argument is of type ‘void *’
extern GC XCreateGC(
^
In file included from qsfr_2005a_graphics.c:8:0,
from qsfr_2005a_tool_box.c:45:
rotated.c:510:45: warning: passing argument 3 of ‘XCreateGC’ makes integer from pointer without a cast [-Wint-conversion]
depth_one_gc=XCreateGC(dpy, empty_stipple, NULL, 0);
^
In file included from qsfr_2005a_graphics.c:7:0,
from qsfr_2005a_tool_box.c:45:
/usr/include/X11/Xlib.h:1584:11: note: expected ‘long unsigned int’ but argument is of type ‘void *’
extern GC XCreateGC(
^
In file included from qsfr_2005a_graphics.c:8:0,
from qsfr_2005a_tool_box.c:45:
rotated.c:566:43: warning: passing argument 3 of ‘XCreateGC’ makes integer from pointer without a cast [-Wint-conversion]
depth_one_gc=XCreateGC(dpy, new_bitmap, NULL, 0);
^
In file included from qsfr_2005a_graphics.c:7:0,
from qsfr_2005a_tool_box.c:45:
/usr/include/X11/Xlib.h:1584:11: note: expected ‘long unsigned int’ but argument is of type ‘void *’
extern GC XCreateGC(
^
In file included from qsfr_2005a_graphics.c:8:0,
from qsfr_2005a_tool_box.c:45:
rotated.c: In function ‘XRotDrawHorizontalString’:
rotated.c:670:36: warning: passing argument 3 of ‘XCreateGC’ makes integer from pointer without a cast [-Wint-conversion]
my_gc=XCreateGC(dpy, drawable, NULL, 0);
^
In file included from qsfr_2005a_graphics.c:7:0,
from qsfr_2005a_tool_box.c:45:
/usr/include/X11/Xlib.h:1584:11: note: expected ‘long unsigned int’ but argument is of type ‘void *’
extern GC XCreateGC(
^
In file included from qsfr_2005a_graphics.c:8:0,
from qsfr_2005a_tool_box.c:45:
rotated.c: In function ‘XRotCreateTextItem’:
rotated.c:982:36: warning: passing argument 3 of ‘XCreateGC’ makes integer from pointer without a cast [-Wint-conversion]
font_gc=XCreateGC(dpy, canvas, NULL, 0);
^
In file included from qsfr_2005a_graphics.c:7:0,
from qsfr_2005a_tool_box.c:45:
/usr/include/X11/Xlib.h:1584:11: note: expected ‘long unsigned int’ but argument is of type ‘void *’
extern GC XCreateGC(
^
In file included from qsfr_2005a_graphics.c:8:0,
from qsfr_2005a_tool_box.c:45:
rotated.c: In function ‘XRotAddToLinkedList’:
rotated.c:1240:18: warning: format ‘%ld’ expects argument of type ‘long int’, but argument 4 has type ‘int’ [-Wformat=]
DEBUG_PRINT4("current cache size=%ld, new item=%ld, limit=%ld\n",
^
rotated.c:66:53: note: in definition of macro ‘DEBUG_PRINT4’
#define DEBUG_PRINT4(a, b, c, d) if (debug) printf (a, b, c, d)
^
rotated.c:1253:15: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long int’ [-Wformat=]
DEBUG_PRINT2("Removed %d bytes\n", i1->size);
^
rotated.c:64:47: note: in definition of macro ‘DEBUG_PRINT2’
#define DEBUG_PRINT2(a, b) if (debug) printf (a, b)
^
In file included from qsfr_2005a_tool_box.c:45:0:
qsfr_2005a_graphics.c: In function ‘initX’:
qsfr_2005a_graphics.c:48:27: warning: passing argument 3 of ‘XCreateGC’ makes integer from pointer without a cast [-Wint-conversion]
gc=XCreateGC(display,win,NULL,NULL);
^
In file included from qsfr_2005a_graphics.c:7:0,
from qsfr_2005a_tool_box.c:45:
/usr/include/X11/Xlib.h:1584:11: note: expected ‘long unsigned int’ but argument is of type ‘void *’
extern GC XCreateGC(
^
In file included from qsfr_2005a_tool_box.c:45:0:
qsfr_2005a_graphics.c: In function ‘Plot_Correlation_Matrix’:
qsfr_2005a_graphics.c:748:10: warning: implicit declaration of function ‘XpmWriteFileFromPixmap’ [-Wimplicit-function-declaration]
XpmWriteFileFromPixmap(display, fname_out, pix, 0, NULL);
^
qsfr_2005a_tool_box.c: At top level:
qsfr_2005a_tool_box.c:413:4: warning: return type defaults to ‘int’ [-Wimplicit-int]
main(int argc, char **argv)
^
physics#XPHYS9G7XGC2LLT:~/Desktop/viewQSFR/viewQSFR$ sudo make
gcc -o view_qsfr qsfr_2005a_tool_box.c -lX11 -lm -lc -L/usr/X11R6/lib -lXpm
In file included from qsfr_2005a_tool_box.c:44:0:
qsfr_2005a_readQSFR.c: In function ‘Read_QSFR’:
qsfr_2005a_readQSFR.c:706:19: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[201]’ [-Wformat=]
scanf("%s",&fname_extra);
^
In file included from qsfr_2005a_graphics.c:8:0,
from qsfr_2005a_tool_box.c:45:
rotated.c: In function ‘XRotPaintAlignedString’:
rotated.c:450:9: warning: return makes integer from pointer without a cast [-Wint-conversion]
return NULL;
^
rotated.c:453:36: warning: passing argument 3 of ‘XCreateGC’ makes integer from pointer without a cast [-Wint-conversion]
my_gc=XCreateGC(dpy, drawable, NULL, 0);
^
In file included from qsfr_2005a_graphics.c:7:0,
from qsfr_2005a_tool_box.c:45:
/usr/include/X11/Xlib.h:1584:11: note: expected ‘long unsigned int’ but argument is of type ‘void *’
extern GC XCreateGC(
^
In file included from qsfr_2005a_graphics.c:8:0,
from qsfr_2005a_tool_box.c:45:
rotated.c:510:45: warning: passing argument 3 of ‘XCreateGC’ makes integer from pointer without a cast [-Wint-conversion]
depth_one_gc=XCreateGC(dpy, empty_stipple, NULL, 0);
^
In file included from qsfr_2005a_graphics.c:7:0,
from qsfr_2005a_tool_box.c:45:
/usr/include/X11/Xlib.h:1584:11: note: expected ‘long unsigned int’ but argument is of type ‘void *’
extern GC XCreateGC(
^
In file included from qsfr_2005a_graphics.c:8:0,
from qsfr_2005a_tool_box.c:45:
rotated.c:566:43: warning: passing argument 3 of ‘XCreateGC’ makes integer from pointer without a cast [-Wint-conversion]
depth_one_gc=XCreateGC(dpy, new_bitmap, NULL, 0);
^
In file included from qsfr_2005a_graphics.c:7:0,
from qsfr_2005a_tool_box.c:45:
/usr/include/X11/Xlib.h:1584:11: note: expected ‘long unsigned int’ but argument is of type ‘void *’
extern GC XCreateGC(
^
In file included from qsfr_2005a_graphics.c:8:0,
from qsfr_2005a_tool_box.c:45:
rotated.c: In function ‘XRotDrawHorizontalString’:
rotated.c:670:36: warning: passing argument 3 of ‘XCreateGC’ makes integer from pointer without a cast [-Wint-conversion]
my_gc=XCreateGC(dpy, drawable, NULL, 0);
^
In file included from qsfr_2005a_graphics.c:7:0,
from qsfr_2005a_tool_box.c:45:
/usr/include/X11/Xlib.h:1584:11: note: expected ‘long unsigned int’ but argument is of type ‘void *’
extern GC XCreateGC(
^
In file included from qsfr_2005a_graphics.c:8:0,
from qsfr_2005a_tool_box.c:45:
rotated.c: In function ‘XRotCreateTextItem’:
rotated.c:982:36: warning: passing argument 3 of ‘XCreateGC’ makes integer from pointer without a cast [-Wint-conversion]
font_gc=XCreateGC(dpy, canvas, NULL, 0);
^
In file included from qsfr_2005a_graphics.c:7:0,
from qsfr_2005a_tool_box.c:45:
/usr/include/X11/Xlib.h:1584:11: note: expected ‘long unsigned int’ but argument is of type ‘void *’
extern GC XCreateGC(
^
In file included from qsfr_2005a_graphics.c:8:0,
from qsfr_2005a_tool_box.c:45:
rotated.c: In function ‘XRotAddToLinkedList’:
rotated.c:1240:18: warning: format ‘%ld’ expects argument of type ‘long int’, but argument 4 has type ‘int’ [-Wformat=]
DEBUG_PRINT4("current cache size=%ld, new item=%ld, limit=%ld\n",
^
rotated.c:66:53: note: in definition of macro ‘DEBUG_PRINT4’
#define DEBUG_PRINT4(a, b, c, d) if (debug) printf (a, b, c, d)
^
rotated.c:1253:15: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long int’ [-Wformat=]
DEBUG_PRINT2("Removed %d bytes\n", i1->size);
^
rotated.c:64:47: note: in definition of macro ‘DEBUG_PRINT2’
#define DEBUG_PRINT2(a, b) if (debug) printf (a, b)
^
In file included from qsfr_2005a_tool_box.c:45:0:
qsfr_2005a_graphics.c: In function ‘initX’:
qsfr_2005a_graphics.c:48:27: warning: passing argument 3 of ‘XCreateGC’ makes integer from pointer without a cast [-Wint-conversion]
gc=XCreateGC(display,win,NULL,NULL);
^
In file included from qsfr_2005a_graphics.c:7:0,
from qsfr_2005a_tool_box.c:45:
/usr/include/X11/Xlib.h:1584:11: note: expected ‘long unsigned int’ but argument is of type ‘void *’
extern GC XCreateGC(
^
In file included from qsfr_2005a_tool_box.c:45:0:
qsfr_2005a_graphics.c: In function ‘Plot_Correlation_Matrix’:
qsfr_2005a_graphics.c:748:10: warning: implicit declaration of function ‘XpmWriteFileFromPixmap’ [-Wimplicit-function-declaration]
XpmWriteFileFromPixmap(display, fname_out, pix, 0, NULL);
^
qsfr_2005a_tool_box.c: At top level:
qsfr_2005a_tool_box.c:413:4: warning: return type defaults to ‘int’ [-Wimplicit-int]
main(int argc, char **argv)
Unless you are using the -Werror flag to turn warnings into errors then the compiler will still have produced an executable that you can run.
The warnings are probably due to the fact that your new system has a newer compiler that is better at detecting problems in your code.
My suggestion would be to fix the warnings if you can/have time. Otherwise just run the program and ignore the warnings (for now, but fix them eventually).
Is your new computer 64 bit?
Is your older computer 32 bit?
The types of warnings I see suggest that you have 32 bit code that you might be trying to compile on a 64 bit computer (as well as the compiler being a much newer version).

Why can't you use scanf() in C for multiple input arguments of different types?

Why is this considered illegal in C ?
#include <stdio.h>
int main()
{
int integer;
char character;
float floatingPoint;
scanf(" %d %c %f", integer, character, floatingPoint);
return 0;
}
The above code produces the following error message under cc compiler.
cc Chapter2ex1.c
Chapter2ex1.c: In function ‘main’:
Chapter2ex1.c:8:5: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat=]
scanf(" %d%c%f", integer, character, floatingPoint);
^
Chapter2ex1.c:8:5: warning: format ‘%c’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat=]
Chapter2ex1.c:8:5: warning: format ‘%f’ expects argument of type ‘float *’, but argument 4 has type ‘double’ [-Wformat=]
You need to write
// v---------v-----------v-- addresses taken here
scanf(" %d %c %f", &integer, &character, &floatingPoint);
scanf needs to know the places where it should write the values it reads, not the values that currently reside there.

Is there anything that i missed in signed number in C language?

This is my basic C test program.
After I built it, I just entered negative number like -1, -2, etc. in console.
But the result is "oh", not "another number".
I don't know why this happens, because negative numbers should make the 'if' statement true.
int main(int argc, char* argv[]){
long int num;
scanf("%d", &num);
if(num ==1 || num < 0){
printf("another number\n");
}else{
printf("oh\n");
}
}
When you use the %d format string with scanf, the corresponding argument will be treated as int*. But you have passed a long int*. The value scanf stores will not be the same size as what your if statement reads.
Formally, you get undefined behavior. In practice, on most platforms scanf will write only part of the variable, and the rest will be left with an arbitrary value, with the usual bad effects on future use.
Use %ld for long variables, %d for ints. Change your code to one of these:
int num;
scanf("%d", &num);
or
long int num;
scanf("%ld", &num);
/tmp$ gcc -Wall foo.c
foo.c: In function ‘main’:
foo.c:4:5: warning: implicit declaration of function ‘scanf’ [-Wimplicit-function-declaration]
foo.c:4:5: warning: incompatible implicit declaration of built-in function ‘scanf’ [enabled by default]
foo.c:4:5: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘long int *’ [-Wformat]
foo.c:7:9: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
foo.c:7:9: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
foo.c:9:9: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
foo.c:11:1: warning: control reaches end of non-void function [-Wreturn-type]
fix the causes of those warnings and all the bugs will go away.

Resources