You are on page 1of 6

BASICS OF C

Variables
● A variable in C is a storage place which has some memory allocated to it.
Basically, a variable used to store some form of data.
● Types of Variables in C
1. Local Variable
❖ A variable that is declared and used inside the function or block is
called a local variable.
❖ Its scope is limited to a function or block.
2. Global Variable
❖ A variable that is declared outside the function or block is called a
global variable.
❖ It is declared at the starting of program
3.Static Variable
❖ A variable that retains its value between multiple function calls is
known as a static variable.

Keywords
● Keywords are specific reserved words in C each of which has a specific
feature associated with it
● There are a total of 44 keywords in C:
Data types:
Some very common data types in C:
● char: The most basic data type in C. It stores a single character and
requires a single byte of memory in almost all compilers.
● int: Used to store an integer.
● float: It is used to store decimal numbers (numbers with floating point
value) with single precision.
● double: It is used to store decimal numbers (numbers with floating point
value) with double precision.

Looping Statements in C
They execute the sequence of statements many times until the stated condition
becomes false. A loop in C consists of two parts, a body of a loop and a control
statement.

Functions
● A function is a set of statements that take inputs, do some specific
computation and produce output.
● Avoid the use of repeated code for same task

Static functions:
❖ In C, functions are global by default. The “static” keyword before a
function name makes it static.
❖ Unlike global functions in C, access to static functions is restricted to
the file where they are declared. Therefore, when we want to restrict
access to functions, we make them static. Another reason for
making functions static can be the reuse of the same function name
in other files.

1.Program to generate first n integers:

int main()
{

int n,i;
printf("Enter a number:");
scanf("%d",&n);

printf("First %d natural numbers are:\n",n);


for(i=0;i<=n;i++)
{
printf("%d ",i);
}
getch();
return 0;
}

2.Program to generate first n prime numbers:

int main()
{
int n, count=1, flag, i=2, j;
printf("Enter how many prime numbers? \n");
scanf("%d", &n);
/* Generating prime numbers */
while(count <= n)
{
flag = 0;
for(j=2; j <= i/2; j++)
{
if(i%j==0)
{
flag=1;
break;
}
}
if(flag==0)
{
printf("%d\t",i);
count++;
}
i++;
}
getch();
return 0;
}

3.Program to check whether a number is prime or not:

#include <stdio.h>
#include <stdlib.h>

int main()
{
int p,i,m=0,flag=0;
printf("Enter the number to check prime:");
scanf("%d",&p);
m=p/2;

for(i=2;i<=m;i++){
if(p%i==0)
flag =1;
break;

}
if (flag == 0)
printf("%d is a prime number.", p);
else
printf("%d is not a prime number.", p);

return 0;
}

4.Program to generate sum of first n numbers:

#include <stdio.h>
#include <stdlib.h>

int main()
{
int num, i, sum = 0;
printf(" Enter a positive number: ");
scanf("%d", &num);

for (i = 0; i <= num; i++)


{
sum = sum + i;
}
printf("\n Sum of the first %d number is: %d", num, sum);
getch();
return 0;
}

You might also like