c programming for beginners tutorial
What is c?
C is a programming language developed at AT & T' s Bell laboratories of USA in 1972. It was written and designed by a man named Dennis Ritchie.
Why it is called C?
It is called "C" because of it came after programming language "B" Which was created by Thompson.
Why C is popular?
C is popular because it is simple, easy, reliable to use. In industries new programs, tools, and technologies emerge and vanish day by day, a language that has survived for more than three decades has to be really good.
Why do we need to learn C?
C++, c#, java, and many other languages use the concept of (OOP) Object Oriented Programming to organize the program. OOP has lots of advantages to offer. TO use (OOP) you would still need a good hold over the language elements of C and the basic programming skills. So it makes more sense to first learn C and then migrate to other programming languages.
Characters used in C
Keywords :
In C, keywords are the words which are already defined to the C compiler.
There are only 32 keyword in in C.
32 keywords are as under :
- 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
. Keyword cannot be used as a variable name.
Types of constants in C
Primary Constants : Includes Integer, Real, and Character constants.
Secondary Constants : Includes Array, Pointer, Structure, Union. Etc.
How constants are constructed?
Constructing Integer Constant :
. An Integer constant must have at least one digit.
. It must not have a decimal point.
. It can be either positive or negative.
. If no sign go before Integer Constant. it is consider to be positive.
. No commas or blanks are allowed within an integer constant.
Example : 9, 87, +654, -321, 9000
Constructing Real Constant :
. Real constants include Floating, point Constants.
. Real constants are written in two forms Fractional and Exponential form.
. A Real constant must have at least one digit.
. It must have a decimal point.
. It can be either positive or negative.
. If no sign go before Integer Constant. it is consider to be positive.
. No commas or blanks are allowed within a Real constant.
Example : 12.3, 45.32, -789.342, +0.546
Use of Exponential Form :
The exponential form is used, when the value of the constant is either too small or too larger. In exponential form, the real constant is represented in two parts, Mantissa, and Exponent.
Mantissa : Mantissa appears before part "e".
Exponent : Exponents are the digits after "e".
Example : 0.000453 can be written in Exponential form as 4.53e-4 (4.53*10-⁴).
Constructing Real Constant in Exponential Form :
. The Mantissa part and Exponential part must be separated by a letter "e".
. The Mantissa part may have a positive or negative sign.
. Default sign of Mantissa part is assumed as positive.
. The Exponent must have at least one digit.
Example : 1.2e-⁴, -2.3e5, +6.7e1
Constructing Character Constants :
. A Character Constant must be a single alphabet, digit or single special symbol.
. A Character Constant must be enclosed within single inverted commas.
. Both inverted commas must be point to the left.
Example : 's', 'I', 'R'.
C Variables
Variable : A Variable is name given to memory location, used to store data.
Types of C Variables :
A particular type of Variable can hold only the same type of Constant.
For Example :
An integer variable can only hold an integer constant.
A real variable can only hold a real constant.
A character variable can only hold a character constant.
Rules for constructing different types of constants are different but constructing variable name of all types, follows the same rule given below.
Rules for constructing Variable Names :
. Combination of any 1 to 31 alphabets, digits or underscore(_) can be used to construct variable names. (do not create unnecessarily long variable names as it adds to your typing effort.)
. The first character in the variable name must be an alphabet or underscore(_).
. Do not use commas or blanks within a variable name.
. Do not use any special symbol other than an underscore(_).
Example :
Char_good, int_low, high_1.
Variables Usage :
. Before using any Variable in the program, it should be declared first.
Example:
int a,b,c; /*declaration*/
c=a*b; /*usage*/
* is an arithmetic operator.
Building of a C program.
There are certain rules about the building of a C program that we need to follow before typing.
- Each instruction in a C program is written as a separate statement.
- The statement in a program must appear in the same order in which we wish them to be Executed.
- Blank space may be inserted between two words to improve the readability of the statement.
- All statements should be in lower case letters.
- There is no specific rule in C for the position at which a statement is to be written in a given line.
- Every C statement must end with a semicolon (;). Semicolon (;) is used as a statement terminator.
Comments in a C program
Comments are used in a C program to clarify either the purpose of the program or the purpose of the particular statement in the program.
We should remember some rules while typing a comment in a C program.
- Comment about the program should be enclosed within /* */.
- We can write any number of comments at any place in the program.
For Example :
/*Addition*/c=a+b;
c=a+/*Addition*/b;
c=a+b;/*Addition*/
- One comment cannot be written inside the other comment.
For Example:
/*comment/*comment*/*/ is invalid.
- A single comment can be written in more than one line.
For Example:
/*This
is a
comment*/
- We can also write a comment in this way.
// comment
main( )
- main( ) is a predefined keyword or a function used as a container for a set of statements.
- All statements that belong to main( ) are enclosed within a pair of braces {}.
For Example:
int main( )
{
statement 1;
statement 2;
statement 3;
}
- We use int before main( ) because the function always returns an integer value. The integer value that we are returning is 0. 0 indicates success. In case if statement in main( ) fails to do its work, it can return a non-zero number from main( ). This indicates failure.
- Some compilers, permit us to return nothing from main( ), in such case, we should use the keyword "void" instead of int.
printf( )
printf( ) is a function used to display the output of the program.
- Once the output is ready it needs to be displayed on the screen. We have to use printf( ) to do so.
- It is necessary to use #include <studio.h> at the starting of the program so that printf( ) function will became function able.
- The general form of printf( ) function is,
printf("format specifier",list of variables);
Format specifiers define the type of data to be printed on standard output.
format specifier can contain,
%c for printing character values.
%d for printing integer values.
%f for printing real values.
We can also use the printf( ) function in following way:
printf("%f",float);
printf("%c%d%f",character,integer,real);
printf("resul=%d",integer);
printf("%d%d%d",2,1+2,a+b);
printf("integer=%d\nreal=%f",i,r);
\n is used to print the output in the new line and it takes the cursor to the next line.
scanf( ) :
scanf( ) is a function used to take input from the keyboard by the user.
Our First program begins:
Compilation and Execution :
Once the program is typed, It needs to be convert to machine language instructions before the machine can execute it. To carry this conversion we need another software known as compiler which first compiles the program and after that execution process starts.
Comments
Post a Comment
Please do not enter any spam link in the comment box.