A little question about c language

In summary, the C language is a general-purpose programming language that is known for its speed, efficiency, and ability to directly access hardware. Its syntax is unique and allows for low-level control, making it a popular choice for programming. It has a large community, robust tools and libraries, and is highly portable. A simple "Hello, World!" program serves as an example of C code. Despite advancements in technology, C remains relevant and widely used due to its foundational role in other languages and its efficiency and versatility for various applications.
  • #1
nenyan
67
0
#include <stdio.h>
#include <stdlib.h>
int main()
{ double a[10];
int i=0;
for(i=0;i<10;i++)
a=0.0;
for(i=0;i<10;i++)
printf("a[%d]=%e, "), i, a;}

The result is radom number. What's wrong with my code?
 
Technology news on Phys.org
  • #2
nenyan said:
The result is radom number. What's wrong with my code?
The problem is here:
printf("a[%d]=%e, "), i, a;


Where is the closing parenthesis on the printf statement?
 
  • #3
ok,,,i found the error
 
  • #4
D H said:
The problem is here:

Where is the closing parenthesis on the printf statement?

Thanks a lot
 
  • #5


There are a few potential issues with your code that could be causing the unexpected output. First, it is important to note that C does not initialize arrays to 0 by default, so it is possible that the values in the array are not actually 0. To ensure that the array is initialized to 0, you can use the memset function from the <string.h> library.

Another potential issue could be with the printf statement. It appears that you are missing a format specifier for the value of i, which could be causing unexpected output. A corrected printf statement could be: printf("a[%d]=%e\n", i, a);

Additionally, it is not clear what your intention is with the second for loop. It is essentially just printing the values of the array again, which may not be necessary. If you are trying to reset the values of the array to 0, you can simply use the memset function mentioned earlier.

Finally, it is always a good practice to include comments in your code to explain what each line is doing. This can help with identifying potential issues and make your code more understandable to others.
 

Related to A little question about c language

1. What is the purpose of the C language?

The C language is a general-purpose programming language that was designed for system programming, but has since been used for a wide variety of applications. It is known for its speed, efficiency, and ability to directly access computer hardware, making it a popular choice for software development.

2. How is the syntax of C different from other programming languages?

The syntax of C is unique in that it is relatively simple and allows for low-level control over computer hardware. It uses a combination of keywords, punctuation, and identifiers to create statements, and is known for its use of pointers and memory management.

3. What makes C a popular choice for programming?

C is a popular choice for programming because it is a powerful and efficient language that can be used for a wide range of applications. It also has a large community of developers, a robust set of libraries and tools, and is highly portable, meaning it can be used on different operating systems and hardware platforms.

4. Can you give an example of a program written in C?

Sure, here is a simple "Hello, World!" program written in C:

#include <stdio.h>
int main()
{
printf("Hello, World!");
return 0;
}

5. Is C still relevant in today's technology landscape?

Yes, C is still a relevant and widely used language in today's technology landscape. It is the foundation for many other popular languages, such as C++, Java, and Python, and is often used in operating systems, embedded systems, and high-performance applications. Many companies and organizations continue to use C for its speed, efficiency, and versatility.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
4
Views
822
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
4
Views
949
  • Programming and Computer Science
Replies
7
Views
966
  • Programming and Computer Science
Replies
22
Views
2K
Back
Top