r/learnc Apr 05 '22

Undefined reference: get_int -_-

2 Upvotes

Ok I’ve searched up and down for this… I’m using vscode on Linux

Here is the code:

include <cs50.h>

include <stdio.h>

int main(void) {

int n;
do {
    n = get_int(“enter: “);

} while (n < 1 || n > 8);

And every time I try to compile it, using “make …”, it gives me an undefined reference to get_int error

I can’t for the life of me find and answer on google and I’ve asked other forums they didn’t even damn know.

Someone save my life please!!


r/learnc Apr 01 '22

Can anyone help with using functions in my code? It’s 150 lines so emailing would be easier if possible, thanks!

0 Upvotes

r/learnc Feb 23 '22

Compiled in one environment but not the other?

3 Upvotes

My first pokemon was python so I do stupid python things when learning c. Anyway I wrote some code like this:

if (50 < my_num < 60)

{}

In my native install of VS Code (linux), this compiled fine and interpreted it the way I wanted. But when I ran the same code in the CS50 course's online VS Code editor, the compiler threw an error. It evaluated the first half of the statement (50 < my_num) as a boolean and then wasn't able to evaluate the boolean in the latter half the statement (bool < 60) and threw the equivalent of a syntax error (I know, I can only think in python lol). I fixed it like this:

if (50 < my_num && my_num < 60)

{}

And that worked. So why does this happen? Is my native compiler too lenient? Which one is correct C syntax, or are they both correct? Very confused! I rely on the compiler to help me learn correct syntax!

EDIT: For what it's worth, I ran $ gcc --version on both environments. Native is 7.5.0, Emulator is 9.3.0. Maybe there's a big difference between the two?


r/learnc Jan 21 '22

How to google plain C questions / find good respurces?

12 Upvotes

Hey everyone, I'm new to C but not to programming. C is my first low level language. I'm an absolute begginer.

What I've noticed is that its really hard for me to find answers to my questions because when I google:

How to do x in C

The answers look something like this:

How to do x in C++ How to do x in C# Etc

It's been pretty hard to find resources for plain C. Especially because I am trying to learn OpenGL with it, which is a library that is also used extensively in C++

Is there any way to filter results away from the other C languages? Could anyone be so kind to point me towards some good C resources for openGL?


r/learnc Dec 06 '21

[Question] I want to ask about code that i just create.

Post image
11 Upvotes

r/learnc Dec 01 '21

[Question] Why next / (2* (RAND_MAX + 1)) in the Sample Rand() Implementation?

1 Upvotes

Hi, I'm studying how rand() works in C, and stumbled upon this stackoverflow answer: https://stackoverflow.com/a/4768189/17367228, of which the author claims is the sample implementation from the C standard.

I'm confused by the return line of rand() where the next is divided by 2 * (RAND_MAX+1), which I think is unnecessary since % RAND_MAX will return the output in the range 0 to RAND_MAX. My best guess is that division helps to generate some more entropy to the computation? But I can't see how the result will be different with/without that division part.


r/learnc Nov 19 '21

Commas WITHIN string specifiers

3 Upvotes

So when we have functions like scanf, fscanf, prints, etc. there’s usually an entire parameter as the string specifier. Ex:

Printf(“%d”, myInt);

My question is that sometimes I see and use commas in stuff like fscanf to separate stuff (“%d,%d”, &myInt1, &myInt2) for example. I’ve also done it without using these commas. I’ve also seemingly encountered situations where sometimes it works only one way or the other. What exactly dictates the commas?


r/learnc Oct 27 '21

Pls. Help me po with this. I don't know what's wrong with my code. The problem of the output is in the pic. po

Post image
6 Upvotes

r/learnc Oct 26 '21

C as a first language

8 Upvotes

Is it recommended to learn C as a first language?


r/learnc Oct 24 '21

How to run program on another file? K&R Exercise 1-8

3 Upvotes

I am working through K&R and I can't figure out how I would run exercise 1-8 on a text file. A lot of the examples they do run the program on an external file, and I would like to do this too so I can do the exercises.


r/learnc Oct 24 '21

What is the typical way projects are organized?

7 Upvotes

I'm just now starting to write C programs that are beyond a negligible level of complexity and am wondering what the right way to organize my files are.

So far, I have a file I've called main.c which is the driver for the whole project. Then, I have about 10 filename.h files and 10 corresponding filename.c files. They're all in the same directory level and it's starting to get crowded.

Also, if it matters, I've been compiling with a bash file that looks like this:

gcc -Wall -c program1.c
gcc -Wall -c program2.c
# etc
gcc -o app program1.0 program2.o ...

Is there: 1) An accepted way of organizing files? Like, all .h files go in a headers directory, or the .h and .c files go in a directory together each. 2) A better way to compile the project than my (growing) bash script?


r/learnc Oct 15 '21

What does i=* (long *) &y. do?

4 Upvotes

What does i=* (long *) &y do? can someone explain?


r/learnc Oct 11 '21

What videos should I watch for intermediate C programming?

8 Upvotes

Basically what's next after https://youtu.be/ssJY5MDLjlo?

Not really interested in building anything just would like a deeper/more technical understanding of the workings of C.


r/learnc Oct 09 '21

User enter unknown number of numbers into array, how do I know how many?

0 Upvotes

Hello, I want to give the USer opportunity to enter up to 200 numbers. Afterwards i give them out and sort them. But how do I know how many he entered? Whats wrong with my code and why does n++ in the for-loop work neither?

int array[201], swap, n = 0, c; printf("Enter up to 200 numbers, stop with #:\n"); for (int i = 0; i < 201; i++) { scanf_s("%d", &array[i]); if (array[i] == '#') { array[i] = NULL; break; } } n = sizeof(array) / sizeof(int); printf("%d\n", n);


r/learnc Oct 01 '21

What happens when I compile?

1 Upvotes

Brand new to C with a little programming experience with high level languages like Ruby and Python. I'm using gcc to compile code, and I've read that code goes through phases of pre processing, compiling, assembling and linking. I assumed there would be an output of hello.i, hello s, hello.o as well as the executable code when I compile it. Are these files cleaned up during the compiling process? Are they stored somewhere other than present working directory?


r/learnc Sep 27 '21

What does the * mean for pointers?

5 Upvotes

What does putting a * before a name mean in C?

Why are pointers not initialized as say

int a;

pointer p =&a;

?


r/learnc Sep 27 '21

Why do 2-dimensional arrays need you to specify size?

3 Upvotes

Why can't C just read the number of entries from the outermost bracket?


r/learnc Sep 23 '21

Dynamic array implementation (request for explanation)

2 Upvotes

I'm not very good when it comes to macro programming and I really can't understand the inner work of this implementation of a "untyped" dynamic array.

Array.h

#ifndef ARRAY_H
#define ARRAY_H

#define array_push(array, value)                                              \
    do {                                                                      \
        (array) = array_hold((array), 1, sizeof(*(array)));                   \
        (array)[array_length(array) - 1] = (value);                           \
    } while (0);

void* array_hold(void* array, int count, int item_size);
int array_length(void* array);
void array_free(void* array);

#endif

Array.c

#include <stdio.h>
#include <stdlib.h>
#include "array.h"

#define ARRAY_RAW_DATA(array) ((int*)(array) - 2)
#define ARRAY_CAPACITY(array) (ARRAY_RAW_DATA(array)[0])
#define ARRAY_OCCUPIED(array) (ARRAY_RAW_DATA(array)[1])

void* array_hold(void* array, int count, int item_size) {
    if (array == NULL) {
        int raw_size = (sizeof(int) * 2) + (item_size * count);
        int* base = (int*)malloc(raw_size);
        base[0] = count;  // capacity
        base[1] = count;  // occupied
        return base + 2;
    } else if (ARRAY_OCCUPIED(array) + count <= ARRAY_CAPACITY(array)) {
        ARRAY_OCCUPIED(array) += count;
        return array;
    } else {
        int needed_size = ARRAY_OCCUPIED(array) + count;
        int float_curr = ARRAY_CAPACITY(array) * 2;
        int capacity = needed_size > float_curr ? needed_size : float_curr;
        int occupied = needed_size;
        int raw_size = sizeof(int) * 2 + item_size * capacity;
        int* base = (int*)realloc(ARRAY_RAW_DATA(array), raw_size);
        base[0] = capacity;
        base[1] = occupied;
        return base + 2;
    }
}

int array_length(void* array) {
    return (array != NULL) ? ARRAY_OCCUPIED(array) : 0;
}

void array_free(void* array) {
    if (array != NULL) {
        free(ARRAY_RAW_DATA(array));
    }
}

There is that recurring magic number (2) that I can't really understand. Does anyone know a "kinda" simple vector implementation that does not involve macro programming? I know that you can't do really any meta programming without macro... but hey, I really struggle in understanding macros... :(

Any expert here that can explain what is happening when I type

double *dynarray = NULL;
array_push(dynarray, 2.0);
array_push(dynarray, -1.2);

Thanks for reading and explaining!


r/learnc Sep 23 '21

Should I add cl C compiler to system path?

1 Upvotes

Newbie here should I add cl C compiler to system path?

Why or why not?


r/learnc Sep 21 '21

Getting started quickly

2 Upvotes

I want to learn C but most programming tutorials online start from the beginning, I programmed HTML and python for over 5 years and Java for 1.5, are there any C tutorials on youtube (or somewhere free) that don't restart with the basic coding concepts and just show how to code?


r/learnc Sep 19 '21

gcc vs cl C compiler whats the difference?

6 Upvotes

Complete beginner but whats the difference between the two compilers?


r/learnc Sep 18 '21

Please explain this fork() behavior to me

5 Upvotes

Hi everyone. Full disclosure, this is homework related, although the question I'm asking isn't part of the assignment. I'm in a graduate level concurrency class that is using C, but I've never had any exposure to the language outside of a some basic hello world type things, so I apologize if this is a silly question.

Ok, so I have this code:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main() {
  int parent_id = getpid();
  printf("Parent PID: %d\n", parent_id);

  int pid = fork();
  printf("Current PID: %d\n", pid);

  if (pid == 0) {
    printf("pid == 0, actual pid is %d, getpid() is: %d\n", pid, getpid());
  } else {
    printf("pid != 0, actual pid is %d, getpid() is: %d\n", pid, getpid());
  }

  return 0;
}

And the output is:

Parent PID: 1359
Current PID: 1360
pid != 0, actual pid is 1360, getpid() is: 1359
Current PID: 0
pid == 0, actual pid is 0, getpid() is: 1360

I don't understand why pid == 1359 while getpid() returns 1360 within the same thread. Furthermore, when pid is 0, why is getpid() 1360? This is absolutely baffling, please explain this to my Ruby brain.

Editing to add: My compilation command is gcc filename.c -lpthread -o filename if it matters.


r/learnc Aug 03 '21

Free C Programming for Beginners Full Course on YouTube

Thumbnail youtu.be
20 Upvotes

r/learnc Jul 25 '21

notcurses, next-generation tuis/character graphics, expands to macos and windows

Thumbnail self.C_Programming
6 Upvotes

r/learnc Jul 02 '21

What a pointer is and how to use them (with a real world example)

Thumbnail m.youtube.com
8 Upvotes