Why my code sometimes prints trash and other times not?

  • Thread starter Thread starter GustavoGG
  • Start date Start date
  • Tags Tags
    Code
Click For Summary

Discussion Overview

The discussion revolves around a C programming issue related to the handling of complex numbers and the behavior of the scanf function. Participants explore the reasons why the code sometimes produces unexpected output, particularly focusing on memory allocation and type definitions.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant suggests defining variables a and b as float values instead of float complex to align with the scanf format, proposing that the current definition leads to undefined behavior due to memory issues.
  • Another participant explains that defining a and b as float complex results in them occupying larger memory locations, which may cause the scanf function to read incorrect values into the imaginary parts, leading to "trash" output.
  • A later reply emphasizes the importance of compiling with warnings enabled to catch type mismatches in printf and scanf, illustrating this with an example of invalid code that compiles but leads to crashes.
  • One participant introduces an alternative way to define complex numbers using a struct, detailing how to initialize and use it, while noting that this approach was common in early C programming.

Areas of Agreement / Disagreement

Participants generally agree on the potential issues with the original code's variable definitions and the importance of type correctness, but there is no consensus on the best approach to defining complex numbers in C.

Contextual Notes

Some participants mention the historical context of complex number handling in C, but there are no resolved mathematical steps or definitive solutions presented in the discussion.

GustavoGG
Messages
2
Reaction score
0
TL;DR
Im tring to use the library complex.h in C, but at the moment of compile and run it, the firsts times prints trash and then it runs well but after that one, again prints trash and thus, and i dont if the problem is with my computer or whats going on?
I send the next code to a friend and it runs him perfectly but not to me.
C:
#include<stdio.h>
#include<math.h>
#include<complex.h>
int main(){

    float complex a,b,x=2,y,z;
    printf("Write the real part followed by the imaginary part of your complex number: \n");
    scanf("%f %f",&a,&b);
    z=a+I*b;
    printf("z= %f + i%f\n",creal(z),cimag(z));
    y=cpow(z,x);
    printf("z2= %f + i%f\n",creal(y),cimag(y));

    return 0;
}

<< Mentor Note -- code tags added >>
 
Technology news on Phys.org
why not define a and b as float values only?

float a,b;
float complex x=2,y,z;

to be consistent with the scanf format of "%f %f"

What I think is happening is that defining A and B to be float complex makes A and B each have an 8 byte memory location ie 4 bytes for the real part and four bytes for the imaginary part.

The scanf is reading in your data and converting your first text value to a four byte float to go into the real part of A and the second text value is converted into a four byte float for the real part of B meaning that imaginary parts of A and B are whatever is in memory when you program begins (ie memory is not always zeroed at startup).
 
Last edited:
  • Informative
  • Love
Likes   Reactions: anorlunda and GustavoGG
Thanks it worked perfectly.
I did not know what you just explained but its clear and now I get it
 
Always compile with -Wall (or the "All Warnings" equivalent.)

Most compiles can do checks for type correctness on printf, scanf... even though they don't have to.

Put this code into your IDE
Code:
printf("%s %f %d %s", 1.0f, "Hello", (void*)0);
This code is perfectly valid C even though none of the types match and it doesn't even have right number of arguments, will compile, will also crash. Tweak your preferences until your IDE complains about this line.
 
One way early C programs used to define a complex number was to use a typedef struct:

C:
typedef struct COMPLEX {
    float real;
    float imag;
} complex:

complex x;

void main() {

    complex x;

    // INITIALIZING x
    x.real = 30.0;
    x.imag = 40.0;

    scanf(stdin,"%f %f", x.real, x.imag);

    printf("complex number: %10.4f +  %10.4fi", x.real, x.imag);
}

Of course, designers would add macros to handle setting values and basic arithmetic operations (like vectors) and working with the complex struct. Later compiler designers would extend these notions to handle complex numbers as part of our more modern languages.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
4K
Replies
7
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
1
Views
2K
Replies
47
Views
5K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
2
Views
3K
  • · Replies 7 ·
Replies
7
Views
1K