Basic Structure of a C Program

The basic elements of a C program are the data declarations, functions, and comments.

#include <stdio.h>

int main(void){
    
    //comments
    
    /*
     *also  
     *comments
     */
    
    //declaration statements
    int a = 5;
    char *str = "I have seen the fnords";
    
    //library function call
    printf("%d %s\n", a, str);
    
    //return statement
    return 0;
}

The main() function is special, as it is the first function called in a program. All other functions in a program are called directly or indirectly from main().

The return statement is used to inform the operating system that the program exited normally. Returning a nonzero value indicates an error.

Note that in C, an end-of-line does not indicate the end of a statement; instead, statements are delineated with semicolons.

As we have seen earlier, the standard printf() function is used to output data to standard output.

#include <stdio.h>

int main(void){
    
    int a = 7;
    int b = 9;
    double c = 1.618;
    
    /*
     * output the results
     * of some simple mathematical expressions
     */
    printf("a + b = %d\n", a + b);
    printf("b * c = %f\n", b * c);
    printf("a / c = %f\n", a / c);
    
    
    return 0;
}

Computers are of course here to do computations. A computer can quickly resolve complex mathematical expressions. The C programming language has five simple mathematical operators: multiply, divide, add, subtract, and modulus. Of these, the only one that is not commonly used in everyday arithmetic is the modulus operator, represented by a percentage sign.

#include <stdio.h>

int main(void){
    
    /*
     * lets use the modulus operator
     * to get the remainder value
     */
    printf("%d %% %d = %d\n",9, 3, 9 % 3);
    printf("%d %% %d = %d\n", 10, 3, 10 % 3);
    printf("%d %% %d = %d\n", 15, 4, 15 % 4);
    
    return 0;
    
}

With C we can store values in variables.  Each variable has a string identifier, a name, basically. In addition to the name, each variable also has a variable type. The type defines what sort of data the variable can hold.

Before we can use a variable, we must define it in a declaration statement. A variable declaration defines the name of the variable and specifies the type of data the variable is meant to store.

#include <stdio.h>

int main(void){
    
    /*
     * declare four variables
     */
    int a;
    char b;
    double c;
    long long int d;
    
    
    //see what values they store
    printf("a = %d\n", a);
    printf("b = %c\n", b);
    printf("c = %f\n", c);
    printf("d = %lld\n", d);
    
    //return zero to the
    //operating system
    return 0;
    
}

One common variable type is int, or integer. Integer numbers have no fractional part or decimal point. Instead, decimal numbers are stored in the double type. Letters are stored in the char variable type.

When a variable is created, it is given a garbage value of whatever data happened to be in memory. We can have a variable store more meaningful data via an assignment statement.

#include <stdio.h>

int main(void){
    
    //declare an int variable
    int a;
    
    printf("%d\n", a);
    
    //assign the variable a value
    a = 73;
    
    printf("%d\n", a);
    
    
    return 0;
    
}

We use the standard library function printf() to display data. The printf() function uses special characters called conversion specifiers to output the value stored in a variable. The %d characters make up the integer conversion specifier, and the %f characters are the float or double conversion specifier.  The variables that we want displayed are listed after the format string; these variables and the format string make up the parameter list for the printf() function.

If you are interested in learning more about C and you have an Amazon Kindle or a Kindle app for your smartphone or tablet, take a look at my book http://www.amazon.com/Big-Als-C-Standard-ebook/dp/B00A4JGE0M

 

 

 

 

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s