Why my code sometimes prints trash and other times not?

  • Thread starter GustavoGG
  • Start date
  • Tags
    Code
In summary, this code defines a complex number using a typedef struct. The types are set correctly and it prints correctly.
  • #1
GustavoGG
2
0
TL;DR Summary
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
  • #2
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 anorlunda and GustavoGG
  • #3
Thanks it worked perfectly.
I did not know what you just explained but its clear and now I get it
 
  • #4
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.
 
  • #5
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.
 

1. Why does my code sometimes print random characters or symbols?

This could be due to a few different reasons. One possibility is that there is a bug in your code that is causing unexpected output. Another possibility is that your code is not handling certain input or edge cases properly, resulting in the unexpected output. It's important to thoroughly test your code and troubleshoot any potential bugs to ensure consistent and correct output.

2. How can I prevent my code from printing garbage values?

To prevent your code from printing garbage values, it's important to use proper data types and ensure that all variables are properly initialized before use. Additionally, thoroughly testing and debugging your code can help identify and fix any potential issues that may cause garbage values to be printed.

3. Why does my code work sometimes but not others?

This could be due to a number of factors, including differences in input data or external factors that may affect the performance of your code. It's important to thoroughly test your code with different inputs and conditions to ensure consistent functionality.

4. How can I make my code more consistent and reliable?

To make your code more consistent and reliable, it's important to follow best coding practices, such as using proper indentation, commenting your code, and avoiding unnecessary complexity. Additionally, thoroughly testing and debugging your code can help identify and fix any potential issues that may cause inconsistent or unreliable output.

5. Can other factors, such as computer hardware, affect the output of my code?

Yes, other factors such as computer hardware or software can potentially affect the output of your code. This is why it's important to test your code on different systems and environments to ensure consistent functionality. Additionally, optimizing your code for performance can help mitigate the impact of external factors on your code's output.

Similar threads

  • Programming and Computer Science
Replies
2
Views
3K
  • Programming and Computer Science
Replies
4
Views
733
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
940
  • Programming and Computer Science
Replies
7
Views
890
  • Programming and Computer Science
Replies
3
Views
993
  • Programming and Computer Science
2
Replies
47
Views
4K
  • Programming and Computer Science
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
Back
Top