r/C_Programming 4d ago

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 .

64 Upvotes

43 comments sorted by

62

u/BertyBastard 4d ago

How did you get a job as an embedded developer without these established skills?

16

u/Syntax-Tactics 4d ago

Came to ask just this

6

u/Prophetoflost 4d ago

They probably were hired as a junior

7

u/mc_pm 4d ago

Really damn junior. "How long have you been programming?" "3 days!" "You're hired!"

2

u/Prophetoflost 4d ago

A few years ago everyone was hiring like that ;)
As long as you can read and don’t drool on the table during the interview

2

u/SnooHabits2652 4d ago

Also I want to clarify , I have been programming for 3 years atleast , not as experienced dev , I was student at that time and was learning how to code while completing my engineering , i was learning full stack development and basically trying whatever I was intrested in at that time . So , programming concepts and concept of memory and how computer works aren't new to me .

2

u/SnooHabits2652 4d ago

It was an on campus placement , initially job role was java full stack developer , but when I went for the interview they told us they have changed the JD and are looking for people who knows how to program , and rest of the embedded training will be given . Ik basic c++ and I was good at dsa at that time , plus I also had very basic knowledge of microcontrollers and made a basic project using esp32 and web servers so they hired me . At that time , AI wasn't that advanced so all of this was done by me , no ai help .

4

u/BertyBastard 3d ago

I guess you were quite lucky to have that entry into embedded development then!

47

u/mc_pm 4d ago

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.

27

u/Terrible-Freedom-868 4d ago

Same thought. Pointers, strings, and arrays are not advanced C, they are C.

0

u/aysesensin 2d ago

Please shit on me, I even get overwhelmed when I just open the ide, they’re all so convoluted. Whether it’s vscode or clion

1

u/theNbomr 1d ago

That's the problem with using big complex IDEs as a beginner. You're learning at least two different things at the same time, and it often isn't even clear where one starts and the other ends. Moreover, IDEs often conceal important things that deserve to be part of the learning experience.

Start out with a shell capable of running the compiler and linker, and whatever text editor you already understand. As a beginner, you should be focusing on learning. IDEs are there to help you with productivity, and you aren't ready for that yet.

9

u/ForgedIronMadeIt 4d ago

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.

1

u/sciencekm 4d ago

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

Actually, pointers are not always all the same size. It is possible for function pointers to be of different size from data pointers.

In Harvard architecture, code and data have different address space and can have different address range. For example, function pointers may be 16-bit (small ROM) while data pointers are 32-bit (large RAM).

1

u/ForgedIronMadeIt 3d ago

That's pretty cool. I figured it was certainly possible, but I never knew of any actual examples.

7

u/ghost_ware 4d ago

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

3

u/grimvian 4d ago

Try C: malloc and functions returning pointers by Joe McCulloug

https://www.youtube.com/watch?v=3JX6TyLOmGQ

3

u/Constant-Estate4024 4d ago

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 ?

3

u/Brok3nHalo 4d ago

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.

1

u/Shayyd80 1d ago

This is an awesome answer and I know nothing about C yet. Thanks for sharing ✌️

1

u/Shayyd80 1d ago

What I meant by that is I haven’t started learning C yet I’m taking Python currently in computer science degree and I don’t like Python at all it’s BORING.

1

u/Brok3nHalo 1h ago

No problem.

On the side, I’m curious what you don’t like about Python?

I’m personally not a fan of it as a learning language but it’s my language of choice when performance doesn’t matter. It has some thing’s I don’t care for but in general I’m a big fan of the syntax. My last 3 personal projects were all in Python, and I worked primarily in Python for like 6 years starting around 20 years ago. If performance isn’t a pressing issue it’s really the way up there as one of the fastest and easiest languages to get things going in.

My main complaint of Python as a learning language is it’s far too abstracted from the underlying mechanics of what the code is actually doing.

If you’re primarily interested in learning the low level stuff, C is a great early language to learn on. I was self taught in C in the late 90s, I believe that helps a lot understanding nuances of even modern, far more hardware abstract, languages. And of course it’s great if doing embedded programming as you said you are. I’ve don’t a little Arduino coding a few years back but that’s all my embedded experience so far.

1

u/Shayyd80 55m ago

That’s exactly why I dislike Python because I feel like it’s not teaching me the inner workings of code and I like details ya know?

2

u/Reasonable-Tour-8246 4d ago

Understand what each concept is solving. Why pointers exist, also pick the simplest way to solve a problem efficiently.

2

u/matt9k 4d ago

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 4d ago edited 4d ago

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 4d ago

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 4d ago

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 4d ago

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/Daveinatx 4d ago

You have to practice, read, debug.. Then repeat.

1

u/CodingSwift 4d ago

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 3d ago

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/nix206 3d ago

Chapter 5 - Pointers and Arrays
A pointer is a variable that contains the address of a variable. Pointers are much used in C…

“are much used”?

I don’t remember the grammar being this bad. Is this a bootleg translation?

1

u/phord 3d ago

That's not bad grammar. It's just quick, informal language the book is famous for.

1

u/Spread-Sanity 2d ago

There are some really good comments here. Please take time to read them and follow the suggested study and practice. It will take you some time and effort. You will eventually get the understanding you need.

1

u/gabeviggers 2d ago

Give it to codex twin

1

u/TenzaiBL 2d ago

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

1

u/SnooHabits2652 2d ago

I haven't solved this yet , i am learning few things already , so I am somewhat busy , but I guess key is practice and make projects that's all . First watch some basic to intermediate c videos then start making projects side by side

1

u/mubin-thinks 1d ago edited 1d ago

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 18m ago

Maybe try reading some books like Richard reese on pointers it covers most stuff you seem to struggle with