soniccool
soniccool

Reputation: 6058

sprintf not declared in scope?

I have a small snippet of code here from something I designed but I keep getting the error :

sprintf not declared in scope 

Do I include something in the #includes or how can I get this working? I was working on it on VS at my mom's but came home and I can't get it on code blocks

if (tmp2 <= B_dest[hr - 6])
{
    sprintf(name, "B%d", tmp3);
}else{
    sprintf(name, "A%d", tmp3);
}

Upvotes: 17

Views: 48690

Answers (3)

serup
serup

Reputation: 547

I had similar problem with C::B and found that the problem is more than just compiler paths - it seems that the IDE itself had problems opening #include <...> files -- this however could be solved by Settings -> Editor -> Other settings -> use encoding when opening files : default

my encoding was not on default, and this somehow caused problems for the IDE to open include <...>

It however did NOT solve the problem with "was not declared in this scope"

Upvotes: 0

srbhkmr
srbhkmr

Reputation: 2104

Make sure you've #include <cstdio>

and access sprintf as std::sprintf() as pointed by @Potatoswatter.


or do the old c-style: #include <stdio.h> to include the definition of sprintf.

Upvotes: 6

Alok Save
Alok Save

Reputation: 206518

You need to include stdio.h.

#include<stdio.h>

The stdio.h declares the function sprintf, Without the header the compiler has no way of understand what sprintf means and hence it gives you the error.

In C++ Note that,

Including cstdio imports the symbol names in std namespace and possibly in Global namespace.
Including stdio.h imports the symbol names in Global namespace and possibly in std namespace.

The same applies for all c-styled headers.

Upvotes: 31

Related Questions