• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Programming |OT| C is better than C++! No, C++ is better than C

All you have to do is this

Code:
unsigned int umax = -1;

That will work on nearly every machine you ever use, but it wouldn't work on a system that represents the signed integer -1 as anything other than all 1s. The representation of signed numbers isn't defined in C.
 
That will work on nearly every machine you ever use, but it wouldn't work on a system that represents the signed integer -1 as anything other than all 1s. The representation of signed numbers isn't defined in C.

It's confusing, but signed to unsigned conversion is independent of the representation of the signed number. So the above expression is defined behavior, and will always be correct.

http://stackoverflow.com/questions/809227/is-it-safe-to-use-1-to-set-all-bits-to-true
 

tokkun

Member
That will work on nearly every machine you ever use, but it wouldn't work on a system that represents the signed integer -1 as anything other than all 1s. The representation of signed numbers isn't defined in C.

The signed number format is irrelevant because the standard specifies that when you cast a negative number to unsigned the conversion is performed by adding (MAX + 1) to the negative number. So if the number is -1, it must be converted to MAX according to the standard.
 

Nelo Ice

Banned
So posted this in the web design thread but didn't get any responses so I'll try here.


I've tried teaching myself coding before and quit after hitting a road block early in the process. But after reading through and asking around on GAF, I came upon Team treehouse and I literally just finished their first course on web design and built my own website. Anyway was wondering if you guys had any advice on what do next as far as getting experience and building up a portfolio.

I really enjoyed the treehouse course and for once some stuff started to click. I'm thinking now I'll finish up the web design track then move onto the front end web dev track. Though apparently it's a good idea to take the framework basics course in between those 2 tracks.

I'm not in school so I'm basically teaching myself everything and I have no degree. Only did some CC but realized I had no interest in any of the degrees nor did I want waste anymore time and $$ until I could figure out exactly what I wanted to do.

I decided upon coding because besides the obvious $$ aspect I've always been fascinated by it and even when I failed to teach myself I actually enjoyed learning it. I also enjoyed every minute of typing up code and seeing it come to life in the form of a website from the web design treehouse course.

Since I'll be pursuing this without a degree or formal education I'm going to need a good portfolio to prove I have experience and that I know what I'm doing. So far the only things I can think once I get a better handle on things, is to build my build my friend's theme park news site. Besides helping him out, he wanted me to contribute to it anyway and I figure it will be good experience and something to add to my portfolio once I get the ball rolling.

I should also mention I live in the bay area so I'm in the perfect spot to learn but if anyone knows any good communities or places to learn from here I'm all ears. My goal is eventually become self-employed but if/when I get good enough my friend told me he could get me a job at his company.
 

Skinpop

Member
does anybody publish a printed version of the OpenGL 4.5 reference manual? I'm going to be going through an extended period without internet at home due to a house fire and I need to refer to the reference manual quite a bit. I have a super old version of the reference manual from 1995 for release 1, but it's changed considerably. Typically, I'll just pull up the man pages but that won't be an option short term.

I've been looking online - I can find plenty of books that walk you through openGL but nothing that is a straight up reference manual. Any idea if such a book has been published?

i use http://docs.gl/ for my opengl coding, it's a pretty great resource and you can download it for offline use http://docs.gl/about.html.
 

Granadier

Is currently on Stage 1: Denial regarding the service game future
So posted this in the web design thread but didn't get any responses so I'll try here.

There's a web design thread :eek:

------

Advice wise, there's only two things.
Keep learning. Every. Single. Day. It doesn't matter what it is, as long as it's something you didn't know yesterday.
Build things. Aim for a website a week. Even if it's just some dorky site that plays Hiyayayayayayaya on repeat for 24 hours a day. You'll be solving problems and getting experience building things.

Once you start to feel more comfortable with Javascript and HTML/CSS start to look into the various tools and frameworks that exist for those languages, (there are thousands). The big ones being:
  • jQuery
  • Bootstrap / Foundation
  • SASS/LESS
  • AngularJS / ReactJS / EmberJS
  • NodeJS
I'm probably missing some, but I'm braindead from calc. Start with jQuery though if you are looking to learn more about intermediate/advanced Javascript.
 

Nelo Ice

Banned
There's a web design thread :eek:

------

Advice wise, there's only two things.
Keep learning. Every. Single. Day. It doesn't matter what it is, as long as it's something you didn't know yesterday.
Build things. Aim for a website a week. Even if it's just some dorky site that plays Hiyayayayayayaya on repeat for 24 hours a day. You'll be solving problems and getting experience building things.

Once you start to feel more comfortable with Javascript and HTML/CSS start to look into the various tools and frameworks that exist for those languages, (there are thousands). The big ones being:
  • jQuery
  • Bootstrap / Foundation
  • SASS/LESS
  • AngularJS / ReactJS / EmberJS
  • NodeJS
I'm probably missing some, but I'm braindead from calc. Start with jQuery though if you are looking to learn more about intermediate/advanced Javascript.

Yup right here.
http://www.neogaf.com/forum/showthread.php?t=756776

Yeah right now I've just been going through the Web Design track on Treehouse and hoping once I finish that I'll feel comfortable using a text editor and making a site from scratch. Right now I haven't a clue how to make a site from scratch. Of course right now I'm learning more in dept stuff about CS and the stuff I was doing in the web design course is starting to make sense. Kind of getting a clue on how to make a website from nothing lol.

On a side note, I'm quite proud of myself for actually sitting and learning something since usually I just distract myself with the internet or something rather than sit there and learn lol. Like last time I got stuck and just completely gave up. But for the most part I've taken time nearly every day to at least watch a new video and learn something.
 

Nesotenso

Member
need some help with printing out an array of strings in c

Code:
#include<stdio.h>

int main(int argc, char *argv[])
{
	char *names[]={"name1", "name2", "name3", "name4"};
	printf("%s\n",names);

	return 0;
}

The warning is below
warning: format &#8216;%s&#8217; expects argument of type &#8216;char *&#8217;, but argument 2 has type &#8216;char **&#8217; [-Wformat=]
printf("%s\n",names);

edit: ok it seems printf wasn't designed for an array of strings and I have to feed each string element separately? for loop then?
 

-KRS-

Member
I tried an array with integer elements and printf didn't work either. so printf requires you to specify the index of the elements?

Something like this:
Code:
char *names[] = {"name1", "name2", "name3"};

int i;
for (i=0; i < 3; i++)
    printf("%s\n", names[i]);
 

Nesotenso

Member
Something like this:
Code:
char *names[] = {"name1", "name2", "name3"};

int i;
for (i=0; i < 3; i++)
    printf("%s\n", names[i]);

thanks for the reply.
I got the solution to it.

I was confused because I could print both of the following examples
Code:
char name[] ="Apple";
char *name1 = "Orange";

printf("%s %s\n", name, name1);
but I think for an array of string elements or any other variable type you have to make sure printf refers to the index.
 

leroidys

Member
thanks for the reply.
I got the solution to it.

I was confused because I could print both of the following examples
Code:
char name[] ="Apple";
char *name1 = "Orange";

printf("%s %s\n", name, name1);
but I think for an array of string elements or any other variable type you have to printf refers to the index.

That kinda works, but it's not quite right. A string is an array itself (of chars). There's no real string type in C. Format strings will print your char* up to a null character ('\0').
 

injurai

Banned
thanks for the reply.
I got the solution to it.

I was confused because I could print both of the following examples
Code:
char name[] ="Apple";
char *name1 = "Orange";

printf("%s %s\n", name, name1);
but I think for an array of string elements or any other variable type you have to make sure printf refers to the index.

the pointer to the head of an array and the first element are the same. So name[1] and name with both point to "Apple" which is why that worked out.
 

poweld

Member
the pointer to the head of an array and the first element are the same. So name[1] and name with both point to "Apple" which is why that worked out.

This is not correct.

name[1] is not only the second element in the name[] array, but it is a character ('p), not a char*.
 

injurai

Banned
This is not correct.

name[1] is not only the second element in the name[] array, but it is a character ('p), not a char*.

ooof, you are right. Thought it was an array of char *. Thoroughly embarrassed on the index slip up though... I'm handing over my programmer card.
 

tokkun

Member
I tried an array with integer elements and printf didn't work either. so printf requires you to specify the index of the elements?

Do you understand why it is not possible to pass an array of integers to printf? Because it illustrates an important lesson about C programming.
 

Nesotenso

Member
Do you understand why it is not possible to pass an array of integers to printf? Because it illustrates an important lesson about C programming.

memory management is paramount?

I think tokkun means more along the lines of "arrays are a fictional concept in C, unlike in other languages, they're simply a series of pointers".
oh, ok

Thanks everyone, this has been helpful.
 

Haly

One day I realized that sadness is just another word for not enough coffee.
I think tokkun means more along the lines of "arrays are a fictional concept in C, unlike in other languages, they're simply a series of pointers".
 

upandaway

Member
Speaking of C, I've started to like it a lot more after I did our memory management exercise a while ago. There's something to doing things at such a low level and with that much freedom, when you code according to how you think things really work underneath the surface and it ends up doing its job perfectly.

Doubt I'll ever use it if given a choice but as an exercise it was fun.
 
Speaking of C, I've started to like it a lot more after I did our memory management exercise a while ago. There's something to doing things at such a low level and with that much freedom, when you code according to how you think things really work underneath the surface and it ends up doing its job perfectly.

Doubt I'll ever use it if given a choice but as an exercise it was fun.

C is a jet fighter. In HLLs nothing is faster, but it requires far more attention and practice. I hated it at first, but it's my goto language now.
 

tokkun

Member
I think tokkun means more along the lines of "arrays are a fictional concept in C, unlike in other languages, they're simply a series of pointers".

More specifically, if you just pass a pointer to an array of integers, you are only specifying the start of the array, not the end. There is no way for printf to know where an array of integers ends, because printf just sees the contents of memory, and it can't tell if the bits it sees are one of the integers in the array or data from another variable or uninitialized data.

So any time you want to pass an array to another function, you also need to either pass its length or use a guard value to indicate the end of the array.
 
Wouldn't you still need to use a for loop specifying the index to print arrays in other languages like Java and C#?

Yes, probably. Fortran 66 used to have rules for format specifiers that allowed a whole array to be printed; it worked well for tabulations. Most modern (post-1970) languages lack the specific focus on batch processing that made it worthwhile to put that kind of thing into a language spec. If you want that kind of library facility, you write it yourself. It's fairly trivial.
 

tokkun

Member
Wouldn't you still need to use a for loop specifying the index to print arrays in other languages like Java and C#?

If you are doing any sort of iterative operation there is going to be some kind of loop construct *somewhere*. However, it is possible to hide the iteration within the library function. You could have a PrintfArray system/library function that would allow you to print arrays without doing iteration in the user code.

They key difference here is in the interface. In Java and C#, you could write a function that prints arrays that only takes an array type as a parameter. You can do the same thing in C++ with a vector data type. The C++/Java/C# containers have a length field encoded into the object, so it is possible to pass them as a single parameter to a function. A C array does not have an encoded length, so you need to pass the length as a separate parameter.

If you want to pass a C array type without passing a separate length argument, the array must be terminated with a guard value. printf is capable of handling C-strings (which are char arrays) because they have a NUL character as the guard value. However using only guard values can be dangerous because you can't place an upper bound on the length of the array. This is the basis of buffer overflow security vulnerabilities. You can try this out by passing printf a char pointer to a region of memory without a NUL terminator. It will probably print a bunch of extra junk and then segfault.
 
What is a good OS to code on? I don't have a Mac, so I either have Windows 8.1 or some version of Linux.

If Linux, which one? There are so many. I'm looking for ease of use and little frustrations.
 
What is a good OS to code on? I don't have a Mac, so I either have Windows 8.1 or some version of Linux.

If Linux, which one? There are so many. I'm looking for ease of use and little frustrations.

The one you're most comfortable using. Ubuntu is the easiest distro to get into and Linux is a great place to learn to program so I'd tip my hat to that direction.
 
I've heard of Elementary and Linux Mint. Are there better alternatives?

I have a very high end desktop.

http://pcpartpicker.com/p/jNyhTW

Linux will run on just about anything so even if you have something old or crappy you'll be fine.

Both of those distros are basically a version of Ubuntu with a different graphical front end. Ubuntu is easy to get into and has a lot of support. If you're wanting to get started programming without much hassle, that's your best bet.
 
Does anyone here have any experience with F#? It seems cool but its also kinda blowing my mind (although the syntax kinda reminds me of SQL, which i use heavily in my job).

Mostly curious if people use it and if so,what they do with it.
 

leroidys

Member
What is a good OS to code on? I don't have a Mac, so I either have Windows 8.1 or some version of Linux.

If Linux, which one? There are so many. I'm looking for ease of use and little frustrations.

I think it's fun to explore different OS, but if you want something powerful, quick, and easy just grab Ubuntu and install gnome.
 
Windows is quite a good choice of platform, especially if you intend to code applications that will run on Windows. The Microsoft way of doing things is quite different, so if you learn how to do things on Linux you will probably have to relearn the same thing on Windows. On the other hand, support for some languages is much better on Linux or OSX (which is a Unix-like operating system underneath).
 

injurai

Banned
What is a good OS to code on? I don't have a Mac, so I either have Windows 8.1 or some version of Linux.

If Linux, which one? There are so many. I'm looking for ease of use and little frustrations.

Both are Ubuntu based, which is to say debian in lineage. Just take it slow, learn a bit of bash (the terminal language as well as the scripting language to automate terminal tasks)

Familiarize yourself with updating your OS and it's package (via the package manager.)

Learn how to download new programs via the package manager.

That's pretty much all you need to get started, everything else you learn as needed.
 
What is a good OS to code on? I don't have a Mac, so I either have Windows 8.1 or some version of Linux.

If Linux, which one? There are so many. I'm looking for ease of use and little frustrations.

Since you specifically are looking for ease of use and little frustrations, I would choose Windows without a moment of hesitation, and I would question any other choice.
 
memory management is paramount?


Hmm, no, that's not really related to the reason. Suppose you have this code:

Code:
const int foo[] = {1, 2, 3, 4, 5};

Let's look at this "foo" thing in a debugger.

My debugger shows me this:

Code:
Name            Value          Type
foo           0x011f6ae4     const int[5]

0x011f6ae4 is a memory location. That's what a pointer (e.g. array) is. Just like integers hold values like 10, or -50, pointers also hold values, but those values are memory locations. Just to confirm this I will use my debugger to dump the memory at location 0x011f6ae4.

0x011F6AE4 01 00 00 00 02 00 00 00 03 00 00 00 04 00 00 00 05 00 00 00 00 00 00 00 00 00 00 00 00 .............................

integers are 4 bytes each, so looking at this dump shows you what an array looks like. Every 4 bytes there's a new integer. The first 4 bytes are "01 00 00 00" and the second set of 4 bytes is "02 00 00 00" and the third set is "03 00 00 00" etc. So it's something like this:

Code:
           |  foo[0]   |  foo[1]   |  foo[2]   |  foo[3]   |  foo[4]   |
0x011F6AE4 |01 00 00 00|02 00 00 00|03 00 00 00|04 00 00 00|05 00 00 00|00 00 00 00 00 00 00 00 00  .............................

Since a pointer is just like any other variable where the value contains an address, the following lines are all equivalent.

Code:
printf("%d", foo);
printf("%d", 18836196);  // This is 0x011F6AE4 converted to decimal.
printf("18836196");
int value = 18836196;
printf("%d", value);

In other words, it's just going to print the address. If you want to print 1, 2, 3, 4, or 5 you have to pass to pass 1, 2, 3, 4, or 5 into printf. Just like you would expect

Code:
printf("%d", 1);

to print 1, if you want to print 1 from the array you need to pass it the 1st value, or:

Code:
printf("%d", foo[0]);
 
I'd agree, with the exception being C coding.

Strange, because I was going to primarily recommend it for C and C++, with not as strong of a recommendation for other languages like Java, Python, etc.

Why do you consider C as the exception? Visual C++ is free, installs with 0 understanding of a command shell, and will get someone up and running faster than any other platform. And the debugger is better than anything else on any platform too.
 

injurai

Banned
Strange, because I was going to primarily recommend it for C and C++, with not as strong of a recommendation for other languages like Java, Python, etc.

Why do you consider C as the exception? Visual C++ is free, installs with 0 understanding of a command shell, and will get someone up and running faster than any other platform. And the debugger is better than anything else on any platform too.

I think it's because of C's history being written as the language for Unix, and unix being the OS for C. I'm certainly partial to GCC and posix compliance. Used to the gcc libs, the man pages and gdb.

I've used things like CCS on windows to write embedded C, really handing for looking at and manipulating memory in real time. So I see the value of a full development environment. But I feel linux allows me to pull away the abstraction further, and work closer to the OS and the hardware.

But I've only used mingw in windows, so that is also where I'm coming from. I think I'll try out Visual C++ on your recommendation now. But I'm glad I've at least developed foundations in a linux environment.
 
I think it's because of C's history being written as the language for Unix, and unix being the OS for C. I'm certainly partial to GCC and posix compliance. Used to the gcc libs, the man pages and gdb.

I've used things like CCS on windows to write embedded C, really handing for looking at and manipulating memory in real time. So I see the value of a full development environment. But I feel linux allows me to pull away the abstraction further, and work closer to the OS and the hardware.

But I've only used mingw in windows, so that is also where I'm coming from. I think I'll try out Visual C++ on your recommendation now. But I'm glad I've at least developed foundations in a linux environment.

FWIW the Windows API is C based too. It's just that most people coming from a non-Windows background never get exposure to it because they start with MinGW, and then try to use the half-baked posix api through this additional layer. Not saying the posix api is "half-baked" its native environment, btw, just that certain aspects of it don't map well to windows at a fundamental level, and as such using it is clunky through MinGW and doesn't really get you everything the OS has to offer.

I think it's just what you're accustomed to. Personally I feel that programming on Windows gets me as close as I could possibly want to the OS and the hardware just because I'm already familiar with all the native APIs and how to use everything, similar to how you're already familiar with glibc, man pages, etc.
 

injurai

Banned
Wow, I really was not aware of that. Probably would have gone on thinking the same old things had this conversation not happened. I'll check it out now, because Windows is my main OS outside pure hobby coding.
 
Glad to help :) If you're like me and like reading books, I would probably recommend this, widely considered the bible of Windows system programming. Nothing about GUI apps here though. And even though it's called Windows via C/C++, it's really via C. The book uses C++ in a few places to make some things more convenient, but the actual Windows API it uses is all C-based.

I'm not sure how much Windows knowledge is already assumed though. The first Windows book I ever read was this one. It's a pretty dry read but it might cover some more fundamental stuff that the other book assumes, and even though it's 15 years old, almost everything is still relevant.

(BTW, for starting out. In Visual Studio make sure you create a "Console Application", and not a "Win32 Application". otherwise you will get an ugly looking WinMain function that you probably won't know how to make sense of unless you read the Petzold book).

One thing I like about the Windows API is how much control you have over everything. It leads to functions with some unwieldy signatures sometimes, but you can do really crazy stuff.

If you get stuck, feel free to ask!
 
Ok, I'm trying to use the rename() function in C to move a file from a directory to the /tmp directory, but it keeps failing. It'll work in /home, /Treechopper, /Documents, but not /tmp. What do I not understand?
 
Top Bottom