Problems with C program- Not C++ and not Java

In summary, the program reads a word and a number from the user and stores them in variables. Then, it prints the word and number separated by an underscore.
  • #1
jasonsmith206
10
0
A user types a word and a number on a single line. Read them into the provided variables. Then print: word_number. End with newline. Example output if user entered: Amy 5

Amy_5
#include <stdio.h>int main(void) {
char userWord[20] = "";
int userNum = 0;

/* Your solution goes here */

return 0;
}Was part of homework but its over a week old. Just want to know how to work it and any help would be greatly appreciated.

Thanks!
 
Technology news on Phys.org
  • #2
Hi, and welcome to the forum.

One way to finish the program is
Code:
  scanf("%s %d", userWord, &userNum);
  printf("%s_%d\n", userWord, userNum);

To understand it, you need to know about pointers and their relation to arrays.
 
  • #3
wow!

i did everything right on scanf but instead of printing what you've got i put (userWord and userNum to = word_number).. so close yet so far. Thank you so much for your help that one was driving me crazy!
 

1. What are common problems with C programs?

One common problem with C programs is memory management, as C does not have automatic garbage collection. This can lead to memory leaks and other issues if not managed properly. Another frequent problem is pointer errors, which can cause unexpected behavior or crashes. Additionally, C programs can be vulnerable to buffer overflows, where data is written beyond the allocated memory space, leading to security vulnerabilities. Lastly, debugging C programs can be challenging, as they do not have built-in error checking and reporting mechanisms.

2. How can I fix memory leaks in C programs?

To fix memory leaks in C programs, you can use functions such as malloc() and free() to dynamically allocate and deallocate memory. You should also make sure to free any memory that is no longer needed, and avoid using global variables which can cause memory leaks. It is also helpful to use tools such as Valgrind to detect and fix memory leaks in C programs.

3. What is the difference between a pointer and a reference in C?

In C, a pointer is a variable that holds the memory address of another variable. Pointers are used to indirectly access and manipulate data stored in memory. On the other hand, a reference is an alias for another variable, meaning it is another name for the same memory location. References are often used in function parameters to avoid copying large amounts of data.

4. How can I prevent buffer overflows in C programs?

To prevent buffer overflows in C programs, you can use functions such as fgets() instead of gets() to read user input, as fgets() allows you to specify the maximum number of characters to read. You should also make sure to allocate enough memory for your buffers and validate user input to avoid writing beyond the allocated space.

5. What tools can I use to debug C programs?

There are several tools available for debugging C programs, such as the GNU Debugger (GDB) and the Interactive DisAssembler (IDA). These tools allow you to step through your code, set breakpoints, and view the values of variables at different points in your program. Additionally, using printf() statements to print out variable values can also be a helpful debugging technique.

Similar threads

  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
3
Views
10K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
2
Views
12K
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
4
Views
734
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
30
Views
2K
  • Programming and Computer Science
Replies
1
Views
6K
Back
Top