Before, we get started with C programming and installation guide we first need to understand the basic terminology associated with it.
To help us build the logic and frame sentences (basically syntactical sentences understandable by compiler).
Why C Programming?
the most obvious question in the mind of a budding programming is why to learn this language, because:
- it has modularity
- it is compiler based
- it is used in platform development
- it is structure oriented
- it is a middle level language
- uses of pointers
- uses recursions
- it is simple to understand over other languages
- it is light but powerful enough to create some light projects to a malware
- it is fast and efficient in nature
- it has portability
# this symbol is called as preprocessor. As the name says it, it pre-processes some work before executing the whole program.
This symbol is used with include this term is used to include a specific file basically header files having .h extension which are in your C bin folder these special files are programs pre-written which are called off while we start writing our program and each file has its own work we can create our own header files which we will discuss later in this course, some header files are stdio.h, conio.h, math.h, stdlib.h, etc.
Main() , the program will start after this declaration of main function. It can be used with void, int or based upon its usage and return type.
{ } , this is a constructor and de-constructor used in program for creating loops or different stages in while creating your program.
// , used for single line comment in C. Comments help in creating more readable programs understandable by the developer and for later debug process.
/* */ , this is used for multiple line comments if you wish to put a particular segment of code or multiple lines under comment then this is more helpful.
It is always advisable and encouraged that you should use comments while writing your program. It makes it look more clean and understandable.
Declaring variables should also be meaningful so that you are able search it out in the later process while debugging else can create confusion, for small lines of code it won't create much problems but if you go for 100-1000 lines of code then you need to create variables which are easier to detect.
\n , this is used for new line. \t , this is used for tab space.
; , this semicolon is used for terminating a line and if missed after a line it will give you syntactical error.
Const , keyword used to declare constants.
Printf , used to print output, whereas, Scanf , this keyword is used to get the address of the value stored mainly for getting the inputs.
File saved in C is saved with extension .c, for example if the file name is test then on saving it will be saved as test.c
Data Types in C programming
int | at least 2, usually 4 | %d , %i | ||
char |
1 | %c |
||
float |
4 | %f |
||
double |
8 |
%lf |
||
short int |
2 usually | %hd |
||
unsigned int |
at least 2, usually 4 | %u |
||
long int |
at least 4, usually 8 | %ld , %li |
||
long long int |
at least 8 | %lld , %lli |
||
unsigned long int |
at least 4 | %lu |
||
unsigned long long int |
at least 8 | %llu |
||
signed char |
1 | %c |
||
unsigned char |
1 | %c |
||
long double | at least 10, usually 12 or 16 | %Lf | | |
Above given are some fundamental data types apart from these we use boolean as well.
Some other data types are: arrays, pointers, function types, structures, etc these come under Derived data types.
Some other can be user defined that we will discuss later as we move further with the course.
Operators in C Programming
Operator | Meaning of Operator | |
---|---|---|
+ | addition or unary plus | |
- | subtraction or unary minus | |
* | multiplication | |
/ | division | |
% | remainder after division (modulo division) |
Operators are also prefix or post-fix eg : b++/b-- is post-fix operator and ++b/--b is prefix operator based upon the operator used will increment or decrement this you will observe more when will be using in a loop basically the prefix in loop increments before and post-fix increments after the things have been performed.
Ternary operator
These include ? :
while writing a statement for a condition we can use this like:
c=(a>b)?a:b
this statement will save your time rather scripting a long if else statement it says, if a is greater than b then print a else b and the result will be stored in variable c.
Size of Operator
syntax will be :
>> sizeof (variable)
byte format data
- int - 2
- float - 4
- double - 8
- character - 1
Priority & Associativity
priority associativity
{ } , ( ) , [ ] 1 left to right+ + , - - , ! 2 right to left
* , / . % 3 left to right
+ , - 4 left to right
< , < = , > , > = , = = , ! = 5 left to right
&& 6 left to right
| | 7 left to right
? : 8 right to left
= , + = , - = , * = , / = , % = 9 right to left
for example:
- 10 - 3 % 8 + 6 / 4
- 17 - 8 / 4 * 2 + 3 - ++a
let a = 10
result = - 4
Storage Specifiers
Type Storage Scope Life Default
auto cpu memory body within function garbage value
static cpu memory function program 0
extern cpu memory program till the end 0
register register memory body within function garbage value
>> Use of Static Keyword
Example 1:
#include<stdio.h>
int
main()
{
static
int
i=5;
if
(--i){
main();
printf
(
"%d "
,i);
}
}
0 0 0 0
Explanation: Since i is a static variable and is stored in Data Section, all calls to main share same i
Example 2:
#include<stdio.h>
int main()
{
static int var = 5;
printf("%d ",var--);
if(var)
main();
}
Output:
5 4 3 2 1
Explanation: The only difference here is, sequence of calling main and printf is changed, therefore different output.
Static keyword in C basically is used for a function or a variable the scope of this keyword is within the main function. Due to which it creates a restore point for the loop and compiler can make decisions. In the above example if the static word is removed then this program runs infinite number of times as it doesn't knows about its starting point.
Comments
Post a Comment
if you have any doubts, pls let me know