Find the output - C Programs
1.
#include<stdio.h>
int main(){
FILE *fp1, *fp2;
fp1=fopen("file.c", "w");
fp2=fopen("file.c", "w");
fputc('A', fp1);
fputc('B', fp2);
fclose(fp1);
fclose(fp2);
return 0;
}
Output: B
Here fputc('A', fp1); stores 'A' in the file1.c then fputc('B', fp2); overwrites the contents of the file1.c with value 'B'. Because the fp1 and fp2 opens the file1.c in write mode.
Hence the file1.c contents is 'B'.
2.#include<stdio.h>
int main(){
printf("%c\n", ~('C'*-1));
return 0;
}
Output: B
ASCII Value of 'C' is 67.
67*-1=-67
applying bitwise not(~) to -67
First find binary value for 67
67=01000011
take 1's complement:
that is make 0's complement and add 1
10111100+1=10111101
will result in 66, which is the ASCII value of 'B'
3.
#include<stdio.h>
int main()
{
FILE *fp;
unsigned char ch;
/* file 'abc.c' contains "This is IndiaBIX " */
fp=fopen("abc.c", "r");
if(fp == NULL)
{
printf("Unable to open file");
exit(1);
}
while((ch=getc(fp)) != EOF)
printf("%c", ch);
fclose(fp);
printf("\n", ch);
return 0;
}
Output:Infinite Loop
The macro EOF means -1.
while((ch=getc(fp)) != EOF) Here getc function read the character and convert it to an integer value and store it in the variable ch, but it is declared as an unsigned char. So the while loop runs infinitely.
4.
#include<stdio.h>
int main()
{
char *p;
p="%d\n";
p++;
p++;
printf(p-2, 23);
return 0;
}
Output: 23
p points to the string "%d\n" and so when the pointer is incremented twice so the pointer is pointed to null position(means it moves to the right of "%d\n").
So in the printf statement the pointer is again moved to the beginning of the string "%d\n"so that the printf statement is similar to printf("%d\n",23);
So the output will be 23.
#include<stdio.h>
int main(){
FILE *fp1, *fp2;
fp1=fopen("file.c", "w");
fp2=fopen("file.c", "w");
fputc('A', fp1);
fputc('B', fp2);
fclose(fp1);
fclose(fp2);
return 0;
}
Output: B
Here fputc('A', fp1); stores 'A' in the file1.c then fputc('B', fp2); overwrites the contents of the file1.c with value 'B'. Because the fp1 and fp2 opens the file1.c in write mode.
Hence the file1.c contents is 'B'.
2.#include<stdio.h>
int main(){
printf("%c\n", ~('C'*-1));
return 0;
}
Output: B
ASCII Value of 'C' is 67.
67*-1=-67
applying bitwise not(~) to -67
First find binary value for 67
67=01000011
take 1's complement:
that is make 0's complement and add 1
10111100+1=10111101
will result in 66, which is the ASCII value of 'B'
3.
#include<stdio.h>
int main()
{
FILE *fp;
unsigned char ch;
/* file 'abc.c' contains "This is IndiaBIX " */
fp=fopen("abc.c", "r");
if(fp == NULL)
{
printf("Unable to open file");
exit(1);
}
while((ch=getc(fp)) != EOF)
printf("%c", ch);
fclose(fp);
printf("\n", ch);
return 0;
}
Output:Infinite Loop
The macro EOF means -1.
while((ch=getc(fp)) != EOF) Here getc function read the character and convert it to an integer value and store it in the variable ch, but it is declared as an unsigned char. So the while loop runs infinitely.
4.
#include<stdio.h>
int main()
{
char *p;
p="%d\n";
p++;
p++;
printf(p-2, 23);
return 0;
}
Output: 23
p points to the string "%d\n" and so when the pointer is incremented twice so the pointer is pointed to null position(means it moves to the right of "%d\n").
So in the printf statement the pointer is again moved to the beginning of the string "%d\n"so that the printf statement is similar to printf("%d\n",23);
So the output will be 23.
5.
#include<stdio.h>
int main()
{
FILE *ptr;
char i;
ptr = fopen("myfile.c", "r");
while((i=fgetc(ptr))!=NULL)
printf("%c", i);
return 0;
}
Output: Infinite loop
When an EOF is encountered fgetc() returns EOF. Instead of checking the condition for EOF we have checked it for NULL. so the program will generate infinite loop.
6.
#include<stdio.h>
int main()
{
printf("%%%%\n");
return 0;
}
Output: %%
we can't print % in C.
To print % we need %% in C for printing single %.
Hence to print %% we need %%%%.
As similarly if you want to print \n in C the syntax is
printf("\\n");
7.
#include<stdio.h>
int main()
{
FILE *fp;
char ch, str[7];
fp=fopen("try.c", "r"); /* file 'try.c' contains "This is Nagpur" */
fseek(fp, 9L, SEEK_CUR);
fgets(str, 5, fp);
puts(str);
return 0;
}
Output: agpu
fgets prototype :char * fgets(char *str,int n,FILE *fp)
fgets reads n-1 characters or till it encounters \n in input whichever comes firsts
8.
#include<stdio.h>
int main()
{
int i;
printf("%d\n", scanf("%d", &i));
return 0;
}
Output : 1
The scanf function returns the number of input is given.
9.
What does fp point to in the program ?
#include<stdio.h>
int main()
{
FILE *fp;
fp=fopen("trial", "r");
return 0;
}
Answer : A structure which contains a char pointer which points to the first character of a file.
10.
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment