Strange bug with C character copying program [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 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';

Related

C lang strlen function returns wrong value [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 2 years ago.
Improve this question
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void func(char* str) {
int i = 0;
for (i = 0; i < 8191; i += 1) {
str[i] = ('a');
}
}
int main(void) {
char buff[8192] = { 0, };
int len = 0;
func(buff);// printf("%s\n", buff);
len = strlen(buff);
printf("len:%d\n");
//printf("%s\n",buff);
return 0;
}
I try to expect the len : 8191 ,
but returns wrong number..
why is this happen??
could you explain why this happens??
printf("len:%d\n"); is incorrect. For each conversion specification such as %d, there must be an argument in the function call that gives the value to be printed. It should be printf("len:%d\n", len);.
Your compiler likely warned you of this. If it did not, enable warnings in your compiler and pay attention to them.

Primes in C: RunTime Error [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
#include <stdio.h>
int isPrime(int n){
int ndiv = 0;
int i;
for(i=1;i<=n;i=i+1){
if(n%i == 0){
ndiv = ndiv+1;
}
}
if(ndiv == 2){
return 1;
}
else{
return 0;
}
}
int nextPrime(int n){}
int main(){
int a = isPrime(7);
printf(a);
//printf(isPrime(4));
}
This code gives me a run time error, I think there's a problem here with the way I deal with data types while using a functions and the printf command, but I can't really figure it out. Help!
f in printf stands for "format". You need to supply a format string for printing: printf("%d\n", a)
Your isPrime is inefficient: you do not need to attempt dividing all the way up to the number itself. You could stop once you reach the square root of the number
Moreover, you could exit the loop early once you see that the number is not prime.
Once you fix these errors, your program would start running and producing the output that you expect.
Here is a small example of how to use printf. You can find more format specifiers here.
#include <stdio.h>
int main()
{
int a = 97;
int b = 98;
char hello[6] = "world";
printf("%d\n", a);
printf("%d\n", b);
printf("%s\n", hello);
return 0;
}
It is because your method of printing a variable is wrong. Here's the right one.
int main(){
int a = isPrime(7);
printf("%d",a);
}
I'm no C/C++ expert, but try
printf("%d", a);
%d is a format placeholder expecting an integer number, essentially.
That looks like an interesting isPrime function. Not very efficient at all, but different from what I have seen in the past. You can also loop over all the numbers between 1 and n, and just return false (or 0) if you find any that divise n. Or look up more efficient algorithms.

Extracting text from 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 5 years ago.
Improve this question
I am a beginner C-programmer. Recently I've been trying to practise using string functions in C.
As such, I wrote the following program:
MessageDetector.c
#include <stdio.h>
#include <string.h>
int main(void)
{
char a[100] = "Alex:HeyGoodMorning!:1911hrs:0:1012:2017:::";
char *p = strtok(a,":");
char n[20];
int i = 1;
while(p != NULL) {
strcpy(n,p);
p = strtok(NULL,":"); //limit to characters before semi-colon
i++;
if (i = 2) { //after 2 occurrences of the semi-colon. print a string
printf("%s\n",n);
break;
}
}
return 0;
}
The output of my program is as follows:
Alex
However, I would like the program to output
HeyGoodMorning!
What are the changes I should make to the above program? Your help is greatfully appreciated
Initialize the variable i with 0 and use comparison instead of assignment in this condition
int i = 0;
//...
if(i == 2){//
Take into account that the first call of strcpy is redundant.
In fact you could do the same without a loop. For example
char a[100] = "Alex:HeyGoodMorning!:1911hrs:0:1012:2017:::";
char *p;
if ((p = strtok(a, ":")) && (p = strtok(NULL, ":")))
{
puts(p);
}

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;

unexpected value C structure [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 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.

Resources