C Language - Posted In Problem Solving


Welcome, Guest. Please login or register.
Username:   Password:

Hosted in the Cloud RSS

Recent Topics

Gallery Latest


796 Views
0 comment(s)

774 Views
0 comment(s)


1127 Views
0 comment(s)

991 Views
0 comment(s)


861 Views
0 comment(s)

877 Views
1 comment(s)

Search The Gallery

User Info

Welcome, Guest. Please login or register.
October 22, 2017, 01:42:11 AM

Login with username, password and session length

Forum
Web
Gallery
Here you can post questions and get answers to help you solve problems with Windows based PC’s

We welcome your questions, with over 15 years experience with PC’s we might even find the answer for you.

Pages: [1]

C Language

Viewing: 0 Members and 1 Guest       Total Views: 2497

Offline 8pla.net Topic starter
United States WWW Posts: 154
Rank: Apprentice Adept

TWO DIMENSIONAL ARRAY OF STRINGS

Code:
#include <stdio.h>
#define COLS 80
#define ROWS 8

main()
{
    char strings[ROWS][COLS];
    int i;

    printf("\nDATA ENTRY\n");

    for(i=0;i<ROWS;i++){
       printf("\n%02d: ",i+1);
       scanf(" %[^\n]",strings[i]);
       strings[i][COLS-1]='\0';
    }

    printf("\nTRANSCRIPT\n");

    for(i=0; i<ROWS; i++){
       printf ("\n%02d: %s\n",i+1 ,strings[i]);
    }

}



PROGRAM INPUT AND OUTPUT


01: Hello

02: Hi there

03: How are you?

04: I'm not sure.

05: Why

06: Because

07: Really

08: Yes

TRANSCRIPT

01: Hello

02: Hi there

03: How are you?

04: I'm not sure.

05: Why

06: Because

07: Really

08: Yes



Disclaimer:  This is all for discussion purposes only.


UPDATE:  Added this line....  

Code:
strings[i][COLS-1]='\0';

               Manually truncates data that is entered
               beyond the number of COLS defined.

Posted June 05, 2016, 04:33:28 AM
Last Edit: June 05, 2016, 22:52:06 PM by 8pla.net
Logged
My Very Enormous Monster Just Stopped Using Nine

Offline 8pla.net Topic starter
United States WWW Posts: 154
Rank: Apprentice Adept

A little discussion may be in order here.  Firstly, and not to scare anyone, but C Language is very powerful but has the potential to be unsafe. It is used to make operating systems, but may be unsafe due to buffer overflows.  So, I would like to suggest extra caution be taken by anyone learning C Language. It may crash or hard lock a computer. This could mean whatever is open but not saved, would be lost if there is no other choice but to reboot. Apparently, on new operating systems this may be less likely than it used to be.  However it still may be a possibility.  And, there are further safety issues, beyond this discussion.    

That's the reason for the disclaimer that limits this information to discussion purposes only. For C Language learners, one hardware based suggestion, to play it safe, may be to use an old computer, that you don't mind crashing, instead of a new one that you do mind, especially someone else's new computer. A software suggestion, may be use a virtual machine PC, which may be the safest option. C Language compiles beautifully on old computers and virtual PCs, because it is classic computer programming.    

Let's discuss the C Language source code listing above. Basically this computer program takes data entry into a list.  The data may be text entries, until the input loop reaches eight. At that point, the program prints out a report of all the data entry to the screen. Note that data entry is not saved by the program.

In the source code listing, included on top is the stdio.h, standard input output header file. Then, defined as COLS and ROWS are the number of columns and rows.  That means anywhere you see COLS or ROWS, the number defined takes its place.  In this case 80 columns and 8 rows.  These #define statements are optional. The numbers can be hard coded instead, but #defines are good for making the code more readable.  And, easier because you only have to change #defines in one place. Updating the numbers in the #define statements and compiling again updates the number of columns and rows.

Next, there is the main() function, which is where program execution begins.  A two dimensional array of strings follows, to hold the data entry text in a list.  There is an integer variable for an index. Output begins with DATA ENTRY, followed by an input prompt to enter data using the "scanf" function, until the enter key is pressed. The next line minimally addresses buffer overflow, to address the case where the user enters data beyond the number of COLS.  It just manually cuts off the overflow. Improving this minimal one liner will easily double the size of the source code listing, which for discussion purposes is unnecessary. This all runs inside a loop for each of the eight rows. After all input is done, it outputs a formatted report, which looks like the user input screen, except it prints TRANSCRIPT as the report header.  

In Linux, for the sake of conversation only, this may be compiled with gcc C Language compiler.
For example:  gcc transcript.c -o transcript.exe    

I used the extension .exe because it is more familiar to Windows developers. However, any extension works in Linux if you compile, such as: gcc transcript.c -o transcript.go    

In conclusion, I would like to suggest that C Language may be an enjoyable developer experience from the command line.  It compiles without an IDE or project files. However, Integrated Development Environments and project files can still be used if desired.  Another plus is the application program is small and simple to distribute. Thanks for reading. I hope you enjoyed, and found this discussion on C Language useful.
Posted June 06, 2016, 00:00:23 AM
Last Edit: June 06, 2016, 01:25:26 AM by 8pla.net
Logged
My Very Enormous Monster Just Stopped Using Nine

Offline Data
United Kingdom Administrator
Posts: 4684
Rank: Certified

Interesting and educational - even if some of it does go over my head.
Posted June 06, 2016, 12:50:55 PM Logged

Offline 8pla.net Topic starter
United States WWW Posts: 154
Rank: Apprentice Adept

Thanks my friend.

Good feedback!   What I'll do is convert the C Language to PHP in order to appeal to web developers.
Calling on all PHP gurus in our community to discover how C Language is the predecessor of PHP...

Code:
<?php

//  include 'stdio.php';                         /* Include added for discussion purposes (not needed).  */

    
define("COLS"80);
    
define("ROWS"8);

    
/*  main  */

    
$strings[ROWS][COLS]=array();
    
$i;

    
printf("\nDATA ENTRY\n");

    for(
$i=0;$i<ROWS;$i++){
       
printf("\n%02d: ",$i+1);
       
fscanf(STDIN" %[^\n]"$strings[$i]); 
    }

    
printf("\nTRANSCRIPT\n");

    for(
$i=0$i<ROWS$i++){
       
printf ("\n%02d: %s\n",$i+,$strings[$i]);
    }

?>


Look how neatly this converts!  Isn't this crazy, how they are nearly the same source code?

If you are not familiar with C Language, that's OK... Please comment based on your PHP knowledge, or any other programming language you favour.  Perl is another fantastic example too. We are all friends here.   My goal is to make this as easy as possible.
Posted June 07, 2016, 03:12:39 AM
Last Edit: June 07, 2016, 03:45:43 AM by 8pla.net
Logged
My Very Enormous Monster Just Stopped Using Nine

Offline DaveMorton
United States WWW Global Moderator
Posts: 2538
Rank: Certified
Comfort the Disturbed! Disturb the Comfortable!

Given that C has been around for a decade or 3 longer than the internet (or even the "modern" PC, for that matter), it's the predecessor for a LOT of languages. Tongue Still, a solid working knowledge of C in all of it's flavours can't be anything but a good thing for those who use a keyboard and mouse to bend the "Evil Machine" (Piers Anthony/Xanth reference) to their will. I may not comment much here, but I value the information, so please feel free to continue to enlighten us. And thanks, for doing so. Smiley
Posted June 07, 2016, 20:47:53 PM Logged
Safe, Reliable Insanity, Since 1961!
Chat With Morti!

CAPTCHA4us

Offline 8pla.net Topic starter
United States WWW Posts: 154
Rank: Apprentice Adept

Well, if you've developed a PHP program that you like.  And, you find that you need to make it executable...  Say, to distribute it... Then, C Language may be a quick alternative to a PHP compiler.

Neat trick... Placing a PHP tag inside a C Language comment highlights the syntax in color.

Code:
/*  COMMENTS:   <?php to C Language   */

#include <stdio.h>

void main()
{
    
char mankind[80];
    
char machine[80];
    
printf("INTAKE: ");
    
gets(mankind);
    while (
strcmp(machine,mankind) != 0)
    {
        
printf("EXHAUST: %s\nVERIFY: "mankind);
        
gets(machine); 
    }
    
printf("\nPROCEED...\n\n");
}

Posted June 13, 2016, 20:42:05 PM
Last Edit: June 13, 2016, 21:30:07 PM by 8pla.net
Logged
My Very Enormous Monster Just Stopped Using Nine

Online Freddy
United Kingdom WWW Administrator
Posts: 3143
Rank: Certified

We did C when I was in college, but that was a long time ago. I have looked at it a few times since, but have so far not had a project that really needs it.

C# I am getting more familiar with at the moment. It's similar.
Posted June 13, 2016, 21:34:20 PM Logged
Pages: [1]
Jump to:  

Site Activity
Most Online Today: 166
154 Guests, 2 Users
Freddy, Urania