545 views
1 1 vote
1.

#include <stdio.h>

int main() {
    
    int i;
    char a[] = ""; // no tab space in between
    if(printf("%s",a))
    printf("string empty");
    else
    printf("string is not empty");
    return 0;
}

 

Output: string is not empty

 

2.

#include <stdio.h>

int main() {
    
    int i;
    char a[] = " ";  // used a tab space in between
    if(printf("%s",a))
    printf("string empty");
    else
    printf("string is not empty");
    return 0;
}

Output: string empty

 

Why and how so?

1 Answer

Best answer
3 3 votes
Program 1.

Return value of printf() -> No. of characters printed on the standard output

char a[] contains NULL string i.e. the character '\0'.

When printft()  is executed , it prints nothing, ie printf() returns 0 (No. of characters printed are zero). Hence the if body isn't executed.

Program 2.

char a[] contains string of length 1, ie a 'space character'.

When printf() is executed, prints the space character onto the console, returns 1 (No. of characters printed). 'if' condition evaluates to TRUE.
• selected by
Position:
Show:

Related questions

0 0 votes
4 4 answers
601
601 views
SUBRATA_DAS 2 asked Feb 26, 2025
601 views
int a = -1, b = 1;int c = ++a && b ++;printf("%d, %d, %d",a,b,c);give me the ans with appropriate reason. according to me increment operator have higher precedence, then ...
0 0 votes
0 0 answers
474
474 views
lalitver10 asked Aug 14, 2024
474 views
#include <stdio.h>int main(){ int x = 3; int z = (++x)+ (x = 10); printf("%d", z); return 0;} 
0 0 votes
1 1 answer
644
644 views
srijankarak_123 asked May 7, 2024
644 views
let suppose address of first index of array is n and size of each block of array is u. then the index of second element is a+u.let suppose the array has m elements.what w...
1 1 vote
1 1 answer
616
616 views
jugnu1337 asked May 10, 2022
616 views
SOURCE NAME::: UNDERSTANDING POINTER IN C (BOOK) BY YASHWANT KANETKARmy answer is C am i right?