0% found this document useful (0 votes)
29 views22 pages

Introduction To C Upto Function

The document discusses various fundamental concepts in C programming language including: 1. Tokens are the basic building blocks in C and programs are constructed using a combination of tokens. Data types specify the type of data a variable can store. 2. Key concepts like variables, constants, identifiers, keywords, input/output statements and operators are explained. 3. Control flow statements like branching, loops and jumps are covered which allow altering the flow of a program based on conditions.

Uploaded by

Naveen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views22 pages

Introduction To C Upto Function

The document discusses various fundamental concepts in C programming language including: 1. Tokens are the basic building blocks in C and programs are constructed using a combination of tokens. Data types specify the type of data a variable can store. 2. Key concepts like variables, constants, identifiers, keywords, input/output statements and operators are explained. 3. Control flow statements like branching, loops and jumps are covered which allow altering the flow of a program based on conditions.

Uploaded by

Naveen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Developed by Dennis Ritchie for the Unix operating system at

Bell Labs in 1972.


It was first implemented on the Digital Equipment Corporation
PDP-11 computer.
Tokens are the basic building blocks in C Language. The program is
constructed using a combination of these tokens.
A data type specifies the type of data that a variable can store
such as integer, floating, character, etc.

Example: Literals
int number=90;
int x=071;
int z=Ox1AB;
float=89.7;
char ch=‘y’;
char c[]=“raj”
Variable: The value of the variable is changed during
Identifiers Rules the program execution.
• Identifier has to begin with a Syntax: Data type variable_name = value;
letter or underscore _ Example: int age=20;
age=20; //modified
followed by alphanumeric
Constant: The value of the variable can not be changed
characters i.e fixed
• Cannot use a keyword as Syntax: const datatype constant_name=value;
identifiers. Example: const float PI=3.14;
• It should not contain white Symbolic Constant: using pre-processor directives
space and special characters. Syntax: #define CONSTANT_NAME=value
• case sensitive Example: #define PI=3.14
Enumeration:
• Identifier names are unique. An enumeration type (also called enum) is a data type that
• Identifiers should be written consists of integral constants. (Attaching name to number)
Syntax: enum enum_name{const1, const2=value, const3, ... };
in such a way that it is default const1 has the value of 0
meaningful, short, and easy const2 has assigned value
const3 has the value is const2+1 (next value of const2)
to read. Example: enum week{sun,mon,tue,wed,thu,fri,sat}
Keywords are reversed in programming language. It is used to constructed the program
Input and Output Statement in C : Input-Output simply means to give some
data or information to the computer so that it can perform various types of
actions and produces the desired result, and this result is termed as output
Operators are used to perform operations on variables and values.
#include<stdio.h>
void main()
{
int a,b;
printf(“enter the a & b”);
scanf(“%d%d”,&a,&b);
printf(“%d”,a+b)
}
Note: X=6, and Y=12 binary value respectively X=0110 ,
Y=1100

Operators Meaning Examples


& Bitwise AND X & Y = 0000 1000
| Bitwise OR X | Y = 0000 1110
^ Bitwise exclusive OR X ^ Y = 0000 1010
~ complement ~X = 1111 1001
<< Shift left X << 2 = 0001 1000
>> Shift right Y >> 1 = 0000 0110
Branching statements allow the flow of execution to jump to a different
part of the program. i.e program execution flow is altered based on the
condition.
#include<stdio.h>
#include<stdio.h> #include<stdio.h>
void main()
void main() void main()
{
{ {
float salary,hike=20;
int n; int a,b,c;
printf("enter the salary");
printf("Read N"); printf("Read a,b,c");
scanf("%f",&salary);
scanf("%d",&n); scanf("%d%d%d",&a,&b,&c);
if(salary > = 40000){
if(n%2==0) if(a>b && a>c)
float bonus=salary*hike/100.0;
printf("%d is Even",n); printf("A is Big %d",a);
salary=salary+bonus;
else else if(b>c)
}
printf("%d is Odd",n); printf("B is Big %d",b);
printf("%f",salary);
} else
}
printf("C is Big %d",c);
}
#include <stdio.h>
#include<stdio.h> int main() {
void main() int day; case 6:
{ printf("Read day Number"); printf("Saturday");
char cs[10]; scanf("%d",&day); break;
int age; switch (day) { case 7:
printf("Read Citizenship"); case 1: printf("Sunday");
scanf("%s",&cs); printf("Monday"); break;
printf("Read age"); break; default:
scanf("%d",&age); case 2: printf("invalid");
if(cs=="indian"){ printf("Tuesday"); }
if(age>=18) break; }
printf("Eligible for vote"); case 3:
else printf("Wednesday");
printf("Not Eligible"); break;
} case 4:
else printf("Thursday");
printf("Citizenship not matched"); break;
} case 5:
printf("Friday");
break;
Iteration (Looping) is used to execute the block of code several
times according to the condition given in the loop.

#include<stdio.h>
#include<stdio.h>
#include<stdio.h> void main()
void main()
void main() {
{
{ int N; int i=1,N;
int i=1,N;
printf(“enter the N ”); printf(“enter the N ”);
printf(“enter the N ”);
scanf(“%d”,&N); scanf(“%d”,&N);
scanf(“%d”,&N);
for(int i=1;i<=N;i++) while(i<=N){
do{
{ printf(“%d”,i);
printf(“%d”,i);
printf(“%d”,i); i++;
i++;
} }
}while(i<=N);
} }
}
Break Statement: Leave the current block that is breaks/terminates the current
Loop. Also used to leave case statement in switch
Continue Statement: It terminates the current iteration and transfer the control to
the next iteration in loop.
Goto Statement: goto statement is used for altering the normal
sequence of program execution by transferring control to some other
part of the program.(Forward/Backward Jump)

#include <stdio.h>
#include <stdio.h>
void main() {
void main()
int sum=0,n;
{
printf("Sum of First N/2 Numbers?");
int num,i=1;
scanf("%d",&n);
printf("Which table to be Print?");
for(int i = 1; i <= n/2 ; i++){
scanf("%d",&num);
sum = sum+i;
table:
if(i == 5) //stop calculation
printf("%d x %d = %d\n",num,i,num*i);
goto addition;
i++;
}
if(i<=10)
addition:
goto table;
printf("%d", sum);
}
}
Storage Class in C: A storage class defines the scope (visibility) and life-time of
variables and/or functions within a C Program.
Global Scope (extern): Can be accessed anywhere in a program
Block Scope / Local Scope: A Block is a set of statements enclosed within left and right braces
({ and } respectively)
• The auto storage class is the default storage class for all local variables
• The static storage class instructs the compiler to keep a local variable in existence during
the life-time of the program instead of creating and destroying it each time it comes into and
goes out of scope.
• The register storage class is used to define local variables that should be stored in a register
instead of RAM
example:
auto int mark=75;

extern int interest=100;

static int test;

register int i=0;


#include<stdio.h> #include<stdio.h> CODE DECLARE DEFINE INITIALIZ
void main() void main() E

{ { extern int a; Yes No No


auto int a=10,b=a; int N;
printf(“enter the N ”); int a; Yes Yes No
printf(“%d”,a+b)
} scanf(“%d”,&N); int a = 1; Yes Yes Yes
for(register int i=1;i<=N;i++)
{ file1.h
printf(“%d”,i); extern int opengenus;
}
#include<stdio.h>
} og.c
int inc();
#include "file1.h"
void main()
#include <stdio.h> int opengenus = 1;
{
extern int x = 32;
printf(“%d”,inc());
int b = 8; Main.c
printf(“%d”,inc());
void main() { #include "file1.h"
printf(“%d”,inc());
auto int a = 28; #include <stdio.h>
}
extern int b; void main(void)
int inc()
printf("%d", a); {
{
printf("x and b : %d,%d",x,b); printf("%d", opengenus);
static int i=0;
x = 15; }
return ++i;
printf(" x : %d",x);
}
} Compile: gcc Main.c og.c
A function is a block of statements that performs a specific task. Break up
complex problem into sub problems. Functions are used to modularize the
program. It provides the reusability
Functions are broadly classified into two types which are as follows −
1. Predefined or Library or Built-in functions
2. User Defined functions

•Predefined or Library functions are already defined in the system libraries. Programmer can
reuse it and helpful to write error free code
#include<stdio.h>
#include<math.h>
void main (){
int x,y;
printf ("enter a number");
scanf ("%d", &x)
y = pow(x,3);
printf("cube = %d", y);
}
•User Defined Functions: These functions must be defined by the
programmer or user.
Function and its Types:
1. Functions with no arguments and no return values.
2. Function with no arguments and return values
3. Functions with arguments and no return values.
4. Functions with arguments and return values

#include<stdio.h>
#include<stdio.h> #include<stdio.h> #include<stdio.h>
int add(int a,int b);
void add(); int add(); void add(int,int);
void main()
void main() void main() void main()
{
{ { {
int a,b;
add(); int r=add(); int a,b;
printf(“enter the a & b”);
} printf(“%d”,r); printf(“enter the a & b”);
scanf(“%d%d”,&a,&b);
void add() } scanf(“%d%d”,&a,&b);
int r=add(a,b);
{ int add() add(a,b);
printf(“%d”,r);
int a,b; { }
}
printf(“enter the a & b”); int a,b;
scanf(“%d%d”,&a,&b); printf(“enter the a & b”); void add(int a, int b)
int add(int a, int b)
printf(“%d”,a+b) scanf(“%d%d”,&a,&b); {
{
} return a+b; printf(“%d”,a+b)
return a+b;
} }
}
Recursive Function: making a function call itself In an inline function, a function call is replaced by the actual
program code. Most of the Inline functions are used for small
computations.

inline returntype functionname(int a, int b) //inline function


{

return statments;
}
#include<stdio.h>
int factorial(int n);
void main() {
#include<stdio.h>
int n,fact;
inline int add(int a,int b){
printf("\n Enter the number: ");
return a+b;
scanf("%d",&n);
}
fact = factorial(n);
void main()
printf("\n The factorial of the number %d is %d",n,fact);
{
}
int a,b;
int factorial(int n) {
printf(“enter the a & b”);
if(n==0 || n==1)
scanf(“%d%d”,&a,&b);
return 1;
int r=add(a,b);
else
printf(“%d”,r);
return(n * factorial(n-1));
}
}
#include<stdio.h> #include<stdio.h>
void swap(int,int); void swap(int *a,int *b);
void main() void main()
{ {
int a,b; int a,b;
printf(“enter the a & b”); printf(“enter the a & b”);
scanf(“%d%d”,&a,&b); scanf(“%d%d”,&a,&b);
printf(“A is %d B is %d”,a,b); printf(“A is %d B is %d”,a,b);
swap(a,b); swap(&a,&b);
printf(“A is %d B is %d”,a,b);
} }
void swap(int a, int b)
{ void swap(int *a, int *b)
int t=a; {
a=b; int t=*a;
b=t; *a=*b;
printf(“A is %d B is %d”,a,b); *b=t;
} }

You might also like