Click to like/follow us on:


LEARNING HOW TO C
Objectives:
  • Staring at C
  • Parts of C
  • Keywords and Tokens
  • The rules of C
  • Using \n
  • Breaking long lines up using \ (backslash) 

                                  for ECE271 Assignment click here

Snippet 1
C Programme Structure/Syntax

Syntax simply refers to sentence structure i.e. the studies of the rules for forming admissible sentences (statements) or the grammatical arrangement of words in sentences (statements). So in C programming, it is how we arrange our keywords/ or tokens to form a valid statement otherwise miss arrangement leads to error known as syntax error.

A simple C programme Contains minimum of the following Syntax and Tokens:

1        #include <filename>
2        main function()
3        {
4        
5        //Comment(s)
6        Statement(s);
7        
8        return expression/value;
9        }


Never worry about the about structure I will explain it later. Let move so that we can write our first programme. The above structure is like an architectural work or a sketch not good diagram. Now to the next snippet.



Snippet 2
 EXECUTION OF C PROGRAMME
Execution of programme can simple seen as programme development cycle. These are the steps to be followed in writing and running a C programme. I will be using DEV-C++ compiler to teach this course thus all instruction will be in respect to the IDE (DEV-C++).

The steps are as follows:

1.      Creation of Source Programme(Code): Here you create the source code using an IDE, save the file using the name that depicts what it does.

2.      Compile the source code: Here the IDE will create a file called “youfilename.exe”, exe signifying that the file is now executable by the windows machine.

3.      Debugging: Here we look for bugs and remove them if there is any.

NOTE: Step 1 can involve writing new, editing, reediting the source code. And step 2 inlvove compiling and run the programme


Snippet 3
FIRST SIMPLE C PROGRAMME
Here we will create our first programme. You can name your programme almost anything, but not just anything, name your programme according to what it does (It is a convention) here we want to display a text so I name it Display.c.

1        /* This is my first programme
2        to display a Welcome to the Faculty of Engineering */
3        
4        #include <stdio.h>
5        
6        int main()
7        {
8                 //my first programme in C
9                 printf(“Welcome to the Faculty of Engineering!\n”);

10             return 0;
11    }


Type in the above programme into your IDE and compile, and then run it. The output is shown below


Welcome to the Faculty of Engineering!
Press any key to continue . . .

Make sure type the code correctly, when you have finish move to the next snippet for the analysis.
  
Snippet 3
ANALYSIS OF THE ABOVE PROGRAMME
  1. The first line of the programme /* this is my first programme to display a Welcome to the….*/. This will be ignored by the compiler it is called a Comment. 
  2.  Next line #include <stdio.h> is a preprocessor directives which tells a C compiler to include a file before going to actual compilation in this case stdio.h. stdio is an acronym for Standard Input and Output, .h indicates that stdio is header file. The header file must be preceded with <, and end with > or “and “(Less than and Greater than or two double quotes).
  3.  Next line, int main() is the main function. The main function is where other code executes. A C programme must have a main function for the programme to run/execute.
  4. Next line come the { (Left brace) represents the beginning of the programme. That is where execution starts.
  5.  Next line is //my first programme in C This is another method of comment.
  6. Next line printf(“Welcome to the Faculty of Engineering!\n”); This function causes its arguments to be printed/display on the screen. “\n”, Takes the cursor to a newline.
  7. The next line return 0; terminates main() function and returns the value 0.
  8.  The next line } (Right brace) represents the end of the programme.

Snippet 4
The C language keywords/Tokens

A token is an atomic unit (smallest indivisible unit) in a programme. Types of tokens are:
1. Keywords Examples: float, int, double, while, for.
2. Identifiers Examples: main, amount
3. Constants Examples: 12.4, 7894
4. Strings Examples: “CSM”, “Thursday”
5. Special Symbols Examples: [,], {, }, (, )
6. Operators Examples: +, *, /

Keywords are predefined tokens in C. Their can only be used for their intended action; they cannot be used for any other purpose. C has 32 keyworks viz:
auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while.

Snippet 5
The Basic Rules of C Programme

Here we will write a programme to display the basic rules of C language. The name of the programme is BasicCRules.c


1        #include <stdio.h>
2        int main()
3        {
4        
5        printf("Braces come in pair!");
6        printf("comments come in pairs!");
7        printf("all statement ends with semicolon!");
8        printf("spaces are optional!");
9        printf("must have a main function!");
10      printf("c is done mostly in lowercase!\
11      it is a case-sensitive language.");
12    
13    system("PAUSE");
14    return 0;
15    }

Compile the code and run, However if yours is not OK, then check your code, correct your mistake(s). Right you are correct you got it! Here is the output

Braces come in pair!comments come in pairs!all statement ends with semicolon!spa
ces are optional!must have a main function!c is done mostly in lowercase!it is a
 case-sensitive language.Press any key to continue . . .

Wow! That is nice and fantastic! Only if you did it well. But you may not be happy about the result I guess. In the output above, notice that the word space is carried forward to the next line. In general, line 5 to 10 all is separate in the source code, but however the output may not be what we desired to see. You notice each statement where join together but in the source code it is not so.Now let See how to move/display each statement to a newline as you have written it in the source code.

Making Use of the Newline Character “\n”

To format the display/output we will be making use of the newline character, \n to make each statement to display in a newline. So add this newline character, \n to the end of each printf() statement above, as shown below:

     5        printf("Braces come in pair!\n");
     6        printf("comments come in pairs!\n");
     7        printf("all statement ends with semicolon!\n");
     8        printf("spaces are optional!\n")      ;
     9        printf("must have a main function!\n");
    10    printf("c is done mostly in lowercase!\
    11    it is a case-sensitive language.\n\n");


Compile again and run the code. Your display will look like this:

Braces come in pair!
comments come in pairs!
all statement ends with semicolon!
spaces are optional!
must have a main function!
c is done mostly in lowercase!it is a case-sensitive language.

Press any key to continue . . .

And that is just it! The newline character “\n” helps to move the cursor to a newline. After printing line 5, the compiler encounters the newline character \n and moves the cursor to next line, line 6 to print what it(line 6) contain in same way, when comments come in pairs! Is displayed or printed the compiler immediately encounters the newline character and move the courser again to the next/newline and soon.

Also notice that in our source code, that line 10 was broken into two by using the \ (backslash character); so the backslash character helps us to break up long line for readability.




MORE FILE ARE BEING UPLOADED


SHARE THIS POST WITH YOUR FRIENDS...::

1 comment:

  1. شمع دنا باید باتوجه به مدل موتور و سال ساخت آن انتخاب شود. شما می توانید برای انتخاب مطمئن و جلوگیری از خسارت مالی، بهترین شمع مناسب دنا خود را با کمک پشتیبانی یدک پارس تهیه کنید. برای تهیه بهترین قیمت شمع دنا با ضمانت اصل بودن، یدک پارس بهترین جا است.

    ReplyDelete

All Posts

Access all posts here!

Categories

Unordered List

Sample Text

Lists of Jobs in the United States

List of Courses & Outlines For UNN Students

Popular Posts

Like us on Facebook

Recent Posts

    Text Widget