Fundamentals of C
Fundamentals of C
Pointers
Memory Allocation
Recursion
Bit-manipulation
Features of the C Language
Pointers
C language, like other languages, allows reference
to a memory location by a name assigned to it. In
addition, C allows a user to refer to a memory
location (by its internal address or byte number)
by using pointers.
Memory Allocation
In almost all programming languages, memory is
assigned to a variable name at the time of
definition. C language also allows dynamic allocation
of memory, i.e. a program itself can request the
operating system to release memory for the use of
the program at the time of execution.
Features of the C Language
Recursion
A function may call itself. This feature, called
recursion, is supported by C language.
Bit-Manipulation
Unlike other languages, C allows manipulation
of data in its lowest form of storage - BITS.
4) Built-in functions
There are only 32 keywords in ANSI C, having
many built-in functions. These functions are
helpful when building a program in C.
5) Quality to extend itself
Another crucial ability of C is to extend itself. C language
has its own set of functions in the C library. So, it
becomes easy to use these functions. We can add our own
functions to the C Standard Library and make code
simpler.
7) Middle-level language
C is a middle-level programming language that means it
supports high-level programming as well as low-level
programming. It supports the use of kernels and drivers
in low-level programming and also supports system
software applications in the high-level programming
language.
8) Implementation of algorithms and data structures
The use of algorithms and data structures in C has made
program computations very fast and smooth. Thus, the C
language can be used in complex calculations and operations
such as MATLAB.
9) Procedural programming language
C follows a proper procedure for its functions and
subroutines. As it uses procedural programming, it becomes
easier for C to identify code structure and to solve any
problem in a specific series of code. In procedural
programming C variables and functions are declared before
use.
10) Dynamic memory allocation
C provides dynamic memory allocation that means you are
free to allocate memory at run time. For example, if you
don’t know how much memory is required by objects in your
program, you can still run a program in C and assign the
memory at the same time.
11) System-programming
C follows a system based programming system. It means the
programming is done for the hardware devices.
So, with this, we are aware of why C considered a very
powerful language and why is it important to know the
advantages of C?
Disadvantages of C Programming language
1. Concept of OOPs
C is a very vast language, but it does not support
the concept of OOPs
(Inheritance, Polymorphism, Encapsulation,
Abstraction, Data Hiding). C simply follows the
procedural programming approach.
2. Run-time checking
In the C programming language, the errors or
the bugs aren’t detected after each line of code.
Instead, the compiler shows all the errors after
writing the program. It makes the checking of
code very complex in large programs.
3. Concept of namespace
C does not implement the concept of
namespaces. A namespace is structured as a
chain of commands to allow the reuse of names
in different contexts. Without namespaces, we
cannot declare two variables of the same name.
But, C programming lacks in this feature, and
hence you cannot define a variable with the
same name in C.
For example,
we cannot create a sentence without using
words, similarly, we cannot create a
program in C without using tokens in C.
do if static while
C Identifiers
In the C language identifiers are the names given to
variables, constants, functions, structures, pointers,
or any other user-defined data, to identify them.
for example:
int myvariable = 101;
$myvar; // incorrect
x!y; // again incorrect
Using the Data Types Available in C language
The definition:
char alph;
char a, c; char ac 1 -
1 -
Unary Operator
A unary operator is an operator applied to the single
operand. For example: increment operator (++),
decrement operator (--), sizeof, (type)*
Binary Operator
The binary operator is an operator applied between
two operands. The following is the list of the binary
operators:
Ex:-Arithmetic Operators, Relational Operators, Shift
Operators, Logical Operators, Bitwise Operators,
Conditional Operators, Assignment Operator, Misc
Operator.
Constants in C
#define PI = 3.14159265359
#include <stdio.h>
#define LENGTH 10
#define WIDTH 5
#define NEWLINE '\n'
int main()
{
int area;
area = LENGTH * WIDTH;
printf("length: %d width: %d", LENGTH, WIDTH);
printf("%c", NEWLINE);
printf("value of area : %d", area);
return 0;
}
Types of Variables in C Language
1. Local Variable in C
void main()
{
int a = 22, b = 44;
local();
void local()
{
int a = 50, b = 80;
printf("Values in the local() function a = %d and b = %d\n", a, b);
}
Output
#include <stdio.h>
int a = 20; // Global declaration
void global();
void main() {
printf("In main() function a = %d\n", a); // Prints 20
global();
a = a + 15; // Uses global variable
printf("In main() function a = %d\n", a); // Prints 55
}
void global() {
a = a + 20; // Uses global variable
printf("In global() function a = %d\n", a); // Prints 40
}
2. Global Variable in C
Output:
In main() function a = 20
In test() function a = 40
In main() function a = 55
int main()
{
bool x=false, y=true; // variable initialization.
printf("%d\n",x);
printf("%d\n",y);
return 0;
}
Boolean expressions:
For example:
#include <stdbool.h>
bool isSunny = true;
bool isRaining = false;
int main()
{
if (isSunny && !isRaining)
{
printf("It's a perfect day for a picnic!\n");
}
Return 0;
}
Example :
#include <stdio.h>
int main()
{
const int a;
const int b = 12;
printf("The default value of variable a : %d", a);
printf("The value of variable b : %d", b);
return 0;
}
Special characters in C
Some special characters are used in C, and they
have a special meaning which cannot be used for
another purpose.
Single-level functions
Multiple-level functions
Single-Level Functions
main()
{
/*print a message*/
printf("Welcome to C");
}
In a C program, main() is always the first function to be
executed by the computer and should be present in all
programs. It is like any other function available in the C
library, or like one developed by a user.
Parenthesis () are used for passing parameters (if any) to
a function.
2) main()
{
/*print a line*/
printf("This function is perfect";
}
3) main()
}
printf("This function seems to be okay")
{
4) main()
{
This is a perfect comment line printf("Is it okay?");
}
Multiple-Level Functions
The following is an example showing
functions at multiple levels - one function
being called by another function
main()
{
/*print a message*/
printf("Welcome to C.");
disp_msg();
printf("for good learning.");
}
disp_msg()
{
/*print another message*/
printf("All the best ");
}
The output of the preceding program is:
#include<stdio.h>
/*function to accept and display a character*/
main()
{
char alph;
alph=getc(stdin); /*Accept character*
fflush(stdin); /*Clears the stdin buffer*/
putc(alph,stdout); /*Displays a character*/
}
The function main() first defines a memory location,
alph, of char type, and then invokes the functions
getc() and putc().
The function getc() accepts as parameter the
reference of device, and allows one character
to be read from this device during program
execution. In this case, the device is stdin.
The character read is returned and assigned to
alph.
Practice
Write a function to input a character and
display the character input twice.
The function to input and display a character, discussed
earlier, can also be written as:
#include<stdio.h>
main()
{
char c;
c=getchar();
fflush(stdin);
putchar(c);
}
Compiler :
A compiler is a specialized system tool that
translates a program written in a specific
programming language into the assembly language
code. The assembly language code is specific to each
machine and is governed by the CPU of the machine.
Assembler:
The assembler enters the arena after the compiler
has played its part. The assembler translates our
assembly code to the machine code and then stores
the result in an object file. This file contains the
binary representation of our program.
Moving further, the assembler gives a memory
location to each object and instruction in our
code. The memory location can be physical as well as
virtual. A virtual location is an offset that is relative
to the base address of the first instruction.
Linker:
Next, we move to the linker module. The linker spawns
to action after the assembler has done its
job. The linker combines all external programs (such
as libraries and other shared components) with our
program to create a final executable. At the end of
the linking step, we get the executable for our program.
So, the linker takes all object files as input, resolves all
memory references, and finally merges these object
files to make an executable file.
Loader:
The loader is a specialized operating system module
that comes last in the picture. It loads the final
executable code into memory.
Afterward, it creates the program and data stack.
Then, it initializes various registers and finally gives
control to the CPU so that it can start executing the
code.
Practice
1) Write a function that prompts for and
accepts a name with a maximum of 25
characters, and displays the following message.
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b = %d \n",c);
return 0;
}
C Assignment Operators
An assignment operator is used for assigning a
value to a variable. The most common
assignment operator is =
= a =b a = b
+= a += b a = a+b
-= a -= b a = a-b
*= a *= b a = a*b
/= a /= b a = a/b
%= a %= b a = a%b
Example : Assignment Operators
// Working of assignment operators
#include <stdio.h>
int main()
{
int a = 5, c;
c = a; // c is 5
printf("c = %d\n", c);
c += a; // c is 10
printf("c = %d\n", c);
c -= a; // c is 5
printf("c = %d\n", c);
c *= a; // c is 25
printf("c = %d\n", c);
c /= a; // c is 5
printf("c = %d\n", c);
c %= a; // c = 0
printf("c = %d\n", c);
return 0;
}
Using Operator Precedence
C, defines a set of rules for using operators.
These rules specify the order in which the
expression will be evaluated, when there are
multiple operators in an expression.
For example,
3 * 5 + 2;
In the preceding code snippet, the result of the
expression will be 17, because first 3 will be multiplied by
5, and then 2 will be added to the result. In this case,
precedence of the * operator is higher than the +
operator.
However, you can apply the parenthesis on the preceding
code, as shown in the following code snippet:
3 * (5 + 2);
In the preceding code snippet, the result of the
expression will change to 21 as first 2 will be added to 5,
and then result will be multiplied by 3. In this case, the
precedence of parenthesis is higher than the precedence
of operator.
Just A Minute (JAM)
#include<stdio.h>
main()
{
int i=45+90/5*6;
printf("%d",i);
}
Just A Minute (JAM)
#include<stdio.h>
main()
{
int a=5,i;
printf("a=%d\n",a);
printf("i=%d\n",i);
}
Escape Sequence in C
An escape sequence in C language is a sequence
of characters that doesn't represent itself
when used inside string literal or character.
printf() function
The printf() function is used for output. It
prints the given statement to the console.
printf("format string",argument_list);
#include<stdio.h>
int main(){
int num;
printf("enter a number:");
scanf("%d",&num);
printf("cube of number is:%d ",num*num*num);
return 0;
}
Format Specifiers for I/O
Data Type Format Specifier
int %d
char %c
float %f
double %lf
long double %Lf
short int %hd
unsigned int %u
long int %li
long long int %lli
unsigned long int %lu
unsigned long long int %llu
sizeof operator in C
sizeof is a much-used operator in the C. It
is a compile-time unary operator which can
be used to compute the size of its operand.
return 0;
}
Using Constructs
There are two types of constructs in C language:
Conditional constructs
Loop constructs
Conditional Constructs
== Equal to 5 == 3 is evaluated to 0
> Greater than 5 > 3 is evaluated to 1
< Less than 5 < 3 is evaluated to 0
!= Not equal to 5 != 3 is evaluated to 1
>= Greater than or equal to 5 >= 3 is evaluated to 1
<= Less than or equal to 5 <= 3 is evaluated to 0
Example : Relational Operators
// Working of relational operators
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10;
#include <stdio.h>
int main()
{
int a = 10, b = 20;
if (a > 0 && b > 0)
{
printf("Both values are greater than 0\n");
}
else
{
printf("Both or any one values are less
than 0\n");
}
return 0;
}
2. Logical OR Operator
The condition becomes true if any one of them is non-zero.
Otherwise, it returns false i.e, 0 as the value. Below is the truth
table for the logical OR operator.
Example
// C program for Logical OR Operator
#include <stdio.h>
int main()
{
int a = -1, b = 20;
if (a > 0 || b > 0)
{
printf("Any one of the given value is "
"greater than 0\n");
}
else
{
printf("Any one values are less than or
equals to 0\n");
}
return 0;
}
3. Logical NOT Operator
If the condition is true then the logical NOT operator will
make it false and vice-versa. Below is the truth table for the
logical NOT operator.
Example
// C program for Logical NOT Operator
#include <stdio.h>
int main()
{
int a = 10, b = 20;
#include <stdio.h>
int main()
{
int a;
float b;
double c;
char d;
printf("Size of int=%lu bytes\n",sizeof(a));
printf("Size of float=%lu bytes\n",sizeof(b));
printf("Size of double=%lu bytes\n",sizeof(c));
printf("Size of char=%lu byte\n",sizeof(d));
return 0;
}
Working with Conditional Constructs
For example,
if it is raining, you will take an umbrella. In the same
way, you can incorporate the decision making
techniques in the C programming language. The
decision making technique can be implemented in the C
programs by using the following conditional constructs:
The if construct
The if…else construct
Nesting of if ... else Statements
The else if Ladder
The switch construct
The ?: Operator.
The if statement
It is one of the simplest decision-making
statement which is used to decide whether a
block of C code will execute if a certain
condition is true.
Example
//C Program to demonstate the use of if statement
.
void main()
{
//defining an 'age' variable
int age=20;
//checking the age
if(age>18)
{
puts("Age is greater than 18");
}
puts(“Part of Main()”);
}
C if-else Statement
An if….else statement includes two blocks that are if
block and else block. It is the next form of the control
statement, which allows the execution of C code in a more
controlled way. It is used when you require to check two
different conditions and execute a different set of codes.
The else statement is used for specifying the execution
of a block of code if the condition is false.
Syntax
if (condition)
{
// block of code will execute if the condition is true
}
else
{
// block of code will execute if the condition is false
}
If the condition is true, then the statements inside if
block will be executed, but if the condition is false,
then the statements of the else block will be
executed.
//A C Program to demonstrate the use of
else statement.
//It is a program of odd and even number.
void main()
{
//defining a variable
int number=13;
//Check if the number is divisible by 2 or not
if(number%2==0)
{
puts("even number");
}
else
{
puts("odd number");
}
}
The if...else Construct
The following function shows the use of
the if..else conditional construct:
#include<stdio.h> main()
{
char chr;
puts(“Enter a character”);
chr=getchar();
fflush(stdin);
if(chr==’A’)
puts(“Character is A”);
else
puts(“Character is not A”);
}
In the preceding example, if the input
character is A, the message displayed is
Character is A, otherwise the message Character
is not A is displayed. Also, note that the
condition has to be specified within
parenthesis.
For example,
if the statement if (chr == 'A') was written as if
(chr = 'A') then chr would have got the value A,
and it being non-zero, the condition would be
evaluated as TRUE. Thus, the message Character
is A would have been printed regardless of the
input.
The function may also be written in the
following way:
#include<stdio.h>
main()
{
char chr;
puts(“Enter a character”);
chr=getchar();
fflush(stdin);
if(chr!=’A’)
puts(“Character is not A”);
else
puts(“Character is A”);
}
Syntax:
if(condition)
{
//code to be executed
if(condition)
{
//code to be executed
}
}
//C Program to demonstrate the use of Nested If Statement.
void main()
{
//Creating two variables for age and weight
int age=20;
int weight=80;
//applying condition on age and weight
if(age>=18)
{
if(weight>50)
{
puts("You are eligible to donate blood");
}
}
}
#include<stdio.h>
main()
{
char inp;
puts(“Enter a character”);
inp=getchar();
fflush(stdin);
if(inp>=’A’)
if(inp<=’Z’)
puts(“Upper Case”);
else if(inp>=’a’)
if(inp<=’z’)
puts(“Lower Case”);
else
puts(“Input character > z”);
else
puts(“Input character > Z but less than a”);
else
puts(“Input character less than A”);
}
C if-else-if ladder Statement
The if-else-if ladder statement executes one
condition from multiple statements.
Syntax:
if(condition1)
{
//code to be executed if condition1 is true
}
else if(condition2)
{
//code to be executed if condition2 is true
}
else if(condition3)
{
//code to be executed if condition3 is true
}
...
else
{
//code to be executed if all the conditions are false
}
//C Program to demonstrate the use of If else-if ladder.
//It is a program of grading system for fail, D grade, C grade, B grade, A grade a
nd A+.
void main()
{
int marks=65;
if(marks<50){
printf("fail");
}
else if(marks>=50 && marks<60){
printf("D grade");
}
else if(marks>=60 && marks<70){
printf("C grade");
}
else if(marks>=70 && marks<80){
printf("B grade");
}
else if(marks>=80 && marks<90){
printf("A grade");
}else if(marks>=90 && marks<100){
printf("A+ grade");
}
else
{
printf("Invalid!");
}
}
#include <stdio.h>
/* function to determine if input is a vowel */
main ()
{
char in_chr;
puts ("Enter a character in lower case:”);
in_chr = getchar ();
fflush (stdin);
if(in_chr== ‘a’)
puts ("Vowel a");
else if (in_chr == ‘e')
puts ("Vowel e");
else if (in_chr == ‘i')
puts ("Vowel i");
else if (in_chr == ‘o')
puts ("Vowel o");
else if (in_chr == ‘u')
puts ("Vowel u");
else
puts(“The character is not a vowel”);
The switch construct
The switch statement in C is an alternate to if-
else-if ladder statement which allows us to
execute multiple operations for the different
possible values of a single variable called switch
variable.
case value2:
//code to be executed;
break; //optional
......
default:
code to be executed if all cases are not matched;
}
Rules for switch statement in C language
Example:
#include<stdio.h>
main()
{
int number=13;
//Using ternary operator
(number%2==0)?puts("even number"):puts("odd
number");
}
C Program to swap two numbers
int main()
{
int a = 5, b = 10, temp;
temp = a;
a = b;
b = temp;
return 0;
}
Left Shift(<<) and Right
Shift(>>) Operators in C
In C, left shift (<<) and right shift (>>)
operators are binary bitwise operators that
are used to shift the bits either left or right
of the first operand by the number of
positions specified by the second operand
allowing efficient data manipulation.
Left Shift (<<) Operators
The left shift(<<) is a binary operator that
takes two numbers, left shifts the bits of the
first operand, and the second operand decides
the number of places to shift.
Syntax
varName << no_of_position
a << b;
where,
a is the integer value to be shifted.
b specifies how many positions to shift the bits.
Example:
Let’s take a=100; which is 01100100 in Binary
Form.
Now, if “a is left-shifted by 2”
scanf("%d", &y);
printf("After performing left shift operation : ");
Syntax
operand >> number
a >> b;
where,
a is the integer value to be shifted.
b specifies how many positions to shift the bits.
Example:
let’s take a=8;
which is 1000 in Binary Form. Now, if “a is
right-shifted by 2”
i.e a=a>>2 then a will become a=a/(2^2).
scanf("%d", &y);
printf("After performing Right shift operation : ");
For example,
if we need to print ‘Hello Friends’ 10-
times then, instead of using the printf
statement 10 times, we can use printf
once inside a loop which runs up to 10
iterations.
What are the advantages of Looping?
Increment/decrement
while loop in C
The while loop in c is to be used in the scenario
where the block of statements is executed in
the while loop until the condition specified in
the while loop is satisfied. It is also called a
pre-tested loop.
initialisation;
while(condition)
{
block of statements to be executed ;
increment ;
}
Flowchart of while loop
Write a C-program to print 10 natural
numbers
#include<stdio.h>
main()
{
int i=1;
while(i<=10)
{
printf("%d\n",i);
i=i+1;
}
}
Program to print table for the given
number using while loop in C
#include<stdio.h>
main()
{
int i=1,number,b;
printf("Enter a number:");
scanf("%d",&number);
while(i<=10)
{
b=number*i;
printf("%d\n", b);
i=i+1;
}
}
The following function shows the use of the while
construct in accepting an input as y or n only. Any other
input is rejected by the function until a valid input is
provided. Consider the following example:
#include<stdio.h>
main()
{
char inp, valid=’n’;
while(valid==’n’)
{ /*repeat as long as condition is TRUE*/
valid=’ ’;
puts(“Enter y or n”);
inp=getchar();
fflush(stdin);
if(inp==’y’)
puts(“Valid input”);
else if(inp==’n’)
puts(“Valid input”);
else
{
valid=’n’;
puts(“Invalid data”);
}
}
}
The following function uses a while construct to display menu and to
accept and display menu choices until a valid choice has been entered:
#include<stdio.h>
/* function to generate a menu, to accept and display */
/* choice using while loop construct */
main ()
{
char chc, ok; ok = ‘y’ ;
switch (chc)
{
case ‘1’: puts (“Choice is 1”); break;
case ‘2’: puts (“Choice is 2”); break;
case ‘3’: puts (“Choice is 3”); break;
case ‘4’: puts (“Choice is 4”); break;
default: puts(“Invalid Choice”);
ok=’n’;
} /*end of switch block*/
} /*end of while block*/
}
The following function draws a box with a dimension
of 5 rows with side walls using a while statement:
#include<stdio.h>
/*function to draw a box of 5 */
main()
{
int row=0;
puts("---------"); /*Top line*/
while(row<5)
{
puts("| |");
/*Ten lines with side boundary*/
row=row+1;
}
puts("---------"); /*bottom line*/
}
Write a program in C to accept a number and revere it..
#include <stdio.h>
int main()
{
int n, r = 0;
printf("Enter a number to reverse\n");
scanf("%d", &n);
fflush(stdin);
return 0;
}
Armstrong Number in C
Armstrong number is a number that is equal to the
sum of cubes of its digits. For example 0, 1, 153, 370,
371 and 407 are the Armstrong numbers.
153 = (1*1*1)+(5*5*5)+(3*3*3)
where:
(1*1*1)=1
(5*5*5)=125
(3*3*3)=27
So:
1+125+27=153
#include<stdio.h>
int main()
{
int n,r,sum=0,temp;
printf("enter the number=");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(temp==sum)
printf("armstrong number ");
else
printf("not armstrong number");
return 0;
}
do-while loop in C
The do...while loop construct, like the while loop
construct, is used in situations where the number
of iterations to be performed is not known, and is
dependent on a condition, which changes within the
loop-body.
do
{
code to be executed ;
increment;
} while(condition);
difference between the while loop and
the do...while loop
The following program uses the do...while loop for the menu
program written earlier using the while loop:
#include<stdio.h>
/* function to generate a menu, to accept and display */
/* choice using while loop construct */
main ()
{
char chc, ok;
do
{ /* beginning of do….while block */
ok='y'; /*initialize ok*/
puts("Menu");
puts("1. Create a Directory");
puts("2. Delete a Directory");
puts("3. Show a Directory");
puts("4. Exit");
puts(" "); /* display a blank line*/
chc = getchar();
fflush(stdin);
switch (chc)
{
case '1': puts ("Choice is 1"); break;
case '2': puts ("Choice is 2"); break;
case '3': puts ("Choice is 3"); break;
case '4': puts ("Choice is 4"); break;
default: puts("Invalid Choice");
ok='n';
} /*end of switch block*/
} while(ok=='y'); /*end of while block*/
}
Exercise
1) Write a function to accept characters from
the keyboard until the character ‘!’ is input,
and to display whether the total number of
non-vowel characters entered is more than,
less than, or equal to the total number of
vowels entered.
#include <stdio.h>
int main() {
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
// 0 and 1 are not prime numbers change flag to 1 for non-prime number
if (n == 0 || n == 1)
flag = 1;
Initialization Expression
Test Expression
Increment/Decrement Expression
How does a For loop work?
Control falls into the for loop.
1. Initialization is done
2. The flow jumps to Condition
3. Condition is tested.
1. If the Condition yields true, the flow goes into the
Body
2. If the Condition yields false, the flow goes outside
the loop
4. The statements inside the body of the loop get
executed.
5. The flow goes to the Increment/Decrement Exp
7. The for loop has ended and the flow has gone
outside.
The following function uses a for loop
to accept a string and display 10 times
timesstrings entered from the
#include<stdio.h>
/*function
keyboard:to accept and display 10 strings*/
main()
{
char str1[50]; int times;
for(times=0;times<10;times=times+1)
{
puts(“Enter string”);
gets (str1);
fflush (stdin);
puts(str1);
}
}
int main() {
int i, n, t1 = 0, t2 = 1, nextTerm;
return 0;
}
Practice
1) Write a function to accept five characters
from the character set, and to display
whether the number of lower-case
characters is greater than, less than, or
equal to number of upper-case characters.
Display an error message if the input is not
an alphabet.
Syntax1 | Syntax2
----------------------------
goto label; | label:
. | .
. | .
. | .
label: | goto label;
int main()
{
int num = 26;
checkEvenOrNot(num);
return 0;
}
Type 2:
In this case, we will see a situation similar to as
shown in Syntax2 above. Suppose we need to
write a program that prints numbers from 1 to
10 using the goto statement. The below program
explains how to do this.
// C program to print numbers from 1 to 10 using goto statement
#include <stdio.h>
int main()
{
printNumbers();
return 0;
}
Disadvantages of Using goto Statement
void main()
{
int x=10; //local variable (also automatic)
auto int y=20; //automatic variable
}
Static Variable in C
Static in C is a keyword to indicate a particular
memory location that remains occupied during the
program’s lifetime. In essence, defining something as
static makes it available throughout the entire
program, which is particularly useful when wanting to
keep values persistent between function calls.
1. Static variable
Syntax
Output
int function()
{
static int count=0;
count++;
return count;
}
2. Static Function
It is a function declared with a static keyword.
Its lifetime is throughout the program. The static
function can be accessed within a file only.
Example :
#include<stdio.h>
int main() {
function();
return 0;
}
Extern Variable in C
External or global variables are declared using
the ‘extern’ keyword. You can declare an
external variable any number of times but, the
value can be assigned only once. Their default
value is 0 or null.