Learning 'C'.

70 Tango Charlie

Well-Known Member
Credits
885
@captain-sensible
Hey Cap,
I learned what you posted to me concerning 'g++' and 'clang'. From what I can tell, those two items are used in 'C++'. I have been trying to learn 'C'. I learned 'gcc' for compiling. That's why those two terms did not ring a bell with me. 'clang' is still beyond my pay-grade. LOL.
TC
 


captain-sensible

Well-Known Member
Credits
5,817
ok its actually easier then you think.

Code:
#include <cstdio>

int main()
{
printf("hello, my name is Charlie ");
printf("When i\'m done i\'m going to put serious C programmer on my curriculum vitae ");
printf( " \n ");
return 0;
}
open a linux text editor i use geany copy and paste and then save the above as "charlie.cpp"
say on your Desktop. Then open a terminal and:

cd Desktop
note there should be no need for sudo next

at $
g++ charlie.cpp -o charlie


//that should output a file maybe with diamond icon

next at $:

./charlie



//that should give out put in terminal
 

captain-sensible

Well-Known Member
Credits
5,817
oh i forgot my preamble on computer languages.

A "language" is a means of communication for instance I can speak TWI to my wife and she can understand. A computer language is a means of communicating with a computer. Some languages like python and php you write as text and use them that way. Where they are used say php on a web server they are "interpreted" line by line and used.

Java can be used on any platform due to the "Java Virtual Machine" and pseudocode is involved.

C++ which is an object orientated and therefore uses the concept of a class; files are usually saved as .cpp and then there is use of g++ to compile them.
 

70 Tango Charlie

Well-Known Member
Credits
885
Hey Cap,
Here's what I got.
This is the text editor that comes with Mint 19.3
cpp-1.png

I saved this as you mentioned 'charlie.cpp'.

And the output -
cpp-2.png


Perhaps I put spaces where they should not be.
In the 'g++charlie.cpp-ocharlie' should there be no spaces?
 

captain-sensible

Well-Known Member
Credits
5,817
ok from the command line what does this give :

$ ls --color

no you were correct using space and letter o ; i can see terminal is at Desktop but my guess is that charlie.cpp file is not on your desktop but in home directory. I can see on file editor ~/

~ being a shortcut for home, so look in home folder


at least it shows compiler is working though! no rush with this .. mind you if you can do this you know whats coming next ....compile a kernel from source. Just image it on the golf course - John Doe , yes i'm into ICT windows 10 don't you know !
you: " i dabble a bit with computers compiled a kernel last week "!
 
Last edited:

70 Tango Charlie

Well-Known Member
Credits
885
Here's what I got from ls --color

Ls --color.png




You are correct; it is saved in the home folder.
Home.png


Onward we go KemoSabe! OK Tonto!
 

70 Tango Charlie

Well-Known Member
Credits
885
Here's what I got when I entered G++ ....................... into 'Home'

Home g++.png


Cheerio!
 

70 Tango Charlie

Well-Known Member
Credits
885
And then; when I entered ./charlie, this is what I have:

Home g++ 2.png


Now that we have that straight............... Hah!!!
Onto the kernel business.
 

70 Tango Charlie

Well-Known Member
Credits
885
Congratulations you can now add c++ programmer to your curriculum vitae.if you get a job out of it I want 10%
Job? Who wants a job?
I'll leave that to all you young fellas. LOL.
However, if I ever make any money {or beer} from all this, I will surely share it with you!
TC
 

70 Tango Charlie

Well-Known Member
Credits
885
As you know, I am a little bit older than almost all you guys.
When I was in my 40s and on up to about mid 70s, I had my portion of beer; and yours, and Wiz, and Condobloke, and probably all the rest of the guys. Usually a six pack at the bowling alley and then a couple more at the bar on the way home every Tuesday night.
Consequently, my limits get reached sooner than yours now. I don't know what happened but a few years ago my system just could not take much anymore.
Anyhow, don't let me spoil anyones' party. Drink up, guys!
TC
 

70 Tango Charlie

Well-Known Member
Credits
885
@captain-sensible
Cap,
I need your help with Slackware and Ventoy.
Ventoy downloads fine but I can't seem to get it to install.
Slack is a different can of worms. I think I'll need a step by step set of instructs on that.
Here's all the info on my cp I'm using now.
INXI.png

TC
 

70 Tango Charlie

Well-Known Member
Credits
885
I got as far as extracting Ventoy and then to this screen:

Ventoy.png


I extracted it to the Downloads directory, but I see that it's not the right one. I don't know what the right one is.
TC
 

captain-sensible

Well-Known Member
Credits
5,817
As you know, I am a little bit older than almost all you guys.
When I was in my 40s and on up to about mid 70s, I had my portion of beer; and yours, and Wiz, and Condobloke, and probably all the rest of the guys. Usually a six pack at the bowling alley and then a couple more at the bar on the way home every Tuesday night.
Consequently, my limits get reached sooner than yours now. I don't know what happened but a few years ago my system just could not take much anymore.
Anyhow, don't let me spoil anyones' party. Drink up, guys!
TC
my drinking quota is currently one tin of beer per night
 

JasKinasis

Well-Known Member
Credits
2,416
ok its actually easier then you think.

Code:
#include <cstdio>

int main()
{
printf("hello, my name is Charlie ");
printf("When i\'m done i\'m going to put serious C programmer on my curriculum vitae ");
printf( " \n ");
return 0;
}
open a linux text editor i use geany copy and paste and then save the above as "charlie.cpp"
say on your Desktop. Then open a terminal and:

cd Desktop
note there should be no need for sudo next

at $
g++ charlie.cpp -o charlie


//that should output a file maybe with diamond icon

next at $:

./charlie



//that should give out put in terminal

If you're interested in C++, I'd recommend using actual C++ in the code, rather than C. Not that there is anything wrong with C. It's just that what you have there is no longer C and it's only loosely C++.

Because you have #include'd cstdio which is a C++ wrapper around the old C input/output functionality - it's technically C++ code, but you're using the C standard library to access i/o functionality. Which is fine, if that's what you want to use.

But also - as it stands - because you've included cstdio and NOT stdio.h - that code will only compile using a C++ compiler like g++ or clang++. It will no longer compile under a C compiler like gcc, or clang.

[aside]
Regarding Charlie's clang question:
Clang is a C compiler. Clang++ is a C++ compiler. The Clang compilers are an alternative to GNU's gcc/g++ etc. And use the LLVM compiler backend.
[/aside]

Going back to mixing C with C++:
If you use ldd to examine the link dependencies in the version compiled and linked using g++, you'll see something like this:
Code:
$ ldd charlie
        linux-vdso.so.1 (0x00007ffd1019e000)
        libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fa3e22dc000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fa3e2159000)
        libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fa3e213f000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fa3e1f7e000)
        /lib64/ld-linux-x86-64.so.2 (0x00007fa3e24b9000)
You can see that there are a lot of dependencies that have been linked in. NOTE - I'm on 64 bit linux!

If you are compiling pure C code - it would be more efficient to compile and link it as C using gcc. Then your executable will only be linked with the standard C library and the ld-linux library.

So if you used pure C, like this:
C:
#include <stdio.h>
int main()
{
printf("Hello my name is charlie.... etc etc.\n");
return 0;
}
You can compile and link the above as pure C using gcc or clang and there will be far less linker dependencies:
Code:
$ ldd charlie
        linux-vdso.so.1 (0x00007fff96f8d000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f384d852000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f384da6c000)
However - if you want to be able to compile the code in gcc AND g++ - you could simply use the previous pure C snippet and compile it with g++ (or clang++), because any valid C program is also a valid C++ program.

Or alternatively - you could do something with the pre-processor like this:
C:
#ifdef _cplusplus
#include <cstdio>
#else
#include <stdio.h>
#endif

int main()
{
    printf("Hello my name is Charlie - etc etc\n");
    return 0;
}
Now the code is simultaneously C (using stdio.h) AND is also C++ (but using the C standard library via cstdio). How it's interpreted will depend on which compiler we compile it with.

The code above will (conditionally) be compiled as C using gcc (or clang) or compiled as C++, using g++ (or clang++).

In the above, the preprocessor only includes cstdio if we're compiling with a C++ compiler, otherwise it includes stdio.h if we're compiling using a C compiler.

But as mentioned, the pure C snippet I posted - which only included stdio.h - will compile just fine under a C++ compiler. Because any valid C program will compile as C++.

In a large project - where you might have a lot of legacy C code that is compiled as C alongside more recent code written and compiled as C++ - there are various other techniques you can use, to allow different modules to compile/link and interact cleanly with one another. But that goes way beyond the scope of this thread!

Going back to using C++:
Instead of using legacy C functionality like printf - it would be better to use std::cout, which is in the standard C++ iostream library.

So your code would look more like this:
C++:
#include <iostream>

int main()
{
    std::cout << "Hello, my name is Charlie - etc etc....\n";
    return 0;
}
And before anybody says anything about adding using namespace std; after the includes, so you don't have to type std::cout every time you use cout - this is considered bad practice as it will bring the entire std:: namespace into your project. (See here)

Because std::cout is only used once in the above - it makes sense to explicitly specify it. If it was to be used multiple times - I would consider adding a more specific "using" declaration like using std::cout; after the includes.
Then we could use cout << "blah blah blah.\n" instead of std::cout << "blah blah blah.\n" each time.
But again, in the above example - it's only used once - so it makes more sense to explicitly specify std::cout.

perhaps leave this thread for C++ for those maybe starting out may want to chip in. I noticed @JasKinasis has a thread on more advanced C++
Over the years, I imagine I've probably answered a few C and C++ related questions here! Sadly, not very many recently though!
Some of them may even have been lost to the sands of time after Rob restarted the site from scratch some years ago!

Is the article in question the one about creating QT based applications without using an IDE like QT Creator? Or a different one?
 

captain-sensible

Well-Known Member
Credits
5,817
cheers Jas
The only language I have half a clue with is php although I have dabbled with python and bash to the level I can get by with slackbuilds. I think delving into C++ is a good project for the next 20 years.

As far as i understand it C was mostly procedural whereas C++ has moved into use of class with class members and methods, which suits me since thats how php is used.
 
Last edited:


Staff online

Members online


Top