LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-08-2022, 01:41 PM   #16
lucmove
Senior Member
 
Registered: Aug 2005
Location: Brazil
Distribution: Debian
Posts: 1,237

Original Poster
Rep: Reputation: 88

Quote:
Originally Posted by astrogeek View Post
You need to be very clear about that then. Your previous thread was all about finding an interpreter which understood C syntax in order to avoid using a compiler. Others here may be excused for thinking that you are using an interpreter.
Ah yes. It is a totally fair assumption.

But as it turns out, I ran into Code::Blocks and it's what I'm using now. Pressing F9 to build and run in 0.001s is good enough for me, and the IDE organizes everything in some kind of "project" arrangement that seems to be a thing in C. Macros and auto completion don't hurt either. You see, we can change attitudes as we learn more.

Anyway, I have it configured to use the Tiny C Compiler, but I also used GNU GCC and the results seem to be the same.

So I think I am compiling now, correct me if I'm wrong.
 
Old 09-08-2022, 02:07 PM   #17
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 5,950
Blog Entries: 23

Rep: Reputation: 3937Reputation: 3937Reputation: 3937Reputation: 3937Reputation: 3937Reputation: 3937Reputation: 3937Reputation: 3937Reputation: 3937Reputation: 3937Reputation: 3937
Quote:
Originally Posted by lucmove View Post
But as it turns out, I ran into Code::Blocks and it's what I'm using now.
...
So I think I am compiling now, correct me if I'm wrong.
Good move! Now you are on an unobstructed path to C enlightenment!

I am not a user of CodeBlocks so cannot comment on the IDE specific aspects. I would encourage you to at least explore compiling your examples from a shell, to expose yourself directly to the process and simplicity of it all, but at your discretion.

Good luck!
 
Old 09-08-2022, 02:23 PM   #18
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,715
Blog Entries: 13

Rep: Reputation: 4770Reputation: 4770Reputation: 4770Reputation: 4770Reputation: 4770Reputation: 4770Reputation: 4770Reputation: 4770Reputation: 4770Reputation: 4770Reputation: 4770
You're learning that, "code can compile but still have bugs"

If this code blocks app provides you with a debugging capability, I suggest you explore it, or find some other suitable way (suitable for you) to debug code.

By the way you sort of have already found a first way to debug: printf(), where you learned that there was a surprise.

A debugger which allows you to break (halt) execution and examine variables, plus single step and continue to examine variables, will be a good thing to show you the depth of this type of problem.

Suggestions:
- use a debugger to output the initial array
- use a debugger to examine the address locations in memory for the array, and beyond - notice that you've modified undefined memory
- now add to that code and put another array definition right after the first one, then write into the supposed location [6] and see if it actually stomps on the next array, or not (all depending how smart the compiler may be)

Stuff like that. Explore. Or not. Your choice.
 
2 members found this post helpful.
Old 09-08-2022, 02:53 PM   #19
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 5,950
Blog Entries: 23

Rep: Reputation: 3937Reputation: 3937Reputation: 3937Reputation: 3937Reputation: 3937Reputation: 3937Reputation: 3937Reputation: 3937Reputation: 3937Reputation: 3937Reputation: 3937
Quote:
Originally Posted by lucmove View Post
the IDE organizes everything in some kind of "project" arrangement that seems to be a thing in C. Macros and auto completion don't hurt either. You see, we can change attitudes as we learn more.
The organization of files in C projects is "a thing in C" with aspects beyond the filesystem, or how convenient they are. You probably don't need to dive into the details at this point, but just something to put on your to-learn list, you will encounter it along the way (to borrow loosely from K&R):

In C any identifier (what you are thinking of as variable and function names) has two kinds of scope that you must consider - lexical scope and linkage.

Lexical scope is the region of source text within which the meaning of an identifier is understood.

Linkage determines the connections between objects and functions which may appear in separately compiled translation units (i.e. loosley files for now).

Scope and linkage, and therefore organization of source text, is used to expose or conceal information, among other things, and is an important aspect of the C language.

So, while your IDE of choice may provide a default or basic organization based on common C practices, it is not the only way to organize your code - you can do more with it if needed!

But that is a lesson for another day!

Last edited by astrogeek; 09-08-2022 at 02:54 PM.
 
1 members found this post helpful.
Old 09-08-2022, 03:18 PM   #20
lucmove
Senior Member
 
Registered: Aug 2005
Location: Brazil
Distribution: Debian
Posts: 1,237

Original Poster
Rep: Reputation: 88
Interesting. I don't understand it, but I almost do. I know I will.
 
Old 09-08-2022, 03:46 PM   #21
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 10,636

Rep: Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024
In fairness to C, it does "warn" you:

Code:
❯ cat boundschecking.c 
#include <stdio.h>

int main() {
    int myNumbers[] = {25, 50, 75, 100};
    myNumbers[5] = 500;
    printf("%d\n", myNumbers[0]);
    printf("%d\n", myNumbers[1]);
    printf("%d\n", myNumbers[2]);
    printf("%d\n", myNumbers[3]);
    printf("%d\n", myNumbers[4]);
    printf("%d\n", myNumbers[5]);
}

❯ gcc boundschecking.c 
boundschecking.c:5:5: warning: array index 5 is past the end of the array (which contains 4 elements) [-Warray-bounds]
    myNumbers[5] = 500;
    ^         ~
boundschecking.c:4:5: note: array 'myNumbers' declared here
    int myNumbers[] = {25, 50, 75, 100};
    ^
boundschecking.c:10:20: warning: array index 4 is past the end of the array (which contains 4 elements) [-Warray-bounds]
    printf("%d\n", myNumbers[4]);
                   ^         ~
boundschecking.c:4:5: note: array 'myNumbers' declared here
    int myNumbers[] = {25, 50, 75, 100};
    ^
boundschecking.c:11:20: warning: array index 5 is past the end of the array (which contains 4 elements) [-Warray-bounds]
    printf("%d\n", myNumbers[5]);
                   ^         ~
boundschecking.c:4:5: note: array 'myNumbers' declared here
    int myNumbers[] = {25, 50, 75, 100};
    ^
3 warnings generated.

Last edited by dugan; 09-08-2022 at 03:53 PM.
 
3 members found this post helpful.
Old 09-09-2022, 12:21 AM   #22
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 18,996

Rep: Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449Reputation: 6449
Quote:
Originally Posted by lucmove View Post
Thank you for the replies.

But I am very disappointed. I mean, how is that even allowed to happen? I've been using script languages for 20 years, languages that are often scoffed at while C is always regard in very high respect. But none of the scripting languages I have used would allow that nonsense. They would return NULL or the empty string, or maybe even throw an error. But the highfallutin C just makes up a random value out of thin air? That is mickey mouse.
That is again bad approach. It's better to say you're surprised because you didn't expect that. But it's actually C, and that's how it works. Don't assume anything and you won't be disappointed again.
C is a free language, you can do whatever you want (obviously only what is doable), you are not restricted in any way. Only the OS (kernel) and the available resources will restrain you.
From the other hand this freedom will lead to the problem you described, anybody can write useless code easily (obviously you can do it in any language). That's why we have additional tools to help identify those issues. The first one is the compiler itself, which will drop warnings or error messages if it can't process your code or if it finds something suspicious. The next one is probably the editor you use. And also we have static code analyzers and dynamic code analyzers to pinpoint those cases.
 
Old 09-09-2022, 01:09 AM   #23
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 18,081

Rep: Reputation: 2665Reputation: 2665Reputation: 2665Reputation: 2665Reputation: 2665Reputation: 2665Reputation: 2665Reputation: 2665Reputation: 2665Reputation: 2665Reputation: 2665
With C, always turn up the compile/link warnings to max and fix them all - you'll thank me later.

C is a very powerful language. Concomitantly, it expects the programmer to know what he/she is doing.

Remember it was originally invented to build Unix, so it's a sys programmer's lang
 
1 members found this post helpful.
Old 09-09-2022, 07:21 AM   #24
Michael Uplawski
Senior Member
 
Registered: Dec 2015
Location: Non. Je suis propriétaire – No. I am an owner.
Distribution: Apple-selling shops, markets and direct marketing
Posts: 1,365
Blog Entries: 36

Rep: Reputation: 722Reputation: 722Reputation: 722Reputation: 722Reputation: 722Reputation: 722Reputation: 722
Quote:
Originally Posted by chrism01 View Post
Remember it was originally invented to build Unix, so it's a sys programmer's lang
It is also a language which was built on the experience with older languages to meet the precise exigence of a couple of precise persons. That it came handy for an impressive bunch of developers worldwide proves some point or other, but not the pertinence of intentions. What alternative to this type of language would have existed at that time, as capable and flexible as 'C'? Seriously, I do not know. The modula type languages? Why would they (then) not have been sys programmers' languages?

I would even have guessed (probably wrongly) that an even stricter type-checking than in 'C' would be preferable for system programming. So why not Ada?

My guess is, that 'C' was small and quick to learn. That it is difficult to master is always for later in a developer's career.

I am off-topic. Feel free to say so.

Last edited by Michael Uplawski; 09-09-2022 at 07:23 AM.
 
Old 09-09-2022, 07:48 AM   #25
EdGr
Member
 
Registered: Dec 2010
Location: California, USA
Distribution: I run my own OS
Posts: 865

Rep: Reputation: 431Reputation: 431Reputation: 431Reputation: 431Reputation: 431
Quote:
Originally Posted by Michael Uplawski View Post
What alternative to this type of language would have existed at that time, as capable and flexible as 'C'?
There was another language that offered similar capabilities as C - Mainsail. Its syntax was ugly; it remained proprietary; and its use died out after the 1980s.
Ed
 
Old 09-09-2022, 08:41 AM   #26
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 10,636

Rep: Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024Reputation: 5024
AFAIK, this is the language C was based on:

https://en.m.wikipedia.org/wiki/B_(p...ming_language))

Of course, the language it was actually competing with was assembler. That would have been the other choice for writing the Unix kernel.

Last edited by dugan; 09-09-2022 at 08:51 AM.
 
Old 09-09-2022, 09:47 AM   #27
lucmove
Senior Member
 
Registered: Aug 2005
Location: Brazil
Distribution: Debian
Posts: 1,237

Original Poster
Rep: Reputation: 88
Quote:
Originally Posted by Michael Uplawski View Post
I am off-topic. Feel free to say so.
No, I don't think you're off-topic at all.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Assigning a value to a variable also creates an empty file named as the value in bash StorageDon Linux - Newbie 3 11-01-2016 08:52 PM
strange value assignments variable = value, value?? ostrow30 Programming 2 07-24-2011 07:59 AM
difference between value *value and value * value PoleStar Linux - Newbie 1 11-26-2010 03:37 PM
Bash Variable Array, Trying to add another value into the array helptonewbie Linux - Newbie 6 03-02-2009 11:18 PM
LXer: Piracy creates jobs, FOSS creates opportunities LXer Syndicated Linux News 0 11-02-2006 11:33 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:37 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration