ASHUTOSH MOHAPATRA-18BLC1035
QUESTION:
Write a C program to count the sum of numbers in a string.
CODE:
#include <stdio.h>
void main()
char string[80];
int count, nc = 0, sum = 0;
printf("Enter the string containing both digits and alphabet \n");//printf instruction is present in
//the standard i/o library of C, it calls the write system call.
scanf("%s", string);
//scanf instruction is present in the standard i/o library of C, it calls the read system call.
for (count = 0; string[count] != '\0'; count++)
if ((string[count] >= '0') && (string[count] <= '9'))
nc += 1;
sum += (string[count] - '0');
printf("NO. of Digits in the string = %d\n", nc);
printf("Sum of all digits = %d\n", sum);
exit(0);//exit system call terminates process immediately
1) printf() is one of the APIs or interfaces exposed to user space to
call functions from C library. printf() actually uses write() system call.
The write() system call is actually responsible for sending data to the
output.It interrupts the processor to print the string and the process
continues after it is completely printed.
2) scanf(): scanf() is functions in libc (the C standard library), and
it call the read()operating system syscalls respectively, talking to the file
descriptors stdin (fscanf allow you to specify the file stream you want to
read from).
3) exit(0): exit is a system call used to finish a running process from
which it is called. The parameter to exit is used to inform the parent
process about the status of child process. So, exit(0) can be used (and
often used) to indicate successful execution of a process and exit(1) to
flag an error.
Explanation of read and write system call
Calls to read() and write() (and all syscalls) result in a 'context switch' out
of your user-level application into kernel mode, which means it can
perform privileged operations, such as talking directly to hardware.
Depending on how you started the application, the 'stdin' and 'stdout' file
descriptors are probably bound to a console device (such as tty0), or
some sort of virtual console device (like that exposed by an xterm).
read() and write() safely copy the data to/from a kernel buffer called a
'uio'.
The format-string conversion part of scanf and printf does not occur in
kernel mode, but just in ordinary user mode (inside 'libc'), the general
rule of thumb with syscalls is you switch to kernel mode as infrequently
as possible, both to avoid the performance overhead of context
switching, and for security (we need to be careful about anything that
happens in kernel mode! less code in kernel mode means
less bugs/security holes in the operating system).
Explanation of exit system call
exit() terminates the process normally.
status: Status value returned to the parent process. Generally, a
status value of 0 or EXIT_SUCCESS indicates success, and any other
value or the constant EXIT_FAILURE is used to indicate an error.
exit() performs following operations.
* Flushes unwritten buffered data.
* Closes all open files.
* Removes temporary files.
* Returns an integer exit status to the operating system.
When exit() is called, any open file descriptors belonging to the
process are closed and any children of the process are inherited by
process 1, init, and the process parent is sent a SIGCHLD signal.
The mystery behind exit() is that it takes only integer args in the range 0
– 255 . Out of range exit values can result in unexpected exit codes.
An exit value greater than 255 returns an exit code modulo 256.
Explanation of interrupt in scanf()
Above program is handed over to CPU for execution then CPU starts executing this
program.While Executing Following Statement CPU is waiting for input from the
user –
1. Then Current Program execution is halted and Control is given to the IO
device i.e Keyboard to Enter the number.