r/C_Programming • u/SnooHabits2652 • Jul 24 '26
Question How to get better at programming in C
Ok , so currently I work as Embedded Developer , this is my first job , I will be honest I don't like the hardware part of it but I like coding , I have learn few languages before typescript , c++ (little bit) and learning java now ,
Thing is , I can code few things in c , but when it comes to pointers and advance c , my mind goes blank, I am not looking for a ways to be good Embedded Developer , right now I just want to become good at programming in c , what will u suggest ? My weak points are pointers , strings , array , this is all I can remember right now also snprintf functions .
I am a beginner in c , so please don't judge , ik about hackerrank , ik i can use that to sharpen my c skills but some of the questions are very hard .
Edit : basically pointers aren't new to me but when i involve pointers with strings or double array , and how where u place the * changes the whole meaning , like u can make an pointer point to the array or u can make array of pointers . And so on ,
Ik basic of c++ and somewhat polyglot , java and typescript so it's not my first language .
67
u/BertyBastard Jul 24 '26
How did you get a job as an embedded developer without these established skills?
48
u/mc_pm Jul 24 '26
Pointers, Strings & Arrays -- the way they are the same or different -- is absolutely core to the language. Arguably, you don't know C if you don't know this.
So I would go back to something really basic, find a tutorial you like, and then follow along with it. When they type some code, you type the code. When they run it, you run it -- or figure out what your bug is.
Type the code in yourself, so your "fingers learn" how the syntax feels, and then start programming your own little mini projects in C to exercise those concepts, and others -- you can't really learn how to program without doing a lot of it.
10
u/ForgedIronMadeIt Jul 24 '26
Firstly:
Pointers, as is true of all data types, are just numbers. These numbers are interpreted as memory locations by code. A pointer will be the same size at the system's memory address space (or at least whatever the current status of the CPU for the given process, 32-bit on 64-bit is a thing after all) meaning that a pointer will be 4 bytes on a 32-bit system and 8 bytes on a 64-bit system. A hypothetical 40-bit architecture would have 5 bytes for pointers. The asterisk just indicates to the compiler that the variable being declared is a pointer to the type being specified. Think of it as a modifier if you want. To get the address of something, use the address-of operator, the ampersand.
There are a lot of fun tricks with pointers, but understanding the above is important. Get this right first. I would suggest implementing a simple linked list program in order to get the hang of the basics.
Secondly:
As is true with most things, learning and getting better takes both applied experience (actually doing the thing) as well as some amount of more academic study. Watch whatever videos on C and read books/blogs/articles/whatever. Nearly all concepts in software engineering have some kind of parallel in C, so studying more abstract things may be useful too.
9
u/ghost_ware Jul 24 '26
I would unironically love to know the story of how you got a job as an embedded developer without understanding pointers. No shade, I'm just really curious
5
u/Brok3nHalo Jul 24 '26
I skimmed the answers and didn’t quite see anything that simply explains the concepts of the items you’re asking about. It’s been a long while since I worked in either C or C++ so a bit rusty and typing this on a phone so forgive any errors or formatting issues.
I find most confusion around pointers is they’re often explained very poorly and the people explaining often preamble the topic about it being complicated and difficult which makes people learning doubt themselves more into overcomplicating it.
So the first thing to understand is what exactly a variable declaration means. Some of this is likely recap but I want to be thorough.
char letter = ‘a’;
As you know, this line reserves a section of memory and places a value inside it. The type, in this case “char”, tells the computer how much space to reserve in memory. The name, “letter” is only used for developer readability, at runtime this is dynamically replaced with whatever address the space is reserved at. This could change every run but say it’s assigned to the memory address 0x00A5, so you can think of it that every time you type “letter” in your code it’s replaced by “0x00A5”
So when you use a variable, that variable is just the address of your value inside RAM, so what’s the deal with pointers then?
Well a pointer is just a variable storing an address.
char* letterPointer = &letter;
& just means get the address of this variable instead of the value
char* is just defining that letterPointer block of memory containing the memory address of value of size char.
So when you do “char letter2 = * *letterPointer”* the* ** is just a symbol that says instead of directly getting the value out of this variable, use the value as a address to get the other value.
For arrays, they’re just pointers too. An array of 1 element is pretty much identical to a pointer of the same type in C.
Array syntax is just a convenient way to creat and access a pointer to a memory block of more than one value stored in a row of the same type.
char letters[3] = {‘a’, ‘b’, ‘c’};
This line is creating the variable “letters” as a pointer to the address of the start of 3 times the size of a char variable.l and assigning this character into the reserved memory.
The interesting thing about that is since it’s just different ways of accessing the address inside the pointer the following is equivalent:
letters[0] == *letters
To extend that:
letters[1] == *(letters + 1)
Since “letters[1]” means look up what is at the address stored in letters, move down memory the size of one char (that is known because the type of letters is char) and retrieve that value. The same is that if you do “letters + 1” it knows it’s addressed to char so +1 is interpreted to mean add the size of one char, then just like with any other time it’s used, * just means retrieve the value at the following address.
So as you can see pointers and arrays are basically the same thing, you just typically use pointer syntax when you have 1 value you want to point to in memory and use array syntax when you want to have a memory block of multiple values.
Now string in C can be a dead simple topic or a very complicated one depending on what scope you have to worry about.
If you’re only worried about basic English strings and don’t kneed to worry about internationalization at all, they’re just char arrays where the last character of your string is null (‘\n’). You just creat a char[] of the length equal to a greater than the length of the string you want to store and put your characters in it. This can be done the long way or with a literal:
char string[] = “test”;
Doing it this ways knows it has to creat an array of length 5 for the 4 visible characters and the \0 to mark the end of the string. If you want to have more space than the initial value you can specify how big you want it, this is useful if the value changes and you may need to store more character:
char string[20] = “test”;
Generally when interacting with c strings you’ll use string.h from the standard library that gives you a number of utility functions for manipulating and interpreting strings. These functions know where the end of the string is by stopping when they hit the null character.
If there is no null character in the array the functions may continue to read past the end of the array into random memory until they hit the first null and would have undefined behavior.
Relatedly, when working with strings or any type of array data it is important to be aware of the length or your arrays and making sure when appending all the data can fit. If you try to append data to the end that won’t fit, or otherwise do operations past the end of a array, you’ll be writing into inline sections of memory that could be reserved for other things and be corrupted.
Finally the more advanced part of strings. The issue with standard char[] based strings is a char is only 8bit and therefore can only represent 256 values, 0-255. Basic char based c strings typically use the ASCII standard where half the values are taken by the English alphabet, numbers, punctuation, and whatever symbols you have on your keyboard. The second half could be symbols or extended character sets but there’s simply not enough values to store more than one language and some languages such as Chinese have more characters than you can possibly represent in 8 bits on their own.
The first solution was UTF-16, if 8 bits isn’t enough, why not 16. This just doubles the size of each character to 16 using the wchar_t type instead of char. This helped, that’s a lot more possible values but there’s simply we’re still things that wouldn’t fit. This work largely the same as ASCII, just there’s a lot more characters, strings take twice the memory, and there’s a wchar.h that are utility functions you use instead of string.h that has the wide versions off all the string functions.
The more modern version of multilingual string support is multibyte strings, commonly UTF-8, also known as Unicode. Unlike ASCII and UTF-16, not all characters are the same length in UTF-8. Instead English is represented by the same values as in ASCII, but the leftover bits are then used as identifiers to say how many bytes the character is so if a character doesn’t fit in the 8bit range it can take 2, 3, or 4 bytes, and maybe more, it’s been a while since I had to work on low level Unicode so I’m a bit fuzzy on the rules.
This has advantages and disadvantages vs ASCII and UTF-16. First obviously is it can store many more values than either. Secondly for English string take less memory than working with UTF-16 because they can only use 8-bits where all characters use 16 bit values in UTF-16. On the downside though processing and manipulation becomes much more complicated and less efficient due to the unpredictable size of characters. When working with the other two you can make assumptions with the 1:1 relationship between number of characters and position in memory. But now you can’t just increase the index on the array by one to go to the next character, it could land you in the middle of a character, nor make assumptions about array length and character limits for some examples. Instead code needs to be aware of what’s in the string and check the length of each character to process it. Dealing with this stuff is its own separate topic and I highly recommend reading at least the Wikipedia article on UTF-8 and Unicode.
4
u/grimvian Jul 24 '26
Try C: malloc and functions returning pointers by Joe McCulloug
https://www.youtube.com/watch?v=3JX6TyLOmGQ
1
u/TenzaiBL Jul 26 '26
I literally was about to ask about the same stuff and I struggle in the same things as well, so if my question is not too early I wanna know how did you solve it
3
u/Constant-Estate4024 Jul 24 '26
I'm not much aware of the resources but I've been looking to get a job / intern in this role. Would you mind giving a little guide on how to go about it being a fresher ?
2
u/Reasonable-Tour-8246 Jul 24 '26
Understand what each concept is solving. Why pointers exist, also pick the simplest way to solve a problem efficiently.
2
u/matt9k Jul 24 '26
Everyone recommends this book, but it’s because it’s really good - I would check out C Programming: A Modern Approach. It’s broken out by topic. This means you can read the chapters on arrays, on pointers, on strings etc. It will give you examples of different functions and you can go try and use them.
I would recommend looking through the chapter on one of these ideas, then trying to implement a toy program using one of them. You can with something small like “printing the numbers 1-10 using an array” and going from there.
2
u/SmokeMuch7356 Jul 24 '26 edited Jul 24 '26
Programming is a skill, and like all skills requires non-trivial amounts of practice. You have to write a lot of code before some things make sense. And yes, some stuff will be hard to understand at first, but there's no other way forward but through.
If you're not there already, go to the desktop version of this page and check the links under "Resources" in the sidebar to the right. Those should help you learn what you need.
Pointers and arrays are not difficult or advanced concepts, and frankly are fundamental to programming in C; you can't write useful C code without using either. Even if you're not doing a ton of text processing you at least need to understand what strings are and how they work.
Again, check the links in the sidebar.
2
u/Desperate-Tower2460 Jul 24 '26
I recommend you try making your own string library
Try making your own C string library without relying on the standard library's string library
When I first started C, I was so confused with all the pointers and arrays and stuff
But after I started making my own string library, basically everything difficult about C clicked for me
It taught me how memory really works, how to manage memory, how to be defensive with your code
After getting past the pointer and memory stage, it allows you to do really cool things with C, I personally think pointer arithmetic is so cool to play with Not only that, you get to fully understand how your code truly works
I hope my recommendation helps you
1
u/Junior-Matter-5134 Jul 24 '26
Go look online see something cool try to make it the more cool thing you make the more you will run and you will love doing it for example I was traveling with a friend and his car was old when he start parking back his car there is a pixel animation of indicators which telling if something blocking the way or it too close to touching something I found it cool and try to replicate it myself I was not able to make it fully but I learn a lot about pixel arts and terminal arts like you making animation in terminal of code.
1
u/Prestigious_Boat_386 Jul 24 '26
Best thing is to read good code and try to understand why they do certain things. For more technical details documentation is better but idk where you'd look for one for general c programming. Maybe the compiler you use?
1
u/wsppan Jul 24 '26
Follow this Tutorial On Pointers And Arrays In C
1
u/Daveinatx Jul 24 '26
You have to practice, read, debug.. Then repeat.
1
u/CodingSwift Jul 24 '26
C was one of the first programming languages that I learning and I’d really recommend the book The Indispensable Guide to C with Engineering Applications by Paul Davies. It’s a really old book but it’s hands down the best book on C programming that I have ever read.
I’d also recommend writing lots of small programs that solve simple problems in the areas that you’d like to learn. You might want to check out Codable, an app that uses Apple on-device AI to generate coding challenges in 10 different programming languages, including C.
https://apps.apple.com/us/app/codable-code-challenges/id6787467242
C is a great programming language and understanding pointers, and how to use them safely, is easier the more code you write.
Good luck!
1
u/phord Jul 24 '26
Read Chapter 5. Then read it again. This is an amazing book. https://www.cs.sfu.ca/~ashriram/Courses/CS295/assets/books/C_Book_2nd.pdf
1
u/gabeviggers Jul 26 '26
Give it to codex twin
1
u/mubin-thinks Jul 27 '26 edited Jul 27 '26
Get rid of the fear of "I can't understand" before going to any topic.
Then, get a copy of the K&R C 2nd Edition book and go through the book and all the exercises. Some are a bit difficult, look at others solutions by asking AI or looking at Github.
1
u/linux_tales Jul 28 '26
Maybe try reading some books like Richard reese on pointers it covers most stuff you seem to struggle with
1
u/nomatepa Jul 31 '26
I've personally found it useful to implement data structures in C to learn about pointers and memory management. A kind of project that worked for me is to choose your top 3 favorite data structures (trees, linked lists, stacks, whatever) and build them a nice and comprehensive interface in C. This helps you practice allocating memory for non-trivial structures, passing pointers to them around, and later freeing these structures. If you've got the time I'd do this to practice.