Questions tagged [c]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
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 ...
6
votes
5answers
33k 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
4answers
6k views

Memory overlap in C

So I am learning C and I have to implement the memset() and memcpy() functions. I've looked them up in linux man pages and I saw that the first takes care of overlapping memory situations but the ...
4
votes
1answer
45k views

implicit declaration of function is invalid in C99

I'm getting an error. Here's my info from Terminal: make: *** [tests/myFunction] Error 1 ~/workspace/ $ make ./tests/myFunction clang -fsanitize=integer -fsanitize=undefined -ggdb3 -O0 -std=c11 -Wall ...
4
votes
1answer
272 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 ...
3
votes
2answers
33k 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 ...
3
votes
2answers
43k views

How do I share a variable between source files in C with extern?

I know that global variables in C sometimes have the extern keyword. What is an extern variable? What is the declaration like? What is its scope? This is related to sharing variables across source ...
3
votes
4answers
2k views

Pset2 Readabilty: Why are my grade levels calculating incorrectly?

For Pset2: Readability, I'm writing a program in C that prompts the user for text input, analyzes how many letters, words, and sentences are in the text entered by the user, and calculates the grade ...
3
votes
3answers
816 views

C vs Python in MOOCs?

Before starting CS50, I'd been looking for several other courses. I've noticed that many universities (including MIT) seem to prefer Python as a starting language. Here, after a brief adventure with ...
3
votes
1answer
15k 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 ...
3
votes
1answer
781 views

pset 1 greedy: how to properly convert float to int

My problem is specifically how to convert the float that the user gives (dollars), into an accurate integer (cents). My code: printf("O hai! How much change is owed?\n"); float change_owed = ...
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
1answer
6k views

Pset3: Plurality - Weird output!

This program is for counting the plurality for a list of candidates, Check Description for more info, The program works like this: Enter the candidate's name as an argument when running the program ...
2
votes
2answers
280 views

pset4 recover: recovered images seems to still be corrupted

My code compiles and runs without any errors. Images are generated, but the images are incomplete, here's an example of 000.jpg: https://i.imgur.com/Ovs2Xv8.png. I can't really find what's wrong in my ...
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" , ...
2
votes
3answers
786 views

jpg's fourth byte

Reviewing my code after successfully recovering the images in pset4, I see a part I just copied (from Zamyla's video) without understanding. Mainly, the following: (block[3] & 0xf0) == 0xe0 I ...
2
votes
1answer
526 views

UndefinedBehaviorSanitizer:DEADLYSIGNAL, what does that mean?

What does UndefinedBehaviorSanitizer:DEADLYSIGNAL ==752==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x000000439000 (pc 0x000000428b5b bp 0x7ffdc38f56b0 sp 0x7ffdc38f5380 T752) ==752==...
2
votes
1answer
1k views

Does only malloc allocated memory on the heap?

Does only malloc() allocate memory on the heap or when we use pointer and normal variables put some value then it will be stored in some memory location on heap. To be clear, below are cases: //...
2
votes
1answer
230 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 ...
2
votes
1answer
740 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 ...
2
votes
1answer
1k views

:( recovers 049.jpg correctly recovered image does not match

I have problem with pset4/recover. I recover all images and can see last image when I click on it but when I run check50 it returns with error: :( recovers 049.jpg correctly recovered image does not ...
2
votes
1answer
886 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
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 ...
2
votes
3answers
819 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 ...
2
votes
4answers
932 views

How can I get the cs50.h to run in Visual Studio Code?

I downloaded both the cs50.c and cs50.h files from GitHub and put them in the folder where all the header files are. VSC even recognizes the #include <cs50.h> in the code but I get the error in ...
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 ...
1
vote
1answer
1k views

What's the difference between #include <abc.h> and #include “abc.h”

I know this might sound a little unrelated, but, while working on hacker3, I created my own implementation of A* and had decided to keep it seperate from the main program to ensure I can still mess up ...
1
vote
1answer
526 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 ...
1
vote
2answers
928 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,...
1
vote
4answers
109 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
vote
2answers
2k views

GetString() giving me a segfault in C

I am running a program in C using a GetString function (not from the CS50 library). When I run the program I get a segmentation fault error. In GDB, when I backtrace it tells me: Program received ...
1
vote
2answers
207 views

Help with blur filter for pset4! Output image is significantly darker than input

This is the output and input image that my code generates: https://imgur.com/a/mT7YqCD void blur(int height, int width, RGBTRIPLE image[height][width]) { RGBTRIPLE newImage[height][width]; ...
1
vote
3answers
117 views

Why is this while loop generating errors

I'm trying to get a 15 or 16 digits numbers (integers) from users and then using a do while loop to check if the user entered a valid number, otherwise, they will be re-ask to enter the number. THE ...
1
vote
2answers
219 views

I can´t get (pset1) greedy/cash.c pennies to work properly!

i´ve been trying to figure out what is wrong with my code for the pset1 cash or greedy program, i know about float imprecision but when i print the last value for "ch" it prints .01000, thus meaning ...
1
vote
1answer
4k views

CS50 IDE undefined reference to `get_int'

I've created a new workspace on cloud9, and ran update50 as suggested in the answer to this similar question: Undefined reference to get_int However, I still receive the exception 'undefined ...
1
vote
1answer
477 views

cs50/pset2/vigenere: Code works but check50 rejects 2 item

check50 message: https://cs50.me/checks/c053757544cd74cafb77554759754b8ee4ee37a8 The code doesn't encrypts "a" as "a" using "a" as keyword. It adds a wierd character in the end and I don't know why. ...
1
vote
2answers
218 views

Easiest method to organize code

I'm just finished the entire cs50 course (yay), except the final project, and I'm ready to start submitting all of my psets. Before I submit, I'm going to proofread all my code and simplify/style it ...
1
vote
2answers
36 views

C: Pset 3: Binary Search - 'expected an exit code of 0, not 1' (not finding n)

This is an attempt at binary search. n is the user input (from main script). This code compiles but doesn't find the needle in the haystack. I'm a bit lost and would welcome assistance. bool ...
1
vote
2answers
44 views

Valgrind issue on PSET5 - Edited

sorry that this is a repost of a deleted question. I made a mistake in reading the specifications and didn't realise I wasn't able to alter the function declarations in dictionary.c & .h. The ...
1
vote
1answer
66 views

Check function (pset5, speller) gives multiple, incorrect outputs for some reason

I've completed most parts of speller, but for some reason, when I try to test the check function by printing a 0 for every time it is supposed to return true and printing 1 whenever it should return ...
1
vote
3answers
5k views

Pset 2: Finding out readability level using Coleman-Liau index using C (CS50 2019)

I am currently stuck at a problem for a few days to find out why it's happening. I am writing a program to find out number of letters, words, and sentences. Using these values, I am using Coleman-...
1
vote
1answer
212 views

array size in C

I have question regarding array size declaration in C. I see that if I declare an array in the main function without specifying its size like int arr[]; arr[0] = 7; arr[1]= 8; it shows an error ...
1
vote
1answer
408 views

Difference between s and *s in pointer

This code print Only first letter i.e 'H' #include <stdio.h> int main(void) { char *s = "Hello"; printf("%c\n", *s); } But this return 'Hello' #include <stdio.h> int main(void)...
1
vote
1answer
49 views

Pset5 - Some misspelled words not identified

I have completed the load() and check() function for pset5, however, I have been facing problems as some words are not identified as misspellings such as the words "org" and "int" which are not in the ...
1
vote
1answer
7k views

C: Vigenere - incompatible pointer types passing 'char **' to parameter of type 'const char *

I'm trying to iterate repeatedly over key (a word) which the user inputs on the command line. This keyword is used to encipher a word which the user inputs when prompted by the word 'plaintext'. I ...
1
vote
1answer
99 views

What does the R in RUSAGE stand for?

Came across this function and couldn't intuitively define the implied name. No search on stack quickly returned the answer.
1
vote
1answer
41 views

recover.c encounters seg fault after 1024 bytes

In pset4, recover.c, my program finds the start of the jpegs at 1024 bytes. After that, it encounters a seg fault and I don't see why. Here's my code: #include <stdio.h> #include <stdlib.h&...
1
vote
1answer
264 views

Debugger not working properly for pset4?

I'm working on pset4 and trying to do the following: Allow us to suggest that you also run copy within GDB while answering these questions as follows: Click Debug atop CS50 IDE, which ...
1
vote
1answer
15 views

copy.c:70 Is the second %4 needed?

This code here: // determine padding for scanlines int padding = (4 - (bi.biWidth * sizeof(RGBTRIPLE)) % 4) % 4; is the second %4 necessary? wouldn't int padding = 4 - bi.biWidth * sizeof(...
1
vote
2answers
583 views

Update a value in the function

I want to update variables relating to velocities in a different function to where it is declared. But am not fully understanding the syntax for it. I have the prototype as follows: void moveBall(...

1
2 3 4 5
11