Monday, October 7, 2019

C Basic

C Basic, Comments

Character Set

The characters that can be used to from words, numbers and expressions depend upon the computer on which the program is run. However, a subset of characters is available that can be used on most personal, micro mini and mainframe computers to form a standard program. The characters in C are grouped into the following categories :
  • Letters
  • Digits
  • Special characters
  • White spaces

    A complete table of character set

    Letters:
    A to Z in uppercase or capital letters.
    a to z in lowercase or small letters.
    Digits :
    0, 1, 2, 3, 4, 5, 6, 7, 8, 9
    Special Characters :
    CharacterDescriptionCharacterDescription
    ,comma&ampersand
    .period^caret
    ;semicolon*asterisk
    :colon-minus sign
    ?question mark+plus sign
    'apostrophe<opening angular bracket or less than sign
    "quotation mark>closing angular bracket or greater than sign
    !exclamation mark(left parenthesis
    |vertical bar)right parenthesis
    /slash[left square bracket
    \backslash]right square bracket
    ~tiled{left brace
    _under score}right brace
    $doller sign%per cent sign
    #number sign

White Spaces:
Space, line-feed, tab, form-feed, carriage-return, vertical-tab, and new-line characters are called "white-space characters" which are not visible on the screen.
White-space characters enhance readability of the program files and do same job as spaces between words and lines in the source code. C compiler ignores white-space characters at the time of reading the source code unless you use the white-space characters in string literals.

Trigraph Characters

The source character set of C source programs is contained within seven-bit ASCII character set, but is a superset of the ISO 646-1983 Invariant Code Set.
Trigraphs are a sequence of three characters starting with two consecutive question marks(??) followed by another character and allow the compiler to replace with their corresponding punctuation characters. Trigraph sequences are used in C program files in some keyboards which do not support some characters mentioned above table (Special Characters). The following table shows the list of trigraph sequences followed by an example.
Trigraph sequencePunctuation characterDescription
??=#comma
??([period
??)]semicolon
??<{colon
??>}question mark
??!|apostrophe
??/\quotation mark
??'^exclamation mark
??-~vertical bar


For example, if you attempt to print the string ??<character??> with this printf statement
 #include <stdio.h>
main() 
{
printf( "??< Character ??>" );
 }

the string printed is { Character } because ??< and ??> is trigraph sequences that are replaced with the "{" and "}" character.

Comments

Comments are a good way to write notations to explain what a program does. Comments can appear anywhere in a program. C compiler ignores comments at the time of reading the source code C language supports the following two different ways of commenting.

Starts with a "/*" and ends with "*/" at the end of the comment. Comments in C language can occupy more than one line but cannot be nested. Comments can appear anywhere in a C program. This following example is a comment accepted by the compiler:
/* Comments in C language can
occupy more than one lines */
printf( "Welcome to C programming" );
Comments can appear in the same line after the statement:
printf( "Welcome to C programming" );  /* Comments can come in same line after the statement */ 
Comments in C language cannot contain nested comments. The example bellow causes an error:
/* This is the outer comments
 /*Start to write statements*/
printf( "Welcome to C programming" );
*/
Some compilers also support single-line comment preceded by two forward slashes (//). Here is an example.
// This is a single line comment.
printf( "Welcome to C programming" );



Trigraph characters in C language

Why trigraph characters were used?

In C language we use different characters on different purposes. Say for example {} are used to define a scope, [] are used to define dimension to an array, \ is used to write any escape sequence, # is an initial character to any pre-processor statement, | is a bitwise OR, ^ is a bitwise XOR and ~ is a negation operator.
Keyboards used with some platforms were missing some of these characters. So it was not possible to write C code on these machines.


Trigraph characters:

  • Some of the characters like {}, [], \, |, ~ and ^ are missing in the above keyboard. Hence practically it may not be possible to write a C program using this keyboard.
  • To solve this problem C suggested to use combination of 3 characters to produce a single character called trigraph character.
  • A trigraph is a sequence of three characters, the first two of which are question marks
  • C supports the following 9 trigraph characters.
    Trigraph sequenceEqual character
    ??=#
    ??([
    ??)]
    ??/\
    ??<{
    ??>}
    ??!|
    ??’^
    ??-~
The trigraphs preprocessor replaces all occurrences of trigraph sequences by their single-character equivalents before any other processing.

We need to write a simple hello world program in the following way in the platform which doesn’t support #, {} and \ characters.
1
2
3
4
5
6
??=include<stdio.h>
int main()
??<
printf("Hello??/nWorld");
return 0;
??>
The trigraphs pre-processor changes the above code as
1
2
3
4
5
6
#include<stdio.h>
int main()
{
printf("Hello\nWorld");
return 0;
}
Trigraphs are not commonly supported by all the compilers. Some compilers support an option to turn recognition of trigraphs on. Some issues warnings when they encounter trigraphs in the source files

Executing a C program with trigraph characters in Turbo C:

Turbo C provides a trigraph preprocessor called trigraph, which accepts a C source file as input and replaces all the trigraph characters with their associated characters. In simple, trigraph preprocessor changes the source file.
The trigraph preprocessor is available in the Turbo C bin folder. Make sure that, the path is set to the bin folder of Turbo C as specified in 1.6 How to execute a C program in command prompt using Turbo C session to access the trigraph tool from any where

Once trigraph preprocessor replaces all the trigraph characters with their appropriate characters in the source then C program can be compiled and executed as a normal program using tcc
1. Open the text editor to write the program
>edit prog.c
2. Type the program, save (alt+f, s) and exit (alt+f, x) from the editor
1
2
3
4
5
6
7
/* program to print hello world */
??=include<stdio.h>
int main()
??<
printf("Hello??/nWorld");
return 0;
??>
3. Replace all the trigraph characters in the source code (prog.c) using trigraph preprocessor
>trigraph prog.c
4. We can see the replaced source file using the type command
>type prog.c
#include<stdio.h>
int main()
{
printf("Hello\nWorld");
return 0;
}

5. Compile the program using tcc (Turbo C compiler)
>tcc prog.c
6. Execute the program
>prog
Hello
World

Executing a C program with trigraph characters in windows:

Now we will see how a C program with trigraphs is executed using MinGw gcc compiler on windows command prompt.
1. Open the text editor to write the program
>edit hello.c
2. Type the program, save (alt+f, s) and exit (alt+f, x) from the editor
1
2
3
4
5
6
7
/* program to print hello world */
??=include<stdio.h>
int main()
??<
printf("Hello??/nWorld");
return 0;
??>
3. Compile the program using “gcc”.
Note:
-trigraphs is a compiler option to enable trigraph preprocessor
-o is a compiler option to generate executable file with file name (hello.exe)
>gcc -trigraphs -o hello hello.c
4. Execute the program
>hello
Hello
World


Executing a C program with trigraph characters in Linux:

Now we will see how a C program with trigraphs is executed using gcc compiler in Linux.
1. Open the gedit to write the C program
$gedit sum.c
2. Type the program, save (ctrl+s) and exit (ctrl+q)  from the editor
1
2
3
4
5
6
7
8
9
10
/* program to take two numbers and print sum */
??=include<stdio.h>
int main()
??<
int x,y;
printf("Enter two numbers:??/n");
scanf("%d%d",&x,&y);
printf("Sum %d??/n",x+y);
return 0;
??>
3. Compile the program using gcc
$gcc -trigraphs -o sum sum.c
4. Execute the program
$./sum
Enter two numbers:
45
20
Sum 65

Conclusion:
Almost all the keyboards and platforms now a days support the standard character set, hence we no need to use trigraph characters in any C program

C Hello World Example

A C program basically consists of the following parts: 

  • Preprocessor Commands 
  • Functions 
  • Variables 
  • Statements & Expressions 
  • Comments Let us look at a simple code that would print the words "Hello World":


 #include int main() 

 /* my first program in C */ 
 printf("Hello, World! \n"); 
 return 0; 
}

 Let us look various parts of the above program:

1. The first line of the program #include is a preprocessor command, which tells a C compiler to include stdio.h file before going to actual compilation. 

2. The next line int main() is the main function where program execution begins. 

3. The next line /*...*/ will be ignored by the compiler and it has been put to add additional comments in the program. So such lines are called comments in the program. 

4. The next line printf(...) is another function available in C which causes the message "Hello, World!" to be displayed on the screen. 

5. The next line return 0; terminates main()function and returns the value 0. 

Compile & Execute C Program 

Let’s look at how to save the source code in a file, and how to compile and run it. Following are the simple steps: 

1. Open a text editor and add the above-mentioned code. 

2. Save the file as hello.c 

3. Open a command prompt and go to the directory where you saved the file. 

4. Type gcc hello.c and press enter to compile your code. 

5. If there are no errors in your code, the command prompt will take you to the next line and would generate a.out executable file. 

6. Now, type a.out to execute your program. 

7. You will be able to see "Hello World" printed on the screen 

Hello, World! 

Make sure that gcc compiler is in your path and that you are running it in the directory containing source file hello.c.

No comments:

Post a Comment