C printf not working on ubuntu 13.10 terminal

I'm new in ubuntu so please bear with me.

I need to create a C based program for a course in my university. I was using openSUSE as the OS and konsole as the terminal emulator when I was in the university's lab.

So basically I need to install openSUSE on my system or use a VM to do so. But i feel lazy to do that, so I tried to run it on my Ubuntu instead of openSUSE.

However, no C code seems working on Ubuntu's terminal. The compiling is success, but its not running, or at least the printf is not running.

This is my code, a very very simple one :

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
int main()
{ printf("test"); return 0;
}

When I compile it with gcc test.c -o test everything works fine and i get an executable file.

Then I try to run it by ./test, but the printf is not printed. No error or warning was shown.

Am I missing something?

Note : my gcc is the new one, it should has no problem.

5

1 Answer

Here is the output, of your program:enter image description here

The text from printf ("test"); is printed and you can see it before the line shubham@shubham-pc:~$

Since there is no \n in your program, a newline is not printed at end and hence the default line of console gets printed after it

To solve this your program should look like:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{ printf("%s \n","test"); return 0;
}

The thing I've done here is that, I used a format string (%s) to print test and added a newline (\n) after it.

Here is the output after the edits:

enter image description here

2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like