Questions tagged [cs50]

Code questions pertaining to Harvard's self-study introductory. It is STRONGLY RECOMMENDED to additionally tag with C or Python. Also consider using the spin-off https://cs50.stackexchange.com/

Note there is a spin-off site at https://cs50.stackexchange.com/. Consider joining the community and directing your questions there.


The CS50 (a.k.a. CSCI S-50) courses are computer science courses from Harvard University as part of its edX program.

There is a C library available at CS50 Library which provides support functions for the course.

There is also a Python library at CS50 Library which provides support functions for the course.

The tag should be used primarily for questions also tagged or (though the course does use some other languages too, and if there is a need, the extra languages would be valid too).

This tag should be used for questions which use the "cs50.h" header, python-cs50 or -lcs50 library. Another symptom that it is appropriate is the use of the type string (which is defined with typedef char *string; in the cs50.h header) or functions such as get_char(), get_double(), get_float(), get_string(), get_int(), get_long_long() and SQL().

The term 'pset' with a number (e.g. PSET5 — for Problem Set 5), with varying capitalizations is also often an indication that could be appropriate.

  • As an alternative to edx, there is a standalone website (called CS50x) where all the course materials and problem sets can be found.
  • There is CS50 Stack Exchange site dedicated to the course.
  • You can run CS50 programs in Visual Studio Code for CS50 (Codespaces)
  • The previous CS50 IDE has been deprecated. Use the Codespaces link above instead. For reference, here is the link to the previous CS50 IDE
  • CS50 has a "CS50 Sandbox" to run in a test environment. The CS50 Sandbox will be decommissioned on July 1, 2023. Sandbox URL: http://sandbox.cs50.io/ A suggested alternative after that date is Replit
3444 questions
81
votes
6 answers

What do .c and .h file extensions mean to C?

It's all in the title; super-simple I reckon, but it's so hard to search for syntactical things anywhere. These are two library files that I'm copying from CS50.net, and I'm wondering why they have two different extensions.
Alex Mcp
  • 19,037
  • 12
  • 60
  • 93
15
votes
4 answers

malloc vs array in C

I am taking an open online course CS50 from Harvard. The last lecture I had was about memory allocation and pointers (two concepts which are absolutely new to me). What was taught is that malloc(10*sizeof(char)) allocates enough bytes on the heap…
zesy
  • 481
  • 5
  • 12
13
votes
1 answer

C - Undefined symbols for architecture x86_64 when compiling on Mac OSX Lion

I'm getting some problems on compiling a very very simple name.c file on Mac OSX Lion. Now, I started following Harvard CS50 course on cs50.net. I'm not totally new to programming but I was curious on how this course has been taught. This is the…
Pillo
  • 165
  • 1
  • 4
  • 11
11
votes
6 answers

Include an external library in C

I'm attempting to use a C library for an opencourseware course from Harvard. The instructor's instructions for setting up the external lib can be found here. I am following the instructions specific to ubuntu as I am trying to use this lib on my…
ironicaldiction
  • 1,200
  • 4
  • 12
  • 27
10
votes
3 answers

*Beginner* C: incompatible integer to pointer conversion passing 'char' to parameter of type 'const char *'

I'm trying to convert each letter in a string to it's ASCII number. Using int letter = (atoi(ptext[i])); gives me this error: error: incompatible integer to pointer conversion passing 'char' to parameter of type 'const char *'; take the …
Ting
  • 151
  • 2
  • 2
  • 8
8
votes
3 answers

How can I improve spell check time in C program?

As one of the assignments in Harvard's CS50 course, students are tasked with creating a spell checking program. The main thrust of the assignment is speed - pure speed - and I've gotten to the point where I'm beating the staff's implementation, but…
Drew
  • 89
  • 7
8
votes
3 answers

tolower() Function not Working in C99

I am using the CS50 appliance from Harvard and trying to make a character lowercase. I am trying to use the tolower() function but when I try to use it I get the message implicit declaration of function 'tolower' is invalid in C99. Anyone care to…
ChapmIndustries
  • 1,401
  • 4
  • 17
  • 19
7
votes
1 answer

CS50 tideman - :( lock_pairs skips final pair if it creates cycle

I'm new to stackoverflow and pretty new to programming. I'm working on the tideman problem for the CS50 course. https://cs50.harvard.edu/x/2020/psets/3/tideman/ When I run check50 everything checks out except for one: :( lock_pairs skips final pair…
KarinMG
  • 73
  • 1
  • 3
7
votes
6 answers

AttributeError: 'NoneType' object has no attribute '_instantiate_plugins' (Cannot import create_engine)

import os from sqlalchemy import create_engine from sqlalchemy.orm import scoped_session, sessionmaker engine=create_engine(os.getenv("DATABASE_URL")) db = scoped_session(sessionmaker(bind=engine)) def main(): flights = db.execute("SELECT…
Mahbub
  • 75
  • 1
  • 1
  • 5
6
votes
2 answers

How does ftell affect a binary file being read in mode 'r' instead of 'rb'?

I've a rather curious question, not very practical at all really. The error (reading a binary file in r mode) is in plain sight but I'm confused by something else. Here's the…
Chase
  • 5,315
  • 2
  • 15
  • 41
6
votes
3 answers

Why is it okay to declare a STRUCT inside a nested loop?

Here's a snippet of some supplied (CS50) code that declares the same STRUCT 'triple', over and over again inside of a nested loop. Why is this okay? In my mind, it would be more efficient to declare the STRUCT 'triple' just above the scope of the…
6
votes
4 answers

Signed integer overflow: 999999999 * 10 cannot be represented in type 'int' Error

Why is there a runtime error? I made range2 a long long. Code: /*VARIABLES FOR WHILE LOOP*/ long long range1 = 9; int length = 1; /*FIND NUM'S LENGTH*/ while (NUM > range1) { long long range2 = range1 * 10 + 9; length += 1; …
Khalid Mukadam
  • 73
  • 1
  • 1
  • 4
5
votes
3 answers

cs50 pset5 Speller optimisation

I've completed speller and passed all checks. But I'm still bugged about the performance. I did my best with research and running tests, but my implementation is slower by 10-20% compared to staff's one. How can I improve my code? // Implements…
meotet
  • 53
  • 1
  • 5
5
votes
3 answers

using Clang in windows 10 for C/C++

I am currently learning C, taking a CS50 online class via EDX/Harvard. They have us using Clang inside the CS50 IDE/Sandbox which is cloud-based. They haven't explained how to use Clang outside of that tho. Thus I am wondering; How do I setup clang…
Josh Gray
  • 69
  • 1
  • 1
  • 5
5
votes
3 answers

Trying to implement Luhn's Algorithm in C

Iam trying to implement Luhn's algorithm in the C language to check credit card validity, for those who don't know... this is it: Multiply every other digit by 2, starting with the number’s second-to-last digit, and then add those products’ digits…
someGuy5864
  • 63
  • 1
  • 7
1
2 3
99 100