The equivalent in C would be an array of elements:
int x[10];
Is an array of ints and its size = 10
Initializing an array in C can be done in several ways, for example:
int x[10] = {0,1,2,3,4,5,6,7,8,9};
int x[] = {0,1,2,3,4,5,6,7,8,9};
Or you can assign a single element:
int x[10];
x[0] = 0;
x[1] = 1;
.
.
x[9] = 9;
You should read more about arrays and how they work in c.
Answer from user5710871 on Stack OverflowThe equivalent in C would be an array of elements:
int x[10];
Is an array of ints and its size = 10
Initializing an array in C can be done in several ways, for example:
int x[10] = {0,1,2,3,4,5,6,7,8,9};
int x[] = {0,1,2,3,4,5,6,7,8,9};
Or you can assign a single element:
int x[10];
x[0] = 0;
x[1] = 1;
.
.
x[9] = 9;
You should read more about arrays and how they work in c.
There is no such thing as a list in C. list is not in the C standard library, so I'm not sure where you found that. In C, you would generally use an array.
int L[] = {1,2,3,4,5};
int a = L[0];
int b = L[1];
I just want to ask a simple question that is if we made an uncontinous list in c a declared a function of type list , then how to print the function in the main , for integers we use%d ,for floats we use %f what about functions of type list (by theway this list can hold integers and string and float Here is the code i want make the main for
include <stdio.h>
include <stdlib.h>
include <stdbool.h>
define LMax 10
typedef struct Book{ int id; char author[40]; char publisher [50]; int year; } Book;
typedef struct BookList{ Book A[LMax]; int length; } BookList;
// Insertion at the beginning of the list BookList insertBegin(BookList l, Book b){ int i; for(i=l.length-1; i>=0; i--) l.A[i+1]=l.A[i]; l.A[0]=b; l.length++; return l; }
// Insertion at the end of the list BookList insertEnd(BookList l, Book b){ l.A[l.length]=b; l.length++; return l; }
// Insertion at a given position in the list BookList insertPos(BookList l, Book b, int pos){ int i; for(i=l.length; i>pos; i--) l.A[i]=l.A[i-1]; l.A[i]=b; l.length++; return l; }
// Search based on identifier int Search(BookList l, int id){ int i;
for(i = 0; i<l.length; i++){ if(l.A[i].id == id){ return i; } } return -1; }
// Insertion before the book with identifier 378 using the function Search BookList insertBefore(BookList l, Book b){ int pos;
pos= Search(l, 378);
if(pos == -1){
printf("The book with the identifier 378 does not exist.\n");
}
else{
l = insertPos(l, b, pos);
}
return l;}
// Insertion after the book with identifier 378 using the function Search BookList insertAfter(BookList l, Book b){ int pos;
pos= Search(l, 378);
if(pos == -1){
printf("The book with the identifier 378 does not exist.\n");
}
else{
l = insertPos(l, b, pos+1);
}
return l;}
void print(BookList l){ int i; // Displaying the books in the library printf("\nBooks in the library:\n"); for (int i = 0; i < l.length; i++) { printf("ID: %d, Author: %s, Publisher: %s, Year: %d\n", l.A[i].id, l.A[i].author, l.A[i].publisher, l.A[i].year); } }
As part of my goal for this year, I’ve been learning C, and I realized the best way to solidify my understanding is by teaching. So, I decided to start a blog and post weekly tutorials on C programming. Here's my first real post. I’d love to hear your thoughts and feedback!
https://www.learninglowlevel.com/posts/cm28ealr800009lgz16hgzd6e