Although there are various languages nowadays C still maintains it’s position over other languages due to some of its unique features. Some of the features are mentioned below-
— Portability: Cross-platform portability of program with minimal changes.
— Simple and Efficient: High-level language feature support makes it programmer-friendly as well as it is very close to the low level(assembly) which makes it fast.
— Memory Manipulation: Arbitrary memory address access and pointer arithmetic is an important feature that makes C a perfect fit for operating systems and embedded systems.
Command line argument is basically input parameters of the main function in C and it is used when user need to pass input from outside(terminal) in program
1 |
int main(int argc, char *argv[]) |
argc = Supplied argument count.
argv[0] = Program executable.
argv[1]= First Input Parameter supplied in main from the terminal.
argv[2]= Second Input Parameter in supplied in main from terminal.
Example:
1 |
./test "Amit" |
argv[0]=test
argv[1]=”Amit”
argc=2
There are 4 stages involved in the creation of an executable file from the source. files.
— Pre-processing
— Compilation
— Assemble
— Linking
For detail about all stages click here
Storage class decides the memory allocation for a variable, it’s scope and visibility in a program. There are 4 storage classes in C-
Storage Class | Example | storage in Memory | Scope | Visibility |
---|---|---|---|---|
auto | auto int x; | stack | only in block where it is declared. | only in block where it is declared. |
register | register int x; | CPU Register | only in block where it is declared. | only in block where it is declared. |
static | static int x; static int x=10; | static int x;- Uninitialized Data Segment static int x=10; Initialized Data segment | In whole file where it is declared | only inside block or file where it is declared. |
extern | extern int x; | initialized Data segment | in whole program | in whole program |
A variable declared with static keyword is-
— Only accessible within the file(Global static variable) or block(local static variable) where it is declared.
— Global Static variable or function can not be access outside the file.
— Static variable whether it is global or local default value will be zero.
— static variable initialize only once whether it is local or global.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include<stdio.h> static int xglobal; //global static variable int main() { test(); //1st calling test(); //2nd calling } void test() { static int xlocal; //local static variable printf("xlocal=%d\n",xlocal); xlocal++; } |
run the above program;
xlocal=0 —> when test function call first then it will print default value of static variable xlocal.
xlocal =1—-> when test function calls the second time then it will not re-initialize and print the updated value from last function call.
extern keyword is used to declare global variable or function which is
— accessible outside the file.
You can see globalExternVarX is declared in test1.c and defined in test2.c. while it’s definition in test2.c but it can be used in test1.c because it is declared as an extern variable.
— Not occupy any memory until it is not initialized but initialization must be only once in the whole program.
–extern variable could not be initialized inside function.
Call by value- Function calling in which input argument is variable or constant.
1 2 |
addition(4,5);//addition is function with constant input parameters. addition(x,y);// addition is function with variable input parameters. |
Call by reference- Function calling in which input argument is the address of the variable.
1 |
addition(&x,&y);// addition is function with variabl's address as input parameter. |
recursion is the process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function.
Example : factorial programming
5! = 5*4! = 5*4*3! = 5*4*3*2! = 5*4*3*2*1! = 5*4*3*2*1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <stdio.h> //function for factorial long int factorial(int n) { if(n==1) return 1; return n*factorial(n-1); } int main() { int num; long int fact=0; printf("Enter an integer number: "); scanf("%d",&num); fact=factorial(num); printf("Factorial of %d is = %ld",num,fact); printf("\n"); return 0; } |
In the above example, to get factorial of 5 you can see factorial function (!) calls itself again and again with a different value(5,4,3,2,1). This process is called recursion and function are known as a recursive function.More
typedef is used for alias name to the standard datatypes.
1 |
typedef datatype alias_name; |
Suppose you want to give an alias name uint32_t and uint8_t to the standard data type unsigned int and unsigned char.then you have to write this code in your project.
1 2 3 |
typedef unsigned int uint32_t; typedef unsigned char uint8_t; typedef char int;//wrong, we can't take a standard data type as the alias name. |
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <stdio.h> typedef unsigned int uint32_t; typedef unsigned char uint8_t; #define Name "Amit" int main(void) { uint8_t x=20; //Here uint8_t is unsigned int printf("%d",x); printf("%s",Name); //=printf("%s","Amit"); return 0; } |
— typedef is used to give alias name to standard data type while #define is exact replacement of words.
1 2 3 4 |
typedef unsigned char uint8_t; #define int char #define NAME "AMIT" typedef NAME "AMIT" //wrong because typedef give alias to only standard data type |
— typedef terminate with semicolon while #define work without semicolon.
— typedef statement processed during compilation while the #define process in the pre-processing stage.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <stdio.h> typedef unsigned int uint32_t; typedef unsigned char uint8_t; #define Name "Amit" int main(void) { uint8_t x=20; //Here uint8_t is unsigned int printf("%d",x); printf("%s",Name); //=printf("%s","Amit"); return 0; } |