0% found this document useful (0 votes)
44 views109 pages

C Programming

The document provides a comprehensive overview of the C programming language, covering fundamental concepts such as data types, operators, statements, and control structures. It includes examples of C code, explanations of various data types, and the use of macros, along with quiz-like questions to test understanding. Additionally, it discusses the void type and provides sample programs for converting units and printing outputs.
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)
44 views109 pages

C Programming

The document provides a comprehensive overview of the C programming language, covering fundamental concepts such as data types, operators, statements, and control structures. It includes examples of C code, explanations of various data types, and the use of macros, along with quiz-like questions to test understanding. Additionally, it discusses the void type and provides sample programs for converting units and printing outputs.
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/ 109

C PROGRAMMING

 Fundamentals And Basic Idea About C Language


 Variables, Data Types, Keywords And Storage
Classes
 Operators In C
 Statements In C
 Pointers In C
 Arrays In C
 Strings In C
 Functions In C
Data Types in C

.
• There are the following data types in C language.
Types Data Types

Basic Data Type int, char, float, double

Derived Data Type array, pointer, structure, union

Enumeration Data Type enum

Void Data Type void


• A 10-bit unsigned integer has the following range:
– 0 to 1000
– 0 to 1024
– 0 to 1023
– 0 to 1025

• The format identifier ‘%i’ is also used for _____ data type?
– A. char
– B. int
– C. float
– D. double

• Which data type is most suitable for storing a number 65000 in a 32-bit system?
– A. signed short
b) unsigned short
c) long
d) int
• What is the size of an int data type?
– A. 4 Bytes
– B. 8 Bytes
– C. Depends on the system/compiler
– D. Cannot be determined.
• #include<stdio.h>
int main()
{
char num = ‘\010';
printf("%d", num);
return 0;
}
A. 010
B. 08
C. 10
D. 8
• What will be the output of the C program?
#include<stdio.h>
int main()
{
printf("%d\t",sizeof(2.5));
printf("%d\t",sizeof(2));
printf("%d",sizeof('A'));
return 0;
}
A. 8 4 2
B. 8 4 1
C. 4 4 1
D. 2.5 2 A
If char=1, int=4, and float=4 bytes size, What will be the output of the program ?
#include<stdio.h>

int main()
{
char ch = 'A';
printf("%d, %d, %d", sizeof(ch), sizeof('A'), sizeof(3.14f));
return 0;
}
1, 2, 4
1, 4, 4
2, 2, 4
2, 4, 8
• #include<stdio.h>
int main(){
signed a;
unsigned b;
a = 6u + -16 + 16u + -6;
b = a + 1;
if(a == b)
printf("%d %d",a,b);
else
printf("%u %u",a, b);
return 0;
}
A. Compilation error
B. 0 0
C. 0 1
D. address address
• What is short int in C programming?
– a) Basic data type of C
b) Qualifier
c) short is the qualifier and int is the basic datatype
d) All of the mentioned

• #include<stdio.h>
int main(){
int num = - -2;
printf("num = %d", num);
return 0;
}
A. Runtime error
B. Compilation error
C. -2
D. 2
• #include<stdio.h>
int main()
{
10;
printf("%d", 10);
}
A. Compilation Error
B. 10
C. Runtime error
D. No output
• What will be the output of the C program?
#include<stdio.h>
int main()
{
enum fruits{ apple, mango } ;
printf("%d %d", apple, mango);
return 0;
}
A. Compilation error
B. 1 2
C. 0 1
D. Runtime error
• What will be the output of the C program?
#include<stdio.h>
int main()
{
printf(" %%% ");
return 0;
}

A. %
B. No output
C. %%
D. %%%
• #include<stdio.h>
int main()
{
void num=10;
printf("%v", num);
return 0;
}
A. Compilation error
B. 10
C. Garbage value
D. 0
The Void type
The void type specifies that no value is available. It is used in three kinds of situations:
1. Function returns as void
2. Function arguments as void
3. Pointers to void
Void is not a valid data type for declaring variables.
Operators
• What will be the output of the C program?
#include<stdio.h>
int main(){
int i = 16;
i =! i > 15;
printf("i = %d",i);
return 0;
}
A. 16
B. 1
C. 0
D. Compilation error
• What will be the output of the C program?
#include<stdio.h>
int main() {
int num = 8;
printf ("%d %d", num << 1, num >> 1);
return 0;
}
A. 7 9
B. 4 16
C. 9 7
D. 16 4
• What will be the output of the C program?

#include<stdio.h>
int main()
{
int a = 5;
a = 1, 2, 3;
printf("%d", a);
return 0;
}
A. 3
B. 5
C. compilation error
D. 1
• What will be the output of the C program?
#include<stdio.h>
int main() {
int a;
a = (1, 2, 3);
printf("%d", a);
return 0;
}
A. 2
B. 3
C. 7
D. 1
• What will be the output of the C program?

#include<stdio.h>
int main()
{
unsigned int num = 4;
printf("%d", ~num);
return 0;
}
A. Compilation error

B. 3

C. -5

D. some garbage value


• What will be the output of the C program?

#include<stdio.h>
int main()
{
int x = 2;
(x & 1) ? printf("true") : printf("false");
return 0;
}
A. Compilation error

B. true

C. false

D. Runtime error
• What will be the output of the C program?

#include<stdio.h>
int main()
{
int a = 4, b = 2;
printf("a^b = %d", a^b);
return 0;

}
A. 12

B. 10

C. 8

D. 6
• What will be the output of the C program?

#include<stdio.h>
int main()
{
int a = 4, b = 2;
printf("a|b = %d\n", a|b);
return 0;
}
A. 6

B. 7

C. 5

D. 4
• What will be the output of the C program?

#include<stdio.h>
int main()
{
int a = 7, b = 4, c = 2;
printf("a|b&c = %d\n", a|b&c);
return 0;
}
A. 3

B. 8

C. 6

D. 7
• What will be the output of the C program?

#include<stdio.h>
int main()
{
int a = NULL - true;
printf("%d",a);
return 0;
}
A. Compilation error

B. 1

C. -1

D. Runtime error
What will be the output of the C program?
#include<stdio.h>
int main()
{
int i = 5;
int a = ++i + ++i;
printf("%d",a);
return 0;
}

A. 14 B. 13 C. 12 D. 11

1.Pre inc/dec
2.Substitution
3.Evaluation
4.Assignment
5.Post inc/dec
What will be the output of the C program?
#include<stdio.h>
int main()
{
int i = 5;
int a = ++i + ++i + ++i;
printf("%d",a);
return 0;
}
A. 24
B. 23
C. 21
D. 22
What will be the output of the C program?

#include<stdio.h>
int main()
{
int i = 5;
int a = --i + --i;
printf("%d",a);
return 0;
}
A. 8

B. 5

C. 7

D. 6
What will be the output of the C program?

#include<stdio.h>
int main()
{
int a = 2, b = 2, c = 0, d = 2, m;
m = a++ && b++ && c++ || d++;
printf("%d %d %d %d %d",a, b, c, d, m);
return 0;
}
A. Compilation error

B. 3 3 1 3 1

C. 3 3 1 3 0

D. some garbage value


What will be the output of the C program?

#include<stdio.h>
int main()
{
int i = 5;
int a = --i + ++i - i-- + --i;
printf("%d",a);
return 0;
}
A. 7

B. 9

C. 8

D. 10
What will be the output of the C program?

#include<stdio.h>
int main()
{
int a = 1, b = 3, c;
c = b << a;
b = c * (b * (++a)--);
a = a >> b;
printf("%d",b);
return 0;
}
A. 36

B. Compilation error

C. 30

D. 24
What will be the output of the C program?

#include<stdio.h>
int main()
{
int i = 10;
i++;
i * i;
printf("%d\n",i);
return 0;
}
A. 121

B. 100

C. 10

D. 11
Write a program to read mass in kilograms
(kg) which is in the format of int. Convert the
mass entered in kilograms into pounds(lb),
prints the result to the console as shown in
the example.

Sample Input and Output:Enter the weight in


kg: 120 The weight in lb is 264.0
Hint: The formula for conversion is lb = 2.2 *
kilograms.
Write a program to read temperature in Celsius and
print the temperature in fahrenheit.

Hint: The formula for conversion is F = 1.8 *


Temperature_in_Celsius + 32.0.

During execution, the program should print the


message on the console as:Enter the temperature
in celsius: For example, if the user gives the input
as:Enter the temperature in celsius: 56The
program should print the result as:The
temperature in fahrenheit is 132.8
Statements
• Expression Statements.
• Compound Statements.
• Selection Statements.
• Iterative Statements.
• Jump Statements.
Control Statements
What will be the output of the C program?
#include<stdio.h>
int x = 0;
int main(){
if(x == x)
printf("hai this is if");
else
printf("hai this is else");
return 0;
}
A. hai this is if
B. hai this is else
C. prints nothing
D. Compile Time Error
What will be the output of the C program?
#include<stdio.h>
#define FALSE -1
#define NULL 0
#define TRUE 1

int main(){
if(NULL)
printf("NULL");
else if(FALSE)
printf("TRUE");
else
printf("FALSE");
return 0;
}
A. FALSE
B. NULL
C. TRUE
D. Compilation Error
Macros and its types
A macro is a piece of code in a program that is replaced by the value of the macro.
Macro is defined by #define directive. Whenever a macro name is
encountered by the compiler, it replaces the name with the definition of the
macro. Macro definitions need not be terminated by a semi-colon(;).
Types Of Macros
• Object-like Macros-An object-like macro is a simple identifier that will be
replaced by a code fragment.
• Chain Macros-Macros inside macros are termed as chain macros. In chain
macros first of all parent macro is expanded then the child macro is expanded.
#define INSTAGRAM FOLLOWERS
#define FOLLOWERS 138
int main()
{ printf(“hello",INSTAGRAM);
return 0;
}

• Multi-line Macros- An object-like macro could have a multi-line. So to create a


multi-line macro you have to use backslash-newline.
#define ELE 1, \
2, \
3
int main()
{

int arr[] = { ELE };


printf("Elements of Array are:\n");
for (int i = 0; i < 3; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Function-like Macro: These macros are the same as a function call. It replaces
the entire code instead of a function name. Pair of parentheses immediately
after the macro name is necessary. If we put a space between the macro
name and the parentheses in the macro definition, then the macro will not
work.
#define min(a, b) (((a) < (b)) ? (a) : (b))
int main()
{ int a = 18;
int b = 76;

printf("Minimum value between"


" %d and %d is %d\n",
a, b, min(a, b));

return 0;
}
Macros are abbreviations for lengthy and frequently used
statements. When a macro is called the entire code is
substituted by a single line though the macro definition is of
several lines.
What will be the output of the C program?
#include<stdio.h>
int main(){
int i = 0, j = 0;
if(i++ == j++)
printf("%d %d", i--, j--);
else
printf("%d %d", i, j);
return 0;
}

A. 0 0
B. 0 1
C. 1 0
D. 1 1
What will be the output of the C program?
#include<stdio.h>
int main(){
int i = 0, j = 1, k = 0;
if(++k, j, i++)
printf("%d %d %d", i, j, k);
return 0;
}
A. Prints Nothing
B. 1 1 0
C. 0 1 0
D. Compilation Error
What will be the output of the C program?
#include<stdio.h>
int main(){
int i;
if(true)
printf("This will work");
else
printf("This will not work");
return 0;
}
A. This will work
B. This will not work
C. Compilation Error
D. Runtime Error
What will be the output of the C program?
#include<stdio.h>
int main()
{
char str[] = "\0";
if(printf("%s",str))
printf("inside if block");
else
printf("inside else block");
return 0;
}
A. inside else block
B. inside if block
C. Compilation Error
D. None of the above
What will be the output of the C program?
#include<stdio.h>
int main()
{
if(printf("0"))
printf("inside if block");
else
printf("inside else block");
return 0;
}
A. inside if block
B. inside else block
C. 0inside else block
D. 0inside if block
. What will be the output of the C program?
#include<stdio.h>
#define NULL 0
int main()
{
if(printf("0") == NULL)
printf("inside if block");
else
printf("inside else block");
return 0;
}
A. 0inside if block
B. 0inside else block
C. None of the above
D. inside if block
What will be the output of the C program?
#include<stdio.h>
int main(){
int i = 5, j = 4;
if(!printf(""))
printf("%d %d", i, j);
else
printf("%d %d", i++, ++j);
return 0;
}
A. 6 5
B. 5 5
C. 5 4
D. 6 4
What will be the output of the C program?
#include<stdio.h>
int main()
{
int i = 1, j = 0 ;
if(i-- == j)
printf("i = %d", --i);
else
printf("j = %d", ++j);
return 0;
}
A. i = 1
B. i = -1
C. i = 0
D. j = 1
What will be the output of the C program?
#include<stdio.h>
int main()
{
int i = 5, j = 5;
if(i == j);
printf("Equal");
else
printf("Not Equal");
return 0;
}

A. Compilation Error
B. Runtime Error
C. Equal
D. Not Equal
What will be the output of the C program?
#include<stdio.h>
int main(){
float me = 5.25;
double you = 5.25;
if(me == you)
printf(“India is great");
else
break;
return 0;
}
A. Prints Nothing
B. India is great
C. Runtime Error
D. Compilation Error
What will be the output of the C program?
#include<stdio.h>
int main(){
int i = 25;
if(i == 25);
i = 50;
if(i == 25)
i = i + 1;
else
i = i + 1;
printf("%d", i);
return 0;
}
A. 50
B. 51
C. 26
D. 27
What will be the output of the C program ?
#include<stdio.h>
int main(){
if("May I Get in")
printf("yes, Get in");
else
printf("No");
return 0;
}

A. Compilation Error
B. No
C. yes, Get in
D. None of above
What will be the output of the C program?
#include<stdio.h>
int main()
{
int i = 5, j = 6, k = 7;
if(i > j == k)
printf("%d %d %d", i++, ++j, --k);
else
printf("%d %d %d", i, j, k);
return 0;
}

A. 5 7 6
B. 5 6 7
C. 6 6 6
D. 5 7 7
What will be the output of the C program?
#include<stdio.h>
int main()
{
int i = 2;
if(i == (1, 2))
printf("Hai");
else
printf("No Hai");
return 0;
}
A. Compilation Error
B. Runtime Error
C. Hai
D. No Hai
Switch
Rainbow Colours
The program must accept a character CH representing the color in a rainbow as the input.
If CH is V, it represents Violet.
If CH is I, it represents Indigo.
If CH is B, it represents Blue.
If CH is G, it represents Green.
If CH is Y, it represents Yellow.
If CH is O, it represents Orange.
If CH is R, it represents Red.
The program must print the corresponding colour of the character as the output. Else the program must
print Invalid as the output.
Example Input/Output 1:
Input:
V
Output:
Violet
Example Input/Output 2:
Input:
L
Output:
Invalid
Day in a Week
The program must accept an integer D representing a day of the week.
The program must print the corresponding readable string value of
the day of the week as the output.
Note: The integer value for Monday is 1, Tuesday is 2 and so on until 7
is for Sunday.
Example Input/Output 1:
Input:
2
Output:
Tuesday
Example Input/Output 2:
Input:
5
Output:
Friday
Loops
• The output of the code below is ?
#include <stdio.h>
int a;
void main()
{
if (a)
printf(“Hello”);
else
printf(“world”);
}
• The C code ‘for(;;)’ represents an infinite loop. It can
be terminated by ______
– 1.break
– 2.exit(0)
– 3.abort()
– 4. terminate
• The correct syntax for running two variable for loop
simultaneously is
– for (i = 0; i < n; i++) for (j = 0; j < n; j += 5)
– for (i = 0, j = 0;i < n, j < n; i++, j += 5)
– for (i = 0; i < n;i++){} for (j = 0; j < n;j += 5){}
– none of the mentioned
• Which for loop has range of similar indexes of
‘i’ used in for (i = 0;i < n; i++)?
– a.for (i = n; i>0; i–)
– b.for (i = n; i >= 0; i–)
– c.for (i = n-1; i>0; i–)
– d.for (i = n-1; i>-1; i–)
• C Program to Print Prime Numbers In A Given Range
Ex:- if the user enters a range as 40 – 50
In that range 41, 43, 47, these three number are prime number.

#include <stdio.h>
void main(){

int a=30,b=50;
for(int i=a;i<=b;i++){
int count=0;
for(int j=2;j<i/2;j++){
if(i%j==0){
count++;
break;
}
}
if(count==0){
printf("%d ",i);
}
}
}

• C Program to Find Sum of Digits of a Number


Ex:- number is 231456
2 + 3 + 1 + 4 + 5 + 6 = 21
Sum of digit of a given number is 21
Storage Classes
Syntax:
storage_class datatype identifier;

Ex:
auto int a;
Default
value,life size type
time,scope,m
emory
allocated
Scope of variable
• Block
• Function
• Program
Int a=10 program scope
Void main(){
int a=20; function scope
{
int a; block scope
Pf(“%d”,a);//garabage value
}
a=a+10;
Pf(“%d”,a);//30
check();
}
Void check(){
Pf(“%d”,a);//10
}
Automatic :
If we not specify any storage class by default automatic
Keyword auto
Memory ram
Default value garbage value
Lifetime&scope always local variable declaration either inside block or
function
Ex
auto in a;error
main(){
auto int a=10;method scope
{
auto int a=10;
pf(“%d”,a);//garbage value
}
pf(“%d”,a);//10
}
Register:
Keyword register
Memory inside cpu registers{we can acess these variables much
faster compare to automatic }
Default value garbage value
Lifetime&scope always local variable declaration either inside block or
function

Ex EXECUTION Harddisk[secondary
Processor memory]
Primary memory Complet main(){
R e prm int a=10, sum=0,i;
E Instruction R fetching for(i=1;i<10;i++) {
auto
G by sum +=i;
a=10,
I instruction A }
i=1
S pf(“%d”,sum);
Sum=0
T M }
register
E a=10, Switchin
R i=1 g time
S Sum=0 taken
FUNCTIONS
• Predefined Functions
• Create a Function
• Call a Function
Different aspects of function calling
• function without arguments and without return
value
• function without arguments and with return value
• function with arguments and without return value
• function with arguments and with return value
Recursion
• Write a C program to check whether a number is
prime, armstrong, perfect number or not using
functions. How to check prime or armstrong or
perfect number in C programming using functions.
Example
Input
Input any number: 11
Output
11 is prime number
11 is not a armstrong number
11 is not a perfect number
1) Fibonacci Series
Write a c program to print fibonacci series without
using recursion and using recursion.
Input: 10
Output: 0 1 1 2 3 5 8 13 21 34
2) Prime number
Write a c program to check prime number.
Input: 44
Output: not prime number
Input: 7
Output: prime number
3) Palindrome number
Write a c program to check palindrome number.
Input: 329
Output: not palindrome number
Input: 12321
Output: palindrome number
Sum of Digits
Write a c program to print sum of digits.
Input: 234
Output: 9
Input: 12345
Output: 15
Reverse Number
Write a c program to reverse given number.
Input: 123
Output: 321
Swap two numbers without using third variable
Write a c program to swap two numbers without
using third variable.
Input: a=10 b=20
Output: a=20 b=10
Print "hello" without using semicolon
Write a c program to print "hello" without using
semicolon
C Program without main() function
Write a c program to print "Hello" without using
main() function.
Pointers
Syn:
datatype *identifier;/datatype* identifier

Types of pointers
1.Typed pointer-points to specific type of data
int* int data
double* double data
2.Untyped pointer- can points to any data[generic pointer]
void* any data
We use 2 operators *,&
&it return address of a particular variable
* returns the value inside a specific address
{
Int i=100;
Int* ptr;
Ptr=&I;
Printf(“%d”,i);
Printf(“%u”,ptr);
Printf(“%u”,&i);
Printf(“%u”,&ptr);
Printf(“%d”,*ptr);
Printf(“%d”,*(&i));
}
Size of pointer
Compiler-type int size ponter size
16 bit 2 bytes 2 bytes
32bit 4 4

Pointers to function
Return-type (*identity)(arg-list)
 Declaration of pointer completely depend on
prototype of function.
Ex:
Int (*ptr)(int,int);
int add(int x,int y){
{ can points to any function Int z=x+y;
Which is taking two int Return z;
}
Arg &return int}
Main(){ frame

Int r1,r2; int add(int x,int y){


Int z=x+y;
Int (*ptr)(int,int);
Return z;
R1=add(10,20); }
R2=multiply(1,2,3);
2046
Pf(r1,r2);
Ptr=&add;//2046 int multiply(int x,int
R1=ptr(3,4); y,int z){
Int z=x*y*z;
Pf(r1); Error-
Return z;
incompitable
Ptr=&multipy; pointer }
arguments
}
1. What is the output of this C code?

int main()
{
char *p = NULL;
char *q = 0;
if (p)
printf(" p ");
else
printf("nullp");
if (q)
printf("q\n");
else
printf(" nullq\n");
}
A. nullp nullq
B. Depends on the compiler
C. x nullq where x can be p or nullp depending on the value of NULL
D. p q
• What is the output of this C code?

int main()
{
int *ptr, a = 10;
ptr = &a;
*ptr += 1;
printf("%d,%d/n", *ptr, a);
}
• A. 10,10
• B. 10,11
• C. 11,10
• D. 11,11
• What is the output of this C code?

int x = 0;
void main()
{
int *ptr = &x;
printf("%p\n", ptr);
x++;
printf("%p\n ", ptr);
}
• A. Same address
• B. Different address
• C. Compile time error
• D. Varies
• What is the output of this C code?

int x = 0;
void main()
{
int *const ptr = &x;
printf("%p\n", ptr);
ptr++;
printf("%p\n ", ptr);
}
• A. 0 1
• B. Compile time error
• C. 0xbfd605e8 0xbfd605ec
• D. 0xbfd605e8 0xbfd605e8
• What is the output of this C code?

void foo(int*);
int main()
{
int i = 10;
foo((&i)++);
}
void foo(int *p)
{
printf("%d\n", *p);
}
• A. 10
• B. Some garbage value
• C. Compile time error
• D. Segmentation fault/code crash
• What is the output of this C code?

void foo(int*);
int main()
{
int i = 10, *p = &i;
foo(p++);
}
void foo(int *p)
{
printf("%d\n", *p);
}
• A. 10
• B. Some garbage value
• C. Compile time error
• D. Segmentation fault
• What is the output of this C code?

void foo(float *);


int main()
{
int i = 10, *p = &i;
foo(&i);
}
void foo(float *p)
{
printf("%f\n", *p);
}
• A. 10.000000
• B. 0.000000
• C. Compile time error
• D. Undefined behaviour
C Double Pointer (Pointer to Pointer)
void main ()
{
int a = 10;
int *p;
int **pp;
p = &a;
pp = &p;
printf("address of a: %x\n",p);
printf("address of p: %x\n",pp);
printf("value stored at p: %d\n",*p);
printf("value stored at pp: %d\n",**pp);
}
Pointer Arithmetic in C
• Increment
• Decrement
• Addition
• Subtraction
new_address= current_address + i * size_of(data type)

int *p;
p=&number;
p=p+1;
Constant Pointers
<type of pointer> *const <name of pointer>;
int a=1;
int b=2;
int *const ptr;
ptr=&a;
ptr=&b;
printf("Value of ptr is :%d",*ptr);
return 0;
Pointer to Constant
const <type of pointer>* <name of pointer>
int a=100;
int b=200;
const int* ptr;
ptr=&b;
*ptr=300;
printf("Value of ptr is :%d",*ptr);
Constant Pointer to a Constant
const <type of pointer>* const <name of the poi
nter>;
int a=10;
int b=90;
const int* const ptr=&a;
*ptr=12;
ptr=&b;
void pointer
void *pointer name;
Null Pointer
int main()
{
int *ptr;
printf("Address: %d", ptr);
printf("Value: %d", *ptr);
return 0;
}
int main()
{
int *ptr=NULL;
if(ptr!=NULL)
{
printf("value of ptr is : %d",*ptr);
}
else
{
printf("Invalid pointer");
}
return 0;
}
Call by value
Void main(){ main {calling}
int a=10,b=20;
pf(“before swap:%d \t %d”,a,b);//10,20 a=10 b=20

Swap(a,b); 2056
2046

control back to main function


pf(“after swap:%d \t %d”,a,b);//10,20
}
Void swap(int x,int y){
Swap{called function}
int temp=x;
X=y; x=10 y=20
Y=temp; 20 10
pf(“after swap:%d \t %d”,x,y);//20,10 2076 2086

} temp=10
2046
Call by Reference
Void main(){ main {calling}
int a=10,b=20;
pf(“before swap:%d \t %d”,a,b);//10,20 a=10 b=20
20 10
Swap(&a,&b); 2056
2046

control back to main function


pf(“after swap:%d \t %d”,a,b);//20,10
}
Void swap(int *x,int *y){
Swap{called function}
int temp=*x;
*X=*y; x=2046 y=2056
*Y=temp;
pf(“after swap:%d \t %d”,*x,*y);//20,10 2076 2086

} temp=10
2046
DMA
#include <stdio.h>
#include<stdlib.h>
int main(){
int n;
scanf("%d",&n);
int *ptr= (int*)malloc(n*sizeof(int));
//printf("%p",ptr);
if(ptr==NULL){
printf("memory not suficient");
exit(0);
}else{
/* for(int i=0;i<n;i++){
printf("%d\t",sizeof(*(ptr+i)));
}*/
printf("enter %d elements\n",n);
for(int i=0;i<n;i++){
scanf("%d",ptr+i);
}
printf("elements are\n");
for(int i=0;i<n;i++){
printf("%d",*ptr+i);
}
}
return 0;
#include <stdio.h>#include<stdlib.h>int main(){ int n,m; scanf("%d",&n); int *ptr= (int*)malloc(n*sizeof(int));
printf("%p",ptr); if(ptr==NULL){ printf("memory not suficient"); exit(0); }else{ /* for(int i=0;i<n;i++)
{ printf("%d\t",sizeof(*(ptr+i))); }*/ printf("enter %d elements\n",n); for(int i=0;i<n;i++)
{ scanf("%d",ptr+i); } printf("elements are\n"); for(int i=0;i<n;i++){ printf("%d",*ptr+i); }
printf("entr no of students extra"); scanf("%d",&m); ptr=realloc(ptr,m*sizeof(int)); printf("%p\n",ptr);
printf("enter details\n"); { // ptr=ptr+n; for(int i=0;i<m;i++){ scanf("%d",ptr+i+n); } }
printf("student details\n"); printf("%p",ptr); for(int i=0;i<n+m;i++){ printf("%d\n",*ptr+i); } } return 0;}
STRINGS
• It’s one dimensional character array
Syntax
Data-type identity[size];
char
Char str[4]=“ece”;

0 1 2 3
e c e \0(ascii=0)
1000 1 2 3

Str
1000
Char str[4]={‘e’,’c’,’e’};
{
Char sname[20];
Pf(“enter u r name”); Here we canot use address
Sf(“%s”,sname);
}
{ It reads total string until u press
enter
Char sname[20];
Enter u r name
Pf(“enter u r name”); Ece a
Sf(“%s”,sname);//gets(name);
%s %s
Pf(“name is %s”,name);//ece
}
String.h predefined string functions

Strlen():length of string excluding \0 character


#include <stdio.h>
#include<string.h>
int main(){
char str[30]="hello";
char *ptr=str;
printf("%ld",strlen(ptr));
return 0;
}
Strrev():reverse a string
strncat():This function concatenates a specified
number of characters from the second string
to the first.
strncmp():This function compares a specified
number of characters between two strings.
Arrays
C Array Declaration
data_type array_name [size];
• // C Program to illustrate the array declaration
#include <stdio.h>

int main()
{

// declaring array of integers


int arr_int[5];

return 0;
}
C Array Initialization
1. Array Initialization with Declaration
data_type array_name [size] = {value1,
value2, ... valueN};
2. Array Initialization with Declaration without
Size
data_type array_name[] = {1,2,3,4,5};
3. Array Initialization after Declaration (Using
Loops)
for (int i = 0; i < N; i++) {
array_name[i] = valuei;
}
• Access Array Elements
array_name [index];
• Update Array Element
array_name[i] = new_value;

C Array Traversal
for (int i = 0; i < N; i++) {
array_name[i];
}

You might also like