1 :
char array[100];
printf("%d" , scanf("%s" , arr));
//input : Jimmmy
prints 1 as scan returns number of inputs successfully read
2:
printf(" \"JIMMY %% N %% JOLLY\"");
output : "JIMMY % N % JOLLY"
(\\\) backslash are used for escaping double quotes
3: prototype of printf() in C :
int printf(const char *format, ...);
4:
The stdout stream is buffered, so will only display what’s in the buffer after it reaches a newline (or when it’s told to)
eg :
printf(“\%\n”); prints : %
5 :
Use of backspace \b and return \r escape characters is terminal dependent :
http://stackoverflow.com/questions/17236242/usage-of-b-and-r-in-c
6:
int main()
{
printf(1 + "Jjim");
return 0;
}
prints : jim
printf() prototype as int printf( const char *format , ... ) ; The compiler adds 1 to base address of "Jjim" and sends "jim" as an arguemnts to
printf() function defined in stdio.h header file .
7:
int main()
{
printf("%c ", 5["jimmyjolly"]);
return 0;
}
prints : 'j'
Compiler assumes 5["jimmyjolly"] as *(5 + "jimmyjolly") Adding 5 to base address of the string increments the pointer to point 'j'
Applying value-of opertaor gives 'j' as final result
Same logic for "jimmyjolly"[5] where output is j
8 :
gets() can read a string with spaces but a normal scanf() with %s can not.
gets() doesn't do any array bound testing and should not be used
use fgets() instead :
char *fgets(char *s, int n, FILE *stream);
fgets read at most n-1 characters into the array s, stopping if a newline is encountered, the newline is included in the array, which is terminated by ‘\0'.
fgets returns s, or NULL if end of file or error occurs.
9 :
#include <stdio.h>
int main()
{
char str[50] = {0};
scanf("%4s", str);
printf(str);
getchar();
return 0;
}
input : jimmy
ouput : jimmy
statment scanf("%4s" , str ) ; Read maximum 4 characters from console where str is a char array variable eg. char str[50];
10 :
printf(“%.*s”, n, s); in this statement value of pointer will move till nth character of s , printing n characters
.* means The precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.
11 :
printf function returns the number of characters successfully printed on the screen.
12 :
The return type of getchar() is int to accommodate EOF which indicates failure
13 :
for a CPU with memory mapped I/O , I/O protection is ensured by operating system routine (s)
No comments:
Post a Comment