• Hi guest! As you can see, the new Wizard Forums has been revived, and we are glad to have you visiting our site! However, it would be really helpful, both to you and us, if you registered on our website! Registering allows you to see all posts, and make posts yourself, which would be great if you could share your knowledge and opinions with us! You could also make posts to ask questions!

[Opinion] Useless C programs that may be of use to others, primarily on debugging and writing more efficient code

Everyone's got one.
Joined
Sep 9, 2021
Messages
9,682
Reaction score
5,219
Awards
32
This will be an ongoing posting of code that does not compile. As the title states, these are programs that were meant to be useful, but dont compile and ar for that reason deemed worthless. These will be good practice for seasoned or beginner programmers who know the C programming language, like to solve problems, and need practice on debugging program to get them to compile and run.


First wonder model of C code used to sum imaginary numbers following this logic:
The "imaginary" number is j. The example problem is computing the square root of -1, using the following equation to build complex numbers x and y, z is the result:
z = x + jy
Here is the non compiling C code called complexnum.c
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include "float.h"


int main(void)
{
double j,x,y,z =0.0;
printf("\nPleast enter two numbers: ");
scanf("%f %f",&j,&y);
z=x+(j*y);
printf("The sum is %0.f",&z);
return 0;
}

Again, this is meant to be educational. If I solve a given problem, I will post my code. If you figure it out before me, feel free to post your sample code.
 

Amur

Acolyte
Benefactor
Joined
Feb 8, 2022
Messages
438
Reaction score
393
Awards
7
It's #include <stdio.h> instead of #include "stdio.h", "" is local and <> is global.
 

Viktor

Zealot
Joined
Jul 31, 2022
Messages
182
Reaction score
309
Awards
5
You're taking address of "z" and then try to format it as decimal number.
printf("The sum is %0.f",&z);

Correct:
printf("The sum is %0.f", z);

C is clumsy language, I suggest you to use C++, it has <complex> library which lets you deal with complex numbers.
 
Joined
Sep 9, 2021
Messages
9,682
Reaction score
5,219
Awards
32
You're taking address of "z" and then try to format it as decimal number.


Correct:


C is clumsy language, I suggest you to use C++, it has <complex> library which lets you deal with complex numbers.
Thank you Viktor, will make the change and run it with gcc.
Post automatically merged:

This compiles, but does not link on my system.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <float.h>

int main(void)
{
float j,x,y,z =0.0;
x=-1.0;
printf("\nPleast enter two numbers: ");
scanf("%f %f",&j,&y);
z=x+(j*y);
printf("The sum is %0.f",z);
return 0;
}
To be frank, I would've expected an error regarding a math operation on zero of some sort, but it does compile.
Thanks.
 
Last edited:
Top