Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have the following program:
#include <stdio.h>
#include <stdlib.h>
int main()
{
static char string[12];
int length,c,d;
printf("Enter a string :");
gets(string);
length=strlen(string);
printf("\nLength of the string is %d",length);
for(c=0;c<=length-2;c++)
{
d=c+1;
printf("\t%.*s\n",d,string);
}
for(c=length;c>=0;c--)
{
d=c+1;
printf("\t%.*s\n",d,string);
}
}
I am very much confused about the usage of %.*s in the printf statement. I know %s is used for displaying strings, but I am confused the usage of .* before s in this program. Also there is only one datatype (%s) mentioned inside the quotation marks in the printf statement, but there are two variables mentioned in the printf statement.
It is a precision component, which specifies maximum number of bytes for string conversions. Asterisk (*), uses an integer argument, which specifies the value (for precision) to be used.
As an example, the following code:
#include <stdio.h>
int main(int argv, char **argc)
{
char *s = "hello, world";
printf("%.*s\n", 4, s);
return 0;
}
gives output:
hell
The format statement can allow a width and precision value. So, to print a string for a variable length then specify printf("%.*s", length, string). The length is substituted for the asterisk.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am a newbie to C Language. For a my assignment, I wrote this simple code to Print the student grade according to the input marks.
main(){
char grade;
grade = grade_calc(90);
printf("Your Grade is ", grade);
}
char grade_calc(int marks){
if(marks >= 75){
return "A";
}
else {
return "B";
}
}
But it shows error in Function starting line (int marks).
functions.c [Warning] return makes integer from pointer without a cast
Can anyone help me, why it this error happens?
This should do it.
int main(){
char grade;
grade = grade_calc(90);
printf("Your Grade is %c", grade); //%c specify char
// ^^
}
char grade_calc(int marks){
if(marks >= 75){
return 'A';
// ^ ^
}
else {
return 'B';
// ^ ^
}
}
In case you want to know more: C format specifiers.
Also, your return type is conflicting(char vs char*).In case you don't know, "A" is a char* (string) and 'A' is a char (you can learn more about the differences here
. Last but not least, don't forget that main() return int and functions need to be defined before used.
This is the declaration of printf
int printf(const char *format, ...)
printf takes the arguments (what you send to it in the parenthesis) and print it on the screen.
but, printf need to know where to print from (aka what bits you want to print), when to stop and which format are we talking about.
for example:
if we takes a int printf needs to know where is it to see its value.
which type it (int) so printf know to stop printing after 4 bytes. and again the type(int) so the bits will represent as numbers and not chars or floats
every type has its wildcard:
%d for int
%c for char
%s for strings
%f for float
you can look it up
also you need to return a char from the function
so you need to return this:
return('a'); and not return ("a");
so,
"a"
means string literal so the function returns char * and not char.
'a'
means value of the char a
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
//(1)
#include <stdio.h>
int main() {
printf("%d", 5);
}
//(2)
#include <stdio.h>
int main() {
printf("5");
}
What are the difference between (1) code block, and (2) code block?
The difference is that printf("%d", 5) is used to print (to stdout) the value of a variable of type int.
Whereas printf("5") is used to print (to stdout) a string. To get more clear, let's modify the code slightly.
//(1)
#include <stdio.h>
int var = 5;
int main() {
printf("%d", var);
}
//Output : 5
//(2)
#include <stdio.h>
int var = 5;
int main() {
printf("var");
}
Output : var
As you can see, printf() in (1) interprets var as a variable of type int and then prints its value.
Whereas printf() in (2) interprets var as a string and prints it as it is.
Are you talking about printf or main? I think that you are talking about printf.
Return value
Upon successful return, these functions return the number of characters printed (excluding the null byte used to end output to strings).
The functions snprintf() and vsnprintf() do not write more than size bytes (including the terminating null byte ('\0')). If the output was truncated due to this limit then the return value is the number of characters (excluding the terminating null byte) which would have been written to the final string if enough space had been available. Thus, a return value of size or more means that the output was truncated. (See also below under NOTES.)
If an output error is encountered, a negative value is returned.
Both of those calls should return the number one.
The answer to your question is that they both return an integer. However, you can treat an integer like a char. The ascii table shows you the mapping of integers to chars.
Both the code block will give output as 5 .
Please check the image file.
enter image description here
If you have doubts, just try to use typeof keyword of that variable and check according to that
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
here is my code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int x,y,i,j,k;
char c;
char n_avion[6][20]={'BOING747','AIRBUSA380','LEARJET45','DC10','ANTONOV32','CONCORDE'};
char c_avion[6][2]={'B0','AB','Lj','DC','AN','CO'};
int v_avion[6]={800,950,700,900,560,1400};
int r_avion[]={10000,12000,4500,8000,2500,16000};
printf("\nEntrer LE Code d'Avion s'il vous plait : ");
for(i=0; i<6 ;i++){
printf("%c", n_avion[i]);
}
when I try to printout n_avion using %c it gives me this päÿ¼└È
And when I try to printout n_avion using %s it gives me this 70502E
what I really want is this BOING747
Plz any help and Thanks
There are many issues:
strings must be included between " and not between '
c_avion[6][2] should be c_avion[6][3] you need one more char for the string terminator (strings are terminated by a NUL character)
printf("%c", n_avion[i]) is using the wrong format specifier for strings, it should be %s instead of %c
put a \n at the end of the printf format string, otherwise the names will be printed on a single line without spaces (try)
Corrected code:
int main()
{
int x, y, i, j, k;
char c;
char n_avion[6][20] = { "BOING747","AIRBUSA380","LEARJET45","DC10","ANTONOV32","CONCORDE" };
char c_avion[6][3] = { "B0","AB","Lj","DC","AN","CO" };
int v_avion[6] = { 800,950,700,900,560,1400 };
int r_avion[] = { 10000,12000,4500,8000,2500,16000 };
printf("\nEntrer LE Code d'Avion s'il vous plait : ");
for (i = 0; i < 6; i++) {
printf("%s\n", n_avion[i]);
}
}
BTW: my crystall ball tells be you will need to use structs sooner or later.
Other hint:
Don't use "magic numbers" like 6 or 3 but use constants.
For example:
#define NAMELENGTH 20
#define SHORTNAMELENGTH 3
#define NBOFPLANES 6
...
char n_avion[NBOFPLANES][NAMELENGTH] = ...
char c_avion[NBOFPLANES][SHORTNAMELENGTH] = ...
...
etc.
C differentiates 'x' as a character and "x" as a string ('x' and a NULL character). You want to print the strings, so use "%s" after setting your strings.
When you are trying to declare something like this
char n_avion[6][20]={'BOING747','AIRBUSA380','LEARJET45','DC10','ANTONOV32','CONCORDE'};
char c_avion[6][2]={'B0','AB','Lj','DC','AN','CO'};
I hope the intention is to define array of strings, in which case values needs to be in double quotes.
char n_avion[6][20]={"BOING747","AIRBUSA380","LEARJET45","DC10","ANTONOV32","CONCORDE"};
char c_avion[6][2]={"B0","AB","Lj","DC","AN","CO"};
having said that a string is a character array terminated with '\0' character
however here, there is no space for '\0' character at all because the size is restricted to 2 as defined in char c_avion[6][2], so make some room for '\0'
char c_avion[6][3]={"B0","AB","Lj","DC","AN","CO"};
finally the printf
simply put "%s" is for strings and "%c" for characters
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
#include "stdafx.h"
#include<string.h>
#include<stdio.h>
int _tmain(int argc, _TCHAR* argv[])
{
char abc[28] ;
//abc[26] = '\0';
abc[0]=65;
char hj = abc[0];
for(int i=0; i<26; i++)
{
abc[i]=++hj;
printf("%d\n",i);
}
int l=strlen(abc);
abc[l]='\0';
printf("length of abc_array is %d\n",(strlen(abc)));
for(int i=0; i<strlen(abc); i++)
{
printf("%c",abc[i]);
printf("\n");
}
}
The output length of the string is 39, which is wrong. What is the error?
you need to set abc[i] = '\0' before you call strlen.
Standard strlen function is only applicable to strings. String is a sequence of characters terminated by zero character. Passing something that is not a string to strlen results in undefined behavior.
What you are passing to strlen in your code is not a string. This is why you get meaningless results from strlen.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
This code will not run. Can somebody tell me why?
#include <stdio.h>
#include <string.h>
main (){
char user[7];
printf("Username\n");
scanf("%s",user);
if(user == 'admin'){
printf("Hello World");
}else{
printf("Bad");
}
return(0);
}
This is a working example. You need to use strcmp to compare strings. strcmp() returns 0, if the strings are equal.
If the max input length is known, then you should also use length specifier in scanf or one of the suggestion listed here, to prevent a buffer overflow.
#include <stdio.h>
#include <string.h>
int main(int argc, char* argv)
{
char user[7];
printf("Username:\n");
scanf("%6s", user);
if(!strcmp(user, "admin"))
{
printf("Hello World");
}
else
{
printf("Bad");
}
return 0;
}
This is not working because you are comparing two strings the wrong way. You need to use strcmp. Check this answer to see what strcmp returns in C.
To make your program work, change the line
if (user == 'admin')
to
if (strcmp (user, "admin") == 0)
Also, using scanf might be a bit dangerous, in rare cases. I prefer using fgets for strings. To use fgets, do:
fgets (user, sizeof (user), stdin);