unexpected value C structure [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm looking for some help with understanding why I'm getting a value. I've got a very basic menu to select and set values on a KL25Z micro controller (32-bit architecture). I've got an int that wont print a value above 255. What am I doing wrong?
beacon.h
typedef struct _payload_t {
int t1_range;
} PAYLOAD_T;
beacon.c
#define BUFFSIZE 100
PAYLOAD_T payload;
int main (int argc, char *argv[])
{
char line[2];
int ret, select;
// print menu
print_menu();
// get menu input
ret = readline(line, BUFFSIZE, stdin, stdout);
select = atoi(line);
switch(select)
{
case 1:
uprintf(" Target 1 Starting Range: ");
ret = readline(line, BUFFSIZE, stdin, stdout); //--> 257
payload.t1_range = atoi(line);
printf(" Selection = %s\r\n", line); //--> 257
printf("(%d)\r\n",payload.t1_range); //--> 1
break;
... rest of case
}
return 0;
} //end main
Everything works ok until values above 255 are entered, then the values displayed seem to revert back to 1. If payload.t1_range is an int (16 bits) why is it acting like an 8 bit?
Any help or direction would be greatly appreciated.
Thanks!

line is 2 chars long. atoi works on nul terminated strings, so anything over a 1 character number is going to give you undefined behavior.
Try making line bigger.

Related

How to calculate long long / int [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I am trying to learn how to code by myself and is experiencing some difficulty in calculations. Can someone please explain why the pf always return 0 in the below?
int main(void)
{
//solicit input from users
long long int num = get_long_long("Credit card no: ");
eprintf("%lld\n",num);
//initialize
int i =0;
int j =0;
int counter =0;
string status;
//find length of input
while (num>0)
{
num/= 10;
counter++;
}
printf("counter is %i\n",counter);
//Identify card type by prefix
int power=(counter-2);
eprintf("power is %i\n", power);
int dp = pow(10,power);
eprintf("divofp is %i\n", dp);
//prefix=num
long long int pf=(num/dp);
eprintf("pf is %lld\n",pf);
}
pf will always be zero, because num is set to zero at the end of your while loop.
Therefore num/anything will always equal zero.
A good method of debugging, is to step through the code line by line, and look at the values of your variables at each point in time.
This can help you narrow down problems like this.
The problem is where you get the length of your number:
while (num>0)
num/=10;
num will always be 0 after this and thus your final expression will result in 0 because 0/x = 0 (x != 0).

New to C,Loop Issues [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I want to enter value to my array and between 0 and 101 and trying to put zero or 101 will return an error but putting anything from 1-100 will allow me to enter a second number.
but i am stuck when write code to add a second number the loop keeps going in 1 and i cant figure out my error. any help would be apreciated
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
char letter;
int main()
{
int i;
int grades[12] ;
for ( i = 0; i < 12; i++)
do{//loops the following peice of code if values over 100 or less than 0 are entered
printf("Please Enter Grade 1:\n");
scanf("%f", & grades[i]);
if (grades > 100 || grades < 0)
printf("Invalid Entry please enter another between 0 and 100:\n");
}while(grades < 100 || grades > 0);
}
The mistake here is you're comparing the whole array to a value. What you want is one element, plus you need to test that you're outside of the acceptable bounds:
while(grades[i] > 100 || grades[i] < 0);
As Jonathan points out below, the logic in your code tests that it's within, which means only valid answers will loop forever, the opposite of what you wanted.

Why does this loop execute only 4 times? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am beginner in c programming.I just want to know why this loop is not working properly.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
char x[8];
char t;
for (i = 0; i < 8; i++) {
scanf("%c", &t);
x[i] = t;
}
return 0;
}
Because when any input given from keyboard we need to press enter to confirm completion of input. This enter stay in buffer and if next input is char or string, stores enter in string or char var and do not wait to input that char or string. In this case, first input given at execution it stores char in X[0] and enter in x[1] and so on. So executes loop 8 time but it seems to 4 time because it ask input only four times. To check that put one printf in loop
it executes 8 times.
Whenever you press enter to submit, you are entering a whitespace character that is consuming one of your loop iterations.

Strange bug with C character copying program [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I've created a simple program that takes two command line arguments(a char and a number), and prints the char however many times specify with the number to the screen.
Example:
./fstring a 4
aaaa
It mostly works, but for some reason, with specific numbers there is weird input at the end.
./fstring a 8
aaaaaaaa¼#
./fstring a 9
aaaaaaaaa#
./fstring a 10
aaaaaaaaaa#
The same pattern of weirdness happens with 40, 41 and 42, as well as 88, 89 and 90.. and so forth. It seems to happen in increments of forty starting at 8. Here's the code.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void create_string(char chr, int times);
int main(int argc, char *argv[])
{
create_string(*argv[1], atoi(argv[2]));
return 0;
}
void create_string(char c, int t)
{
char buf[t+1];
int i;
for(i = 0; i < t; ++i)
buf[i] = c;
printf("%s\n", buf);
}
I imagine it has something to do with buf, but I can't figure it out.
You forgot about the terminating zero of strings. Write
for(i = 0; i < t; ++i)
buf[i] = c;
buf[i] = '\0';

How to use char in C [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm struggling to get Char to work. It keeps returning an error.
#include <stdio.h>
#include <cs50.h>
int main (void)
{
int tower_height;
char #;
// Inputs
do {
printf("Let's build! Give me a number between 0 and 23 'inclusive'.\n");
tower_height = GetInt();
}
while
(tower_height < 0 || tower_height > 23);
// Outputs
for (tower_height = 0; tower_height <= 23; tower_height++)
printf ("%c = tower_height - 2\n");
}
C identifier names may contain letters, underscore, and digits, as long
as the first character isn't a digit, and as long as the identifier
isn't a keyword. They may not contain #.
# is not a valid variable name.
As pointed out, # is not a valid variable name.
You can see how # is properly used in the first line of your code: #include <stdio.h>
Instead, call your char variable something that uses letters and numbers eg: char c;

Resources