C Programming – Pointer to argc

InPursuitJust for fun, here’s a little program that uses a pointer to return the number of arguments that are submitted to the program at the command line.

#include <stdio.h>

int main(int argc, char argv[])
{
int *ptrArgc;

ptrArgc = &argc;
printf("\n\nOutput number of arguments in argc: %d \n\n", *ptrArgc);

printf("\n\nOutput memory address of argc: %d \n\n", ptrArgc);

return 0;

}

First, notice the line that declares the main() function in the program. The “int argc” declares an integer that automatically fills itself with the number of arguments entered at the command line when you run the program. The name of the program is the first argument, so the smallest number it can return is 1. The “char argv[]” is a character array that contains all the arguments entered at the command line, with “argv[0]” returning the name of the program itself.

The line “int *ptrArgc;” line declares a pointer and the “ptrArgc = &argc;” line assigns the memory address of argc to the pointer.

When printf() is used for the first time, ptrArgc is dereferenced with an asterisk. This causes it to print the value contained in argc instead of the memory address.

The second time printf() is used, ptrArgc is not dereferenced, so it prints the number actually contained in ptrArgc, which is the memory address that holds the value for argc.

Try compiling and running this program, entering different numbers of arguments at the command line. Notice the assigned memory address changes when you run the program.

C – Hello World!

InPursuitThe first program most people learn to write in any computer language is a program called “Hello World!” An online programmer friend recently mentioned that many of the basics you encounter in the programming language are built into “Hello World!” It got me thinking and I decided to post a short example of this classic program with explanations.

Yes, this program can be found many places and I’m reinventing the wheel by writing it. However, every person explains differently and, where someone else may have confused you, I may be the person to explain something in a way you will understand. Also, I find that communicating concepts helps me understand them better.

Here’s the program in C.

#include <stdio.h>

int main() {

   printf("\n\nHello World!\n\n");
   return 0;

}

The first line we encounter is:

#include <stdio.h>

This instruction is called a pre-processor directive. It loads the standard input/output library into the program. Part of the raw power of C is the low level control it gives you over the tasks your computer accomplishes and a good portion of this power is derived from the functionality that libraries provide. The most basic (and most often used) input and output are the keyboard and monitor, used to input and output data (interact with the program).

Libraries contain pre-programmed functions that expand what you can do with a computer language and “stdio.h” is one of the standard libraries included in the C language. You can also write functions of your own to complete repetitive tasks. In fact, there is one function that every C program must have.

Here’s the function that every C program must have to compile and run.

int main() {

}

Programmers refer to this function as “main.” The “int” refers to the type of data that the function returns when it exits, an integer. Functions are capable of returning different types of data or no data at all. A function that returns no data starts with “void” instead and there are other types. I have seen programs that start “main” with “void” instead of “int” but not all compilers will compile them. It is standard to start “main” with “int” and return an integer when the program exits, using a “return” statement.

The actual function is the word “main,” followed by parentheses and a pair of curly braces. The braces can contain data and commands. You can place data inside the parentheses that you want passed to the function. Our example contains a couple of lines of code inside the braces but the parentheses are empty. That is because we are not passing any outside data to “main.”

Here is an example.

return 0;

The “return” statement is used to return data when the function completes. It’s standard for the “main” function to return a zero when it completes, indicating that the program completed and exited normally. The “;” at the end of the line is like the period at the end of a sentence in English. It notifies the compiler that this is the end of the statement.

The one line of the program we haven’t discussed is:

printf("\n\nHello World!\n\n");

This line calls the “printf” function and tells it to print the “Hello World!” message to stdout, which is usually the computer screen. The printf() is the complete function (you only add the braces to functions in which you are writing the commands), everything between the parentheses is the data you are passing to the function and the semicolon at the end of the line tells the compiler this is the end of the statement.

Everything between the double quotes is a string of characters sent to the “printf” function. You can pass data in other ways but, when you are typing a string of characters to send, you must enclose them in double quotes. The “\n” is a newline character and printf() will respond by moving down a line on your screen before writing the message. In this case, the message is printed two lines down from the command used to call the program and is followed by two more new lines, ensuring that there is space before and after the program’s output. This makes the output more readable because it is not surrounded by clutter.

I hope you enjoyed the article. I’m looking forward to any comments, questions or corrections you may have.