10 Replies - 752 Views - Last Post: 15 November 2015 - 06:53 AM Rate Topic: -----

#1 stvnalexbro   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 12-November 15

Trouble Printing Characters

Posted 12 November 2015 - 08:41 AM

Hey im working on a program for school, and heres what i have its not the whole thing but it wont even print a character.
#include <stdio.h>
#include <math.h>
int main()
{
   char FirstInitial, LastInitial;
   printf("Please enter your first and last initials:\n");
   scanf("%c %c", &FirstInitial, LastInitial);
   getchar();

   printf("%c %c", FirstInitial, LastInitial);



}//END OF MAIN

:code:/>/>

Is This A Good Question/Topic? 0
  • +

Replies To: Trouble Printing Characters

#2 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3872
  • View blog
  • Posts: 14,211
  • Joined: 08-August 08

Re: Trouble Printing Characters

Posted 12 November 2015 - 09:03 AM

Look closely at line 7. What's wrong with LastInitial?
Was This Post Helpful? 1
  • +
  • -

#3 stvnalexbro   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 12-November 15

Re: Trouble Printing Characters

Posted 12 November 2015 - 12:44 PM

i'm so dumb thanks so much bro
Was This Post Helpful? 0
  • +
  • -

#4 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2555
  • View blog
  • Posts: 4,739
  • Joined: 30-May 10

Re: Trouble Printing Characters

Posted 12 November 2015 - 01:41 PM

View Poststvnalexbro, on 12 November 2015 - 07:44 PM, said:

i'm so dumb thanks so much bro

You might be interested in using GCC as your compiler then, since it will tell you when you make a mess of printf/scanf formats (along with many other dumb trivial errors as well).
$ gcc -Wall foo.c
foo.c: In function ‘main’:
foo.c:7:3: warning: format ‘%c’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat]
foo.c:14:1: warning: control reaches end of non-void function [-Wreturn-type]
foo.c:7:8: warning: ‘LastInitial’ is used uninitialized in this function [-Wuninitialized]


Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3872
  • View blog
  • Posts: 14,211
  • Joined: 08-August 08

Re: Trouble Printing Characters

Posted 12 November 2015 - 01:47 PM

One thing it won't tell you though, is that main needs to return an integer (hence int main()).
Was This Post Helpful? 0
  • +
  • -

#6 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2555
  • View blog
  • Posts: 4,739
  • Joined: 30-May 10

Re: Trouble Printing Characters

Posted 12 November 2015 - 02:12 PM

#include <stdio.h>
#include <math.h>
/*int*/ main()
{
  char FirstInitial, LastInitial;
  printf("Please enter your first and last initials:\n");
  scanf("%c %c", &FirstInitial, LastInitial);
  getchar();
  
  printf("%c %c", FirstInitial, LastInitial);
  
  
  
}//END OF MAIN


$ gcc -Wall foo.c
foo.c:3:9: warning: return type defaults to ‘int’ [-Wreturn-type]
foo.c: In function ‘main’:
foo.c:7:3: warning: format ‘%c’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat]
foo.c:14:1: warning: control reaches end of non-void function [-Wreturn-type]
foo.c:7:8: warning: ‘LastInitial’ is used uninitialized in this function [-Wuninitialized]


> One thing it won't tell you though, is that main needs to return an integer (hence int main()).
Those -Wreturn-type warnings seem to pretty definitive to me.
Was This Post Helpful? 1
  • +
  • -

#7 stvnalexbro   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 12-November 15

Re: Trouble Printing Characters

Posted 12 November 2015 - 07:35 PM

ok thanks bud
Was This Post Helpful? 0
  • +
  • -

#8 snoopy11   User is offline

  • Engineering ● Software
  • member icon

Reputation: 1594
  • View blog
  • Posts: 5,010
  • Joined: 20-March 10

Re: Trouble Printing Characters

Posted 12 November 2015 - 11:31 PM

Yeah gcc defo does complain about main returning an integer.
Was This Post Helpful? 0
  • +
  • -

#9 alishawalter   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 16
  • Joined: 09-November 15

Re: Trouble Printing Characters

Posted 13 November 2015 - 12:09 AM

while doing coding pay full attention towards it and read the whole code 2-3 times. intial must be written in small letters but you starts with uppercase. these are such silly mistakes which you can take care of.
Was This Post Helpful? 0
  • +
  • -

#10 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3872
  • View blog
  • Posts: 14,211
  • Joined: 08-August 08

Re: Trouble Printing Characters

Posted 13 November 2015 - 05:00 AM

That's odd. All I get is this:
p.cpp:7:34: warning: format specifies type 'char *' but the argument has type
      'int' [-Wformat]
   scanf("%c %c", &FirstInitial, LastInitial);
             ~~                  ^~~~~~~~~~~
p.cpp:7:34: warning: variable 'LastInitial' is uninitialized when used here
      [-Wuninitialized]
   scanf("%c %c", &FirstInitial, LastInitial);
                                 ^~~~~~~~~~~
p.cpp:5:34: note: initialize the variable 'LastInitial' to silence this warning
   char FirstInitial, LastInitial;
                                 ^
                                  = '\0'
2 warnings generated.


Was This Post Helpful? 0
  • +
  • -

#11 stvnalexbro   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 12-November 15

Re: Trouble Printing Characters

Posted 15 November 2015 - 06:53 AM

View Postalishawalter, on 13 November 2015 - 12:09 AM, said:

while doing coding pay full attention towards it and read the whole code 2-3 times. intial must be written in small letters but you starts with uppercase. these are such silly mistakes which you can take care of.

its doesn't matter as long as its the same as the one you declare
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1