User input C programming [closed] - c

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am trying to run a C program that requires the user input.
The program is supposed to prompt the user to enter certain words and I am supposed to search for those words in a data structure.
so the command line is supposed to look like this:
prompt>
the user will enter multiple words to search and I need access to each one of those words separately. AFter the program is done executing on those words, the program needs to restart and keep running until the user types in "quit" in the prompt.
Ex:
prompt> ..... (program will run based on the words input)
prompt> .....
prompt> .....
prompt> quit
I dont know how to prompt for user input in C, can anyone help with this?
Thanks in advance.

1) vi hello.c:
#include <stdio.h>
#define MAX_LEN 80
int
main (int argc, char *argv[])
{
char a_word[MAX_LEN];
printf ("Enter a word: ");
scanf ("%s", a_word);
printf ("You entered: %s\n", a_word);
return 0;
}
2) gcc -G -Wall -pedantic -o hello hello.c
3) ./hello
NOTE:
The syntax will be different depending on your platform and compiler.
Here's another link:
http://www.codingunit.com/c-tutorial-first-c-program-hello-world

This might help
http://en.wikibooks.org/wiki/C_Programming/Simple_input_and_output
basically scanf

Related

How to write a C/C++ wrapper program in Linux that is fully transparent [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Note: This is not a question to ask for a program, It asks about some tech details, see the question bellow first.
I need to write a wrapper program in C/C++ for an existing program. I know we need to use exec/fork/system and pass through the parameters then return the result of the program.
The question is, how to ensure that both the invoker program(that invoke the wrapper) and the wrapped program work exactly like before (ignore timing differences). There maybe subtle things like environment parameters to deal with. fork/system/exec, which to use? Are they enough? Are there other factors to consider?
Let's say you have the following original program:
foo.sh
#!/bin/bash
echo "Called with: ${#}"
exit 23
Make it executable:
$ chmod +x foo.sh
Now the wrapper in C:
wrapper.c
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char* argv[]) {
printf("Executing wrapper code\n");
/* do something ... */
printf("Executing original program\n");
if(execv("./foo.sh", argv) == -1) {
printf("Failed to execute original program: %s\n", strerror(errno));
return -1;
}
}
Run it:
$ gcc wrapper.c
$ ./a.out --foo -b "ar"
Executing wrapper code
Executing original program
Called with: --foo -b ar
$ echo $?
23

Permission denied when trying to run C program in terminal [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm completely new to programming. I'm actually learning from a Harvard class on iTunes U. When trying to code along side the instructor I've ran into a problem. I can't run my .c program in terminal. I've tried the sudo commands, and I've searched with Google and I can't seem to find an answer, probably because I'm so new to programming. It's probably something I've overlooked or I just don't understand yet.
#include <stdio.h>
int
main(void)
{
printf("temperature in f: ");
float f = GetFloat();
float c=f / 9.0 * (f-32);
Printf("%. if F = %. if c\n", f, c)
I'm using Sublime text editor on a MacBook with Mac OS X Yosemite (10.10.x).
Your compiler should have warn you about some errors:
// string.c
#include <stdio.h>
int main(void) // There must be a return statement
{
printf("temperature in f: ");
float f = GetFloat();
float c = f / 9.0 * (f-32);
printf("%f if F = %f if c\n", f, c); // Missing ';' is the most common syntax error
return 0; // Here is the return
} // Do not forget to close your brackets
When you do:
gcc string.c -o myprogram
It will tell you what is wrong in your program. Once you have fixed all the errors you can run the program with:
./myprogram
Understand that you cannot run a C-file: the .c contains human-readable instructions for the machine. You have to compile it, i.e. translate it into a language that your computer will understand : it is roughly your compiled myprogram (and you do not want to open it to look what it contains, it will burn your eyes :p).
Just adding to the below answer your compiler should throw the below error also:
undefined reference to `Printf'
check case of your printf()

Weird behavior when passing * as a command line parameter on linux [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I have this very simple c program:
#include <stdio.h>
int main (int argc, char ** argv){
printf ("%s\n",argv[1]);
}
When running it on Linux/bash like so:
./a.out *
I get the following output:
a.c
why?
Because * is a glob character that expands to the list of files in the current directory.
If you want to pass a literal * you will need to quote or escape it:
./a.out '*'
I didn't know that, but when running a command line that has a glob character , such as * or ?, the command line interpreter first expands the character and only then run the program.
For example, if your program is:
#include <stdio.h>
int main (int argc, char ** argv){
int i;
printf ("argc=%d\n",argc);
for (i=0;i<argc;i++){
printf("%d: %s\n",i,argv[i]);
}
}
and you run it like so:
./a.out *
, then the output will be:
argc=4
0: ./a.out
1: a.c
2: a.c~
3: a.out
Of course, the output will depend on the content of the current directory.

Why is there a `%` in output when I do not add `\n` in the printf statement? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I write a C program:
#include <stdio.h>
int main() {
printf("a\n");
}
And execute it, get this:
a
But if I do not add the \n in the printf statement then I will get this:
a%
Why?
Updated: Here are their images:
The % character is most probably your command-line prompt.
'\n' is a new-line character. Possibly in the first case you see % at the next line, right after a, something like:
// printf("a");
% gcc prog.c
% ./a.out
a%
^
// printf("a\n");
% gcc prog.c
% ./a.out
a
%
^
EDIT: It turned out that some shells print special characters (e.g. %) at the end of partial output lines to preserve them from being covered with the prompt. More details at https://superuser.com/questions/645599/why-is-a-percent-sign-appearing-before-each-prompt-on-zsh-in-windows
The % is the prompt printed by your shell. It is printed when the program terminates. When there's no \n, there is no newline to separate the output of the printf() and the prompt.
This is the prompt of your shell. Since there's no final newline, the output of your program runs straight into the next prompt.

libyahoo Segmentation fault [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I wrote this code:
#include <libyahoo2/yahoo2.h>
#include <libyahoo2/yahoo2_callbacks.h>
int main() {
int id ;
char username[255] = "slam";
char password[255] = "ss" ;
id = yahoo_init(username, password);
enum yahoo_status mYahoo ;
mYahoo = YAHOO_STATUS_AVAILABLE ;
yahoo_login(id , mYahoo );
return 0;
}
Compile it, gcc -l yahoo2 y.c -o yahoo and run it with ./yahoo gives me a error: Segmentation fault
(gdb) run
Program received signal SIGSEGV, Segmentation fault.
0x001379b1 in yahoo_login (id=1, initial=0) at libyahoo2.c:1735
line 1735 code is:
tag = YAHOO_CALLBACK(ext_yahoo_connect_async) (yd->client_id,
host, yss->pager_port, yahoo_connected, ccd, 0);
and see this :
(gdb) list YAHOO_CALLBACK
Function "YAHOO_CALLBACK" not defined.
How do I debug this?
How do I debug this?
Execute these commands:
(gdb) print yd->client_id
(gdb) print yss->pager_port
My guess would be that one or both of the above commands will fail, because yd or yss is NULL.
If so, the problem is earlier in libyahoo2.c, and it (apparently) doesn't check for errors properly.
The reason you can't list YAHOO_CALLBACK is most likely that it is a macro. Look in libyahoo2/yahoo2_callbacks.h -- it's very likely to be defined there.
Also, your link command line:
gcc -l yahoo2 y.c -o yahoo
is totally bogus. Correct command line should be:
gcc y.c -o yahoo -lyahoo2
You may want to read this explanation to understand why order of sources and libraries on command line matters.

Resources