how to print a string vertically in c? - c

Ok the output is supposed to look like this:
./a 3 4 8 2
3
4
8
2
This is what I have so far, but I am lost and can only get the first integer to print (we have to use GetInt, which gets the specified integer in the string):
int main (int argc, char*argv []){
int v;
int i;
i = 1;
v = GetInt(argc, argv, i + 1);
if(argc >= 1){
printf("%d\n", GetInt(argc, argv, i));
}
return 0;
}

Looks like you need to use a for-loop to get each value in turn.

Without actually seeing your implementation of GetInt, something like this (Assumes C90):
#include <stdio.h>
int GetInt( int argc, char* argv[], int i ) {
/*
* you may want to use sscanf, strtol or other functions,
* but you haven't specified
*/
return atoi( argv[i] );
}
int main ( int argc, char* argv[] ) {
int i;
for ( i = 1; i < argc; ++i ) {
printf( "%d\n", GetInt( argc, argv, i ) );
}
return 0;
}
Returns:
$ ./a.out 3 4 8 2
3
4
8
2
Error checking omitted and such. It is an exercise to you to figure out how to deal with non-decimal arguments.

As a note, why do you have to use GetInt()? It's completely superfluous and not part of a standard library, and unless you wrote it yourself (or in class), I can't imagine ever using it. This is what I would do:
#include <stdio.h>
int main(int argc, char *argv[])
{
int i;
for(i = 1; i < argc; i++)
{
printf("%s\n", argv[i]);
}
return 0;
}
I know it doesn't use GetInt(), but it's shorter, and you probably shouldn't be using GetInt() because it's kind of useless. Plus, there's already an atoi() function and, if you don't want to use that because it's outdated, a strtol() function you can recast as an int if you have to.
I'm not citicizing you or anything, I'm just wondering why you're being required to use this superfluous, nonstandard function.

Use printf() in a loop. argc is the number of white space separated arguments in argv[], in the example above, argc = 5.

Related

How do i add up all elements of a command line argument

here is my code
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char* argv[])
{
int a;
for(int i=1;i<=argc;i++){
a+=atoi(argv[i]);
}
printf ("%d",a);
}
I keep getting segmentation faults but i am trying to add up all elements of the command line so for example ./a.out 5 6 7 would give 18 as the output, cheers.
The problem (with the crash) is the loop itself:
for(int i=1;i<=argc;i++)
The argc argument is the number of arguments passed to the program, including the "program name" at argv[0]. So valid indexes for the actual arguments are argv[1] to argv[argc - 1].
Furthermore the argv array is terminated by a null pointer, which will be at argv[argc].
Since you include argv[argc] in your loop you pass a null pointer to atoi which leads to undefined behavior and likely crashes.
The simple solution is to use less-than < instead of less-than-or-equal as the loop condition:
for(int i=1;i<argc;i++)
You never initialized a to 0. Also, use strtol() function.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
int a = 0;
for (int i = 1; i < argc; i++) {
a += strtol(argv[i], NULL, 10);
}
printf("%d\n", a);
return EXIT_SUCCESS;
}

How to provide parameters with command Line

i'm trying to deliver parameters for a program with the command line.
I want, that the program is working as shown now:
- start the program with parameter "program.exe "
- then the should be useable in the programm
How can i approach this thing?
Here is the essential part of my programm:
int main(){
int length;
unsigned int i=0;
length=strlen(word);
for(i=0;i<length;i++) {
printf("%d",word[i]);
}
}
And i wanted to add this word[] parameter via command line. Thanks!
int main( int argc, char* argv[] ) {
return 0;
}
argc => argument count / command line parameter count
argv[x] => argument value / parameter text at position
For command line arguments Use argv and argc
#include <stdio.h>
#include <stdlib.h>
int main( int argc, char* argv[] )
{
int i;
printf("argc is %d\n",argc);
for(i = 1; i < argc ; i++){
printf("%d \n", atoi(argv[i]));
}
}
Run your program as
./a.out 10 20 30
argc is 4
10
20
30

Unix environment running C programs

I am programming C programs in a Unix environment. I need to take a number from a user before the program is executed like so:
./program.out 60
How do I store the integer value in the C program?
You can use argv[] to get command line parameters, e.g.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int n;
if (argc != 2) // check that we have been passed the correct
{ // number of parameters
fprintf(stderr, "Usage: command param\n");
exit(1);
}
n = atoi(argv[1]); // convert first parameter to int
// ... // do whatever you need to with `n`
return 0;
}
int main (int argc, char *argv [ ])
{
//your code
}
argv [1] will then have the address of the numeric string which contains the number.
Then you can change this to an int if needed.
It is quite simple to do and I hope I have got your question right. See below:
#include <stdio.h>
int main(int argc, char* argv[])
{
printf("Number of arguments is: %d\n", argc);
printf("The entered value is %s\n", argv[1]);
return 0;
}
And then compile it on Linux as:
gcc file.c
./a.out 32
The program should print the value you require.
Hope this helps.

Iterating over C string not working

First, my objective with this code: take in a sentence into a C string. Iterate through the sentence and see how many instances of a particular letter occur.
This code is working somewhat but not giving the right number? Not sure why:
#include <stdio.h>
#include <string.h>
int tracker=0;
int letterCount (char *sentence)
{
int s=strlen(sentence);
int i=0;
for (i=0; i<s; i++){
if (sentence[i]=='h') {
tracker++;
}
}
return tracker;
}
int main(int argc, const char * argv[])
{
char *string="Hi there, what's going on? How's it going?";
letterCount(string);
printf("this sentensce has %i H's", tracker);
return 0;
}
The output I'm getting:
this sentensce has 2 H's
Not quite right. Any ideas?
This is the correct code if you mean case insensitive H:
#include <stdio.h>
#include <string.h>
int tracker=0;
int letterCount (char *sentence)
{
int s=strlen(sentence);
int i=0;
for (i=0; i<s; i++){
if (sentence[i]=='h' || sentence[i]=='H') { //'h' is not the same as 'H'
tracker++;
}
}
return tracker;
}
int main(int argc, const char * argv[])
{
char *string="Hi there, what's going on? How's it going?";
letterCount(string);
printf("this sentensce has %i H's", tracker);
return 0;
}
You have just mispelled small and the capital letter in your code.
Remember, the C language is case sensitive!
Although your label talks about the number of Hs, your letterCount looks for hs instead -- and it looks to me like the input you've provided does have two instances of lower-case h, just as it says.
If you want to count them together, you might consider filtering each input with tolower or toupper before checking what you have.
That number looks correct to me: you have 2 'h' characters in that sentence. If you want to count the 'H' characters as well, then you need a separate check.
size_t letterCount(const char* sentence, char c)
{
size_t count = 0;
while(sentence)
{
count += (*sentence == c);
++sentence;
}
return count;
}
What do we see here?
You can't have negative count, so use an unsigned type like size_t
sentence shouldn't be modified, so it should be const
pass in the char you want to match
sentence is a pointer, if it is null you are done. Don't need to call strlen.
sentence is a pointer, the actual pointer is pass by value, so you can modify it (see the increment, no need to make an extra variable)
boolean operators return 1 or 0, so no need to use the if. (Although, I haven't looked at the assembly to see if an if branch or an add 0 is cheaper. YMMV)

Basic C programming question

I've just started to learn C and it's going pretty slow...I wanted to write a program that takes in an integer argument and returns it's doubled value (aka take in integer, multiply by 2, and printf that value).
I purposely did not want to use the scanf function. Here's what I have so far and what is not compiling...
#include <stdio.h>
int main(int index)
{
if (!(index)) {
printf("No index given");
return 1;
}
a = index*2;
printf("Mult by 2 %d",a);
return 0;
}
So basically when the program is executed I want to supply the index integer. So, in cygwin, I would write something like ./a 10 and 10 would be stored into the index variable.
Also, I want to program to return "No index given" and exit if no index value was supplied...
Anyone care to help what I'm doing wrong?
EDIT:
This code returns 1 error upon compilation and is based on the help by #James:
#include <stdio.h>
int main(int 1, char index)
{
int index, a;
if (!(index)) {
printf("No index given");
return 1;
}
a = index*2;
printf("Mult by 2 %d",a);
return 0;
}
EDIT 2: Consider a simpler program where a value is just taken and echoed back (as shown below)
#include <stdio.h>
int main(int argc, char* argv[])
{
int index;
index = argv[1];
printf("Index is %d, ", index);
/*if (!(index)) {
printf("No index given");
return 1;
}
a = index*2;
printf("Mult by 2 %d",a);*/
return 0;
}
This program fails to compile...Any ideas?!? Ugh.
EDIT 3: This is the code that finally compiled and works. Thanks all!
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
if (argc <= 1)
{
printf("No index given");
return 1;
}
int i;
i = atoi(argv[1]); // convert string in argv[1] to integer
int a;
a = i*2;
printf("Mult by 2: %d",a);
return 0;
}
Thanks!
Amit
There are only two guaranteed-to-work prototypes for the main function: one takes no arguments (int main(void)), the other takes two arguments and looks like this:
int main(int argc, char* argv[])
argc is the number of arguments passed to the program, and argv is an array containing the arguments passed to the program.
If you want to pass an argument when you run the program (and not prompt the user for input), you will need to
use the form of main taking arguments,
check to make sure argc is greater than one (argv[0] is the program name, or should be),
convert argv[1] from its string representation to an integer, using strtol, sscanf, or some other library function (avoid atoi: it provides no usable error reporting),
then use that integer.
As you probably know, main() is a "special" function, which expects either 0 or two arguments: int argc, and char **argv, where argc is automagically assigned the number of arguments in the argument array argv. So, whatever arguments you pass to main() will be stored in argv, and that is from where you need to access your arguments.
This link should help.
The arguments you are passing into the program are text, and so main will receive them as strings. It splits the command line arguments by whitespace and passes them in as an array of strings, along with a number stating the number of parameters it is giving you. The program will always have at least one argument, the name of the file you ran the program as (which is in this case "a"). This string is always found at argv[0].
As the other answers stated, the correct signature for main is int main(int argc, char* argv[]). When you run ./a 10, argc will be 2 and argv[1] will be the string "10". You will need to convert this string to an integer to multiply it by 2, using something like int i = atoi(argv[1]); or int i; sscanf(argv[1], "%d", &i);.
Here is a correction to your code using the proper prototype for a Main with command line arguments.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
if (argc <= 1)
{
printf("No index given");
return 1;
}
int i = atoi(argv[1]); // convert string in argv[1] to integer
int a = i*2;
printf("Mult by 2 %d",a);
return 0;
}

Resources