Basics of C Language
Basics of C Language
1. Structure of a C program
After the above discussion, we can formally assess the structure of a C program.
By structure, it is meant that any program can be written in this structure only.
Writing a C program in any other structure will hence lead to a Compilation Error.
The structure of a C program is as follows:
printf("%d", a);
.
.
5. Return Statement: The last part in any C program is the return statement. The
return statement refers to the returning of the values from a function. This return
statement and return value depend upon the return type of the function. For
example, if the return type is void, then there will be no return statement. In any
other case, there will be a return statement and the return value will be of the
type of the specified return type.
Example:
int main()
{
int a;
printf("%d", a);
return 0;
}
int main() It is the main function from where C program execution begins.
getch(); This command is used for any character input from keyboard.
#include <stdio.h>
int main() {
// printf() displays the string inside quotation
printf("Hello, World!");
return 0;
}
Output
Hello, World!
2. int main() – Here main() is the function name and int is the return type of this function.
Every C program must have this function because the execution of program begins with
the main() function. The 0 return value of this function represents successful execution
of program while the return value 1 represents the unsuccessful execution of program.
This is the reason we have return 0; statement at the end of this main function.
Erros in c
Syntax errors: Errors that occur when you violate the rules of writing C/C++ syntax
are known as syntax errors. This compiler error indicates something that must be fixed
before the code can be compiled. All these errors are detected by compiler and thus
are known as compile-time errors.
Most frequent syntax errors are:
Missing Parenthesis (})
Printing the value of variable without declaring it
Missing semicolon like this:
// C program to illustrate
// syntax error
#include<stdio.h>
Void main()
int x = 10;
int y = 15;
}
Logical Errors : On compilation and execution of a program, desired output is not
obtained when certain input values are given. These types of errors which provide
incorrect output but appears to be error free are called logical errors. These are one of
the most common errors done by beginners of programming.
These errors solely depend on the logical thinking of the programmer and are easy to
detect if we follow the line of execution and determine why the program takes that path
of execution.
// C program to illustrate
// logical error
int main()
int i = 0;
{
printf("loop ");
continue;
}
getchar();
return 0;
}
Precedence of operators
The precedence of operators determines which operator is executed first if
there is more than one operator in an expression.
Let us consider an example:
int x = 5 - 17* 6;
In C, the precedence of * is higher than - and = . Hence, 17 * 6 is evaluated
first. Then the expression involving - is evaluated as the precedence of - is
higher than that of = .
Here's a table of operators precedence from higher to lower. The property
of associativity will be discussed shortly.
() Functional call
[] Array element reference
Left to right
-> Indirect member selection
. Direct member selection
! Logical negation
~ Bitwise(1 's) complement
+ Unary plus
- Unary minus
++ Increment
Right to left
-- Decrement
& Dereference (Address)
* Pointer reference
sizeof Returns the size of an object
(type) Typecast (conversion)
* Multiply
/ Divide Left to right
% Remainder
+ Binary plus(Addition)
Left to right
- Binary minus(subtraction)
== Equal to
Left to right
!= Not equal to
= Simple assignment
*= Assign product
/= Assign quotient
%= Assign remainder
+= Assign sum
-= Assign difference Right to left
&= Assign bitwise AND
^= Assign bitwise XOR
|= Assign bitwise OR
<<= Assign left shift
>>= Assign right shift