Академический Документы
Профессиональный Документы
Культура Документы
FILE *tmpfile(void)
Opens a temporary file. Need not specify
a file name.
The temporary file is opened in wb+
mode. If the request cannot be honoured,
NULL is returned.
The system removes the file after it is
closed, or on program exit.
rewind()
}
fclose(fp);
}
Discussion Quiz2
1. What does the function CB evaluate, explain
your answer.
int CB(unsigned int x) {
int count=0;
while(x) {
count++;
x = x&(x-1); }
return count; }
Counts the number of bits set in x
Write a program to multiply two integers
a and b using only bit shifts and
addition.
Effect of shift operations
What is the effect of shifting a number
to the left?
Take for example 5
0 0 0 0 0 1 0 1
b 0 0 0 0 0 1 0 1
How the algorithm for shift and
add works (2)
Check whether the left most bit of b is
set to 1.
If yes, add a (=6) to c, i.e. c = c + a;
a 0 0 0 0 0 1 1 0
b 0 0 0 0 0 1 0 1
How the algorithm for shift and
add works (3)
Shift a to left by 1 and b to right by 1.
Check whether the left most bit of b is
set to 1.
a 0 0 0 0 1 1 0 0
b 0 0 0 0 0 0 1 0
How the algorithm for shift and
add works (4)
Shift a to left by 1 and b to right by 1.
Check whether the left most bit of b is
set to 1. If yes, c = c (6) + a (24)
Continue the loop until b becomes 0.
a 0 0 0 1 1 0 0 0
b 0 0 0 0 0 0 0 1
int main() {
int a = 6, b = 5;
int c = 0;
while (b != 0) {
if ((b & 1) != 0)
c = c + a;
a = a << 1;
b = b >> 1; }
printf("%d", c);
}
char *p[2][3] = { abc, defg, hi,
jklmno, pqrstuvw, xyz };
Expression Equivalent Value
Expression
**p[1]
**(p[1] + 2)
***p
(*(*(p+1) +1))[7]
*(p[1][2] + 2)
defg hi
abc
0 1 2
**p[1] = j
p[0]
char *p[2][3] =
p[1]
**(p[1] + 2) = x
xyz
***p = a jklmno
pqrstuvw
*(*(p+ 1) + 1)[7] = w
*(p[1][2] + 2) = z
Can two unions be compared using the
logical equality operator (==). Explain.
The compiler cannot tell what the union
currently contains and hence cannot
compare them.
Can we use the keyword typedef to
define new data types.
No. The keyword typedef is used to
define new names (aliases) for
previously defined data types.
Can we pass a structure to a function
using call-by-reference.
Structures are always passed to
functions using call-by-value.
Executing commands from
within a C program
The library function system() is used to
access operating system commands.
system(date);
The string passed to system() is treated
as a operating system command.
int main()
{ system(date); }
Using pipes from C program
The function
FILE *popen(command,r/w)
int pclose(File *fp)
is used to communicate with the
operating system.
Creates a pipe between the command
executed and the calling environment.
popen() and pclose(): Example
#include <ctype.h>
#include <stdio.h>
main()
{
int c;
FILE *ifp;
ifp = popen("ls","r");
while ((c=getc(ifp)) != EOF)
putchar (toupper(c));
pclose(ifp);
}
Environment Variables
A set of dynamic variables that can
affect the way a process behaves in a
computer. Example PATH, HOME
The value of an environment variable
can be printed using the $ symbol
before the variable, echo $PATH
Accessing environment
variables
An environment variable can be
accessed from within a C program by
using the standard function (included in
stdlib.h) getenv
char *getenv(const char *name)
If name is an environment variable the
address of the string is returned else NULL
printf(%s,getenv(PATH));
Another example
#include<stdio.h>
#include<stdlib.h>
int main(){
printf("%s%s \n %s%s \n %s%s \n",
"User:", getenv("USER"),
"Shell: ", getenv("SHELL"),
"Home Directory", getenv("HOME"));
}