Can you please tell me why I get an error message while trying to run this program??

#include<stdio.h>
int main( )

{
printf("Hello %s World""SBL");
return 0;

}

Try:

#include<stdio.h>
int main( )

{
char str[]="SBL";

printf("Hello %s World", str);
return 0;

}

The printf function is wainting to print a string designated by "%s".

Greetings,

printf() only takes one string literal in the first argument. To learn more about printf(), click here.

By sending printf("Hello %s World""SBL") to printf(), you will retrieve an error because the string literal can only contain one string. Each "" contains one string, and the data inside. Any addition variables are called leading arguments which take a comma to seperate the difference:

printf("Test %s %i %d", abc, def, ghi);

Example 1.1: Leading arguments

The string literal is everything between the "" double quotations, and everything after the comma(s) are the leading arguments.

It is quite simple to fix your coding problem. Just add a comma between the two string literals, as "SBL" is looked at as one of the leading argument(s).

printf("Hello %s world", "SBL");

- Stack Overflow

Now how come when i run and compile it the dos screen flashes real fats and goes away instead of staying and printing to the screen as my professor explained it???

Try:

#include<stdio.h>
int main( )

{
char str[]="SBL";

printf("Hello %s World", str);
return 0;

}

The printf function is wainting to print a string designated by "%s".

> the dos screen flashes real fats and goes away
Well, are you double-clicking on the DOS program, or running it in Command Prompt? If you are doing the first, DOS programs always do that unless you need to retreive some type of data, or freeze the screen. DOS applications are usually run inside a Prompt, but if not, Windows treats it like an application.

My best bet would be to either pause the screen using:

system("pause");

Or for a more recommended step, just use conio.h's function called getch():

#include <conio.h>

int main() {
	getch(); // wait for key press

	return 0;
}

How getch() works
The function is normally defined in <conio.h>. getch() works by halting the application until the user enters a keystroke. getch() then returns the ASCII value of the keystroke as an int. System-wide keystrokes are processed by the operating system before they are returned by getch().

When getch() receives one of these keystrokes, it returns 0 to the calling program. The subsequent call to getch() returns a value for the key. Entering the following keystrokes will also cause getch() to return 0 to the calling program and return the key value on the subsequent call to getch().

Why system() calls aren't safe
Using system("pause") is a bad habit.

By calling system(), you are invoking the default shell. The shell executes the command-line given to it which runs the 'pause.exe' program. So your simple c program is relying on two external programs for checking a single key press. Though, what if someone deleted or renamed 'pause.exe'? Better yet, what if someone tried to compile your program on unix, or another operating system...It wouldn't work.

There are always alternatives:

#include <stdio.h>

void cmdwait(void) {
	int c;

	do {
		c = getchar();
		if (c == EOF)
			break;
	} while (c != '\n');
}

int main() {
	// press any key to continue
	cmdwait();

	return 0;
}

Instead of calling on system("pause"); just call on cmdwait(); It's alot safer.


I hope this helps,
- Stack Overflow

Hello,

I believe you can also right click on an application, and tell it not to close the window right away. This is an OS thing... my linux shell remains open.

Christian

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.