0% found this document useful (0 votes)
89 views78 pages

Data and Address

The document contains code snippets demonstrating the use of pointers in C programming. The first snippet declares an integer variable x and prints its value and address. The second snippet declares integer and float pointer variables and assigns the address of x to an integer pointer. It prints the values of x and y and addresses accessed through the pointer. The third snippet declares a void generic pointer and integer and pointer-to-pointer variables. It demonstrates accessing values through pointers and printing pointer sizes.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views78 pages

Data and Address

The document contains code snippets demonstrating the use of pointers in C programming. The first snippet declares an integer variable x and prints its value and address. The second snippet declares integer and float pointer variables and assigns the address of x to an integer pointer. It prints the values of x and y and addresses accessed through the pointer. The third snippet declares a void generic pointer and integer and pointer-to-pointer variables. It demonstrates accessing values through pointers and printing pointer sizes.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 78

/* Data and address */ #include <stdio.

h> main() { int x; x=10; printf("Value of x is %d",x); printf("\nAddress of x is %u",&x); printf("\nValue of x is %d\n",*(&x)); }

"pointer1.c" 10L, 169C written [1011@localhost ]$ gcc pointer1.c [1011@localhost ]$ ./a.out Value of x is 10 Address of x is 3215134992 Value of x is 10 [1011@localhost ]$

/* pointer variables */ #include<stdio.h> main() { int x, y, *iptr; float *fptr; x =125; iptr = &x ; /* iptr points to x */ y = 23; /* fptr = &y; is erroneous */ printf("X value is %d and stored at %u\n", x, &x); printf("Y value is %d and stored at %u\n", y, &y); printf("\nInt pointer holds the address %u\n", iptr); printf("Accessing value thru pointer : %d\n", *iptr); iptr = &y; /* iptr points to y */ printf("\nInt pointer now holds the address %u\n", iptr);

printf("Accessing value thru pointer : %d\n", *iptr); printf("\nSize of int pointer: %d bytes", sizeof(iptr)); printf("\nSize of float pointer: %d bytes", sizeof(fptr)); printf("\n\nAddress of main function is %u\n", main); }

"pointer2.c" [New] 21L, 690C written [1011@localhost ]$ gcc pointer2.c [1011@localhost ]$ ./a.out X value is 125 and stored at 3217977096 Y value is 23 and stored at 3217977092

Int pointer holds the address 3217977096 Accessing value thru pointer : 125

Int pointer now holds the address 3217977092 Accessing value thru pointer : 23

Size of int pointer: 4 bytes Size of float pointer: 4 bytes

Address of main function is 134513540 [1011@localhost ]$

/* pointer variables */ #include<stdio.h> main() { int x=12, *p1, **p2; float z=8.5; void *ptr; /* Generic pointer */ ptr = &x; /* ptr points to x (int) */ ptr = &z; /* ptr points to y (float) */ p1 = &x; p2 = &p1; /* Pointer to pointer */ printf("X value is %d and stored at %u\n", x, &x); printf("\nPointer holds the address %u\n", p1); printf("Accessing value thru pointer : %d\n", *p1); printf("Pointer is stored at location : %u/n", &p1); printf("\nPointer-to-pointer holds the address %u\n", &p2); printf("Accessing value thru ptr-to-ptr : %d\n", **p2); printf("\nSize of ptr-to-ptr: %d bytes\n", sizeof(ptr)); }

"pointer3.c" [New] 19L, 619C written [1011@localhost ]$ gcc pointer3.c [1011@localhost ]$ ./a.out X value is 12 and stored at 3219886300

Pointer holds the address 3219886300 Accessing value thru pointer : 12 Pointer is stored at location : 3219886296/n Pointer-to-pointer holds the address 3219886292 Accessing value thru ptr-to-ptr : 12

Size of ptr-to-ptr: 4 bytes [1011@localhost ]$

# Menu program ch='y' while [ $ch == 'y' ] do echo -e "\tMENU 1. List of files 2. Working Directory 3. Date and Time 4. Users of the system 5. Calendar Enter the option : \c"

read choice case "$choice" in 1) ls -1 ;; 2) pwd ;; 3) date ;; 4) who ;; 5) cal esac echo -n "Do you wish to continue (y/n) : " read ch done

"command2.sh" [New] 23L, 317C written [1011@localhost ]$ sh command3.sh sh: command3.sh: No such file or directory [1011@localhost ]$ sh command2.sh MENU 1. List of files 2. Working Directory 3. Date and Time 4. Users of the system 5. Calendar Enter the option : 4 1019 pts/1 2012-03-30 19:17 (192.168.10.20)

root 1007 1011 1023 1022 1014 1016 1012 1008 1004 1003 1009 1017 1024

:0 pts/2 pts/3 pts/4 pts/5 pts/6 pts/7 pts/8 pts/10 pts/11 pts/12 pts/13 pts/14 pts/16

2012-03-21 13:48 2012-03-30 19:17 (192.168.10.9) 2012-03-30 19:18 (192.168.10.34) 2012-03-30 19:51 (192.168.10.19) 2012-03-30 19:20 (192.168.10.21) 2012-03-30 19:29 (192.168.10.27) 2012-03-30 19:22 (192.168.10.26) 2012-03-30 19:22 (192.168.10.33) 2012-03-30 20:03 (192.168.10.35) 2012-03-30 19:39 (192.168.10.7) 2012-03-30 20:45 (192.168.10.6) 2012-03-30 20:42 (192.168.10.185) 2012-03-30 20:19 (192.168.10.29) 2012-03-30 20:43 (192.168.10.15)

Do you wish to continue (y/n) : n [1011@localhost ]$ sh command2.sh

#! /bin/bash x=`date +%H` mrd=`date +%p` if [ $mrd=='am' ] then if [ $x -le 11 ] then echo "Good Morning" fi else if [ $x -le 2 ] then echo "Good Afternoon" elif [ $x -le 6 ] then echo "Good Evening" else echo "Good Night" fi fi

# Number of days in a month mth=`date +%m` mn=`date +%B` case $mth in 02)echo "February usually has 28 days" echo "If leap year, then it ha 29 days";; 04|06|09|11)echo "The current month $mn has 30 days";; *) echo "The current month $mn has 31 days" Esac

for x in do ls -R $x done

"command4.sh" [New] 5L, 27C written [1011@localhost ]$ sh command4.sh [1011@localhost ]$ [1011@localhost ]$

echo -n "Enter filename :" read fname if [ -e $fname ] then if [ -f $fname ] then echo "Regular file" elif [ -d $fname ]

then echo "Directory" else echo "Special file" fi else echo "File does not exist" fi

"command5.sh" [New] 16L, 206C written [1011@localhost ]$ sh command5.sh Enter filename :samp.c File does not exist [1011@localhost ]$

#!/bin/bash #Detect the user at logon

while true do if who|grep $1>/dev/null then echo $1 is logged in exit fi done

# Duplicate file removal if cmp $1 $2 then echo "Files $1, $2 are identical" rm $2

echo "$2 file deleted" else echo"Files $1, $2 are distinct" fi

"command7.sh" 9L, 146C written [1011@localhost ]$ sh command7.sh cmp: missing operand after `cmp' cmp: Try `cmp --help' for more information. command7.sh: line 8: echoFiles , are distinct: command not found [1011@localhost ]$ 1,1 All

while true do gcc -o $1.out $1.c case "$?" in 0)echo "Executing..." ./$1.out exit;; *) sleep 5 vi $1.c esac done

# Odd or even using if-else echo -n "Enter a non-zero number :" read num rem=`expr $num % 2` if [ $rem -eq 0 ]

then echo "$num is Even" else echo "$num is Odd" fi

"condition1.sh" 10L, 163C written [1011@localhost ]$ sh condition1.sh Enter a non-zero number :12 12 is Even [1011@localhost ]$

# Biggest using logical expression echo -n "Give value for A B and C:" read a b c if [ $a -gt $b -a $a -gt $c ] then echo "A is the Biggest number" elif [ $b -gt $c ] then echo "B is the Biggest number" else echo "C is the Biggest number" fi

"condition2.sh" [New] 12L, 242C written [1011@localhost ]$ sh condition2.sh Give value for A B and C:4 3 4 C is the Biggest number [1011@localhost ]$

# Leap year echo -n "Enter a year :" read year rem1=`expr $year % 4` rem2=`expr $year % 100` rem3=`expr $year % 400` if [ $rem3 -eq 0 ] then echo "$year is a Leap Year" elif [ $rem2 -ne 0 -a $rem1 -eq 0 ] then echo "$year is a Leap Year" else echo "$year is Not a leap year" fi

"condition3.sh" [New] 15L, 278C written [1011@localhost ]$ sh condition3.sh Enter a year :1900 1900 is Not a leap year [1011@localhost ]$

echo -n "Enter the mark:" read mark if [ $mark -gt 90 ] then echo "S Grade" elif [ $mark -gt 80 ] then echo "A Grade" elif [ $mark -gt 70 ] then echo "B Grade"

elif [ $mark -gt 60 ] then echo "C Grade" elif [ $mark -gt 55 ] then echo "D Grade" elif [ $mark -ge 50 ] then echo "E Grade" else echo "U Grade" fi

"condition4.sh" [New] 23L, 309C written [1011@localhost ]$ sh condition4.sh Enter the mark:65 C Grade [1011@localhost ]$

echo -n "Enter the first string :" read s1 echo -n "Enter the second string :" read s2

if [ $s1 == $s2 ] then echo "Strings are the same" else echo "Strings are distinct" fi

"condition5.sh" [New] 10L, 174C written [1011@localhost ]$ sh condition5.sh Enter the first string :ece-a Enter the second string :ECE-A Strings are distinct

[1011@localhost ]$

echo -n "Enter employee basic pay : " read basic if [$basic -gt 30000 ] then hra=`expr 5 \* $basic / 100 ` da=`expr 5 \* $basic / 100` tax=`expr 10 \* $basic /100` elif [ $basic -gt 20000 ] then hra=`expr 4 \* $basic / 100` da=`expr 3 \* $basic / 100` tax=`expr 8 \* $basic /100` else hra=`expr 3 \* $basic / 100` da=`expr 2 \* $basic / 100` tax=`expr 5 \* $basic / 100` fi gross=`expr $basic + $da + $hra` netpay=`expr $gross - $tax` echo "Gross pay : $gross" echo "Net pay : $netpay"

"condition6.sh" [New] 21L, 487C written [1011@localhost ]$ sh condition6.sh Enter employee basic pay : 12000 condition6.sh: line 3: [12000: command not found Gross pay : 12600 Net pay : 12000 [1011@localhost ]$

# Multiplication table using for loop clear echo -n "Which multiplication table? : " read n for x in 1 2 3 4 5 6 7 8 9 10 do p=`expr $x \* $n` echo -n "$n X $x = $p" sleep 1 done

# Multiplication table using for loop clear

Which multiplication table? : 6 6 X 1 = 66 X 2 = 126 X 3 = 186 X 4 = 246 X 5 = 306 X 6 = 366 X 7 = 426 X 8 = 486 X 9 = 546 X 10 = 60[1011@localhost ]$

# Armstrong number using while loop echo -n "Enter a number : " read n a=$n s=0 while [ $n -gt 0 ] do r=`expr $n % 10` s=`expr $s + \( $r \* $r \* $r \)` n=`expr $n / 10` done if [ $a -eq $s ] then

echo "Armstrong Number" else echo -n "Not an Armsrong number" fi

"loop2.sh" 17L, 263C written [1011@localhost ]$ sh loop2.sh Enter a number : 370 Armstrong Number [1011@localhost ]$ 6,19 All

# To reverse a number using while loop echo -n "Enter a number : " read n rd=0 while [ $n -gt 0 ] do rem=`expr $n % 10` rd=`expr $rd \* 10 + $rem` n=`expr $n / 10` done echo "Reversed number is $rd"

"loop3.sh" 11L, 199C written [1011@localhost ]$ sh loop3.sh Enter a number : 234 Reversed number is 432 [1011@localhost ]$ 3,7 All

# Fibonacci series using while loop echo -n "Enter number of terms : " read n echo "Fibonacci series" f1=0 f2=1 echo -n "$f1 " echo -n " $f2 "

i=2 while [ $i -lt $n ] do f3=`expr $f1 + $f2` echo -n " $f3 " f1=$f2 f2=$f3 i=`expr $i + 1` done echo

"loop4.sh" 18L, 247C written [1011@localhost ]$ sh loop4.sh Enter number of terms : 8 Fibonacci series 0 1 1 2 3 5 8 13 [1011@localhost ]$

echo -n "Enter the number : " read n i=2 m=`expr $n / 2` until [ $i -gt $m ] do q=`expr $n % $i` if [ $q -eq 0 ] then echo "Not a prime number" exit fi i=`expr $i + 1` done echo "Prime number"

"loop5.sh" [New] 15L, 193C written

[1011@localhost ]$ sh loop5.sh Enter the number : 17 Prime number [1011@localhost ]$

echo -n "Enter a positive number : " read n f=1 until [ $n -lt 1 ] do f=`expr $f \* $n` n=`expr $n - 1` done echo "Factorial value : $f"

"loop6.sh" [New] 10L, 138C written [1011@localhost ]$ sh loop6.sh Enter a positive number : 10 Factorial value : 3628800 [1011@localhost ]$

echo -n "Enter N value : " read n sum=0

i=1 until [ $i -gt $n ] do sum=`expr $sum + $i` i=`expr $i + 1` done echo "The sum of n numbers is $sum"

"loop7.sh" [New] 10L, 145C written [1011@localhost ]$ sh loop7.sh Enter N value : 26 The sum of n numbers is 351

[1011@localhost ]$

clear pc=0 s=0 i=0 until false do echo -n "Enter a number (9999 to quit) : " read n if [ $n -eq 9999 ] then break

fi i=`expr $i + 1` if [ $n -le 0 ] then continue fi pc=`expr $pc + 1` s=`expr $s + $n` done avg=`expr "scale=2; $s / $pc" | bc` echo "Total No. of entries : $i" echo "No. of positive datas : $pc" echo "Positive aggregate : $avg"

clear pc=0 Enter a number (9999 to quit) : 32 Enter a number (9999 to quit) : 78 Enter a number (9999 to quit) : 0 Enter a number (9999 to quit) : 11 Enter a number (9999 to quit) : 47 Enter a number (9999 to quit) : -9 Enter a number (9999 to quit) : 12 Enter a number (9999 to quit) : 7

Enter a number (9999 to quit) : 9999 Total No. of entries : 8 No. of positive datas : 6 Positive aggregate : 31.16 [1011@localhost ]$

# vowel with multiple values in a pattern echo -n "Key in a lower case charsacter :" read choice case $choice in a|e|i|o|u) echo "It's a vowel";; *) echo "It's a consonant" esac

"multiway1.sh" [New] 7L, 178C written [1011@localhost ]$ sh multiway1.sh Key in a lower case charsacter :e It's a vowel [1011@localhost ]$

#Arithmetic operations--multiple statements in a block echo -n "Enter the two numbers :" read a b echo " 1. Addition" echo " 2. Subtraction" echo " 3. Multiplication" echo " 4. Division" echo " 5. Modulo Division" echo -n "Enter the option : " read option case $option in

1) c=`expr $a + $b` echo "$a + $b = $c";; 2) c=`expr $a - $b` echo "$a - $b = $c";; 3) c=`expr $a \* $b` echo "$a * $b = $c";; 4) c=`expr $a / $b` echo "$a / $b = $c";; 5) c=`expr $a % $b` echo "$a % $b = $c";; *) echo "Invalid option" esac

"multiway2.sh" 23L, 513C written [1011@localhost ]$ sh multiway2.sh Enter the two numbers :2 4

1. Addition 2. Subtraction 3. Multiplication 4. Division 5. Modulo Division Enter the option : 1 2+4=6 [1011@localhost ]$

# String matching echo "Two/Four wheeler rental" echo -n "Enter the required vehicle : " read vehicle case $vehicle in "car") echo "For $vehicle Rs.20 per km";; "van") echo "For $vehicle Rs.10 per km";; "jeep") echo "For $vehicle Rs.5 per km";; "bicycle") echo "For $vehicle 20 paisa per km";; *) echo "Sorry, I cannot get a $vehicle for you";; esac

"multiway3.sh" 11L, 351C written [1011@localhost ]$ sh multiway3.sh Two/Four wheeler rental Enter the required vehicle : bicycle For bicycle 20 paisa per km [1011@localhost ]$

#Vote--range matching echo -n "Enter your age : " read age case $age in [0-9] | 1[0-7] )echo "You are not eligible to vote";; *)echo "Eligible to vote" esac

"multiway4.sh" 7L, 157C written [1011@localhost ]$ sh multiway4.sh Enter your age : 12 You are not eligible to vote [1011@localhost ]$

program (degconv.sh) # degree conversion echo -n "Enter Fahrenheit : " read f c= `expr \ ( $f - 32 \ ) \* 5 / 9 ` echo "Centigrade is : $c"

# circle metrics using readonly variable pi=`expr "scale=2; 22 / 7" | bc` readonly pi # pi value cannot be altered

echo -n "Enter value for radius :" read radius area=`expr "scale=2; $pi * $radius * $radius" | bc` circum=`expr "scale=2; 2 * $pi * $radius" | bc` echo "Area : $area" echo "Circumference : $circum"

"shell3.sh" [New] 9L, 320C written [1011@localhost ]$ sh shell3.sh Enter value for radius :12 Area : 452.16 Circumference : 75.36 [1011@localhost ]$

# Swapping values echo -n read a echo -n read b t=$a a=$b b=$t echo "Values after swapping" echo "A value is $a" echo "B value is $b" "Enter value for B : " "Enter value for A : "

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy