Hi all programmers , i was wondering if i read a string and then i was trying to print the first letter of that string what am i supposed to do ?
Hi all programmers , i was wondering if i read a string and then i was trying to print the first letter of that string what am i supposed to do ?
puts(string[0]); should work.
well i tried that lol , it doesnt
Code:#include <stdio.h> #include <conio.h> #include <string.h> main() { char string[20]; gets(string); puts(string[0]); getch(); }
Apart from using gets, what "doesn't work" about that?
try it on a compiler, it gives me an error at the line puts(string[0]);
invalid conversion from a char to const char !!! i dont have a clue about the problem.
Well, you could try putc(string[0]); printf(" First letter is... %c",string[0]); or others...
The point is that strings in C are arrays of characters. The first character is array[0] the last one is always sizeof(array)-1. The only thing that separates them from arrays of bytes is the trialing 0 at the end of each. There are a number of library functions that treat arrays of characters as strings of text so you use them...
Use the normal print routines:
and for that matter use the normal input routines:Code:printf("%c\n", string[0]);
EDIT: Also, reading the error message as it is actually written will help: conversion to const char * is a lot different than conversion to const char.Code:fgets(string, 20, stdin);
mm sorry about that