In making your choice of language it is worth taking account of the following list of considerations.
With Rust - What makes Rust faster than C/C++?
You can run the small C programs below online at OnlineGDB however if your running Linux then I advise you to compile them from the command line as per the instructions below. It's sooooo easy...
In Linux you can use the shell terminal to compile programs properly on your own system. This is the way we did it in the early days and it is still as useful today as it was back then. At some point when you have very large programs it becomes easier to use an Intergrated Development Environment or IDE for short but right now just use a text editor program and the terminal.
Here we assume you are logged in as 'john' on a computer called 'silver'.
When programming it is good to have a directory for your programming activities and one sub-directory for each language. You can create directories using the terminal. You type the text shown in
[john@silver ~]$ mkdir prog
[john@silver ~]$ cd prog
[john@silver prog]$ mkdir c
[john@silver prog]$ cd c
[john@silver c]$
Now you have a working directory for all your 'c' programming and your Current Directory (cd) is now the 'c' directory.
You can always make your current directory the 'c' directory by typing the text shown in
[john@silver ~]$ cd ~/prog/c
[john@silver c]$
Assuming your current directory is 'c' you type the text shown in
[john@silver c]$ mkdir example1
[john@silver c]$ cd example1
[john@silver example1]$
In the terminal we can use the very simple 'nano' text editor to write our program.
[john@silver example1]$ nano hello.c
Or you could use a GUI text editor to do the same job if you like. We are creating a program source code file which we will choose to name 'hello.c'
Use a text editor to type or copy the following (IMPORTANT: Because of the way the web works when you cut and past this program you might get < instead of < and > instead of > just replace them and it should work OK.);
);
#include <stdio.h>
int main()
{
printf("Hello, World!\n");
return 0;
}
then save it in the example1 directory as hello.c You are now ready to COMPILE it. Type;
[john@silver example1]$ gcc -c hello.c
Typing
[john@silver example1]$ ls
hello.c
hello.o
[john@silver example1]$
You are now ready to LINK it. Type;
[john@silver example1]$ gcc hello.o -o hello
Typing
[john@silver example1]$ ls
hello.c
hello.o
hello
[john@silver example1]$
Note that the file "hello" is an executable program. Let's execute it. Type
[john@silver example1]$ ./hello
Hello world!
[john@silver example1]$
That ends the first example.
First we COMPILE the source code of the program in the 'hello.c' file to get the 'hello.o' file. Then we LINK the 'hello.o' file to to get an EXECUTABLE file 'hello'. When there is more than one source code file we compile them all to create many object code files which are then linked to get a single executable file. That is when compiling followed by linking becomes very important.
Assuming your current directory is 'c' you type the text shown in
[john@silver c]$ mkdir example2
[john@silver c]$ cd example2
[john@silver example2]$
In the terminal we can use the very simple 'nano' text editor to write our program.
[john@silver example2]$ nano who.c
Or you could use a GUI text editor to do the same job if you like. We are creating a program source code file which we will choose to name 'who.c'
Use a text editor to type or copy the following;
#include <stdio.h>
#include <string.h>
int main()
{
char name[100];
printf("What is your name? ");
fgets(name, sizeof(name), stdin);
// Remove the trailing newline character from the name
name[strcspn(name, "\n")] = '\0';
if (strcmp(name, "Alice") == 0 || strcmp(name, "Bob") == 0)
printf("Hello, %s!\n", name);
else
printf("Goodbye, %s!\n", name);
return 0;
}
then save it in the example2 directory as who.c You are now ready to COMPILE it. Type;
[john@silver example2]$ gcc -c who.c
Typing
[john@silver example2]$ ls
who.c
who.o
[john@silver example2]$
You are now ready to LINK it. Type;
[john@silver example2]$ gcc who.o -o who
Typing
[john@silver example2]$ ls
who.c
who.o
who
[john@silver example2]$
Note that the file "who" is an executable program. Let's execute it. Type
[john@silver example2]$ ./who
What is your name? Alice
Hello, Alice!
[john@silver example2]$
And here is another. When the computer prints out 'What is your name?' give it some other name and press the 'Enter' key on the keyboard. Here is one scenario;
[john@silver example2]$ ./who
What is your name? Tom
Goodbye, Tom!
[john@silver example2]$
In this example the computer will only say hello to Alice or Bob but to anyone else it says goodbye!
Here is a larger program you can run to find the occurences of the word "Mouse" in a part of the story of the town mouse and the country mouse. You could use the same program for finding occurences of a particular codon in a DNA sequence if you were a biologist, so this little program is not just about fairy stories, it is genuinly useful example code.
If you copy this program (IMPORTANT: Because of the way the web works when you cut and past this program you might get < instead of < and > instead of > just replace them and it should work OK) into an intelligent editor it will highlight the syntax clearly showing the commands and variables etc. in different colours. This makes things a lot clearer.
These programs are not explained here as if you look at one of the many tutorials for the language you will soon start to see what is going on. This program is here just to illustrate the language.
#include <stdio.h>
#define FALSE 0
#define TRUE 1
//Declare a function to compare texts.
int myCompare(char* aa, char* bb)
{
int i=0;
while (TRUE)
{
if (bb[i]=='\0') return TRUE;
if (aa[i]!=bb[i] && aa[i]!='\0') return FALSE;
i=i+1;
}
//Never gets here
}
//Declare a function to find pSubtext in pText from pIndex.
int findIndex(char* pSubtext, char* pText, int pIndex)
{
int i=pIndex; //Declare an integer as an index to pText.
int j; //Declare an integer as an index to pSubtext.
while ( pText[i]!='\0')
{
if ( myCompare(&pText[i],pSubtext) ) {return i;} //Note &pText[i] means take the text starting from index i
i=i+1;
}
return -1; // -1 is used to indicate that nothing more was found
}
int main()
{
//Declare two arrays of characters, mySubtext to search for in myText.
char mySubtext[] = "Mouse";
char myText[] = "A Town Mouse once visited a relative who lived in the country. For lunch the Country Mouse served wheat stalks, roots, and acorns, with a dash of cold water for drink. The Town Mouse ate very sparingly, nibbling a little of this and a little of that, and by her manner making it very plain that she ate the simple food only to be polite. After the meal the friends had a long talk, or rather the Town Mouse talked about her life in the city while the Country Mouse listened. They then went to bed in a cozy nest in the hedgerow and slept in quiet and comfort until morning. In her sleep the Country Mouse dreamed she was a Town Mouse with all the luxuries and delights of city life that her friend had described for her. So the next day when the Town Mouse asked the Country Mouse to go home with her to the city, she gladly said yes. Bye Mouse";
int i; //Declare an integer as an index for myText.
int c=0; //Declare an integer to count with and set it to 0.
printf("Find the index of all occurences of '%s' \n\nin '%s'\n\n", mySubtext, myText);
printf("Note: The first character is index 0.\n\n");
i=0; //Set the index to first character of myText for the loop.
while(myText[i] != '\0') //while the pointer isn't pointing to a terminating zero '\0' carry on with the while loop.
{
i = findIndex(mySubtext, myText, i); //Call findIndex to give the index of the next mySubtext in myText at or after index i.
if(i==-1) break; // -1 is used to indicate that nothing more was found - so finish while loop
printf("Found instance of %s at index: %i \n", mySubtext, i);
c++; //Add 1 to the count.
i++; //Set the index one more than the last match.
}
printf("\nFound '%s' %d times in this text.\n", mySubtext, c);
}
I chose to create another directory 'example3' for this program and I called the source file 'mice.c' then I compiled it and linked it exactly as I did with the last two examples and I executed it like this;
[john@silver example3]$