317 views
1 votes
1 votes
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 votes
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

Related questions

1 votes
1 votes
1 answer
1
jugnu1337 asked May 10, 2022
289 views
SOURCE NAME::: UNDERSTANDING POINTER IN C (BOOK) BY YASHWANT KANETKARmy answer is C am i right?
1 votes
1 votes
0 answers
2
aakash pandey asked Apr 30, 2022
292 views
We use character array to declare string in C. So, if I declare an array likecharacter ch[ ] = {‘a’,’k’,’a’,’/o’};and try printing ch[3] like :printf(“%...
0 votes
0 votes
0 answers
3
Anirudh Kaushal asked Apr 6, 2022
202 views
#include<stdio.h struct marks { int p:3; int c:3; int m:2; }; void main() { struct marks s = {2, -6, 5}; printf("%d %d %d", s.p, s.c, s.m); }What does p:3 means here ?
0 votes
0 votes
0 answers
4