The former Italy Under-21 striker will join the Bluebirds, who finished bottom of the Premier League this season, when his contract with Manchester United expires this summer.
Macheda said on the club's official website: "It's a massive opportunity for me and it is with thanks to (manager) Ole Gunnar Solskjaer that I am here.
"He played a big part of my decision and I'm really looking forward to starting this season."
The 22-year-old exploded onto the scene in 2009 when he came off the bench to score the winners for United against Aston Villa and Sunderland on his first two senior appearances for the Red Devils.
However, he failed to force himself into the first team reckoning and has spent much of the last few years on loan.
Macheda did impress in his last loan spell with Birmingham, where 10 goals in 18 appearances helped the Blues avoid relegation from the Sky Bet Championship.
He added: "Last season I had a good spell on loan at Birmingham City, but this is a new chapter of my career now and I can't wait to get started and show everyone who I am.
"Knowing Ole, he wants to get back into the Premier League as soon as possible and the recent signings show that.
"I'm here for the same reason. I feel that this is a great team and if we can have a good season and work hard then we can go straight back up."
- Sports & Recreation
- Premier League
- Manchester United
- Ole Gunnar Solskjaer