UPDATED:

How To Make A Simple Calculator By C Happy Coding.......... Happy Coding.......... Happy Coding.......... Happy Coding.......... Happy Coding.......... Happy Coding.......... Happy Coding.......... Happy Coding.......... Happy Coding.......... Happy Coding.......... Happy Coding.......... Happy Coding.......... This blog is under construction!!!!
Namecheap.com

Friday, December 23, 2016

Structure of a C program (updated)

After previous post The Basic structure of C....

Programming is done in the form of instructions that contain the individual programming lines. In the Hello World program, the text hello, world! On the screen. 
Basically, only a single programming line is provided:

Printf ("hello, world!");

But that would not be the case. Test it yourself: Just write this line and let the "program" compile and run. The compiler will then report an error. The compiler complains that it can not find a main () function.

A function includes one or more programming lines and is intended to serve a particular purpose. The purpose of our main () function in the Hello World program was to issue a text.

Each C program must have a main () function, but only one. If there are several main () functions, the compiler reports an error because the identifier (= name) main has been used more than once. The word main means in English as much as main, in C the main function is meant, since this is called first. The compiler always searches first for the main () function. The main () function represents the program entry point. After the program is started, the main () function is called first. How it then goes, stands in main (), or is your decision.

It is also common among C programmers when functions are used to write parentheses () behind function names. This notation again suggests that abc () is a function. Abc without brackets is something else (usually a variable, more on that later). If you can not imagine a lot of functions at the moment, this is not bad. At the present time, it is enough to know that a function is for a specifically delimited purpose (for example, outputting text, printf () is also one) and one or more programming lines.

Let's take a look at the main () function. The shortest form would be the following:

Int main ()
{

}

This is also the shortest program! But it does not matter. Type the following source code and test the program:

Int main ()
{

}

The program does not do anything, but the compiler does not report any errors - unless you have mistyped. This indicates that the program is operational. In C, it does not matter if you write something big or small! C is a case-sensitive language (nice-slipped), so it is case-sensitive. If you were to write something instead of main, this would cause the compiler to fail because it misses the main () function (lowercase). The Main () function could be any other function (although a different name would be used here, the more "talking" and better the function name describes the subtask of the function, the better).

Test the following program:

#include <stdio.h>
Int Main ()
{
  Printf ("This line will never be displayed!");
}
Int main ()
{
  Printf ("The main function has been executed!");
}


Look for the exact spelling! The two function names differ only by the first letter, and here only by the upper and lower case! The first function is called Main () and the second main (). After the start, main () is executed and the text The main function has been executed! On the screen. Because after the line with printf ("The main function was executed!"); Nothing will follow, the program will terminate.

Let us return to the simplest version, and let's look at the "components" more closely:

Int main ()
{

}


Main, as mentioned several times, is the function name. In this case, the word int. Int (short form for integer) is one of several possible return types. Functions may return values. Or. A function must return a value if so specified. That int stands before a function name means: This function must return a value of type int.

Let's take a step back. Consider the following scenario: You want to perform a mathematical calculation. For the sake of simplicity, this is supposed to be the square root of a number. The function you have written does this task. To do this, the function takes data (input, e.g., an input from the user), performs the calculation by (processing), and returns the result (output). This process is also known as the EVA principle (= input-processing output).

The data takes the function in the form of parameters. Here we would be again with the brackets (). What is between the parentheses is passed to the function. In line

Printf ("hello, world!");

The text (more precisely, a string, so-called string strings in C, more to be called later) hello, world! To the printf () function as a parameter. What is enclosed in quotation marks "..." belongs together and is passed as a string (string) to printf (). In theory, any number of parameters are possible, depending on how you have defined this as a programmer.

At this point again - forcing - something to anticipate: int is an integer type. Main () must return an integer (positive or negative integer, -10, 70, 0, 30000, whatever). In order to return the return value, which is quasi the result of the processing by the function, there is return. Return you already know:

Return 0;

Use return to specify the value that a function returns. Here the number 0. the return also ends the execution of the function. In main () returns the entire program. So if main () has the return type int and has to return an integer (int), why does the shortest possible program (see above) work anyway? Quite simply an exception, the C-standard also allows. But it is not nice. Therefore: Leave main () always return a value. 0 (zero) usually indicates that the program has been executed correctly.

As already mentioned, there are also functions without a return value. You may see the following variant of main ():

Void main ()
{

}


Void means "nothing", here: NO return value. Void, you can also write between the brackets to point out that no parameters are needed and are not allowed. (There is an exception: parameters can be passed from the command line to main (), but main () must look different, more in chapter 12.)

Void main (void)
{

}

Test it yourself. The program can be compiled without errors and is also running. However, GCC issues a warning: Warning: The return type of "main" is not "int". Main () must be of the int type according to the C standard. Void works (with a warning), but should not be used.

One is still missing: {} (curved brackets) enclose an instruction block. An instruction block includes several programming lines (or one).

In summary, one can for

Int main ()
{

}


So say:

  • Int = return value, here int for an integer
  • Main = identifier (name) of the function, main has a special position and stands for the main function
  • () = Parameters between the parentheses; This data is passed to the function
  • {= Opening curved bracket, beginning of the statement block, this bracket follow the instructions
  • } = Closing curved bracket, terminates the statement block, after the last statement

The following version of the example would also be valid:

Int
   Main
(
  ) 
{
}


This stylistically "impossible" variant can be compiled without errors and without warnings. This is because the compiler decomposes the source code into individual parts, blocks (so-called tokens), and evaluates them for themselves. Spaces, line breaks, tabs, etc. do not matter. How does the compiler know when a program line is off? The semicolon serves this purpose; At the end of a "normal" line like we had it after printf ():


Printf ("hello, world!");

In languages where semicolons (semicolons) are not mandatory at the end of the lines (eg BASIC, but also in JavaScript, it is not mandatory!), You can not distribute statements over several lines (because the compiler does not know where the line is over). What is important here is only: pay attention to the; At the end of the lines!

A word still to indentation: Although spaces do not matter to the compiler, I have inserted the lines within the statement block by 2 spaces in the Hello World example. This helps to preserve the overview when you have multiple nested statement blocks (you'll see later). The number of spaces you indent, or the number of tab positions, is left to you and is a question of the style or conventions you specify.

#include <stdio.h>
Int main ()
{
  Printf ("This line is 2 spaces from the left.");
  Return 0;
}


Include header files with #include


One thing is still missing to fully understand the Hello World program. There was the line:

#include <stdio.h>

The double cross # has a special meaning: it draws a statement to the preprocessor. The preprocessor is a part or its own program, which belongs to the compiler. The preprocessor processes the source code before the actual translation process. Here it does the following: It loads the stdio.h file and copies the entire text (source) from the specified file to the location where the #include statement is located.

Stdio.h is a so-called header file. A header file (also known as an include file) usually stores declarations. A declaration is a notification to the compiler. For example, you can use a declaration to tell the compiler what functions exist. What the function actually does is elsewhere (= definition).

The compiler then sees the statements from the header file as if you had written them directly to the source file. Typically, #include statements are at the beginning of a program. It would also have been possible to swap any source code into a file and then insert it by #include. But I can advise against this and is not the point.

Stdio.h contains declarations of the standard functions required for input and output. If you need printf (), you must include stdio.h. Stdio.h (std as standard) belongs to the C Standard Library (C standard library). That brings every C compiler with. If you want to know more: Standard C Library at Wikipedia.

Depending on where (!) Is the contained file, the filename must either be between brackets <> or quotation marks. This is very important and a frequent beginner error! If the file is located in the standard directory for contained files (each compiler offers the possibility to specify such a directory), use brackets <> (or for standard library files). If the file is in the current (!) Directory (that is, where the source file of the program is), you use quotation marks "". Compare:

#include <stdio.h>

#include "LocalHeaderdatei.h"

I would like to point out once again how important it is to try out what is read. Test every example and everything new! Write smaller programs for exercise, if possible. Programming is only learned through a lot of practice!

No comments:

Post a Comment

Namecheap.com

Popular Posts