It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I would like to create a C program that accepts an argument of the form
-aK
where K is some integer from 0-9.
How would I parse/specify this option?
You might want to check out getopt and/or getopt_long.
a simple requirement like this can be solved with getopt.
Also you can do this:
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char ch, a;
int d;
if(argc == 1) return;
if(argc == 2){
if(strlen(argv[1]) > 2){
sscanf(argv[1],"%c%c%d",&ch,&a,&d);
if(ch == '-' && a == 'a'){
printf("%d is your number",d);
}
}
}
return 0;
}
Related
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I'm learning c and I can't figure out the problem with this code:
#include <stdio.h>
int main(){
int i = 0;
while(i > 10){
printf("hello");
i++;
}
getch();
return 0;
}
I don't get any errors and have tried running it on codeblocks and wxdev c++. So is there something I'm doing wrong. Thanks.
You set
i = 0;
and then test
i > 10
which is always false.
You might want
while (i < 10)
instead.
I is not greater than 10 so it doesnt meet the requirement to enter the while loop
while(i > 10){
...but i is 0 so it's false and skips.
You probably meant to instead write;
while(i < 10) {
Reason: i is not greater than 10.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have an array of chars "0x55".
What I want to do is convert it to a char which is going to be U (because ASCII 0x55 = U).
So how to do this conversion?
#include <windows.h>
int main()
{
array[] = "0x55"
char test;
**// I want to move the string to that test to be one character which is U**
}
Any suggestions?
I think this is what you are after:
int main(int argc,char**argv)
{
char array[] = "0x55";
int value;
char test;
sscanf(array,"%x",&value);
test = value;
return 0;
}
In C++, I would code it a little differently, but this seems more like a C question.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
#include<stdio.h>
void main()
{
float i,j;
i=??;
j=i+1;
if(i==j)
printf("Bug");
}
My teacher gave me this qns to find the value of i so that the printf executes.
He said this is a common bug in a lot of languages.
Always try the boundaries for odd behavior. This worked for me:
#include <float.h>
int _tmain(int argc, _TCHAR* argv[])
{
float i,j;
i = FLT_MAX;
j = i + 1;
if(i == j)
{
printf("they're the same");
}
return 0;
}
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
#include <stdio.h>
#include <string.h>
int test(char ch [10],int i,int j )
{
if(i>=j) return 1;
else if (ch[i]!=ch[j]) return 0;
else return (test(ch,i++,j--));
}
int main ()
{
char ch[10];
int m,k;
printf("Donner une chaine de caracteres :\n");
scanf("%s",ch);
k=strlen(ch);
m=test(ch,0,k-1);
if (m==1) printf ("expression palindrome \n");
else printf ("expression non palindrome \n");
return 0;
}
Try replacing this:
else return (test(ch,i++,j--));
...with this:
else return (test(ch,i+1,j-1));
There's no need to assign back into 'i' and 'j' when making that call, since you don't reference them again in the same function invocation. Moreover, i++ evaluates to the original value of i, and not the value of i + 1 (which is what you want here).
So your original code would never actually modify i and j, which would cause it to recurse infitely and cause a stack overflow (so I can't believe people were saying that this wasn't appropriate for SO).
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
Not counting the function signature (just the body) can anybody produce C code shorter than this function that will reverse a string and return the result as a pointer to the reversed string.. (not using a string reverse library function either)?
char * reverse_str(char * s)
{
char c,*f=s,*p=s;while(*p)p++;while(--p>s){c=*p;*p=*s;*s++=c;}return f;
}
not much longer, but it works.
#include <string.h>
/* precondition: s!=const && s!=NULL && *s!='\0' */
char *mystrrev(char *s)
{
char *a=s,*e=s+strlen(s)-1;
while( a<e )
{
char c=*a;
*a++=*e;
*e--=c;
}
return s;
}