Iterating through a const char [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 1 year ago.
Improve this question
In C if I wanted to iterate through the array and get the ascii value of each letter would I do it by
const char array[] = "name"
for (int i=0; i<4; i++){
printf("%s", array['i']);}
I am still unfamiliar with C and would appreciate any pointers.

The format specifier %s means "print a string" so that's not what you want. If you want the ascii value in decimal use %d or %u. If you want the ascii value in hex use %x.
Also notice that the indexing is array[i]
Further, use strlen instead of a hard coded 4. By doing that you can change "name" to "WhatEver" without having to change the loop condition as well.
Like:
const char array[] = "name";
for (size_t len = strlen(name), i = 0; i < len; i++)
{
printf("%d ", array[i]);
}
puts("");

Related

New to C and can't figure out these outputs [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 last year.
Improve this question
Hi I am new to c and trying to build just a simple print array however I just get what it wants to give me. Here is my code:
#include <stdio.h>
int main(){
int n[5]={5,10,15,20,25};
int i;
printf("displaying integers:");
for ( i=0; i<5; i++)
{
printf("%d \n", &n[i]);
}
return 0;
}
And the output is:
displaying integers:6422280
6422284
6422288
6422292
6422296
Any help would be great I tried creating it as an enter integers and get an output but regardless of input it gave me extremely large numbers. which is why I'll be happy if it just prints. Sorry if it's an obvious one but I've tried 5 different ways all with similar/basically identical results.
What you get printed are address locations of items stored in array.
If you want to print values of items in array, you should not use the address-of operator [ & ]. Try it this way:
printf("%d \n", n[i]);

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.

printing an array of chars ( C) Only first letter [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 7 years ago.
Improve this question
I need to fill manually an array of chars. I declared it like this:
char* coor = malloc(sizeof(char) * 5);
Then I manually assigned every variable to its position:
coor[0] = O_colum;
coor[2] = ((char)(O_row+48));
coor[3] = '-';
coor[4] = D_colum;
coor[5] = ((char)(D_row+48));
(D_Row and O_row are integers, I need that number in character form, not the equivalent value in ASCII; that’s why I do +48)
The problem comes when I try to print it. If I use printf(" %s", coor) it only prints the first characters and I don’t know why. I’m using %s, so it should print all the characters in the string.
When I do this:
char *p = "hello";
printf("%s",p);
It does print hello.
There are two mistakes in your code:
you are skipping the position 1 of the array. This is probably the
reason why it prints only the first element.
you need to add the end string character \0 in the end of the
string.
This should fix it :
char* coor = malloc(sizeof(char) * 6);
coor[0] = O_colum;
coor[1] = ((char)(O_row+48));
coor[2] = '-';
coor[3] = D_colum;
coor[4] = ((char)(D_row+48));
copr[5] = '\0';

How to write a datastructure to a file with fput 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 9 years ago.
Improve this question
What i have here is a struct that i want to print to a file. The structure consists of a series of singel character ints where pek3 Points at the first object containing a number in the structure.
fprintf didnt work and this just gives me the error:
missing ')' Before '->'
FILE *filen;
int h;
talstrul *tepek = pek3;
filen = fopen("summadata.txt","w");
for(h=1; h<=maxlen; h++)
{ int fput(tepek->num,filen);
tepek = tepek->next;
}
fclose(filen);
Your example is incomplete - so we have to guess.
f = fopen("summadata.txt","w");
for(int h=1; h<=maxlen; h++) {
fprintf(f, "%d\n", tepek->num);
tepek = tepek->next;
}
fclose(f);
should work.
fprintf works as follows:
the first argument is the file handle, that is what you get from fopen.
the format string, here "%d\n", describes, what you want to print. Here it is a integer ("%d") and then a newline ("\n").
then comes the arguement(s) to the formatstring. In this case the integer, I guess that is tepek->num.

C - sizeof(string) result is less than it should be [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
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
Improve this question
I know that a string should be n+1 in length, but for some reason my program is printing the sizeof(string) as n-2.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char name [] = "Tom";
int x = sizeof(name);
int i;
printf("sizeof(name) = %d\n", i);
for(i = 0; i < x; i++)
{
printf("Character at %d is %c\n", i, name[i]);
}
return 0;
}
Can anyone explain why?
You're printing i, not x.
i was never initialized, so you get undefined behavior.

Resources