MCQs
scanf is a function that reads data with specified format from a given string stream source.
scanf("%d",&number);
atoi() convert string to integer.
var number;
number = atoi("string");
string.h is the header in the C standard library for the C programming language which contains
macro definitions, constants, and declarations of functions and types used not only for string
handling but also various memory handling functions.
itoa() takes the integer input value input and converts it to a number in base radix. The resulting number a sequence of base-radix digits.
Example:
/* itoa() example */
#include<stdio.h>
#include <stdlib.h>
int main ()
{
int no;
char buff [50];
printf ("Enter number: ");
scanf ("%d",&no);
itoa (no,buff,10);
printf ("Decimal: %s\n",buff);
itoa (no,buff,2);
printf ("Binary: %s\n",buff);
itoa (no,buff,16);
printf ("Hexadecimal: %s\n",buff);
return 0;
}
Output:
Enter a number: 1250
Decimal: 1250
Binary: 10011100010
Hexadecimal: 4e2
The C standard library consists of a set of sections of the ISO C standard which describe
a collection of header files and library routines used to implement common operations,
such as input/output and string handling, in the C programming language. The C standard
library is an interface standard described by a document; it is not an actual library of software
routines available for linkage to C programs.
g = strcmp(s1, s2); returns 0 when the strings are equal, a negative integer when s1 is less than s2, or a positive integer if s1 is greater than s2, that strcmp() not only returns -1, 0 and +1, but also other negative or positive values(returns difference between the first non-matching pair of characters between s1 and s2).
A possible implementation for strcmp() in "The Standard C Library".
int strcmp (const char * s1, const char * s2)
{
for(; *s1 == *s2; ++s1, ++s2)
{
if(*s1 == 0)
return 0;
}
return *(unsigned char *)s1 < *(unsigned char *)s2 ? -1 : 1;
}
fwrite() - Unformatted write in to a file.
fscanf() - Formatted read from a file.
The ftell() function shall obtain the current value of the file-position indicator for the stream pointed to by stream.
Example:
#include<stdio.h>
int main(void)
{
FILE *stream;
stream = fopen("MYFILE.TXT", "w+");
fprintf(stream, "This is a test");
printf("The file pointer is at byte %ld\n", ftell(stream));
fclose(stream);
return 0;
}
FILE - a structure containing the information about a file or text stream needed to perform
input or output operations on it, including:
=> a file descriptor, the current stream position,
=> an end-of-file indicator,
=> an error indicator,
=> a pointer to the stream's buffer, if applicable
fpos_t - a non-array type capable of uniquely identifying the position of every byte in a file.
size_t - an unsigned integer type which is the type of the result of the sizeof operator.
for(i=1; i<=5; i++) Here the for loop runs 5 times.
Loop 1:
scanf("%c", &c); Here we give 'a' as input.
printf("%c", c); prints the character 'a' which is given in the previous "scanf()" statement.
ungetc(c, stdin); "ungetc()" function pushes character 'a' back into input stream.
Loop 2:
Here the scanf("%c", &c); get the input from "stdin" because of "ungetc" function.
printf("%c", c); Now variable c = 'a'. So it prints the character 'a'.
ungetc(c, stdin); "ungetc()" function pushes character 'a' back into input stream.
This above process will be repeated in Loop 3, Loop 4, Loop 5.
C string is a character sequence stored as a one-dimensional character array and
terminated with a null character('', called NULL in ASCII).
The length of a C string is found by searching for the (first) NULL byte.