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 8 years ago.
Improve this question
int main(int argc, char *argv[])
{
int param = *argv[1];
printf("Parameter = %d\n",param);
}
I run it ./myProgram 5 and I expect 5 as an output. What I get is 53 for some reason.
As you can see from the declaration, argv is an array of char pointers. This means that your main function is not being passed the numerical value 5, but the (probably ASCII- or UTF8-) encoded string value "5". If you look at ASCII table, you will see that the character "5" is encoded as the numerical value 53 in ASCII. You can also see that the letter "a" is encoded as number 97, so running ./myProgram a should output Parameter = 97.
To get the numerical value of an ASCII-encoded string you can use atoi(), e.g. write
int main(int argc, char *argv[]) {
int param = atoi(argv[1]);
printf("Parameter = %d\n",param);
}
C does not implcitily convert an array's content to any type.
To try to convert a 0-terminated char-array (a C "string") to an int consider using strtol().
Related
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
I'm a new c programmer.
I was asked to get an ID number as an input and if the ID length < 9 I need to put '0' before the number.
example:
input: 12345
output: 000012345
I can use the libraries: stdio, stdlib, string, math and time.
You can use printf for this:
#include <stdio.h>
int main () {
printf("%09d\n", 12345);
return(0);
}
Documentation
There is an easy solution in here.
But if you want a hard solution, where you juggle around with strings and such, you can do the following:
convert your id into a string (if it isn't already one, but argv should a string by default)
calculate the number of zeros you need, by doing 9-idString Length
create a "buffer" string variable
for loop from 0 to 9-idString and use strcat to add "0"s to your buffer string.
add your id to your buffer string using strcat
Example code would be something like this:
int id = 12345;
char idstring[10];
char buffer[10];
sprintf(idstring, "%d", id);
int missingZerosCount = 9 - strlen(idstring);
strcpy(buffer, "");
while (missingZerosCount > 0) {
strcat(buffer, "0");
missingZerosCount--;
}
strcat(buffer, idstring);
argv variant:
char idstring[10];
char buffer[10];
sprintf(idstring, "%s", argv[1]);
int missingZerosCount = 9 - strlen(idstring);
strcpy(buffer, "");
while (missingZerosCount > 0) {
strcat(buffer, "0");
missingZerosCount--;
}
strcat(buffer, idstring);
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 5 years ago.
Improve this question
Hi i am trying to read two characters from a file and want to send it to uint8_t* as hexadecimal .
Code:
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
int file_handling();
int main()
{
uint8_t *output ;
output=file_handling() ;
printf("\noutput_main --> %02x",output);
}
int file_handling()
{
uint8_t *output_hand ;
char c;
FILE *f_gets = fopen("filename", "r");
if(f_gets==NULL)
{
printf("Please point to a valid key file!\n");
fclose(f_gets);
return 0;
}
char str[3];
if( fgets (str, 3, f_gets)!=NULL )
{
/* writing content to stdout */
puts(str);
output_hand = (uint8_t *)(str);
puts(output_hand);
printf("\noutput %s --- %02x --> size --> %lu",str,*output_hand,sizeof(*output_hand));
}
fclose(f_gets);
return *output_hand;
}
following is output
we we
output we --- 77 --> size --> 1 output_main --> 65
what i can understand is 77 is ascii for w and 65 is ascii for e
but i want to put "we" which i suppose is a hex in uint8_t *output
where is the problem
in main ,if i use pointer "*output=file_handling()" instead of just output i get segmentation fault.
How to read value from a file and put it into uint8_t , where file is having hex characters,how fget identifies it as hex or char.
Thanks
file is a text file
ab
fe
ea
ce
1d
Basically
uint8_t *output;
*output = 0xFA ;
it works
but i want to read from above file and put it into output variable
You have plenty of problems, here's three of them:
You define str to be an array of two characters, which means it can only contain a single-character string. You then call fgets telling it that str it three characters, which means it can and will write out of bounds of your array.
You have declared file_handling to return an int value. You return the first character in the array string, and assign that to the pointer variable output in the main function. You then treat this pointer as a single int value.
In the main function you pass the pointer output but print it as an int value. There's a mismatch between the format specifier and the argument.
The first and third issues lead to undefined behavior.
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 6 years ago.
Improve this question
I want two create two functions that can do this. So one function takes a character, for example the character a and returns the integer 97. The other function takes this integer 97 and returns the character a. I know this can be done by using the ASCII codes of these characters, but then it wouldn't work for characters like é, à, ö. Can this be done using unicode or another way?
For example:
int character_to_integer(char c) {
convert character to integer and return
}
Input: character_to_index('é');
Output: 102 (for example)
char integer_to_character(int i) {
convert integer to character and return
}
Input: integer_to_character(102);
Output: é
I want to do this with it: have an array, so for example int my_array[5] with all elements set to NULL at the start. Then for example, index 0, 3 and 4 (which correspond to a, d and e for example) are set to something other than NULL then I want to loop over it and build a string based off the which indexes aren't NULL, like so:
void build_string_from_array(int my_array) {
char buffer[16];
char c;
for (i = 0; i < 5; i++) {
if (my_array[i] != NULL) {
c = integer_to_character(i);
buffer[i] = c;
}
}
buffer[5] = '\0';
printf("%s\n", buffer);
}
Output: ade
Note, this is just an example, and I know there is probably something wrong with it, but it's just to get my point across. I know this can be done with ASCII codes, where all the characters are only 1 char, but how can this be done so that characters like é, that are seen as 2 chars would also work?
If it's not clear what I mean just ask me and I'll elaborate some more.
For single Byte chars, this is no Problem, since char is a integer:
int i = 'B';
and
char c = 0x33;
will work fine.
But, if you use UTF8 with chars with more than one Byte, you must convert the UTF8-String to a UCS4 String. Sadly there is no Standard API for that.
See also this Post: Converting a UTF-8 text to wchar_t
A other way is use wchar_t everywhere. This will not work well on Windows with chars outside the BMP, since the wchar_t implementation in Windows is brocken (wchar_t is still a Multibyte Character Set on Windows). On Linux it will work, if you not use compound chars.
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'm having the issues of trying to separate 1s or 2d into int 1 and char 's' and then storing them as separate values. How would I go about doing this. I know I can't do it with strtok since there are no delimiters.
Use strtol. For example,
char *endptr;
val = strtol(input_str, &endptr, 10;
next_char = *endptr;
As discussed in the manpage, http://man7.org/linux/man-pages/man3/strtol.3.html, the second parameter is a pointer to a char pointer and after the conversion, the char pointer points to the following character.
You can access each char from the string by index. To convert char to int (not by ascii value, '1' to 1) you just do the following:
int a = c[0]-'0';
and for the char:
char b = c[1];
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 have the following program:
#include <stdio.h>
#include <stdlib.h>
int main()
{
static char string[12];
int length,c,d;
printf("Enter a string :");
gets(string);
length=strlen(string);
printf("\nLength of the string is %d",length);
for(c=0;c<=length-2;c++)
{
d=c+1;
printf("\t%.*s\n",d,string);
}
for(c=length;c>=0;c--)
{
d=c+1;
printf("\t%.*s\n",d,string);
}
}
I am very much confused about the usage of %.*s in the printf statement. I know %s is used for displaying strings, but I am confused the usage of .* before s in this program. Also there is only one datatype (%s) mentioned inside the quotation marks in the printf statement, but there are two variables mentioned in the printf statement.
It is a precision component, which specifies maximum number of bytes for string conversions. Asterisk (*), uses an integer argument, which specifies the value (for precision) to be used.
As an example, the following code:
#include <stdio.h>
int main(int argv, char **argc)
{
char *s = "hello, world";
printf("%.*s\n", 4, s);
return 0;
}
gives output:
hell
The format statement can allow a width and precision value. So, to print a string for a variable length then specify printf("%.*s", length, string). The length is substituted for the asterisk.