Skip to content
← Back to Community
C Wars, Chapter One: The Segfault menace
Profile icon
CodeLongAndPros

Hello, World! πŸ––

To compete with @HahaYes C++ tutorial, I take the side of C, as defined in The C programming language, second edition by Brain Kerninghan and Dennsi Ritchie.

Edit: Chapter two is out

Now:

Let the games begin!

Chapter 1, part 1:

Hello World

The only real way to learn a language is to start programming in it.

So, we will do Hello world.

Code

Please make a new C repl, and enter this code into it.

(No Copy-Paste)

#include <stdio.h> int main() { printf("Hello world\n"); return 0; }

Run

If you are in repl, press C-ret, or 'Run'

If you are on a *NIX system use this:

gcc hello.c -Wall -Werror -o main.o && chmod +x main.o && ./main.o

If you are on Windows, download 50G of IDE and C++/Python/NET compilers (VS) and make a new project and hope your computer won't crash.

Output

You should see this appear in your terminal:

$ gcc hello.c -Wall -Werror -o main.o && chmod +x main.o && ./main.o $ ./main.o Hello world $

Chapter 1, part 2

Meta time

This section is very meta about C, and is not Strictly required.

Every C program has two parts:

  • Functions
  • Variables

Or, to be even more meta:

  • Code
  • Data

Let's dissect hello.c, line for line:

#include <stdio.h>

This may look like elvis elvish, but it is very simple.

The # char indicates a preprocesser directive.

The directive in question Prime include, includes other code in your program.

The prefix std means standard, and io means input/ouput

int main()

This is a function, or a snippet of code.

Every function has the form return_type name(args)

In this one, we have a return type of int, named main that takes no arguments

{

In C, a curly brace ({/}) seperates blocks of code.
In this case, all you need to know is that all code between { and } is part of main

printf("Hello world");

This is another function, like main.

It is named printf. In non-programmer speak, it means print with formatting. The formatting part is in Chapter two. It takes one -or two- arguments, the string to print, and the format string.

All lines that don't start with a # or are a function end with ;. No exceptions. Ever.

In this case, we are asking it to print Hello world with no formmatting.

return 0;

This is a special statment, return. It returns a piece of Data, like a gift.
For main, the return value is normally used to tell if the program failed, or worked.

  • 0: It worked
  • 1: General error
  • 2+: Random error

And, last but not least:

}

This tells your computer that your are done with main and want to move on.

Chapter 1, part 3

Your challenge

You need to make a program that prints out three things:

  • Your favorite Browser (Firefox)
  • The operating system you don't like the most (Windows 10)
  • Your favorite type of pet (Dog)

Please give them to me in the comments

Example output:

$ ./a.out Firefox Windows 10 Dog $

Part One: The Segfault menace

Part Two: The data wars

Part Three: Revenge of the if

Part Four: A new loop

Part Five: The Empire points back

Part Six: Return of the function

Voters
Profile icon
marwan2122
Profile icon
Hamzahjamal
Profile icon
enprogames
Profile icon
Garrett-Duncan
Profile icon
andrejz2438
Profile icon
Thomastomate
Profile icon
elcorreodeangel
Profile icon
RandomGuysRepl
Profile icon
saylamcenk
Profile icon
programmeruser
Comments
hotnewtop
Profile icon
Navinor
#include <stdio.h> int main() { printf("Firefox"); printf("Windows 10"); printf("Guinea pig"); return 0; }

imma pro at coding watch me h4ck hAhAAhAhAHAHAHAHAHa

Profile icon
CodeLongAndPros

@Warhawk947
A0C7891F-1468-44F6-BA8F-0BF9EBAF6C85

Profile icon
Jakman

Also none of this is elvish. C is a high level language.

Profile icon
CodeLongAndPros

@Jakman I was talking about preproc directives, for non programmers.

Profile icon
Jakman
Profile icon
CodeLongAndPros

@Jakman and there’s no preprocdirs in Python.

Profile icon
Jakman

@CodeLongAndPros well thats true. I love when people speak of the english programming language (Python).

Profile icon
CodeLongAndPros

@Jakman Yeah, but ever try to compile Python? I had a 100 mb bin.

Profile icon
Jakman

@CodeLongAndPros im going to be real. I havent touched python in a while.

Profile icon
MocaCDeveloper

@Jakman Eh, it is somewhat high level. But, out of seriousness, it is not a "high level language". Rephrase a bit.
It is a higher level low level language :)

Profile icon
CodeLongAndPros

@targetfanttthat A middle level language.

Profile icon
Jakman

@targetfanttthat Python is very high level. If it isnt neither is Scratch

Profile icon
awesome10
#include <iostream> int main() { std::cout << Chrome << std::endl; std::cout << Linux << std::endl; std::cout << Cat << std::endl; return 0; }

its right, right?

Profile icon
CodeLongAndPros

@awesome10 Well, this is C.

Not C++.

Look at @HahaYes's for C++

Profile icon
awesome10

@CodeLongAndPros it was on purpose

Profile icon
CodeLongAndPros

@awesome10 Well then:
** FAIL **

Profile icon
awesome10

@CodeLongAndPros well then:

#include <stdio.h> int main() { printf("I don't care"); return 0; }
Profile icon
CodeLongAndPros

@awesome10 The correct one is:

#include <stdio.h> using namespace std; import std.stdio: writeln; int main() { writeln("Firefox"); cout << "Windows 10\n"; printf("%s\n", "Dog"); }
Profile icon
awesome10

@CodeLongAndPros

using namespace std;

is bad and i actually like cats

Profile icon
CodeLongAndPros

@awesome10 Well, HahaYes does it...

But I know.

Profile icon
awesome10

@CodeLongAndPros well, i never said i learned all my cpp from hahayes and i actually like both cpp and c and c# the same wait that isn't 2 things watever

Profile icon
HahaYes

@CodeLongAndPros Why thanks man

Profile icon
CodeLongAndPros

@awesome10 Where's C?

Profile icon
awesome10

@HahaYes oh no you weren't supposed to be listening!

Profile icon
awesome10
Profile icon
CodeLongAndPros

@awesome10 Don't worry, I do that to him all the time.

Profile icon
awesome10

@CodeLongAndPros i said i liked cpp, c, and c# the same

Profile icon
HahaYes

@awesome10 I have seen your comments traitor. JK XD

Profile icon
awesome10

@HahaYes haha yes

Profile icon
CodeLongAndPros

@awesome10
Haha,
Segmentation fault (core dumped)

Profile icon
awesome10

@CodeLongAndPros

Haha, ^~~~~~ Traceback (most recent call last): File <stdin>, line one, in <comment> NameError: Haha, is not defined
Profile icon
awesome10

@CodeLongAndPros

Haha, ^~~~~~ Traceback (most recent call last): File <stdin>, line one, in <comment> NameError: Haha, is not defined
Profile icon
HahaYes

No not the traitor @CodeLongAndPros

Profile icon
Jakman
#include <stdio.h> int main(){ char[] browser = "AOL"; char[] opsys = "Bash"; printf("I like the browser %s and hate %s. I do not like pets.",browser,opsys); return 0; }
Profile icon
DynamicSquid

firefox sucks

Profile icon
CodeLongAndPros

@DynamicSquid How is this related?

Profile icon
CodeLongAndPros

@DynamicSquid Oh wait.

I just like FF better than Chrome..

Profile icon
HahaYes

@DynamicSquid Firefox > Chrome > internet explorer

Profile icon
CodeLongAndPros

@HahaYes Elinks is at the top

Profile icon
HahaYes

@CodeLongAndPros hmm let me guess rick roll

Profile icon
CodeLongAndPros

@HahaYes Yeah. #2 comes out tomorrow.

Profile icon
HahaYes

@CodeLongAndPros bro anytime I see a link I think... rick roll

Profile icon
CodeLongAndPros

@HahaYes For your awareness, you get a peek at Chapter two:

Examples of not-ints:

  • c
  • 134qwf
  • 55.0456
  • 192.168.0.0
  • geese
Profile icon
HahaYes

@CodeLongAndPros no. I MUST BE BETTER THAN YOU

Profile icon
CodeLongAndPros

@HahaYes
Not to brag, but I'm great at Quiplash and can make a mean tutorial.

Profile icon
HahaYes

@CodeLongAndPros The tears of the dissapointed fans

Profile icon
CodeLongAndPros

@HahaYes You have a head start.

Profile icon
HahaYes
Profile icon
CodeLongAndPros

@HahaYes But a head start means nothing, compared to the simplicity of C.

Profile icon
HahaYes

@CodeLongAndPros NO I don't like C

Profile icon
CodeLongAndPros

@HahaYes

Customer: Morning Waitress: Morning Customer: What have you got? Waitress: Well, there's egg and bacon Egg sausage and bacon Egg and C Egg, bacon and C Egg, bacon, sausage and C C, bacon, sausage and C C, egg, C, C, bacon and C C, sausage, C, C, C, bacon, C tomato and C C, C, C, egg and C C, C, C, C, C, C, baked beans, C, C, C and C (Choir: C! C! C! C! Lovely C! Lovely C!) Or Lobster Thermidor aux crevettes with a Mornay sauce Served in a Provençale manner with shallots and aubergines Garnished with truffle pÒté, brandy and a fried egg on top and C Wife: Have you got anything without C? Waitress: Well, the C, eggs, sausage and C That's not got much C in it Wife: I don't want any C! Customer: Why can't she have eggs, bacon, C and sausage? Wife: That's got C in it! Customer: Hasn't got much C in it as C, eggs, sausage and C has it? (Choir: C! C! C!...) Wife: Could you do me eggs, bacon, C and sausage without the C, then? Waitress: Iiiiiiiiiiiich!! Wife: What do you mean 'Iiiiiiiiiich'? I don't like C! (Choir: Lovely C! Wonderful C!) Waitress (to choir): Shut up! (Choir: Lovely C! Wonderful C!) Waitress: Shut Up! Bloody Vikings! You can't have egg, bacon, C and sausage without the C Wife: I don't like C! Customer: Shush dear, don't have a fuss. I'll have your C. I love it I'm having C, C, C, C, C, C, C, baked beans C, C, C, and C! (Choir: C! C! C! C! Lovely C! Wonderful C!) Waitress: Shut Up!! Baked beans are off Customer: Well, could I have her C instead of the baked beans then? Waitress: You mean C, C, C, C, C, C, C, C, C, C, C C and C? Choir (intervening): C! C! C! C! Lovely C! Wonderful C! C c-c-c-c-c-C C c-c-c-c-c-c C Lovely C! Lovely C! Lovely C! Lovely C! C C C C!
Profile icon
HahaYes

@CodeLongAndPros C++ better though

Profile icon
CodeLongAndPros

@HahaYes Cause?

Profile icon
Bookie0

Google > all
But Mac πŸ’» > all
@HahaYes

Profile icon
CodeLongAndPros

@Bookie0 No, Linux > Mac + Windows + Chrome

Profile icon
Bookie0

Big Mac πŸ’»

@CodeLongAndPros

Profile icon
CodeLongAndPros

@Bookie0

BIGGER LINUX 🐧

Profile icon
Bookie0

Lol Apple 🍎 @CodeLongAndPros

Profile icon
HahaYes

This is going to compete

Profile icon
CodeLongAndPros

@HahaYes Chapter Two: The data wars will be EVEN BETTER.

Profile icon
HahaYes

@CodeLongAndPros You must not BE BETTER THAN ME

Profile icon
CodeLongAndPros

@HahaYes I mean, it's C++ vs C, so I will always win...

Profile icon
HahaYes

@CodeLongAndPros YOu shalt NOT PASS

Profile icon
CodeLongAndPros

@HahaYes sudo pass

Profile icon
HahaYes

@CodeLongAndPros sudo -install life

Profile icon
CodeLongAndPros

@HahaYes
sudo pacman -S life

Profile icon
CodeLongAndPros

@HahaYes And are you making a statement?

Profile icon
HahaYes

@CodeLongAndPros ubuntu or fedora?

Profile icon
HahaYes

@CodeLongAndPros sudo -statements suck

Profile icon
CodeLongAndPros

@HahaYes I use Manjaro btw

Profile icon
HahaYes

@CodeLongAndPros hmmm is manjaro better than ubuntu?

Profile icon
CodeLongAndPros

@HahaYes I think so, but it's not for total noobs.

Profile icon
HahaYes

@CodeLongAndPros whoa, I just looked at it. It looks awesome

Profile icon
CodeLongAndPros

@HahaYes I use Manjaro KDE:
neofetch

Profile icon
HahaYes

@CodeLongAndPros hmmm which version of manjaro is the best

Profile icon
HahaYes

@CodeLongAndPros oh btw you have a nice laptop

Profile icon
CodeLongAndPros

@HahaYes Thanks.

To answer q1:

There is one version of Manjaro, since it is a rolling release

If you mean Desktop envrioment, there three:

  • XFCE. For weaker laptops
  • KDE. Like XFCE, but a bit heavier and more customizable (Mine)
  • GNOME: For touchscreens, but very heavy on both RAM and Cpu.
Profile icon
HahaYes

@CodeLongAndPros ah, so KDE for my i7 7th gen laptop

Profile icon
HahaYes

@CodeLongAndPros So there is no LTS?

Profile icon
CodeLongAndPros

@HahaYes Every time you run sudo pacman -Syu you have nearly the latest version of everything. (Manjaro holds updates back for a week for testing)

What version of GCC do you have? I have 10.1.0.

Profile icon
CodeLongAndPros

@HahaYes Also, do you have discord? I want to stop cluttering this up...

Profile icon
HahaYes

@CodeLongAndPros I run Ubuntu 20.04

Profile icon
HahaYes

@CodeLongAndPros I have GCC 10.1

Profile icon
CodeLongAndPros

@HahaYes What version of GCC do you have (gcc -v)

Profile icon
HahaYes

@CodeLongAndPros nah no discord

Profile icon
HahaYes
Profile icon
CodeLongAndPros

@HahaYes Well, the point is, Manjaro, (And it's daddy, Arch) have no real versions, because everything is up to date.

Profile icon
HahaYes
Profile icon
HahaYes

Wow! This project is actually really good quality!

Profile icon
HahaYes

WAIT WAT NO NO NO