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.
I'm trying to solve the 8th problem of the project Euler and I'm stuck because I can't manage to create a very long array of char.
There must be a stupid semantic issue, but I'm unable to find it.
char cifre[] = "very long list of numbers here";
Such example works with gcc:
#include <stdio.h>
int main (void)
{
char *x =
"73167176531330624919225119674426574742355349194934\
96983520312774506326239578318016984801869478851843\
85861560789112949495459501737958331952853208805511\
12540698747158523863050715693290963295227443043557\
66896648950445244523161731856403098711121722383113\
62229893423380308135336276614282806444486645238749\
30358907296290491560440772390713810515859307960866\
70172427121883998797908792274921901699720888093776\
65727333001053367881220235421809751254540594752243\
52584907711670556013604839586446706324415722155397\
53697817977846174064955149290862569321978468622482\
83972241375657056057490261407972968652414535100474\
82166370484403199890008895243450658541227588666881\
16427171479924442928230863465674813919123162824586\
17866458359124566529476545682848912883142607690042\
24219022671055626321111109370544217506941658960408\
07198403850962455444362981230987879927244284909188\
84580156166097919133875499200524063689912560717606\
05886116467109405077541002256983155200055935729725\
71636269561882670428252483600823257530420752963450"
;
printf ("%s",x);
}
It prints your long number on screen without any problem. Symbol \ in string tells compiler that string literal is continued on next line. You are free to modify this example as you want. But please note that modifying content of string pointed by x isn't good idea.
Does adding a \ after each line of that 1000 digit number help?
It allows you to enter longer literals that span multiple lines.
Alternatively, surround each line of that long string in ", it will do the same thing.
As others have pointed out, you should probably allocate it dynamically. However, your question is somewhat vague as you didn't indicate where you are running into trouble or what your idea of "very long" is.
But here's some code to get you started:
#define ARRAY_SIZE 10240
char* pArray = (char*)malloc(ARRAY_SIZE);
memset(pArray, 0, ARRAY_SIZE);
free(pArray);
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.
weather.outdooe_temp is a float value which is being updated every time I press a button. set_temp is a float to ascii function. If I use that the thing works, but not if I use the code below.
char Thermo_Buff66[4];
static void SetBox(ScreenObj_t const *pS, EVENT_MSG const *pMsg)
{
//set_temp(weather.outdoor_temp,&a);//it works if i use this function.
sprintf(Thermo_Buff66,"%2.1f",weather.outdoor_temp);
(void)sprintf(Thermo_Buff,"%s\xc2\xb0""",Thermo_Buff66);
(void)DataBoxHandler(pS, &msg, "Set Temp", (uint8_t *)Thermo_Buff);
//currently displaying any # value....!!ing!!
}
char Thermo_Buff66[4];
sprintf(Thermo_Buff66,"%2.1f",weather.outdoor_temp);
The buffer you have allocated (Thermo_Buff66) is too short for a floating number representing outdoor temperature (often 2 digits) plus a . plus a digit after. Indeed, it doesn't have space for the terminating '\0' character. So immediate correction would be to set the size to 5. Still, in case of armageddon (or simply being in a non-SI country ... cough ... US), the temperature could even get to above 100, in which case again you overflow your buffer. Do yourself a favor and use snprintf.
Regardless, you sprintf into a buffer, then using %s you sprintf it into something else, which there is no point to. You can do it all directly in one, removing Thermo_Buff66 altogether:
(void)sprintf(Thermo_Buff, "%.1f\xc2\xb0", weather.outdoor_temp);
(void)DataBoxHandler(pS, &msg, "Set Temp", (uint8_t *)Thermo_Buff);
Side note: the . and the precision digit already take up 2 characters. Setting minimum width to 2 is therefore reduntant. Perhaps you thought the 2 in %2.1 is the number of digits before the .? Well it's not. It's the minimum overall width.
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 have a byte array (dB). I am trying to extract the bytes one by one. Why isn't this code working? Any pointers? Logically am I wrong? Or something wrong with my implementation?
You have byte buffers declared like this:
unsigned char *decodeBuf;
To read a single value from that buffer, at offset i you simply write:
unsigned char b = decodeBuf[i];
try
int main()
{
unsigned char tmp;
tmp = getByte(dB+dOffset); dOffset++;
}
it should work
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.
struct student
{
char am[7];/* èá íáé*/
float final_grade;
float exam_grade;
float essay_grade;
};
struct student mathites[1];
do{
printf("Dwse A.M.:");
scanf("%s",&mathites[j].am);
} while((mathites[j].am<8120000) || (mathites[j].am>812015));
I saw post like this, but everyone asks about comparison with "==" so the answer is strcmp,
something similar for this case?
something similar for this case?
This one is a little different since you're trying to check if a string is within a range of a numeric value.
A few points first:
struct student mathites[1];
if you're going to declare an array of one you may as well not make it an array.
char am[7];/* èá íáé*/
If the largest value you want to compare against is 7 characters "8120000" then you want your array sized 8 (to include space for the null terminator)
scanf("%s",&mathites[j].am);}
You don't need to use the & operator when getting a string, and you should use a limiter so you don't overflow your string
So taken this into account I'd say the code should be more like:
struct student
{
char am[8];/* èá íáé*/
float final_grade;
float exam_grade;
float essay_grade;
};
struct student mathites;
do {
printf("Dwse A.M.:");
scanf("%7s",mathites.am);}
Now your actual question, you need to convert the string to an number first, then check against the value:
long str_value = strtol(mathites.am, NULL, 10);
}
while((str_value <8120000) || (str_value >812015));
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'm looking for a solution to a rather simple problem:
Have a text file with words\strings separated by '\n' Each new string starts after '\n'
Need to get each one of those strings out of the file and create a separate entity out of it.
Yes, this task sounds pretty straightforward except for the vague idea of "creat[ing] a separate entity" out of a string (???).
Here is the general algorithm that I would use if I were to implement such a program:
#include <stdio.h>
#define LEN 0xFF
int main(int argc, char ** argv) {
char line[LEN];
FILE * input_file = NULL;
if ((input_file=fopen("myinputfile.txt","r")) == NULL) {
// Handle error when opening the file...
}
while (fgets(line, LEN, input_file) != NULL) {
// Process each line of input...
}
fclose(input_file);
return 0;
}
Based on the vague description that you gave, what you can do is to use an array (buffer), and in conjunction with a fopen, a loop, and sscanf, you can match the results of the file and put them into separate elements within the array. Then you can access the results. You'll likely need to track some indices.
There's a bunch of other ways that you can do this, but more information is definitely needed.
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'm writing a program in C, and for error handling it tells you to look for an illegal character when you're scanf-ing. In other words, look for a character that isn't an integer.
It is supposed to display an appropriate error message and terminate the program.
I'm a little confused as to how I go about looking for that illegal character, or noticing that it isn't an integer. Any help?
scanf() returns the number of successful arguments. If you do:
int ivar, return_val;
return_val = scanf("%i", &ivar);
return_val should be 1, cause of 1 parameter (ivar). Check the user input:
if (return_val == 1) {
// right input
} else {
// wrong input
}