how can we use a printf statement without a semicolon???
this was a question asked during an interview of a computer engineering student.

Recommended Answers

All 13 Replies

May be a printf within a printf would do the trick.Because the inner printf doesn't require the semicolon.

Here the statement finishes with a comma, not a semicolon:

#include <iostream>

int main() {
  printf("Hello"),
  getchar();
  return 0;
}

csurfer...howz that possible...i mean can u give the syntax plz...


william...this isnt working..i tried it out...

Ahh, that's C+ code, didn't realize which forum this was. Try this:

#include <stdio.h>

int main() {
  printf("Hello"),
  getchar();
  return 0;
}

use this one...
#include<stdio.h>
main()
{
printf("i am an engg"),
getchar();
}

#include <stdio.h>
#define PRINT_STRING(_s) printf("%s",_s)

int main() {
    PRINT_STRING("string");
    return (0);
}

You can also try this.

#include <stdio.h>
#define PRINT_STRING(_s) printf("%s",_s)

int main() {
    PRINT_STRING("string");
    return (0);
}

You can also try this.

That's not correct, that just turns into:

#include <stdio.h>

int main() {
    printf("%s","string");
    return (0);
}

Once the preprocessor's done.

csurfer...howz that possible...i mean can u give the syntax plz...


william...this isnt working..i tried it out...

Well what I meant was something like this :

#include<stdio.h>
int main()
{
       printf("%d%d",printf("Hello Daniweb"),printf("I am new here"));
       return 0;
}

You would get an output as I am new hereHello Daniweb1313 The inner printf's are not using a semicolon as you can see.

"printf" function returns the count of characters it has successfully output on the console and it processes from right to left internally hence first it print I am new here and then Hello Daniweb and then count of the successfully output characters of each string which in this case is 13 13.

how can we use a printf statement without a semicolon???
this was a question asked during an interview of a computer engineering student.

Assuming this was a job interview, I suggest you stay away from any job that asks foolish questions like this. I would not want to work for a company that want it's programmers to use tricks that make code unreadable.

commented: First sensible response to this thread. Exactly what I wanted to say. ;-) +2
commented: Very good suggestion. +6

well maybe u can define ; as a other name then use other name as derived type

how can we use a printf statement without a semicolon???
this was a question asked during an interview of a computer engineering student.

You can always put the call to printf in an if-statement, like this:

#include <stdio.h>

int main(void)
{
    if( printf("Hello World!\n") ) {}
    return 0;
}

As you can see, no semicolon is needed to invoke the printf function.

Assuming this was a job interview, I suggest you stay away from any job that asks foolish questions like this. I would not want to work for a company that want it's programmers to use tricks that make code unreadable.

Quoted for the truth.

commented: Good thinking. +5
commented: the best of the lot +1

@waltp
yeah..this was asked in a job interview

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.