A Galaxy of beautiful minds to help you out the best possible solution to your problems in c fundamentals, data structures through c programming language.
In this example, p is a pointer to a character, and it is set to point to a string constant, "codequotient". So far, so good. But then an attempt is made to modify the constant string by changing p[0]. This isn't legal, because "codequotient" is a constant.
BASIC INPUT AND OUTPUT FUNCTIONS The scanf() and printf() functions
The int scanf(const char *format, ...) function reads input from the standard input stream stdin and scans that input according to format provided.
The int printf(const char *format, ...) function writes output to the standard output stream stdout and produces output according to a format provided.
The format can be a simple constant string, but you can specify %s, %d, %c, %f, etc., to print or read strings, integer, character or float respectively. There are many other formatting options available which can be used based on requirements. For a complete detail you can refer to a man page for these function. For now let us proceed with a simple example which makes things clear:
#include int main( ) { char str[100]; int i;
printf( "Enter a value :"); scanf("%s %d", str, &i);
printf( "\nYou entered: %s %d ", str, i);
return 0; }
When the above code is compiled and executed, it waits for you to input some text when you enter a text and press enter then program proceeds and reads the input and displays it as follows:
$./a.out Enter a value : seven 7 You entered: seven 7
Here, it should be noted that scanf() expect input in the same format as you provided %s and %d, which means you have to provide valid input like "string integer", if you provide "string string" or "integer integer" then it will be assumed as wrong input. Second, while reading a string scanf() stops reading as soon as it encounters a space so "this is test" are three strings for scanf().
Ques : Is it valid or not?
ReplyDeletechar *p ="codequotient";
p[0] = 'C'';
In this example, p is a pointer to a character, and it is set to point to
Deletea string constant, "codequotient". So far, so good. But then an attempt is made to modify the constant string by changing p[0]. This isn't legal, because "codequotient" is a constant.
right answer
DeleteWhat is the output of the following program ?
ReplyDeleteint main()
{
int a=5,b=5;
a=++a + ++a + ++a;
b=b++ + b++ + b++;
printf("%d %d",a,b);
return 1;
}
BASIC INPUT AND OUTPUT FUNCTIONS
ReplyDeleteThe scanf() and printf() functions
The int scanf(const char *format, ...) function reads input from the standard input stream stdin and scans that input according to format provided.
The int printf(const char *format, ...) function writes output to the standard output stream stdout and produces output according to a format provided.
The format can be a simple constant string, but you can specify %s, %d, %c, %f, etc., to print or read strings, integer, character or float respectively. There are many other formatting options available which can be used based on requirements. For a complete detail you can refer to a man page for these function. For now let us proceed with a simple example which makes things clear:
#include
int main( )
{
char str[100];
int i;
printf( "Enter a value :");
scanf("%s %d", str, &i);
printf( "\nYou entered: %s %d ", str, i);
return 0;
}
When the above code is compiled and executed, it waits for you to input some text when you enter a text and press enter then program proceeds and reads the input and displays it as follows:
$./a.out
Enter a value : seven 7
You entered: seven 7
Here, it should be noted that scanf() expect input in the same format as you provided %s and %d, which means you have to provide valid input like "string integer", if you provide "string string" or "integer integer" then it will be assumed as wrong input. Second, while reading a string scanf() stops reading as soon as it encounters a space so "this is test" are three strings for scanf().
What is the output of this program ?
ReplyDelete#include
int main()
{
int i = 3;
switch(i)
{
printf("Outside ");
case 1: printf("Geeks");
break;
case 2: printf("Quiz");
break;
defau1t: printf("GeeksQuiz");
}
return 0;
}
What value Printf() function returns?
ReplyDeleteIn this statement if we take the age variable out of the printf call then recompile what it will display and why?.
ReplyDeleteprintf("I am %d years old.\n", age);