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"
Related
I'm a begginer programmer and I've been experimenting with this code but I can't figure out why it's giving me zero. I've checked for integer divison , and wrong place holders but I can't find any . I also believe I'm using the correct format for every function such as printf , scanf etc. can anyone hepl ?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
double theta , fr , a, pi = 3.1459;
printf("Enter the value of a \n");
scanf("%f",&a);
theta = atan2(a,27.5);
printf("%f",theta);
fr = 1.67*pow(10,-6)*sin(theta);
printf(" frequency = %f",fr);
return 0;
}
You need to use the correct format specifiers for every data type. Your variables are doubles so you need to use %lf for scanning.
You need to learn how to debug programs yourself. If you add one printf mode the problem becomes obvious:
printf("Enter the value of a \n");
scanf("%f",&a);
printf("%f\n", a);
https://godbolt.org/z/cj3zo6Yqn
It is prints zero 0. So scanf did not scan properly.
Changing to
scanf("%lf",&a);
Resolves the problem.
https://godbolt.org/z/jcjrYaG5j
I am learning the language C by myself and with the help of internet.
I came across an exercise, and I was able to read in everything with integers and double, but allowing the user to type in a full sentence and store it in a variable has given me hard time. Can someone explain how I can get a sentence from the user, and store it in a variable. I have tried many things, such as [%^\n] with scanf, and also fget but I am having some trouble. For some reason, it is not working.
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int i = 4;
double d = 4.0;
char s[] = "Orange ";
// Declare second integer, double, and String variables.
int secondInt;
double justDouble;
char variable[500];
// Read and save an integer, double, and String to your variables.
scanf("%d", &secondInt);
scanf("%lf", &justDouble);
scanf("%[^ \n]", variable);
// Print the sum of both integer variables on a new line.
printf("%i\n ", i + secondInt);
// Print the sum of the double variables on a new line.
printf("%.1lf\n ", d + justDouble);
// Concatenate and print the String variables on a new line
printf("%s ", s);
printf("%s ", variable);
// The 's' variable above should be printed first.
return 0;
}
This should do the trick by using fgets() in general
#include <stdlib.h>
#include <stdio.h>
int main()
{
// variable to store the message
char msg[100];
// prompting the user to enter the message
printf("Pls enter a msg: ");
// using fgets() to retrieve a whole sentence from the user
fgets(msg, 100, stdin);
// printing the message to stdout
printf("%s", msg);
}
You can learn more about fgets here:
https://www.tutorialspoint.com/c_standard_library/c_function_fgets.htm
Let me know if anything is not clear so I can improve my answer
To work with scanf, you need to make sure that everything entered gets read.
In your example, you first expect an integer, and then a double. What does the user type to 'finish' entering the integer? Probably a <RET> (or a blank) - and now you need to scanf these too! Or they will 'clog' the input stream.
For example, your second scanf could be scanf(" %lf"... - note the blank before the % sign, it will read (and discard) any number of whitespace (which is <RET>, <TAB>, <space>).
scanf is very powerful, but needs a lot of detail understanding to be used correctly. Most people don't get it, and therefore claim "it's old and bad and shouldn't be used".
In professional software, it is generally avoided; not because it's not capable, but because the chance is too high that is used wrong, or that it is encountered by a developer that changes it and messes it up.
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.
hello I am searching where I did the wrong step?
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int account_on_the_bank=25;
printf("how much money do you have in the banque? \n");
scanf("%f",account_on_the_bank);
printf("Vous avez %d euros sur votre compte",account_on_the_bank);
return 0;
}
where's my problem???? it shows windows has stop working
I have tried everything the same problem shows up always?
This is the problematic line:
scanf("%f",account_on_the_bank);
It has wrong specifier and also not passing the address of the variable to scanf.
It should be:
scanf("%d", &account_on_the_bank);
Please read the basics of C
You have error in your scanf()
scanf("%f",account_on_the_bank);
^ ^
It should be
scanf("%d",&account_on_the_bank);
^ ^
because int needs %d as specifier, and scanf() needs address of the variable in which you want to store the read value(address is obtained by using &)
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);