Showing posts with label C Programming. Show all posts
Showing posts with label C Programming. Show all posts

Constants, Variables and Keywords in C

Posted by Tushar Bedekar

Constants:-

A constant is an entity whose value does not change throughout the program execution.

There are rules to be followed in order to create a constant. Let us discuss the rules to create constants.







Rules for constructing Integer constant

1) An integer constant must have at least one digit.
2) It must not have a decimal point.
3) It can either be positive or negative.
4) No commas or blanks are allowed within an integer constant.

5) If no sign precedes an integer constant, it is assumed to be positive.
6) The allowable range for integer constants is -32768 to 32767


 Actually, the range of the integer constant depends on the compiler. The above mentioned range is for 16-bit compiler. However, for a 32-bit compiler the range would be even greater.

The integer constants
• Whole Numbers
• E.g. 50, 45, -35, -26
• Computer allocates only 2 bytes in memory.
• 16th bit is sign bit. (if 0 then positive value, if 1 then negative value)

Decimal Integer constant:
• 0 to 9
• E.g.: 49, 58, -62 … (40000 cannot come because it is > 32767)

Octal Integer constant:
• 0 to 7
• Add “0” before the value.
• E.g.: 045, 056, 067

Hexadecimal Integer constant:
• 0 to 9 and A to F
• Add 0x before the value
• E.g.: 0×42, 0×56, 0×67


Rules for constructing Real constants


Rules for constructing Real constants (Fractional Form)
1) A real constant must have at least one digit
2) It must have a decimal point
3) It could be either positive or negative
4) If no sign precedes an integer constant, it is assumed to be positive.
5) No commas or blanks are allowed within a real constant.

E.g.: +867.9, -26.9876, 654.0


Rules for constructing Real constants (Exponential Form)

1) The mantissa part and the exponential part should be separated by the letter ‘e’
2) The mantissa may have a positive or negative sign(default sign is positive)
3) The exponent must have at least one digit
4) The exponent must be a positive or negative integer(default sign is positive)
5) The range of real constants in exponential form is -3.4e38 and +3.4e38

E.g.: +3.2e-4, 4.1e8, -0.2e+4, -3.2e-4


Rules for constructing Character constant

1) A character constant is an alphabet, a single digit or a single special symbol enclosed within inverted commas. Both the inverted commas should point to the left. For example, ’S’ is a valid character constant whereas ‘S’ is not.
2) The maximum length of a character constant can be 1 character. Allots 1 byte of memory


Variable:-

A variable is an entity whose value keeps on changing throughout the program execution. However, it’s not a rule that the value of the variable will change. A variable value might remain same throughout the program execution. However the main difference between variable and constant is that we can’t change the value of constant in between the program, but we can change the value of the variable during program execution.


Rules for constructing variable names

A Variable name consists of any combination of alphabets, digits and underscores. Some compiler allows variable names whole length could be up to 247 characters. Still it would be safer to stick to the rule of 31 characters. Please avoid creating long variable name as it adds to your typing effort
The first character of the variable name must either be alphabet or underscore. It should not start with the digit
No commas and blanks are allowed in the variable name
No special symbols other than underscore are allowed in the variable name
We need to declare the type of the variable name before making use of that name in the program. Type declaration can be done as follows:

To declare a variable as integer, follow the below syntax:


int variable_name;
Here int is the type of the variable named variable_name. ‘int’ denotes integer type.

Following are the examples of type declaration statements:




E.g.:

int p, n;
float r;

keyword:-

Keywords are the words whose meaning has already been explained to the C compiler. The keyword cannot be used as the variable name. If we try to do so we are trying to assign new meaning to the keyword. The keywords are also known as ‘Reserved words’.

E.g.: for, if, static, while, do, break etc.

Let us take an example:-
We define a variable named x in a program. Let us say x=9.
After some time during execution we change the value of x to 10. Now, x=10.
Here value of x is getting changed. But the value of 9 and 10 will never change. Hence, x is called a variable and 9 is called a constant.

Read More

Introduction to C Programming Language

Posted by Tushar Bedekar
C is a programming language developed by AT & T’s Bell Laboratories of USA in 1972. It was designed and written by a man named Dennis Richie. C is reliable, simple and easy to use.
One can easily learn C++ , JAVA or any other programming language if one has a knowledge of C.

Basic features:-

  • It is 16 bit programming language. 
  • Many of the popular and robust languages available today requires a basic knowledge of "c Programming". 
  • Many major components of popular operating systems like Windows, UNIX, LINUX is still written in C.
  • We all are now familiar with the smart phones, this smartness comes from a microprocessor,operating system and program mes embedded within that smart phones.Generally all these program-mes are written basically in "c Programming " languages.
  • Because of the fast execution f the c program-mes.
  • many popular gaming frame works(such as Direct X) have been built using c programming language.
  • It is simply and easy for the Beginners who are learning to do computer programming

C- Character Set:-


Similar to the other programming languages the C languages also include character set which includes alphabets, numbers an special symbols. following are the some character that we use in "C Programming" languages.
  • Alphabets :- A to B (ASCII value from 65 to 90)       a to b(ASCII value from 97 to 122)  
  • Digits:- 0 to 9(ASCII value from 48 to 57) 
  • Special Characters:- #,*,7,%,4 and 2 etc.(ASCII values as per the character) 
These ASCII values can be found by the help of a "C program me" for program me please click the link given below. Coming soon

Read More

Source Code Vs Object Code

Posted by Tushar Bedekar

Source Code:-

http://www.allaboutcomputing.net/In computing, source code is any collection of       computer instructions (possibly with comments) written using some human-readable computer    language, usually as text. The source code of a program is specially designed to facilitate the work of computer programmers, who specify the actions to be performed by a computer mostly by writing source code. The source code is often transformed by a compiler program into low-level machine code understood by the computer. The machine code might then be stored for execution at a later time. Alternatively, an interpreter can be used to analyze and perform the outcomes of the source code program directly on the fly.

Most computer applications are distributed in a form that includes executable files, but not their source code. If the source code were included, it would be useful to a user, programmer, or system administrator, who may wish to modify the program or understand how it works.


Aside from its machine-readable forms, source code also appears in books and other media; often in the form of small code snippets, but occasionally complete code bases; a well-known case is the source code of PGP.



Object Code:-


Object code, or sometimes an object module, is what a computer compiler produces. In a general sense object code is a sequence of statements or instructions in a computer language, usually a machine code language (i.e., 1's and 0's)[citation needed] or an intermediate language such as RTL.



Object files can in turn be linked to form an executable file or library file. In order to be used, object code must either be placed in an executable file, a library file, or an object file.

Object code is a portion of machine code that hasn't yet been linked into a complete program. It's the machine code for one particular library or module that will make up the completed product. It may also contain placeholders or offsets not found in the machine code of a completed program that the linker will use to connect everything together. Machine code is binary (1's and 0's)[citation needed] code that can be executed directly by the cpu. If you were to open a "machine code" file in a text editor you would see garbage, including unprintable characters. Object code is a variant of machine code, with a difference that the jumps are sort of parameterized such that a linker can fill them in. An assembler is used to convert assembly code into machine code (object code). A linker links several object (and library) files to generate an executable.


Source:-Wikipedia

Read More

File Input/output In "C" Programming

Posted by Tushar Bedekar

The Standard I/O Library provides similar routines for file I/O to those used  for standard I/O.

The routine getc(fp) is similar to getchar()
and putc(c,fp) is similar to putchar(c).

Thus the statement

            c = getc(fp);

reads the next character from the file referenced by fp and the statement

            putc(c,fp);

writes the character c into file referenced by fp.

       Basic Program me:-

/* file.c: Display contents of a file on screen */

#include <stdio.h>

void main()
{
            FILE *fopen(), *fp;
            int c ;

            fp = fopen( “prog.c”, “r” );
            c = getc( fp ) ;                        
            while (  c != EOF )
            {
                        putchar( c );              
                        c = getc ( fp );              
            }

            fclose( fp );
}

         Description:-

In this program, we open the file prog.c for reading.

We then read a character from the file. This file must exist for this program to work.

If the file is empty, we are at the end, so getc returns EOF a special value to indicate that the end of file has been reached. (Normally -1 is used for EOF)

The while loop simply keeps reading characters from the file and displaying them, until the end of the file is reached.

The function fclose is used to close the file i.e. indicate that we are finished processing this file.

We could reuse the file pointer fp by opening another file.



This program is in effect a special purpose cat command. It displays file contents on the screen, but only for a file called prog.c.
Read More

C File Handling

Posted by Tushar Bedekar
Often it is not enough to just display the data on the screen.This is because if the data is large, only a limited amount of it can be stored in memory and only a limited amount of it can be displayed on the screen.It would be in appropriate to store this data in the memory for one more reason.Memory is volatile(RAM) an its contents are  lost once the program me is terminate. so if we need the same data again it would have t either entered through the key-board again or would have to regenerated pragmatically.obviously both these operations would be tedious.At such times it become necessary to store the data in the manner that can be later retrieved and displayed either in part or in whole.The medium is usually "file" or "disk".


Basic Introduction:- 

We frequently use files for storing information which can be processed by our programs. In order to store information permanently and retrieve it  we need to use files.

Files are not only used for data. Our programs are also stored in files.

The editor which you use to enter your program and save it, simply manipulates files for you.

The Unix commands cat, cp, cmp are all programs which process your files.

In order to use files we have to learn about File I/O i.e. how to write information to a file and how to read information from a file.

We will see that file I/O is almost identical to the terminal I/O that we have being using so far.

The primary difference between manipulating files and doing terminal I/O is that we must specify in our programs which files we wish to use.

As you know, you can have many files on your disk. If you wish to use a file in your programs, then you must specify which file or files you wish to use.

Specifying the file you wish to use is referred to as opening the file.

When you open a file you must also specify what you wish to do with it i.e. Read from the file, Write to the file, or both.

Because you may use a number of different files in your program, you must specify when reading or writing which file you wish to use. This is accomplished by using a variable called a file pointer.

Every file you open has its own file pointer variable. When you wish to write to a file you specify the file by using its file pointer variable.

You declare these file pointer variables as follows:


FILE  *fopen(), *fp1, *fp2, *fp3;


The variables fp1, fp2, fp3 are file pointers. You may use any name you wish.

The file <stdio.h> contains declarations for the Standard I/O library and should always be included at the very beginning of C programs using files.


Constants such as FILE, EOF and NULL are defined in <stdio.h>.

You should note that a file pointer is simply a variable like an integer or character.

It does not point to a file or the data in a file. It is simply used to indicate which file your I/O operation refers to.

A file number is used in the Basic language and a unit number is used in Fortran for the same purpose.

The function fopen is one of the Standard Library functions and returns a file pointer which you use  to refer to the file you have opened e.g.

            fp = fopen( “prog.c”,  “r”) ;

The above statement opens a file called prog.c for reading and associates the file pointer fp with the file.

When we wish to access this file for I/O, we use the file pointer variable fp to refer to it.


You can have up to about 20 files open in your program - you need one file pointer for each file you intend to use. 

For More Sessions Please visit the link Given below:-

                                                                                                                                                                                                                         

                                                           
Read More
back to top