'How to print a character in C [closed]
#include<stdio.h>
int main()
{
char name = 'Hannah'; //add [] after variable name to store multiple variables
int age = 20;
printf('Hi Im %c', name);
return 0;
}
I'm a beginner at C and I keep getting an error when I run this code. Can someone help me out?
[Error output][1] [1]: https://i.stack.imgur.com/fHmPa.png
Solution 1:[1]
You should always declare the storage area where you are going to save the sting by giving it a value. That value defines the length of the string you are storing.
Also use double quotes "" for strings.
int main() { char name[100] = "hannah"; printf("%s", name); // %s is format specifier return 0; }
Solution 2:[2]
char name can contain only one character only because its size is only 1 byte. to store more than one character you need to use an array of characters so you can use like bellow code.
check this code in the online c compiler. link check this reference link Cprograming
#include <stdio.h>
int main()
{
char name = 'h';
int age = 20;
char sName[] = "Hannah";
printf("\n Hi Im %c",name);
printf("\n Name: %s",sName);
printf("\n Age: %d",age);
return 0;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Billions.A.Joel |
| Solution 2 |
