Questions tagged [c]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
0
votes
1answer
2k views

Using function as a statement without using its return value

I just figured out that printf is a function of type int. int printf(const char* format, ...); And functions are supposed to return a value. I've never seen a line of code written like strlen("...
0
votes
1answer
951 views

How to check if input is invalid with C

I am doing greedy, and I want to write a code which says that if the input is not a number, I say something other than it is a number. I know that it is not required, but I just want to know if there ...
0
votes
1answer
86 views

Pset3 BubbleSort not sorting at all

I am trying to implement bubble sort, but in the end, the list isnt sorted at all. It looks like this, and I cant find the error: void sort(int values[], int n) { //Printing unsorted list ...
0
votes
1answer
77 views

Memory allocation when working with files in C

When you fopen a file in C, and you write to it, how is there memory waiting for the additional data? What happens if the file doesn't have free space after it?
0
votes
1answer
4k views

How to prompt user for input again if the input is empty?

I am currently tidying up my code for Mario problem for pset1 and I got stuck on the part where the user should be prompted for the integer again if the input is empty. I've been looking on various ...
1
vote
2answers
340 views

Indentation Help

Is my code correctly indented? Please help. I'm not sure it is correctly indented according to the CS50 Standard.
1
vote
1answer
176 views

How to make variable's value display on screen using printf?

I tried printf(n, " is equal to " x, /n); But it didn't work. How do I make it print the value of n (a variable ), then a space after it then "is equal to " then the value of x.
1
vote
1answer
535 views

recover.c cannot detect jpeg signatures?

Trying to get my recover.c to work. My program fread's 512 bytes into a buffer on every iteration of a loop. The buffer is declared as so: unsigned int* cfbfr = malloc(512); Then, at the start of ...
2
votes
1answer
1k views

C Preprocessor Output: What is <built-in>

I wrote one of the most simple programs I could imagine: int main (void) { int a; } As you can see, there are no include files. The purpose is to understand a little bit more about the ...
0
votes
1answer
541 views

PSet1: Greedy Help

For pset1 "Greedy", i believe I have successfully solved it. However, I get errors. I can't seem to find out the problem in my code either, which makes it even more frustrating. For the first part of ...
0
votes
1answer
29 views

Error can't understand?

sh-4.2# make condition cc condition.c -o condition /tmp/ccFpUJot.o: In function main': condition.c:(.text+0x18): undefined reference togetint' collect2: error: ld returned 1 exit status make: ***...
0
votes
2answers
939 views

Do we need to return 0 in C?

I found a lot of C tutorials on the internet ending their main functions with return 0;, but I don't see David doing that. Can anyone explain to me please? I've only finished watching Week 1 lectures ...
0
votes
1answer
70 views

C Programming in native mac environment -- life after CS50

Life after CS50: I want to continue using all the skills/tools from CS50, but now need to compile/run/debug programs outside of the CS50 appliance on my native Mac. I've been trying and trying to ...
2
votes
1answer
234 views

Why is my `mario` pyramid height restriction not working?

Can someone help me with problem set 1? I got the pyramid working, but my height restrictions seems to not work. When I type in letters and negative numbers for height, the program does not print out ...
1
vote
2answers
57 views

Reducing the size of my `filename` array produces strange behavior when using `sprintf()`

Why is it that if I reduce the size of the array in the code below from filename[4] to filename[3], the filename string is always 001? char filename[4]; // if i change 4 to 3, i get filename as 001 ...
0
votes
1answer
562 views

"make" not working ("undefined reference to GetString")

I'm testing out a basic program called getname that prompts the user for their name to store it in a string variable. However make doesn't seem to work. This is make's output: jharvard@ubuntu:~$ ...
-1
votes
1answer
546 views

How to break out of nested for loops

I would like to stop only the nested for loop, not the do while loop, in the following code. I have tried all the possible positions of break without success. do { draw(d); printf("tile to ...
0
votes
1answer
72 views

Where is unistd being used in this CS50 example (is it superfluous)?

From what I see, the library is not used in this CS50 sample code and yet, it's included: #include <stdio.h> //#include <unistd.h> int main(int argc, char* argv[]) { if (argc != 2) ...
2
votes
1answer
2k views

Passwords Et Cetera, Problem Set 2: Crypto. Walkthrough

I'm wondering how can I do this pset without doing the fist thing that comes to mind when reading this pset. From Itsa Mario to this there is a really big jump. Please talk me about crypt and file I/O ...
2
votes
3answers
838 views

What stuff that's inside array[-1] or array[n + 1]?

Here a portion of the loop I used to implement insertion sort algorithm: while (j > 0 && values[j - 1] > actualElement) { // place the unsorted element inside the correct position ...
0
votes
3answers
159 views

Greedy hangs after prompting for user input

I'm a bit stumped at what I'm missing in my Greedy solution. The program keeps getting hung up right after the prompt for user input. Check50 doesn't give me much information to work from. What is ...
2
votes
1answer
953 views

What's the purpose of the 'salt' string? Isn't it contained in the hashed string anyway?

When checking the manual entry for man crypt, I read: The returned value points to the encrypted password, a series of 13 printable ASCII characters (the first two characters represent the salt ...
2
votes
1answer
2k views

Can't cast a float to an int?

For the life of me I cannot get this float to cast to an int, and I don't know why – I've tried a number of different things, including printing it as an int, which seems to have worked (printf("%d" , ...
1
vote
1answer
425 views

Writing solutions in C++ instead of C

I know C++ from before starting CS50. If I submit a solution written in C++ for problem sets that deal with the C language, will I get credit for my solution?
8
votes
1answer
8k views

What is the difference between bfSize and biSizeImage?

I'm working through pset5's resize right now and I'm at the point where I've got to update the headers for the new file. I'm having trouble computing them, mainly because I can't really tell the ...
1
vote
1answer
1k views

How to Install Stanford CPP Library for Windows (Visual Studio)?

I am using Visual Studio on windows and would like to use Stanford CPP library. Can anyone tell me what is the standard method to include the library header files to run for my visual studio project? ...
1
vote
0answers
2k views

Valgrind says "Invalid read of size 4" while freeing memory recursively in a trie

My PSET "mispelling" works fine and contains no memory leak but when I run the program with Valgrind it says Invalid read of size 4. My unload function calls a deleteTrie function which looks like ...
2
votes
1answer
15k views

How to initialize multiple types of variables in a FOR loop?

Is there a way to initialize two different types of variables in a for loop? For example: for (int i = 0, n = strlen(oldStr), char a, b; i < n; i++) So the variables i and n are ints while a and ...
-1
votes
1answer
439 views

Different methods for transforming a string to lower case in C

In the check() function I've figured that I should transform the input word to all lower case. To do that I wrote this: int wlen = strlen(word); char word1[wlen+1]; //transform the word to all ...
0
votes
0answers
1k views

What does clang's "undefined reference" error mean?

What do the following compilation errors mean and how can I fix them? jharvard@appliance (~/Dropbox/pset3/find): make helpers clang -ggdb3 -O0 -std=c99 -Wall -Werror helpers.c -lcs50 -lm -o ...
-2
votes
1answer
3k views

implicit declaration of function getint() is invalid in c99

#include <cs50.h> #include <stdio.h> int main (void) { printf("get the value of x \n"); int x = getint(); printf("y \n"); int y = getint(); printf("addition of %d and %...
1
vote
1answer
118 views

Accessing const int MAX value from helpers.c

I am trying to implement the hacker3 sorting algorithm. The one that requires a O(n). I think I have figured out the algorithm but I won't spoil it by posting any code here. My question is regarding ...
2
votes
1answer
869 views

Why does accessing an out-of-bounds array element not cause a segmentation fault?

I tried to access an array element greater than which was passed in the search function but it did not cause a segmentation fault. Here is the code in which I was trying to access the element: bool ...
1
vote
3answers
110 views

How do people usually look up syntax?

I've been doing problem set 1, and I seem to be running into blocks with syntax that I do not know, so I can come up with pseudocode and have a rough idea of what I want to do, but then when it comes ...
0
votes
2answers
124 views

How to submit a Pset without appliance?

I want to submit the source code for problem set 1 . But I don't have the cs50 appliance . So what should I do?
0
votes
3answers
2k views

Are const Local Variables Implicitly static?

From Programming in C (3rd edition), page 156 In the case of const variables, they can be stored in read-only memory. So they may not be initialized each time the function is entered. I Just ...
0
votes
0answers
358 views

floating point exception core dumped?

Whenever I try to get the encryption it shows floating point exception (core dumped). According to some Google links this error comes when dividing by 0 but in my code that is not the case. The only ...
0
votes
1answer
232 views

How do I load words in speller that are mixed letters and numbers?

I'm struggling with pset 6 (quite difficult logical and implementation wise, but I like this level of difficulty and this challenge). I'm implementing load and I just found out that I'm 34 words off ...
3
votes
2answers
35k views

How to solve "control may reach end of non void function" error

When I try to compile my code, I get an error message that says "control may reach end of non void function." What does this message mean and how can I avoid this error? Here is the function that's ...
1
vote
1answer
737 views

fifteen.c : printf() not printing anything to the screen

To check the execution sequence of the code, I have added printf statement just below the main(): int main(int argc, string argv[]) { // greet player printf("abc \n"); greet(); ...
0
votes
1answer
156 views

Comparing arrays in C

Is there a good way (or function) to compare all elements of two arrays (or selected elements) without hardcoding (i.e., while(a[1] == b[1] && a[2] == b[2] && ...)) and without using a ...
0
votes
1answer
1k views

what does '-Wall' flag in the clang mean?

I am reading a Makefile of one c file. clang -ggdb -std=c99 -Wall -Werror -o generate generate.c I am unable to understand what -Wall flag in the above line means?
1
vote
2answers
978 views

How can I swap two values and return two integers?

I wanted to write a function that swaps two values, but I'm not sure how to return 2 integers. This code doesn't work, as after swapping the values x is still one and y is still 2: void swap(int in_1,...
0
votes
1answer
120 views

Can I run C code in visual studio 2012?

I'm used to the development environment in visual studio. I was wondering if it would be alright if I ran the problem sets and examples in visual studio? I believe VS does support C but I'm not ...
0
votes
1answer
23 views

Why does this program output 0?

This is a simple program, running in a loop, it prints out x + 1 forever #include <stdio.h> int main (void) { x = 2; while(true) { x += 1; printf("%d\n", x); } }...
3
votes
1answer
17k views

How to free a nested struct?

In preparation for my final project, I am writing a C program that handles task lists. It utilizes a nested struct. For every line it reads from a file it creates a struct of type task. Each task then ...
6
votes
5answers
35k views

Can you print more than one char in C by using "x" * 5 like you can in Python?

In Python you can have a print statement print out a number of the same char by using print("X" * 5) which will give you XXXXX. Can this be done in C with printf?
4
votes
1answer
300 views

Computing speed difference between n++ and n=n+1 in C

I am curious to know the computing speed difference between n++ and n=n+1 in C. I found a rather lengthy discussion on Stack Overflow, where some people are literally contradicting each other. To ...
1
vote
1answer
564 views

where is the Debugger in Gedit for C

I have been having some trouble writing my programs. I have been trying to find the debugger for c to speed up the process, but I have not succeeded. If you have found it can you please tell me where ...
1
vote
4answers
114 views

Can I write an "if" statement without an associated code block?

Is this valid code? if (0 <= tower_height <= 23); If so, how will it be executed?

1
8 9 10
11
12