I know python decently well, I've been trying to learn C for the past ~4 days. I found this code online edited it a tad bit and now it won't run.
#include <stdio.h>
#define MAX_LEN 80
int main (int argc, char *argv[]){
char a_word[MAX_LEN];
printf ("Enter some words:");
scanf ("%s", a_word);
printf ("The result is:" + a_word);
return 0;
}
There is nothing wrong with scanf(), the issue here is with printf() statement. In C, you need to have a format specifier to print the supplied arguments. Read the man page of printf() for further information.
In your code,
printf ("The result is:" + a_word);
should be
printf ("The result is: %s \n" , a_word);
In C, you cannot append strings with +. To append strings, use strcat() or one of the other functions for this purpose. In your concrete example however, you can just do, as Sourav Ghosh already wrote do:
printf("The result is: %s \n" , a_word);
Related
I am an absolute newbie, and I am learning to code.
#include <stdio.h>
#include <stdlib.h>
int main(){
char characterName[] = "Khan";
int characterYear = 2022;
float characterScore = 8.8;
char characterGrade = 'A';
printf("%s passed out of college in %d and had a grade of %s i.e. a score of %f\n",characterName,characterYear,characterGrade,characterScore);
return 0;
}
Can someone help me with this — I was just trying to incorporate all simple data types in one line (for reference) and after I run the program there is no output whatsoever, it just exits! It didn't happen when I had fewer data types in the line.
P.S. This might be the dumbest ever question ever asked on Stack Overflow, but please forgive if I am dumb/silly :)
Your format is wrong:
characterGrade has type char and you want to print it using %s format which requires char * parameter. It has to be %c instead:
"%s passed out of college in %d and had a grade of %c i.e. a score of %f\n"
I wrote the below code but I keep getting a "Command terminated by signal 11".
#include<stdio.h>
int main()
{
char *goodnight_message[20];
printf ("Enter the goodnight message.\n");
scanf ("%s", &goodnight_message);
printf ("%s Buddy.\n", *goodnight_message);
return 0;
}
I suspect that you want one char array with 20 chars, not an array of 20 char* which is what you've got now.
Note that when passing arrays to functions, like scanf and printf, they decay into pointers to the first element. Also note that when using scanf to read strings, always set a maximum number of characters to read (one less than the size of the array to leave room for the null terminator). Otherwise the user may enter more characters and your program will write out of bounds.
You should also always check if scanf is successful by checking if it returns the same number of conversions you requested. You request one only, so check if it returns 1.
A working program would then look like this:
#include <stdio.h>
int main() {
char goodnight_message[20]; // now a char[] instead
printf("Enter the goodnight message.\n");
if(scanf("%19s", goodnight_message) == 1) { // decays into a char*
printf("%s Buddy.\n", goodnight_message); // decays into a char*
}
}
This will however not read more than one word. To make scanf read until a newline you can add a set of characters to match in the conversion specifier with [characters].
If the first character of the set is ^, then all characters not in the set are matched.
We want all characters except \n so lets add the set [^\n]
#include <stdio.h>
int main() {
char goodnight_message[20];
// an alternative to printf when you don't need formatting is puts:
puts("Enter the goodnight message.");
if(scanf("%19[^\n]", goodnight_message) == 1) {
printf("%s Buddy.\n", goodnight_message);
}
}
There seems like you have a misconception of array and pointers..
While using array-like goodnight_message[20];
The correct way to address its address is to use &goodnight_message[0] indicating the beginning of the array or to use goodnight_message simply. Both of these point to the 0th index of the array..
#include<stdio.h>
int main()
{
char *goodnight_message[20];
printf ("Enter the goodnight message.\n");
scanf ("%s", goodnight_message);
printf ("%s Buddy.\n", goodnight_message);
return 0;
}
Tip: You should use gets(goodnight_message) instead of scanf when you want a string that includes a space. You can also do this by using scanf("%[^\n]s",goodnight_message);, like this:
#include<stdio.h>
int main()
{
char *goodnight_message[20];
printf ("Enter the goodnight message.\n");
scanf ("%[^\n]s", goodnight_message);
printf ("%s Buddy.\n", goodnight_message);
return 0;
}
line char *goodnight_message[20]; creates an array of strings (array of char pointers)
to create single string use char *goodnight_message; or char goodnight_message[20];
warning: if you use char *goodnight_message;, you have to malloc it, so just stick to [20] for now
scanf ("%s" reads one string from input, but note that it is only to the next whitespace
for example, input good night would read only good and output good buddy
to read whole line use gets(goodnight_message) or getline
warning: getline is a bit complicated (explanation)
Evening, sorry for beginner question but I have to do a code that receives 7 salutations in different languages, compare to a database, and, if they match, tell which language the salutation was on, if they don't, tell the user the language is unknown.
I think i understood the problem, but my code below doesn't show any results and just closes, can someone tell me why? I know it is very sketchy coding but cant find exactly the mistake. (Telling me a substitute for the multiple variables inside scanf would be much appreciated too).
#include<stdio.h>
#include<string.h>
int main(){
char en[7][15];
char id[7][15] = {"HELLO","HOLA","CIAO","HALLO","BOUNJOUR","ZDRAVSTVUJTE","."};
char re[7][15] = {"INGLES","ESPANOL","ITALIANO","ALEMAN","FRANCES","RUSO","NLS"};
scanf("%s %s %s %s %s %s %s",en[0],en[1],en[2],en[3],en[4],en[5],en[6]);
int i = 0,j=0,m=1,k=0;
while(i<8){
for(j=0;j<7;j++){
m=strcmp(en[i],id[j]);
if(m==0 || j==6){
strcpy(en[i],re[j]);
k=i+1;
printf("Caso %s: %s /n",k,en[i]);
break;
}
}
i++;
}
}
Thank you
The problem is your printf function. You are using %s format specifier for k instead of %d. Just change that line to this:
printf("Caso %d: %s \n",k,en[i]);
Also, the while condition i<8 should be changed to i<7 since there are 7 cases and not 8.
As for the scanf, don't think it's much of an improvement but you can use a for loop as well.
Is there any ways to to program this?
puts("this is a string" + variable);
Maybe different syntax but printing the variable and the string using the puts() in the same line.
Yes, it's called printf. For example:
printf("this is a string %s\n", variable);
Assuming variable is a string (a char *), this will substitute %s with the string, then end it in a newline.
If variable is a number, there are number formats you can use (e.g. %d for an int, etc.)
You can use printf function for Print a formatted string to the
console.
#include <stdio.h>
int main(void) {
char variable[] = "with a variable.";
printf("this is a string %s", variable);
return 0;
}
if you want to learn more about printf function go here
I need help on this exercise from C Primer Plus.
Write a program that requests your first name and does the following with it:
Prints it in a field three characters wider than the name
#include<stdio.h>
#include<string.h>
int main()
{
char a[40];
int p,v=0;
printf("Enter your first name: \n");
scanf("%s",a);
p= strlen(a);
v==p+3;
printf("%s",a);
}
I cant figure out how what to use as a modifier for the width
what should I add in between % and s?
the goal of this excercise is to read and grok the manual page for printf(). The reading part could only be done by you and there is no shortcut. The format specifier is the most complex chapter in C-Programing (other would say 'pointer'), and it is very wise to know where look things up (man-page) in need of remembering.
When you are done reading, you should have a little (or big) understanding of the format-specifier %s with all its possibilities.
EDITH: when you are done reading, and there is still a question whether to use "%*.*s" or "%s" or "%-.*s" etc., please come back with an updated question.
Here is one way to do it:
#include<stdio.h>
#include<string.h>
int main()
{
char a[40];
printf("Enter your first name: \n");
scanf("%s",a);
printf("[%-*s]", (int)(3 + strlen(a)), a);
return(0);
}
The printf() function can do it all.
From the question code, it is clear that "%s" as a format string is understood.
A format string of "%*s" allows the caller to place the (space-padded) width to be specified. For example:
printf("%*s", 10, "Hello");
The above will print "Hello" (as expected), in a 10-character frame. Hence, the command above actually prints: " Hello".
To put the spaces on the other side, tell printf() to left-justify the string using:
printf("%-*s", 10, "Hello");
This results in printing: "Hello "